eric-maynard commented on code in PR #433:
URL: https://github.com/apache/polaris/pull/433#discussion_r1857418460


##########
polaris-service/src/main/java/org/apache/polaris/service/persistence/MetadataCacheManager.java:
##########
@@ -0,0 +1,251 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.service.persistence;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.Optional;
+import java.util.function.Supplier;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableMetadataParser;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.RuntimeIOException;
+import org.apache.iceberg.util.JsonUtil;
+import org.apache.polaris.core.PolarisCallContext;
+import org.apache.polaris.core.PolarisConfiguration;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.TableLikeEntity;
+import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
+import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper;
+import 
org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MetadataCacheManager {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(MetadataCacheManager.class);
+
+  /**
+   * Load the cached {@link Table} or fall back to `fallback` if one doesn't 
exist. If the metadata
+   * is not currently cached, it may be added to the cache.
+   */
+  public static TableMetadata loadTableMetadata(
+      TableIdentifier tableIdentifier,
+      int maxBytesToCache,
+      PolarisCallContext callContext,
+      PolarisMetaStoreManager metastoreManager,
+      PolarisResolutionManifestCatalogView resolvedEntityView,
+      Supplier<TableMetadata> fallback) {
+    LOGGER.debug(String.format("Loading cached metadata for %s", 
tableIdentifier));
+    PolarisResolvedPathWrapper resolvedEntities =
+        resolvedEntityView.getResolvedPath(tableIdentifier, 
PolarisEntitySubType.TABLE);
+    TableLikeEntity tableLikeEntity = 
TableLikeEntity.of(resolvedEntities.getRawLeafEntity());
+    String cacheContent = tableLikeEntity.getMetadataCacheContent();
+    if (cacheContent != null) {
+      LOGGER.debug(String.format("Using cached metadata for %s", 
tableIdentifier));
+      return 
TableMetadataParser.fromJson(tableLikeEntity.getMetadataCacheContent());
+    } else {
+      TableMetadata metadata = fallback.get();
+      PolarisMetaStoreManager.EntityResult cacheResult =
+          cacheTableMetadata(
+              tableLikeEntity,
+              metadata,
+              maxBytesToCache,
+              callContext,
+              metastoreManager,
+              resolvedEntityView);
+      if (!cacheResult.isSuccess()) {
+        LOGGER.debug(String.format("Failed to cache metadata for %s", 
tableIdentifier));
+      }
+      return metadata;
+    }
+  }
+
+  /** Convert a {@link TableMetadata} to JSON, with the size bounded */
+  public static Optional<String> toBoundedJson(TableMetadata metadata, int 
maxBytes) {
+    try (StringWriter unboundedWriter = new StringWriter()) {
+      BoundedStringWriter boundedWriter = new 
BoundedStringWriter(unboundedWriter, maxBytes);
+      JsonGenerator generator = 
JsonUtil.factory().createGenerator(boundedWriter);
+      TableMetadataParser.toJson(metadata, generator);
+      generator.flush();
+      String result = boundedWriter.toString();
+      if (boundedWriter.isLimitExceeded()) {

Review Comment:
   A previous implementation had `BoundedStringWriter` throw an exception when 
the limit is hit, so the `JsonGenerator` actually stops instead of just sending 
strings into the void. 
   
   I am okay going back to this, but I don't like using an exception as flow 
control. WDYT @snazy ?



-- 
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