JingsongLi commented on code in PR #7699: URL: https://github.com/apache/paimon/pull/7699#discussion_r3215029730
########## paimon-common/src/main/java/org/apache/paimon/fs/cache/CachingFileIO.java: ########## @@ -0,0 +1,195 @@ +/* + * 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.catalog.CatalogContext; +import org.apache.paimon.fs.FileIO; +import org.apache.paimon.fs.FileStatus; +import org.apache.paimon.fs.Path; +import org.apache.paimon.fs.PositionOutputStream; +import org.apache.paimon.fs.SeekableInputStream; +import org.apache.paimon.options.CatalogOptions; +import org.apache.paimon.options.MemorySize; +import org.apache.paimon.options.Options; +import org.apache.paimon.utils.FileType; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.EnumSet; +import java.util.Map; +import java.util.Set; + +/** + * A {@link FileIO} wrapper that caches reads at block granularity. + * + * <p>Only file types in the whitelist are cached. Others are read directly from the delegate. + * + * <p>After deserialization, the cache is lazily recreated from the stored configuration. + */ +public class CachingFileIO implements FileIO { + + private static final long serialVersionUID = 1L; + + private final FileIO delegate; + private final Set<FileType> whitelist; + + @Nullable private final String cacheDir; + private final long maxSize; + private final int blockSize; + + private transient volatile LocalCacheManager cache; + + public CachingFileIO( + FileIO delegate, + LocalCacheManager cache, + Set<FileType> whitelist, + @Nullable String cacheDir, + long maxSize, + int blockSize) { + this.delegate = delegate; + this.cache = cache; + this.whitelist = EnumSet.copyOf(whitelist); + this.cacheDir = cacheDir; + this.maxSize = maxSize; + this.blockSize = blockSize; + cache.retain(); + } + + /** + * Wraps the given {@link FileIO} with caching if local cache is enabled in the catalog context. + * + * @param fileIO the FileIO to potentially wrap + * @param context the catalog context containing cache configuration + * @return a CachingFileIO if caching is enabled and configured, otherwise the original FileIO + */ + public static FileIO wrapWithCachingIfNeeded(FileIO fileIO, CatalogContext context) { + if (fileIO instanceof CachingFileIO) { + return fileIO; + } + Options options = context.options(); + if (!options.get(CatalogOptions.LOCAL_CACHE_ENABLED)) { + return fileIO; + } + + MemorySize maxSizeOpt = options.get(CatalogOptions.LOCAL_CACHE_MAX_SIZE); + long maxSize = maxSizeOpt == null ? Long.MAX_VALUE : maxSizeOpt.getBytes(); + int blockSize = (int) options.get(CatalogOptions.LOCAL_CACHE_BLOCK_SIZE).getBytes(); + + String cacheDir = options.get(CatalogOptions.LOCAL_CACHE_DIR); + LocalCacheManager cache; Review Comment: cache may need to be managed by catalog. -- 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]
