laserninja commented on code in PR #10641:
URL: https://github.com/apache/gravitino/pull/10641#discussion_r3030910412


##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergRESTUtils.java:
##########
@@ -39,15 +44,99 @@
 import org.apache.iceberg.catalog.Namespace;
 import org.apache.iceberg.catalog.TableIdentifier;
 import org.apache.iceberg.rest.responses.ErrorResponse;
+import org.apache.iceberg.rest.responses.LoadTableResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class IcebergRESTUtils {
 
+  private static final Logger LOG = 
LoggerFactory.getLogger(IcebergRESTUtils.class);
+
+  public static final String DEFAULT_SNAPSHOTS = "all";
+
+  public static final String SNAPSHOTS_REFS = "refs";
+
   private IcebergRESTUtils() {}
 
   public static <T> Response ok(T t) {
     return 
Response.status(Response.Status.OK).entity(t).type(MediaType.APPLICATION_JSON).build();
   }
 
+  /**
+   * Builds an OK response with the ETag header derived from the table 
metadata location. Uses the
+   * default snapshots value to ensure ETags from create/update/register are 
consistent with the
+   * default loadTable endpoint.
+   *
+   * @param loadTableResponse the table response to include in the body
+   * @return a Response with ETag header set
+   */
+  public static Response buildResponseWithETag(LoadTableResponse 
loadTableResponse) {
+    Optional<EntityTag> etag =
+        generateETag(loadTableResponse.tableMetadata().metadataFileLocation(), 
DEFAULT_SNAPSHOTS);
+    return buildResponseWithETag(loadTableResponse, etag);
+  }
+
+  /**
+   * Builds an OK response with the given ETag header.
+   *
+   * @param loadTableResponse the table response to include in the body
+   * @param etag the pre-computed ETag
+   * @return a Response with ETag header set if etag is present
+   */
+  public static Response buildResponseWithETag(
+      LoadTableResponse loadTableResponse, Optional<EntityTag> etag) {
+    Response.ResponseBuilder responseBuilder =
+        Response.ok(loadTableResponse, MediaType.APPLICATION_JSON_TYPE);
+    etag.ifPresent(responseBuilder::tag);
+    return responseBuilder.build();
+  }
+
+  /**
+   * Generates an ETag based on the table metadata file location. The ETag is 
a SHA-256 hash of the
+   * metadata location, which changes whenever the table metadata is updated. 
Uses the default
+   * snapshots value to ensure consistency.
+   *
+   * @param metadataLocation the metadata file location
+   * @return the generated ETag
+   */
+  public static Optional<EntityTag> generateETag(String metadataLocation) {
+    return generateETag(metadataLocation, DEFAULT_SNAPSHOTS);
+  }
+
+  /**
+   * Generates an ETag based on the table metadata file location and snapshot 
mode. The ETag is a
+   * SHA-256 hash that incorporates both the metadata location and the 
snapshots parameter, ensuring
+   * distinct ETags for different representations of the same table version 
(e.g., snapshots=all vs
+   * snapshots=refs).
+   *
+   * @param metadataLocation the metadata file location
+   * @param snapshots the snapshots query parameter value (e.g., "all", "refs")
+   * @return the generated ETag
+   */
+  public static Optional<EntityTag> generateETag(String metadataLocation, 
String snapshots) {
+    if (metadataLocation == null) {

Review Comment:
   Replaced



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergRESTUtils.java:
##########
@@ -39,15 +44,99 @@
 import org.apache.iceberg.catalog.Namespace;
 import org.apache.iceberg.catalog.TableIdentifier;
 import org.apache.iceberg.rest.responses.ErrorResponse;
+import org.apache.iceberg.rest.responses.LoadTableResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class IcebergRESTUtils {
 
+  private static final Logger LOG = 
LoggerFactory.getLogger(IcebergRESTUtils.class);
+
+  public static final String DEFAULT_SNAPSHOTS = "all";

Review Comment:
   Replaced



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to