Skip to content

Commit 4ee7a6b

Browse files
committed
Writing trailing bytes instead
1 parent 08eccc8 commit 4ee7a6b

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,6 @@ public int docValueCount() {
13511351

13521352
private void set() {
13531353
if (set == false) {
1354-
assert docID() != NO_MORE_DOCS;
13551354
final int index = disi.index();
13561355
start = addresses.get(index);
13571356
end = addresses.get(index + 1L);
@@ -1403,23 +1402,15 @@ public int docID() {
14031402
@Override
14041403
public int nextDoc() throws IOException {
14051404
int doc = ords.nextDoc();
1406-
if (doc != NO_MORE_DOCS) {
1407-
count = ords.docValueCount();
1408-
} else {
1409-
count = 0;
1410-
}
1405+
count = ords.docValueCount();
14111406
i = 0;
14121407
return doc;
14131408
}
14141409

14151410
@Override
14161411
public int advance(int target) throws IOException {
14171412
int doc = ords.advance(target);
1418-
if (doc != NO_MORE_DOCS) {
1419-
count = ords.docValueCount();
1420-
} else {
1421-
count = 0;
1422-
}
1413+
count = ords.docValueCount();
14231414
i = 0;
14241415
return doc;
14251416
}

lucene/core/src/java/org/apache/lucene/util/packed/DirectWriter.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,40 @@ public void finish() throws IOException {
149149
}
150150
assert !finished;
151151
flush();
152-
// pad for fast io: we actually only need this for certain BPV, but its just 3 bytes...
153-
for (int i = 0; i < 3; i++) {
152+
153+
// padding for fast io and allow over read the last index by 1.
154+
final int paddingBytes = numberOfTrailingBytes(bitsPerValue);
155+
for (int i = 0; i < paddingBytes; i++) {
154156
output.writeByte((byte) 0);
155157
}
156158
finished = true;
157159
}
158160

161+
private static int numberOfTrailingBytes(int bitsPerValue) {
162+
switch (bitsPerValue) {
163+
case 1:
164+
case 2:
165+
case 4:
166+
case 8:
167+
return 1;
168+
case 12:
169+
case 16:
170+
return 2;
171+
case 20:
172+
case 24:
173+
case 28:
174+
case 32:
175+
return 5;
176+
case 40:
177+
case 48:
178+
case 56:
179+
case 64:
180+
return 11;
181+
default:
182+
throw new IllegalArgumentException("unsupported bitsPerValue: " + bitsPerValue);
183+
}
184+
}
185+
159186
/** Returns an instance suitable for encoding {@code numValues} using {@code bitsPerValue} */
160187
public static DirectWriter getInstance(DataOutput output, long numValues, int bitsPerValue) {
161188
if (Arrays.binarySearch(SUPPORTED_BITS_PER_VALUE, bitsPerValue) < 0) {

lucene/core/src/test/org/apache/lucene/util/packed/TestDirectPacked.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ private void doTestBpv(Directory directory, int bpv, long offset) throws Excepti
116116
for (int j = 0; j < original.length; j++) {
117117
assertEquals("bpv=" + bpv, original[j], reader.get(j));
118118
}
119+
// Make sure that we can overread the last index by one.
120+
assertEquals("bpv=" + bpv, 0, reader.get(original.length));
119121
input.close();
120122
}
121123
}

0 commit comments

Comments
 (0)