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>
This commit is contained in:
Anna Papitto 2023-05-30 16:20:54 +02:00
parent 181b629f7d
commit 74547f4a68
4 changed files with 18 additions and 6 deletions

View File

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

View File

@ -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<>(

View File

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

View File

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