henrib commented on code in PR #5606:
URL: https://github.com/apache/hive/pull/5606#discussion_r1951290901


##########
standalone-metastore/metastore-catalog/src/main/java/org/apache/iceberg/HiveCachingCatalog.java:
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.iceberg;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.RemovalCause;
+import com.github.benmanes.caffeine.cache.RemovalListener;
+import com.github.benmanes.caffeine.cache.Ticker;
+import java.time.Duration;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.NamespaceNotEmptyException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+
+/**
+ * Class that wraps an Iceberg Catalog to cache tables.
+ * Initial code in:
+ * 
https://github.com/apache/iceberg/blob/1.3.x/core/src/main/java/org/apache/iceberg/CachingCatalog.java
+ * Main difference is the SupportsNamespace.
+ *
+ * <p>See {@link CatalogProperties#CACHE_EXPIRATION_INTERVAL_MS} for more 
details regarding special
+ * values for {@code expirationIntervalMillis}.
+ * @param <CATALOG> the catalog class
+ */
+public class HiveCachingCatalog<CATALOG extends Catalog & SupportsNamespaces> 
implements Catalog, SupportsNamespaces {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HiveCachingCatalog.class);
+  @SuppressWarnings("checkstyle:VisibilityModifier")
+  protected final long expirationMs;
+  @SuppressWarnings("checkstyle:VisibilityModifier")
+  protected final Cache<TableIdentifier, Table> tableCache;
+  private final CATALOG catalog;
+
+  @SuppressWarnings("checkstyle:VisibilityModifier")
+  protected HiveCachingCatalog(CATALOG catalog, long expiration) {
+    Preconditions.checkArgument(expiration > 0,
+        "When %s is <= 0, the catalog cache should be disabled.",
+        CatalogProperties.CACHE_EXPIRATION_INTERVAL_MS);
+    this.catalog = catalog;
+    this.expirationMs = expiration;
+    this.tableCache = createTableCache(Ticker.systemTicker());
+  }
+
+  public CATALOG unwrap() {
+    return catalog;
+  }
+
+  public static <C extends Catalog & SupportsNamespaces>
+  HiveCachingCatalog<C> wrap(C catalog, long expirationIntervalMillis) {
+    return new HiveCachingCatalog(catalog, expirationIntervalMillis);
+  }
+
+
+  private Cache<TableIdentifier, Table> createTableCache(Ticker ticker) {
+    Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder().softValues();
+    if (expirationMs > 0) {
+      return cacheBuilder
+          .removalListener(new MetadataTableInvalidatingRemovalListener())
+          .executor(Runnable::run) // Makes the callbacks to removal listener 
synchronous
+          .expireAfterAccess(Duration.ofMillis(expirationMs))
+          .ticker(ticker)
+          .build();
+    }
+    return cacheBuilder.build();
+  }
+
+  /**
+   * Eventual canonical transformation.
+   * @param tableIdentifier the identifier
+   * @return the canonical representation
+   */
+  protected TableIdentifier canonicalizeIdentifier(TableIdentifier 
tableIdentifier) {

Review Comment:
   I wanted to keep the feature through potential extension if needed.



-- 
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: gitbox-unsubscr...@hive.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org
For additional commands, e-mail: gitbox-h...@hive.apache.org

Reply via email to