Skip to content

Commit 22bbd3d

Browse files
committed
MultiPackIndex: add getObjectCount() method
When we build a chain a multipack indexes, the midx position of an object is the position in its midx plus the object count of its base. We need the midx to provide this count, because due to duplicates, it doesn't necessarily match the sum of objects in the covered packs. Add a getter to the MultiPackIndex to get the count of objects in the index. Change-Id: I8bec9d4aa4079cc0291ef35efe75ccfbd656727e
1 parent 09ad00d commit 22bbd3d

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/midx/MultiPackIndexTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,56 @@ public void jgit_findPosition() throws IOException {
373373
assertNull(midx.find(ObjectId.zeroId()));
374374
}
375375

376+
@Test
377+
public void jgit_getObjectCount() throws IOException {
378+
PackIndex idxOne = FakeIndexFactory.indexOf(List.of(
379+
new FakeIndexFactory.IndexObject(
380+
"0000000000000000000000000000000000000001", 500),
381+
new FakeIndexFactory.IndexObject(
382+
"0000000000000000000000000000000000000005", 12),
383+
new FakeIndexFactory.IndexObject(
384+
"0000000000000000000000000000000000000010", 1500)));
385+
PackIndex idxTwo = FakeIndexFactory.indexOf(List.of(
386+
new FakeIndexFactory.IndexObject(
387+
"0000000000000000000000000000000000000002", 501),
388+
new FakeIndexFactory.IndexObject(
389+
"0000000000000000000000000000000000000003", 13),
390+
new FakeIndexFactory.IndexObject(
391+
"0000000000000000000000000000000000000015", 1501)));
392+
PackIndex idxThree = FakeIndexFactory.indexOf(List.of(
393+
new FakeIndexFactory.IndexObject(
394+
"0000000000000000000000000000000000000004", 502),
395+
new FakeIndexFactory.IndexObject(
396+
"0000000000000000000000000000000000000007", 14),
397+
new FakeIndexFactory.IndexObject(
398+
"0000000000000000000000000000000000000012", 1502)));
399+
400+
Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo, "p3",
401+
idxThree);
402+
MultiPackIndexWriter writer = new MultiPackIndexWriter();
403+
ByteArrayOutputStream out = new ByteArrayOutputStream();
404+
writer.write(NullProgressMonitor.INSTANCE, out, packs);
405+
406+
MultiPackIndex midx = MultiPackIndexLoader
407+
.read(new ByteArrayInputStream(out.toByteArray()));
408+
assertEquals(9, midx.getObjectCount());
409+
}
410+
411+
@Test
412+
public void jgit_getObjectCount_emtpy() throws IOException {
413+
PackIndex idxOne = FakeIndexFactory.indexOf(List.of());
414+
PackIndex idxTwo = FakeIndexFactory.indexOf(List.of());
415+
416+
Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo);
417+
MultiPackIndexWriter writer = new MultiPackIndexWriter();
418+
ByteArrayOutputStream out = new ByteArrayOutputStream();
419+
writer.write(NullProgressMonitor.INSTANCE, out, packs);
420+
MultiPackIndex midx = MultiPackIndexLoader
421+
.read(new ByteArrayInputStream(out.toByteArray()));
422+
423+
assertEquals(0, midx.getObjectCount());
424+
}
425+
376426
private static PackIndex indexWith(String... oids) {
377427
List<FakeIndexFactory.IndexObject> idxObjs = new ArrayList<>(
378428
oids.length);

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/midx/MultiPackIndex.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public interface MultiPackIndex {
6565
*/
6666
int findPosition(AnyObjectId objectId);
6767

68+
/**
69+
* Number of objects in this midx
70+
* <p>
71+
* This number doesn't match with the sum of objects in each covered pack
72+
* because midx removes duplicates.
73+
*
74+
* @return number of objects in this midx
75+
*/
76+
int getObjectCount();
77+
6878
/**
6979
* Find objects matching the prefix abbreviation.
7080
*

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/midx/MultiPackIndexV1.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public int findPosition(AnyObjectId oid) {
6565
return idx.findMultiPackIndexPosition(oid);
6666
}
6767

68+
@Override
69+
public int getObjectCount() {
70+
return idx.getObjectCount();
71+
}
72+
6873
@Override
6974
@Nullable
7075
public PackOffset find(AnyObjectId objectId) {
@@ -290,5 +295,9 @@ private int idOffset(int position) {
290295
long getMemorySize() {
291296
return 4L + byteArrayLengh(oidLookup) + (FANOUT * 4);
292297
}
298+
299+
int getObjectCount() {
300+
return fanoutTable[FANOUT - 1];
301+
}
293302
}
294303
}

0 commit comments

Comments
 (0)