From fa0a93119cd3bc6e2673be25a89b8d7638598f3d Mon Sep 17 00:00:00 2001 From: Kevin Corcoran Date: Wed, 13 Apr 2016 15:55:08 -0700 Subject: [PATCH] Make streamFileThreshold configurable Previously, the streamFileThreshold, the threshold at which a file would be streamed rather than loaded entirely into memory, was only configurable on a global basis. This commit makes this threshold configurable on a per-loader basis. Bug: 490404 Change-Id: I492c18c3155dbf56eedda9044a61d76120fd75f9 Signed-off-by: Kevin Corcoran Signed-off-by: David Pursehouse --- .../internal/storage/file/PackFileTest.java | 21 ++++++++++++++ .../jgit/internal/storage/dfs/DfsReader.java | 5 +--- .../internal/storage/file/WindowCursor.java | 6 ++-- .../org/eclipse/jgit/lib/ObjectReader.java | 29 +++++++++++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileTest.java index ba07d6842f..1c10bb335a 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackFileTest.java @@ -310,6 +310,27 @@ public class PackFileTest extends LocalDiskRepositoryTestCase { } } + @Test + public void testConfigurableStreamFileThreshold() throws Exception { + byte[] data = getRng().nextBytes(300); + RevBlob id = tr.blob(data); + tr.branch("master").commit().add("A", id).create(); + tr.packAndPrune(); + assertTrue("has blob", wc.has(id)); + + ObjectLoader ol = wc.open(id); + ObjectStream in = ol.openStream(); + assertTrue(in instanceof ObjectStream.SmallStream); + assertEquals(300, in.available()); + in.close(); + + wc.setStreamFileThreshold(299); + ol = wc.open(id); + in = ol.openStream(); + assertTrue(in instanceof ObjectStream.Filter); + assertEquals(1, in.available()); + } + private static byte[] clone(int first, byte[] base) { byte[] r = new byte[base.length]; System.arraycopy(base, 1, r, 1, r.length - 1); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReader.java index 2f61dea0d5..8c9329503f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReader.java @@ -113,6 +113,7 @@ public final class DfsReader extends ObjectReader implements ObjectReuseAsIs { DfsReader(DfsObjDatabase db) { this.db = db; + this.streamFileThreshold = db.getReaderOptions().getStreamFileThreshold(); } DfsReaderOptions getOptions() { @@ -125,10 +126,6 @@ public final class DfsReader extends ObjectReader implements ObjectReuseAsIs { return baseCache; } - int getStreamFileThreshold() { - return getOptions().getStreamFileThreshold(); - } - @Override public ObjectReader newReader() { return new DfsReader(db); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCursor.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCursor.java index a2c0561ae1..a742d1747e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCursor.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCursor.java @@ -94,12 +94,14 @@ final class WindowCursor extends ObjectReader implements ObjectReuseAsIs { WindowCursor(FileObjectDatabase db) { this.db = db; this.createdFromInserter = null; + this.streamFileThreshold = WindowCache.getStreamFileThreshold(); } WindowCursor(FileObjectDatabase db, @Nullable ObjectDirectoryInserter createdFromInserter) { this.db = db; this.createdFromInserter = createdFromInserter; + this.streamFileThreshold = WindowCache.getStreamFileThreshold(); } DeltaBaseCache getDeltaBaseCache() { @@ -337,10 +339,6 @@ final class WindowCursor extends ObjectReader implements ObjectReuseAsIs { } } - int getStreamFileThreshold() { - return WindowCache.getStreamFileThreshold(); - } - @Override @Nullable public ObjectInserter getCreatedFromInserter() { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java index b23145d798..263fc99745 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java @@ -65,6 +65,12 @@ public abstract class ObjectReader implements AutoCloseable { /** Type hint indicating the caller doesn't know the type. */ public static final int OBJ_ANY = -1; + /** + * The threshold at which a file will be streamed rather than + * loaded entirely into memory. + */ + protected int streamFileThreshold; + /** * Construct a new reader from the same data. *

@@ -444,6 +450,29 @@ public abstract class ObjectReader implements AutoCloseable { @Override public abstract void close(); + /** + * Sets the threshold at which a file will be streamed rather than loaded + * entirely into memory + * + * @param threshold + * the new threshold + * @since 4.6 + */ + public void setStreamFileThreshold(int threshold) { + streamFileThreshold = threshold; + } + + /** + * Returns the threshold at which a file will be streamed rather than loaded + * entirely into memory + * + * @return the threshold in bytes + * @since 4.6 + */ + public int getStreamFileThreshold() { + return streamFileThreshold; + } + /** * Wraps a delegate ObjectReader. * -- 2.39.5