wuwenchi commented on code in PR #31276:
URL: https://github.com/apache/doris/pull/31276#discussion_r1499250153


##########
fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalRowCountCache.java:
##########
@@ -0,0 +1,113 @@
+// 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.doris.datasource;
+
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.Config;
+import org.apache.doris.statistics.BasicAsyncCacheLoader;
+import org.apache.doris.statistics.StatisticConstants;
+import org.apache.doris.statistics.util.StatisticsUtil;
+
+import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.time.Duration;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+
+public class ExternalRowCountCache {
+
+    private static final Logger LOG = 
LogManager.getLogger(ExternalRowCountCache.class);
+    private final AsyncLoadingCache<RowCountKey, Optional<Long>> rowCountCache;
+
+    public ExternalRowCountCache(ExecutorService executor) {
+        rowCountCache = Caffeine.newBuilder()
+                .maximumSize(Config.max_external_table_row_count_cache_num)
+                
.expireAfterWrite(Duration.ofHours(StatisticConstants.EXTERNAL_ROW_COUNT_EXPIRE_INTERVAL_HOURS))
+                .executor(executor)
+                .buildAsync(new RowCountCacheLoader());
+    }
+
+    public static class RowCountKey {
+        private final long catalogId;
+        private final long dbId;
+        private final long tableId;
+
+        public RowCountKey(long catalogId, long dbId, long tableId) {
+            this.catalogId = catalogId;
+            this.dbId = dbId;
+            this.tableId = tableId;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (!(obj instanceof RowCountKey)) {
+                return false;
+            }
+            return ((RowCountKey) obj).tableId == this.tableId;
+        }
+
+        @Override
+        public int hashCode() {
+            return (int) tableId;
+        }
+    }
+
+    public static class RowCountCacheLoader extends 
BasicAsyncCacheLoader<RowCountKey, Optional<Long>> {
+
+        @Override
+        protected Optional<Long> doLoad(RowCountKey rowCountKey) {
+            try {
+                TableIf table = 
StatisticsUtil.findTable(rowCountKey.catalogId, rowCountKey.dbId, 
rowCountKey.tableId);
+                return Optional.of(table.fetchRowCount());
+            } catch (Exception e) {
+                LOG.warn("Failed to get table with catalogId {}, dbId {}, 
tableId {}", rowCountKey.catalogId,
+                        rowCountKey.dbId, rowCountKey.tableId);
+                return Optional.empty();
+            }
+        }
+    }
+
+    /**
+     * Get cached row count for the given table. Return 0 if cached not loaded 
or table not exists.

Review Comment:
   How do you express an empty table?



-- 
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: commits-unsubscr...@doris.apache.org

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


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

Reply via email to