From 74547f4a68c444b495af014dcfc07df3e09bbc36 Mon Sep 17 00:00:00 2001 From: Anna Papitto Date: Tue, 30 May 2023 16:20:54 +0200 Subject: [PATCH] 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 --- .../storage/file/PackReverseIndexTest.java | 5 ++--- .../jgit/internal/storage/dfs/DfsPackFile.java | 2 +- .../eclipse/jgit/internal/storage/file/Pack.java | 2 +- .../internal/storage/file/PackReverseIndex.java | 15 ++++++++++++++- 4 files changed, 18 insertions(+), 6 deletions(-) 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 292e3e758..cd37c354a 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 c745b8e6e..466d5d435 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 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 6e74136c1..5d401f451 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 1a5adb4a1..fdbd36413 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 @@ -48,6 +48,19 @@ public class PackReverseIndex { /** Mapping from indices in offset order to indices in SHA-1 order. */ 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. @@ -55,7 +68,7 @@ public class PackReverseIndex { * @param packIndex * forward index - entries to (reverse) index. */ - public PackReverseIndex(PackIndex packIndex) { + private PackReverseIndex(PackIndex packIndex) { index = packIndex; final long cnt = index.getObjectCount();