PackReverseIndex: use static builder instead of constructor PackReverseIndex instances are created using the constructor directly, which limits control over the construction logic and refactoring opportunities for the class itself. These will be needed for a file-based implementation of the reverse index. Use a static builder method to create a PackReverseIndex instance using a pack's forward index. Change-Id: I4421d907cd61d9ac932df5377e5e28a81679b63f Signed-off-by: Anna Papitto <annapapitto@google.com>
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackReverseIndexTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackReverseIndexTest.java index 292e3e7..cd37c35 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackReverseIndexTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackReverseIndexTest.java
@@ -37,9 +37,8 @@ public class PackReverseIndexTest extends RepositoryTestCase { public void setUp() throws Exception { super.setUp(); // index with both small (< 2^31) and big offsets - idx = PackIndex.open(JGitTestUtil.getTestResourceFile( - "pack-huge.idx")); - reverseIdx = new PackReverseIndex(idx); + idx = PackIndex.open(JGitTestUtil.getTestResourceFile("pack-huge.idx")); + reverseIdx = PackReverseIndex.computeFromIndex(idx); } /**
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackFile.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackFile.java index c745b8e..466d5d4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackFile.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackFile.java
@@ -1068,7 +1068,7 @@ private DfsBlockCache.Ref<PackReverseIndex> loadReverseIdx( DfsReader ctx, DfsStreamKey revKey, PackIndex idx) { ctx.stats.readReverseIdx++; long start = System.nanoTime(); - PackReverseIndex revidx = new PackReverseIndex(idx); + PackReverseIndex revidx = PackReverseIndex.computeFromIndex(idx); reverseIndex = revidx; ctx.stats.readReverseIdxMicros += elapsedMicros(start); return new DfsBlockCache.Ref<>(
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/Pack.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/Pack.java index 6e74136..5d401f4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/Pack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/Pack.java
@@ -1148,7 +1148,7 @@ synchronized PackBitmapIndex getBitmapIndex() throws IOException { private synchronized PackReverseIndex getReverseIdx() throws IOException { if (reverseIdx == null) - reverseIdx = new PackReverseIndex(idx()); + reverseIdx = PackReverseIndex.computeFromIndex(idx()); return reverseIdx; }
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackReverseIndex.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackReverseIndex.java index 1a5adb4..fdbd364 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackReverseIndex.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackReverseIndex.java
@@ -49,13 +49,26 @@ public class PackReverseIndex { private final int[] nth; /** + * Compute an in-memory pack reverse index from the in-memory pack forward + * index. This computation uses insertion sort, which has a quadratic + * runtime on average. + * + * @param packIndex + * the forward index to compute from + * @return the reverse index instance + */ + public static PackReverseIndex computeFromIndex(PackIndex packIndex) { + return new PackReverseIndex(packIndex); + } + + /** * Create reverse index from straight/forward pack index, by indexing all * its entries. * * @param packIndex * forward index - entries to (reverse) index. */ - public PackReverseIndex(PackIndex packIndex) { + private PackReverseIndex(PackIndex packIndex) { index = packIndex; final long cnt = index.getObjectCount();