Browse Source

Merge "Lazily open ReadableChannel in BlockBasedFile.getOrLoadBlock"

tags/v5.3.0.201901161700-m1
Terry Parker 5 years ago
parent
commit
01f8e53b26

+ 39
- 2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/BlockBasedFile.java View File

@@ -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();
}
}
}
}

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCache.java View File

@@ -754,7 +754,7 @@ public final class DfsBlockCache {
* Supplier for readable channel
*/
@FunctionalInterface
public interface ReadableChannelSupplier {
interface ReadableChannelSupplier {
/**
* @return ReadableChannel
* @throws IOException

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCacheConfig.java View File

@@ -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;
}

+ 1
- 3
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftable.java View File

@@ -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);
}

Loading…
Cancel
Save