1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/*
* Copyright (C) 2024, GerritForge Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.internal.storage.midx;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.jgit.internal.storage.file.PackIndex;
import org.eclipse.jgit.junit.FakeIndexFactory;
import org.eclipse.jgit.junit.JGitTestUtil;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectId;
import org.junit.Test;
public class MultiPackIndexTest {
@Test
public void basic_upstream() throws IOException {
int knownPackId = 22;
int knowOffset = 258;
String knownOid = "3f4ee50f784c1e9550f09a67d2ffc1bc76917bdc";
String knownPackName = "pack-e4b191e4343f2b7ff851026c2d8595a001077344.idx";
String[] packNames = {
"pack-15d67b35f2b6a66ff995e09cedb36b101e0e0262.idx",
"pack-1a979514a5965e71523187a17806e03af44344ed.idx",
"pack-1de6731c035633ba8f5b41dacbc680a5a36ddd90.idx",
"pack-1ee98948e4e362c56f3cdec7f5837d06e152854f.idx",
"pack-1f6fe52ac3d33f3091d8eb8497474554bfa80bc4.idx",
"pack-34b1aa6b437a9d968412454204c2676a88dc55fa.idx",
"pack-3b245f7b4aff32a52d0520608f662bbf403792b9.idx",
"pack-47901f7f8d1c440492035c4165796a330c7f79e0.idx",
"pack-4e7f889b79aea8905a0062ce1bd68e5ef3af6a55.idx",
"pack-71ea652e4aea2cbc609545b4fbc3eda6325d88a1.idx",
"pack-723b1238411a4257c18167e91fbabed313ba332f.idx",
"pack-7bd57092a7daa4dc31277e1ec86f3de8d968ae17.idx",
"pack-883d4f469c5ea0f6d373ee623a758aeaf17715fc.idx",
"pack-8eadd378a011ddaa5ec751f2a6d9789ef501120f.idx",
"pack-92221b6f79a211944ccc6740fc22c9553ea1ba22.idx",
"pack-b139d0cae5f54c70d057a8f4d2cf99f0ae0c326c.idx",
"pack-b4f5c96d1fa6b1fac17a2a43710693c5514a9224.idx",
"pack-bed4bc1521f965e55a5a8a58dffaaefc70ea4753.idx",
"pack-cdc6baa7d90707a3c0dac4c188f797f0f79b97bb.idx",
"pack-d6d58a58fa24b74c8c082f4f63c4d2ddfb824cc9.idx",
"pack-daec59ae07f1091f3b81bd8266481bb5db3c868a.idx",
"pack-e2197d60e09ad9091407eff4e06d39ec940851e1.idx",
"pack-e4b191e4343f2b7ff851026c2d8595a001077344.idx",
"pack-eedf783b5da4caa57be33b08990fe57f245a7413.idx",
"pack-efb23e968801b9050bc70f0115a8a0eec88fb879.idx",
"pack-f919c0660c207ddf6bb0569a3041d682d19fb4f7.idx" };
MultiPackIndex midx = MultiPackIndexLoader
.open(JGitTestUtil.getTestResourceFile("multi-pack-index.v1"));
assertNotNull(midx);
assertArrayEquals(packNames, midx.getPackNames());
MultiPackIndex.PackOffset oo = midx.find(ObjectId.fromString(knownOid));
assertEquals(knowOffset, oo.getOffset());
assertEquals(knownPackId, oo.getPackId());
assertEquals(knownPackName, midx.getPackNames()[oo.getPackId()]);
}
@Test
public void basicMidx() throws IOException {
PackIndex idxOne = FakeIndexFactory.indexOf(List.of(
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000001", 500),
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000005", 12),
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000010", 1500)));
PackIndex idxTwo = FakeIndexFactory.indexOf(List.of(
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000002", 501),
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000003", 13),
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000015", 1501)));
PackIndex idxThree = FakeIndexFactory.indexOf(List.of(
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000004", 502),
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000007", 14),
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000012", 1502)));
Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo, "p3",
idxThree);
MultiPackIndexWriter writer = new MultiPackIndexWriter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.write(NullProgressMonitor.INSTANCE, out, packs);
MultiPackIndex midx = MultiPackIndexLoader
.read(new ByteArrayInputStream(out.toByteArray()));
assertEquals(3, midx.getPackNames().length);
assertInIndex(midx, 0, "0000000000000000000000000000000000000001", 500);
assertInIndex(midx, 0, "0000000000000000000000000000000000000005", 12);
assertInIndex(midx, 0, "0000000000000000000000000000000000000010",
1500);
assertInIndex(midx, 1, "0000000000000000000000000000000000000002", 501);
assertInIndex(midx, 1, "0000000000000000000000000000000000000003", 13);
assertInIndex(midx, 1, "0000000000000000000000000000000000000015",
1501);
assertInIndex(midx, 2, "0000000000000000000000000000000000000004", 502);
assertInIndex(midx, 2, "0000000000000000000000000000000000000007", 14);
assertInIndex(midx, 2, "0000000000000000000000000000000000000012",
1502);
assertNull(midx.find(ObjectId.zeroId()));
}
@Test
public void jgit_largeOffsetChunk() throws IOException {
PackIndex idxOne = FakeIndexFactory.indexOf(List.of(
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000001", (1L << 34)),
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000005", 12)));
PackIndex idxTwo = FakeIndexFactory.indexOf(List.of(
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000002", (1L << 35)),
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000003", 13)));
Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo);
MultiPackIndexWriter writer = new MultiPackIndexWriter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.write(NullProgressMonitor.INSTANCE, out, packs);
MultiPackIndex midx = MultiPackIndexLoader
.read(new ByteArrayInputStream(out.toByteArray()));
assertEquals(2, midx.getPackNames().length);
assertInIndex(midx, 0, "0000000000000000000000000000000000000001",
(1L << 34));
assertInIndex(midx, 0, "0000000000000000000000000000000000000005", 12);
assertInIndex(midx, 1, "0000000000000000000000000000000000000002",
(1L << 35));
}
@Test
public void jgit_largeOffset_noChunk() throws IOException {
// All offsets fit in 32 bits, no large offset chunk
// Most significant bit to 1 is still valid offset
PackIndex idxOne = FakeIndexFactory.indexOf(List.of(
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000001",
0xff00_0000),
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000005", 12)));
PackIndex idxTwo = FakeIndexFactory.indexOf(List.of(
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000002", 501),
new FakeIndexFactory.IndexObject(
"0000000000000000000000000000000000000003", 13)));
Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo);
MultiPackIndexWriter writer = new MultiPackIndexWriter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.write(NullProgressMonitor.INSTANCE, out, packs);
MultiPackIndex midx = MultiPackIndexLoader
.read(new ByteArrayInputStream(out.toByteArray()));
assertEquals(2, midx.getPackNames().length);
assertInIndex(midx, 0, "0000000000000000000000000000000000000001",
0xff00_0000L);
assertInIndex(midx, 0, "0000000000000000000000000000000000000005", 12);
}
@Test
public void jgit_resolve() throws IOException {
AbbreviatedObjectId abbrev = AbbreviatedObjectId
.fromString("32fe829a1c");
PackIndex idxOne = indexWith(
// Noise
"0000000000000000000000000000000000000001",
"3000000000000000000000000000000000000005",
// One before abbrev
"32fe829a1b000000000000000000000000000001",
// matches
"32fe829a1c000000000000000000000000000001",
"32fe829a1c000000000000000000000000000100",
// One after abbrev
"32fe829a1d000000000000000000000000000000");
PackIndex idxTwo = indexWith(
// Noise
"8888880000000000000000000000000000000002",
"bbbbbb0000000000000000000000000000000003",
// Match
"32fe829a1c000000000000000000000000000010");
Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo);
MultiPackIndexWriter writer = new MultiPackIndexWriter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.write(NullProgressMonitor.INSTANCE, out, packs);
MultiPackIndex midx = MultiPackIndexLoader
.read(new ByteArrayInputStream(out.toByteArray()));
Set<ObjectId> results = new HashSet<>();
midx.resolve(results, abbrev, 100);
assertEquals(3, results.size());
assertTrue(results.contains(ObjectId
.fromString("32fe829a1c000000000000000000000000000001")));
assertTrue(results.contains(ObjectId
.fromString("32fe829a1c000000000000000000000000000010")));
assertTrue(results.contains(ObjectId
.fromString("32fe829a1c000000000000000000000000000100")));
}
@Test
public void jgit_resolve_matchLimit() throws IOException {
AbbreviatedObjectId abbrev = AbbreviatedObjectId
.fromString("32fe829a1c");
PackIndex idxOne = indexWith(
// Noise
"0000000000000000000000000000000000000001",
"3000000000000000000000000000000000000005",
// One before abbrev
"32fe829a1b000000000000000000000000000001",
// matches
"32fe829a1c000000000000000000000000000001",
"32fe829a1c000000000000000000000000000100",
// One after abbrev
"32fe829a1d000000000000000000000000000000");
PackIndex idxTwo = indexWith(
// Noise
"8888880000000000000000000000000000000002",
"bbbbbb0000000000000000000000000000000003",
// Match
"32fe829a1c000000000000000000000000000010");
Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo);
MultiPackIndexWriter writer = new MultiPackIndexWriter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.write(NullProgressMonitor.INSTANCE, out, packs);
MultiPackIndex midx = MultiPackIndexLoader
.read(new ByteArrayInputStream(out.toByteArray()));
Set<ObjectId> results = new HashSet<>();
midx.resolve(results, abbrev, 2);
assertEquals(2, results.size());
assertTrue(results.contains(ObjectId
.fromString("32fe829a1c000000000000000000000000000001")));
assertTrue(results.contains(ObjectId
.fromString("32fe829a1c000000000000000000000000000010")));
}
@Test
public void jgit_resolve_noMatches() throws IOException {
AbbreviatedObjectId abbrev = AbbreviatedObjectId
.fromString("4400000000");
PackIndex idxOne = indexWith(
"0000000000000000000000000000000000000001",
"3000000000000000000000000000000000000005",
"32fe829a1b000000000000000000000000000001",
"32fe829a1c000000000000000000000000000001",
"32fe829a1c000000000000000000000000000100",
"32fe829a1d000000000000000000000000000000");
PackIndex idxTwo = indexWith(
// Noise
"8888880000000000000000000000000000000002",
"bbbbbb0000000000000000000000000000000003",
"32fe829a1c000000000000000000000000000010");
Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo);
MultiPackIndexWriter writer = new MultiPackIndexWriter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.write(NullProgressMonitor.INSTANCE, out, packs);
MultiPackIndex midx = MultiPackIndexLoader
.read(new ByteArrayInputStream(out.toByteArray()));
Set<ObjectId> results = new HashSet<>();
midx.resolve(results, abbrev, 200);
assertEquals(0, results.size());
}
@Test
public void jgit_resolve_empty() throws IOException {
AbbreviatedObjectId abbrev = AbbreviatedObjectId
.fromString("4400000000");
PackIndex idxOne = FakeIndexFactory.indexOf(List.of());
PackIndex idxTwo = FakeIndexFactory.indexOf(List.of());
Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo);
MultiPackIndexWriter writer = new MultiPackIndexWriter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.write(NullProgressMonitor.INSTANCE, out, packs);
MultiPackIndex midx = MultiPackIndexLoader
.read(new ByteArrayInputStream(out.toByteArray()));
Set<ObjectId> results = new HashSet<>();
midx.resolve(results, abbrev, 200);
assertEquals(0, results.size());
}
private static PackIndex indexWith(String... oids) {
List<FakeIndexFactory.IndexObject> idxObjs = new ArrayList<>(
oids.length);
int offset = 12;
for (String oid : oids) {
idxObjs.add(new FakeIndexFactory.IndexObject(oid, offset));
offset += 10;
}
return FakeIndexFactory.indexOf(idxObjs);
}
private static void assertInIndex(MultiPackIndex midx, int expectedPackId,
String oid, long expectedOffset) {
MultiPackIndex.PackOffset packOffset = midx
.find(ObjectId.fromString(oid));
assertNotNull(packOffset);
assertEquals("Wrong packId for " + oid, expectedPackId,
packOffset.getPackId());
assertEquals(expectedOffset, packOffset.getOffset());
}
}
|