Aitozi commented on code in PR #7699:
URL: https://github.com/apache/paimon/pull/7699#discussion_r3219965412


##########
paimon-common/src/main/java/org/apache/paimon/fs/cache/LocalDiskCacheManager.java:
##########
@@ -0,0 +1,287 @@
+/*
+ * 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.paimon.fs.cache;
+
+import org.apache.paimon.annotation.VisibleForTesting;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+
+/** Block-level local disk cache with LRU eviction. Thread-safe. */
+public class LocalDiskCacheManager implements LocalCacheManager {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(LocalDiskCacheManager.class);
+
+    private static final Map<CacheKey, LocalDiskCacheManager> SHARED_CACHES =
+            new ConcurrentHashMap<>();
+
+    private final File cacheDir;
+    private final long maxSizeBytes;
+    private final int blockSize;
+    private final Object lock = new Object();
+    private long currentSize;
+
+    public LocalDiskCacheManager(String cacheDir, long maxSizeBytes, int 
blockSize) {
+        this.cacheDir = new File(cacheDir);
+        this.maxSizeBytes = maxSizeBytes;
+        this.blockSize = blockSize;
+        this.cacheDir.mkdirs();
+        this.currentSize = scanSize();
+    }
+
+    /** Returns a shared instance for the given parameters, creating one if 
needed. */
+    public static LocalDiskCacheManager getOrCreate(
+            String cacheDir, long maxSizeBytes, int blockSize) {
+        CacheKey key = new CacheKey(cacheDir, maxSizeBytes, blockSize);
+        return SHARED_CACHES.computeIfAbsent(
+                key, k -> new LocalDiskCacheManager(cacheDir, maxSizeBytes, 
blockSize));
+    }
+
+    public int blockSize() {
+        return blockSize;
+    }
+
+    public byte[] getBlock(String filePath, int blockIndex) {
+        File path = cachePath(filePath, blockIndex);
+        if (!path.exists()) {
+            return null;
+        }
+        try {
+            byte[] data = Files.readAllBytes(path.toPath());
+            // touch to update mtime for LRU
+            path.setLastModified(System.currentTimeMillis());
+            return data;
+        } catch (IOException e) {
+            LOG.debug("Failed to read cache block: {}", path, e);
+            return null;
+        }
+    }
+
+    public void putBlock(String filePath, int blockIndex, byte[] data) {
+        File path = cachePath(filePath, blockIndex);

Review Comment:
   That sounds great, thanks for the explanation.



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