Skip to content

Commit 09ad00d

Browse files
committed
MultiPackIndex: add findPosition method
We will need this method to implement the #findIdxPosition in a DfsPackFile with multipack index. Change-Id: If333de4e5109f58074f566c1c6ef4419d4b42b41
1 parent e28c064 commit 09ad00d

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

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

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ public void jgit_resolve() throws IOException {
213213
MultiPackIndex midx = MultiPackIndexLoader
214214
.read(new ByteArrayInputStream(out.toByteArray()));
215215

216-
217216
Set<ObjectId> results = new HashSet<>();
218217
midx.resolve(results, abbrev, 100);
219218

@@ -257,7 +256,6 @@ public void jgit_resolve_matchLimit() throws IOException {
257256
MultiPackIndex midx = MultiPackIndexLoader
258257
.read(new ByteArrayInputStream(out.toByteArray()));
259258

260-
261259
Set<ObjectId> results = new HashSet<>();
262260
midx.resolve(results, abbrev, 2);
263261

@@ -273,8 +271,7 @@ public void jgit_resolve_noMatches() throws IOException {
273271
AbbreviatedObjectId abbrev = AbbreviatedObjectId
274272
.fromString("4400000000");
275273

276-
PackIndex idxOne = indexWith(
277-
"0000000000000000000000000000000000000001",
274+
PackIndex idxOne = indexWith("0000000000000000000000000000000000000001",
278275
"3000000000000000000000000000000000000005",
279276
"32fe829a1b000000000000000000000000000001",
280277
"32fe829a1c000000000000000000000000000001",
@@ -315,13 +312,67 @@ public void jgit_resolve_empty() throws IOException {
315312
MultiPackIndex midx = MultiPackIndexLoader
316313
.read(new ByteArrayInputStream(out.toByteArray()));
317314

318-
319315
Set<ObjectId> results = new HashSet<>();
320316
midx.resolve(results, abbrev, 200);
321317

322318
assertEquals(0, results.size());
323319
}
324320

321+
@Test
322+
public void jgit_findPosition() throws IOException {
323+
PackIndex idxOne = FakeIndexFactory.indexOf(List.of(
324+
new FakeIndexFactory.IndexObject(
325+
"0000000000000000000000000000000000000001", 500),
326+
new FakeIndexFactory.IndexObject(
327+
"0000000000000000000000000000000000000005", 12),
328+
new FakeIndexFactory.IndexObject(
329+
"0000000000000000000000000000000000000010", 1500)));
330+
PackIndex idxTwo = FakeIndexFactory.indexOf(List.of(
331+
new FakeIndexFactory.IndexObject(
332+
"0000000000000000000000000000000000000002", 501),
333+
new FakeIndexFactory.IndexObject(
334+
"0000000000000000000000000000000000000003", 13),
335+
new FakeIndexFactory.IndexObject(
336+
"0000000000000000000000000000000000000015", 1501)));
337+
PackIndex idxThree = FakeIndexFactory.indexOf(List.of(
338+
new FakeIndexFactory.IndexObject(
339+
"0000000000000000000000000000000000000004", 502),
340+
new FakeIndexFactory.IndexObject(
341+
"0000000000000000000000000000000000000007", 14),
342+
new FakeIndexFactory.IndexObject(
343+
"0000000000000000000000000000000000000012", 1502)));
344+
345+
Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo, "p3",
346+
idxThree);
347+
MultiPackIndexWriter writer = new MultiPackIndexWriter();
348+
ByteArrayOutputStream out = new ByteArrayOutputStream();
349+
writer.write(NullProgressMonitor.INSTANCE, out, packs);
350+
351+
MultiPackIndex midx = MultiPackIndexLoader
352+
.read(new ByteArrayInputStream(out.toByteArray()));
353+
assertEquals(3, midx.getPackNames().length);
354+
assertEquals(0, midx.findPosition(ObjectId
355+
.fromString("0000000000000000000000000000000000000001")));
356+
assertEquals(1, midx.findPosition(ObjectId
357+
.fromString("0000000000000000000000000000000000000002")));
358+
assertEquals(2, midx.findPosition(ObjectId
359+
.fromString("0000000000000000000000000000000000000003")));
360+
assertEquals(3, midx.findPosition(ObjectId
361+
.fromString("0000000000000000000000000000000000000004")));
362+
assertEquals(4, midx.findPosition(ObjectId
363+
.fromString("0000000000000000000000000000000000000005")));
364+
assertEquals(5, midx.findPosition(ObjectId
365+
.fromString("0000000000000000000000000000000000000007")));
366+
assertEquals(6, midx.findPosition(ObjectId
367+
.fromString("0000000000000000000000000000000000000010")));
368+
assertEquals(7, midx.findPosition(ObjectId
369+
.fromString("0000000000000000000000000000000000000012")));
370+
assertEquals(8, midx.findPosition(ObjectId
371+
.fromString("0000000000000000000000000000000000000015")));
372+
373+
assertNull(midx.find(ObjectId.zeroId()));
374+
}
375+
325376
private static PackIndex indexWith(String... oids) {
326377
List<FakeIndexFactory.IndexObject> idxObjs = new ArrayList<>(
327378
oids.length);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ public interface MultiPackIndex {
5151
*/
5252
PackOffset find(AnyObjectId objectId);
5353

54+
/**
55+
* Position of the object in this midx, when all covered objects are ordered
56+
* by SHA1.
57+
* <p>
58+
* As the midx removes duplicates, this position is NOT equivalent to
59+
* "position in pack + total count of objects in previous packs in the
60+
* stack".
61+
*
62+
* @param objectId
63+
* an object id
64+
* @return position of the object in this multipack index
65+
*/
66+
int findPosition(AnyObjectId objectId);
67+
5468
/**
5569
* Find objects matching the prefix abbreviation.
5670
*

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public boolean hasObject(AnyObjectId oid) {
6060
return idx.findMultiPackIndexPosition(oid) != -1;
6161
}
6262

63+
@Override
64+
public int findPosition(AnyObjectId oid) {
65+
return idx.findMultiPackIndexPosition(oid);
66+
}
67+
6368
@Override
6469
@Nullable
6570
public PackOffset find(AnyObjectId objectId) {

0 commit comments

Comments
 (0)