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
|
/*
* 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 java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Set;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.internal.storage.midx.MultiPackIndexLoader.MultiPackIndexFormatException;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.util.NB;
/**
* Support for the MultiPackIndex v1 format.
*
* @see MultiPackIndex
*/
class MultiPackIndexV1 implements MultiPackIndex {
private final OidLookup idx;
private final String[] packNames;
private final byte[] bitmappedPackfiles;
private final OffsetLookup offsets;
private final PackOffset result = new PackOffset();
MultiPackIndexV1(int hashLength, @NonNull byte[] oidFanout,
@NonNull byte[] oidLookup, String[] packNames,
byte[] bitmappedPackfiles, byte[] objectOffsets,
byte[] largeObjectOffsets) throws MultiPackIndexFormatException {
this.bitmappedPackfiles = bitmappedPackfiles;
this.idx = new OidLookup(hashLength, oidFanout, oidLookup);
this.offsets = new OffsetLookup(objectOffsets, largeObjectOffsets);
this.packNames = packNames;
}
@Override
public String[] getPackNames() {
return packNames;
}
@Override
public boolean hasObject(AnyObjectId oid) {
return idx.findMultiPackIndexPosition(oid) != -1;
}
@Override
@Nullable
public PackOffset find(AnyObjectId objectId) {
int position = idx.findMultiPackIndexPosition(objectId);
if (position == -1) {
return null;
}
offsets.getObjectOffset(position, result);
return result;
}
@Override
public void resolve(Set<ObjectId> matches, AbbreviatedObjectId id,
int matchLimit) {
idx.resolve(matches, id, matchLimit);
}
@Override
public long getMemorySize() {
int packNamesSize = Arrays.stream(packNames)
.mapToInt(s -> s.getBytes(StandardCharsets.UTF_8).length).sum();
return packNamesSize + byteArrayLengh(bitmappedPackfiles)
+ idx.getMemorySize() + offsets.getMemorySize();
}
@Override
public String toString() {
return "MultiPackIndexV1 {idx=" + idx + ", packfileNames=" //$NON-NLS-1$ //$NON-NLS-2$
+ Arrays.toString(packNames) + ", bitmappedPackfiles=" //$NON-NLS-1$
+ byteArrayToString(bitmappedPackfiles) + ", objectOffsets=" //$NON-NLS-1$
+ offsets + '}';
}
private static String byteArrayToString(byte[] array) {
return array == null ? "null" : new String(array); //$NON-NLS-1$
}
private static int byteArrayLengh(byte[] array) {
return array == null ? 0 : array.length;
}
/**
* Wraps the small and large offset chunks (if exists), to lookup offsets.
*/
private static class OffsetLookup {
private static final int OBJECT_OFFSETS_DATA_WIDTH = 8;
private static final int BIT_31_ON = 0x80000000;
private static final int TOGGLE_BIT_31 = 0x7fff_ffff;
private final byte[] offsets;
private final byte[] largeOffsets;
/**
* Initialize the ObjectOffsets.
*
* @param offsets
* content of ObjectOffset Chunk.
* @param largeOffsets
* content of largo offsets chunks (can be null).
*/
OffsetLookup(@NonNull byte[] offsets, byte[] largeOffsets) {
this.offsets = offsets;
this.largeOffsets = largeOffsets;
}
/**
* Get the metadata of a commit。
*
* @param position
* the position in the multi-pack-index of the object.
* @param result
* an instance of PackOffset to populate with the result.
*/
void getObjectOffset(int position, PackOffset result) {
int offsetInChunk = position * OBJECT_OFFSETS_DATA_WIDTH;
int packId = NB.decodeInt32(offsets, offsetInChunk);
int offset = NB.decodeInt32(offsets, offsetInChunk + 4);
if ((offset & BIT_31_ON) != 0) {
long bigOffset;
if (largeOffsets == null) {
bigOffset = NB.decodeUInt32(offsets, offsetInChunk + 4);
} else {
int bigOffsetPos = (offset & TOGGLE_BIT_31);
bigOffset = NB.decodeInt64(largeOffsets, bigOffsetPos * 8);
}
result.setValues(packId, bigOffset);
return;
}
result.setValues(packId, offset);
}
long getMemorySize() {
return (long) byteArrayLengh(offsets)
+ byteArrayLengh(largeOffsets);
}
}
/**
* Combines the fanout and oid list chunks, to lookup Oids with an efficient
* binary search
*/
private static class OidLookup {
private static final int FANOUT = 256;
private final int hashLength;
private final int[] fanoutTable;
private final byte[] oidLookup;
/**
* Initialize the MultiPackIndexIndex.
*
* @param hashLength
* length of object hash.
* @param oidFanout
* content of OID Fanout Chunk.
* @param oidLookup
* content of OID Lookup Chunk.
* @throws MultiPackIndexFormatException
* MultiPackIndex file's format is different from we
* expected.
*/
OidLookup(int hashLength, @NonNull byte[] oidFanout,
@NonNull byte[] oidLookup)
throws MultiPackIndexFormatException {
this.hashLength = hashLength;
this.oidLookup = oidLookup;
int[] table = new int[FANOUT];
long uint32;
for (int k = 0; k < table.length; k++) {
uint32 = NB.decodeUInt32(oidFanout, k * 4);
if (uint32 > Integer.MAX_VALUE) {
throw new MultiPackIndexFormatException(
JGitText.get().multiPackIndexFileIsTooLargeForJgit);
}
table[k] = (int) uint32;
}
this.fanoutTable = table;
}
/**
* Find the position in the MultiPackIndex file of the specified id.
*
* @param id
* the id for which the multi-pack-index position will be
* found.
* @return the MultiPackIndex position or -1 if the object was not
* found.
*/
int findMultiPackIndexPosition(AnyObjectId id) {
int levelOne = id.getFirstByte();
int high = fanoutTable[levelOne];
int low = 0;
if (levelOne > 0) {
low = fanoutTable[levelOne - 1];
}
while (low < high) {
int mid = (low + high) >>> 1;
int cmp = id.compareTo(oidLookup, hashLength * mid);
if (cmp < 0) {
high = mid;
} else if (cmp == 0) {
return mid;
} else {
low = mid + 1;
}
}
return -1;
}
void resolve(Set<ObjectId> matches, AbbreviatedObjectId id,
int matchLimit) {
if (matches.size() >= matchLimit) {
return;
}
if (oidLookup.length == 0) {
return;
}
int high = fanoutTable[id.getFirstByte()];
int low = id.getFirstByte() == 0 ? 0
: fanoutTable[id.getFirstByte() - 1];
do {
int p = (low + high) >>> 1;
int cmp = id.prefixCompare(oidLookup, idOffset(p));
if (cmp < 0) {
high = p;
continue;
}
if (cmp > 0) {
low = p + 1;
continue;
}
// Got a match.
// We may have landed in the middle of the matches. Move
// backwards to the start of matches, then walk forwards.
while (0 < p
&& id.prefixCompare(oidLookup, idOffset(p - 1)) == 0) {
p--;
}
while (p < high && id.prefixCompare(oidLookup, idOffset(p)) == 0
&& matches.size() < matchLimit) {
matches.add(ObjectId.fromRaw(oidLookup, idOffset(p)));
p++;
}
return;
} while (low < high);
}
private int idOffset(int position) {
return position * hashLength;
}
long getMemorySize() {
return 4L + byteArrayLengh(oidLookup) + (FANOUT * 4);
}
}
}
|