You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2020/01/21 08:29:52 UTC

[GitHub] [incubator-iceberg] waterlx commented on a change in pull request #653: Add CachingCatalog

waterlx commented on a change in pull request #653: Add CachingCatalog
URL: https://github.com/apache/incubator-iceberg/pull/653#discussion_r368864075
 
 

 ##########
 File path: core/src/main/java/org/apache/iceberg/CachingCatalog.java
 ##########
 @@ -0,0 +1,100 @@
+/*
+ * 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 java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+
+public class CachingCatalog implements Catalog {
+
+  public static Catalog wrap(Catalog catalog) {
+    return new CachingCatalog(catalog);
+  }
+
+  private final Cache<TableIdentifier, Table> tableCache = Caffeine.newBuilder()
+      .softValues()
+      .expireAfterAccess(1, TimeUnit.MINUTES)
+      .build();
+  private final Catalog catalog;
+
+  private CachingCatalog(Catalog catalog) {
+    this.catalog = catalog;
+  }
+
+  @Override
+  public Table loadTable(TableIdentifier ident) {
+    return tableCache.get(ident, catalog::loadTable);
+  }
+
+  @Override
+  public Table createTable(TableIdentifier ident, Schema schema, PartitionSpec spec, String location,
+                           Map<String, String> properties) {
+    AtomicBoolean created = new AtomicBoolean(false);
+    Table table = tableCache.get(ident, identifier -> {
+      created.set(true);
+      return catalog.createTable(identifier, schema, spec, location, properties);
+    });
+
+    if (!created.get()) {
+      throw new AlreadyExistsException("Table already exists: %s", ident);
+    }
+
+    return table;
+  }
+
+  @Override
+  public Transaction newCreateTableTransaction(TableIdentifier ident, Schema schema, PartitionSpec spec,
+                                               String location, Map<String, String> properties) {
+    // create a new transaction without altering the cache. the table doesn't exist until the transaction is committed.
+    // if the table is created before the transaction commits, any cached version is correct and the transaction create
+    // will fail. if the transaction commits before another create, then the cache will be empty.
+    return catalog.newCreateTableTransaction(ident, schema, spec, location, properties);
+  }
+
+  @Override
+  public Transaction newReplaceTableTransaction(TableIdentifier ident, Schema schema, PartitionSpec spec,
+                                                String location, Map<String, String> properties, boolean orCreate) {
+    // create a new transaction without altering the cache. the table doesn't change until the transaction is committed.
+    // when the transaction commits, invalidate the table in the cache if it is present.
+    return CommitCallbackTransaction.addCallback(
+        catalog.newReplaceTableTransaction(ident, schema, spec, location, properties, orCreate),
+        () -> tableCache.invalidate(ident));
+  }
+
+  @Override
+  public boolean dropTable(TableIdentifier ident, boolean purge) {
+    boolean dropped = catalog.dropTable(ident, false);
 
 Review comment:
   Hi @rdblue could you please share your idea why purge is not passed further but dropTable with "false" instead?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org