diff options
author | Minh Thai <mthai@google.com> | 2019-01-09 12:53:51 -0800 |
---|---|---|
committer | Minh Thai <mthai@google.com> | 2019-01-10 14:22:54 -0800 |
commit | 513d80db1066beaa284c97b1515cd069049f6f05 (patch) | |
tree | 53ac679904cf25af4c458a66026260a11e77cb95 /org.eclipse.jgit/src | |
parent | 23c30c6310686392dbbff4385b96a3403e09168a (diff) | |
download | jgit-513d80db1066beaa284c97b1515cd069049f6f05.tar.gz jgit-513d80db1066beaa284c97b1515cd069049f6f05.zip |
Lazily open ReadableChannel in BlockBasedFile.getOrLoadBlock
To avoid opening the readable channel in case of DfsBlockCache
hits. Also cleaning up typos around DfsBlockCache.
Change-Id: I615e349cb4838387c1e6743cdc384d1b81b54369
Signed-off-by: Minh Thai <mthai@google.com>
Diffstat (limited to 'org.eclipse.jgit/src')
4 files changed, 42 insertions, 7 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/BlockBasedFile.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/BlockBasedFile.java index 600b1a44d1..4d14742440 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/BlockBasedFile.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/BlockBasedFile.java @@ -129,8 +129,8 @@ abstract class BlockBasedFile { } DfsBlock getOrLoadBlock(long pos, DfsReader ctx) throws IOException { - try (ReadableChannel rc = ctx.db.openFile(desc, ext)) { - return cache.getOrLoad(this, pos, ctx, () -> rc); + try (LazyChannel c = new LazyChannel(ctx, desc, ext)) { + return cache.getOrLoad(this, pos, ctx, c); } } @@ -203,4 +203,41 @@ abstract class BlockBasedFile { static long elapsedMicros(long start) { return (System.nanoTime() - start) / 1000L; } + + /** + * A supplier of readable channel that opens the channel lazily. + */ + private static class LazyChannel + implements AutoCloseable, DfsBlockCache.ReadableChannelSupplier { + private final DfsReader ctx; + private final DfsPackDescription desc; + private final PackExt ext; + + private ReadableChannel rc = null; + + LazyChannel(DfsReader ctx, DfsPackDescription desc, PackExt ext) { + this.ctx = ctx; + this.desc = desc; + this.ext = ext; + } + + @Override + public ReadableChannel get() throws IOException { + if (rc == null) { + synchronized (this) { + if (rc == null) { + rc = ctx.db.openFile(desc, ext); + } + } + } + return rc; + } + + @Override + public void close() throws IOException { + if (rc != null) { + rc.close(); + } + } + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCache.java index d9e9327d3b..c6e2fae42f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCache.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCache.java @@ -754,7 +754,7 @@ public final class DfsBlockCache { * Supplier for readable channel */ @FunctionalInterface - public interface ReadableChannelSupplier { + interface ReadableChannelSupplier { /** * @return ReadableChannel * @throws IOException diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCacheConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCacheConfig.java index eea9ef4e0b..cd1fa5f78f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCacheConfig.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCacheConfig.java @@ -212,7 +212,7 @@ public class DfsBlockCacheConfig { * consumer of wait time in milliseconds. * @return {@code this} */ - public DfsBlockCacheConfig setReflockWaitTimeConsumer(Consumer<Long> c) { + public DfsBlockCacheConfig setRefLockWaitTimeConsumer(Consumer<Long> c) { refLock = c; return this; } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftable.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftable.java index a80797730f..4853298012 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftable.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftable.java @@ -128,9 +128,7 @@ public class DfsReftable extends BlockBasedFile { open().setReadAheadBytes(readAhead); } - DfsBlock block = cache.getOrLoad(file, pos, ctx, () -> { - return open(); - }); + DfsBlock block = cache.getOrLoad(file, pos, ctx, () -> open()); if (block.start == pos && block.size() >= cnt) { return block.zeroCopyByteBuffer(cnt); } |