Apache Lucene open-source search software
LUCENE-9907: Remove packedInts dependency on StoredFieldsFormat (#64)
Ignacio Vera430b3baa806f
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.javaindex 730cfdfdda1..9bb421cac47 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsReader.java@@ -40,7 +40,6 @@ import static org.apache.lucene.codecs.lucene90.compressing.Lucene90CompressingSimport java.io.EOFException;import java.io.IOException;-import java.util.Arrays;import java.util.Collection;import java.util.Collections;import org.apache.lucene.codecs.CodecUtil;@@ -68,7 +67,6 @@ import org.apache.lucene.util.BitUtil;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.LongsRef;-import org.apache.lucene.util.packed.PackedInts;/*** {@link StoredFieldsReader} impl for {@link Lucene90CompressingStoredFieldsFormat}.@@ -83,7 +81,6 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsReaprivate final long maxPointer;private final IndexInput fieldsStream;private final int chunkSize;- private final int packedIntsVersion;private final CompressionMode compressionMode;private final Decompressor decompressor;private final int numDocs;@@ -103,7 +100,6 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsReathis.indexReader = reader.indexReader.clone();this.maxPointer = reader.maxPointer;this.chunkSize = reader.chunkSize;- this.packedIntsVersion = reader.packedIntsVersion;this.compressionMode = reader.compressionMode;this.decompressor = reader.decompressor.clone();this.numDocs = reader.numDocs;@@ -155,7 +151,6 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsReasegmentSuffix);chunkSize = metaIn.readVInt();- packedIntsVersion = metaIn.readVInt();decompressor = compressionMode.newDecompressor();this.merging = false;@@ -446,55 +441,12 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsReaoffsets[1] = fieldsStream.readVInt();} else {// Number of stored fields per document- final int bitsPerStoredFields = fieldsStream.readVInt();- if (bitsPerStoredFields == 0) {- Arrays.fill(numStoredFields, 0, chunkDocs, fieldsStream.readVInt());- } else if (bitsPerStoredFields > 31) {- throw new CorruptIndexException(- "bitsPerStoredFields=" + bitsPerStoredFields, fieldsStream);- } else {- final PackedInts.ReaderIterator it =- PackedInts.getReaderIteratorNoHeader(- fieldsStream,- PackedInts.Format.PACKED,- packedIntsVersion,- chunkDocs,- bitsPerStoredFields,- 1024);- for (int i = 0; i < chunkDocs; ) {- final LongsRef next = it.next(Integer.MAX_VALUE);- System.arraycopy(next.longs, next.offset, numStoredFields, i, next.length);- i += next.length;- }- }-+ StoredFieldsInts.readInts(fieldsStream, chunkDocs, numStoredFields, 0);// The stream encodes the length of each document and we decode// it into a list of monotonically increasing offsets- final int bitsPerLength = fieldsStream.readVInt();- if (bitsPerLength == 0) {- final int length = fieldsStream.readVInt();- for (int i = 0; i < chunkDocs; ++i) {- offsets[1 + i] = (1 + i) * length;- }- } else if (bitsPerStoredFields > 31) {- throw new CorruptIndexException("bitsPerLength=" + bitsPerLength, fieldsStream);- } else {- final PackedInts.ReaderIterator it =- PackedInts.getReaderIteratorNoHeader(- fieldsStream,- PackedInts.Format.PACKED,- packedIntsVersion,- chunkDocs,- bitsPerLength,- 1024);- for (int i = 0; i < chunkDocs; ) {- final LongsRef next = it.next(Integer.MAX_VALUE);- System.arraycopy(next.longs, next.offset, offsets, i + 1, next.length);- i += next.length;- }- for (int i = 0; i < chunkDocs; ++i) {- offsets[i + 1] += offsets[i];- }+ StoredFieldsInts.readInts(fieldsStream, chunkDocs, offsets, 1);+ for (int i = 0; i < chunkDocs; ++i) {+ offsets[i + 1] += offsets[i];}// Additional validation: only the empty document has a serialized length of 0@@ -734,10 +686,6 @@ public final class Lucene90CompressingStoredFieldsReader extends StoredFieldsReareturn numDocs;}- int getPackedIntsVersion() {- return packedIntsVersion;- }-@Overridepublic long ramBytesUsed() {return indexReader.ramBytesUsed();diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsWriter.javaindex e63cfa17f24..3919f642e14 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsWriter.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/Lucene90CompressingStoredFieldsWriter.java@@ -152,7 +152,6 @@ public final class Lucene90CompressingStoredFieldsWriter extends StoredFieldsWricontext);metaStream.writeVInt(chunkSize);- metaStream.writeVInt(PackedInts.VERSION_CURRENT);success = true;} finally {@@ -196,34 +195,10 @@ public final class Lucene90CompressingStoredFieldsWriter extends StoredFieldsWri}private static void saveInts(int[] values, int length, DataOutput out) throws IOException {- assert length > 0;if (length == 1) {out.writeVInt(values[0]);} else {- boolean allEqual = true;- for (int i = 1; i < length; ++i) {- if (values[i] != values[0]) {- allEqual = false;- break;- }- }- if (allEqual) {- out.writeVInt(0);- out.writeVInt(values[0]);- } else {- long max = 0;- for (int i = 0; i < length; ++i) {- max |= values[i];- }- final int bitsRequired = PackedInts.bitsRequired(max);- out.writeVInt(bitsRequired);- final PackedInts.Writer w =- PackedInts.getWriterNoHeader(out, PackedInts.Format.PACKED, length, bitsRequired, 1);- for (int i = 0; i < length; ++i) {- w.add(values[i]);- }- w.finish();- }+ StoredFieldsInts.writeInts(values, 0, length, out);}}@@ -603,7 +578,6 @@ public final class Lucene90CompressingStoredFieldsWriter extends StoredFieldsWri}} else if (matchingFieldsReader.getCompressionMode() == compressionMode&& matchingFieldsReader.getChunkSize() == chunkSize- && matchingFieldsReader.getPackedIntsVersion() == PackedInts.VERSION_CURRENT&& liveDocs == null&& !tooDirty(matchingFieldsReader)) {// optimized merge, raw byte copydiff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/StoredFieldsInts.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/StoredFieldsInts.javanew file mode 100644index 00000000000..60a4a48bf83--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/compressing/StoredFieldsInts.java@@ -0,0 +1,196 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.codecs.lucene90.compressing;++import java.io.IOException;+import java.util.Arrays;+import org.apache.lucene.store.DataOutput;+import org.apache.lucene.store.IndexInput;++class StoredFieldsInts {++ private static final int BLOCK_SIZE = 128;+ private static final int BLOCK_SIZE_MINUS_ONE = BLOCK_SIZE - 1;++ private StoredFieldsInts() {}++ static void writeInts(int[] values, int start, int count, DataOutput out) throws IOException {+ boolean allEqual = true;+ for (int i = 1; i < count; ++i) {+ if (values[start + i] != values[start]) {+ allEqual = false;+ break;+ }+ }+ if (allEqual) {+ out.writeByte((byte) 0);+ out.writeVInt(values[0]);+ } else {+ long max = 0;+ for (int i = 0; i < count; ++i) {+ max |= Integer.toUnsignedLong(values[start + i]);+ }+ if (max <= 0xff) {+ out.writeByte((byte) 8);+ writeInts8(out, count, values, start);+ } else if (max <= 0xffff) {+ out.writeByte((byte) 16);+ writeInts16(out, count, values, start);+ } else {+ out.writeByte((byte) 32);+ writeInts32(out, count, values, start);+ }+ }+ }++ private static void writeInts8(DataOutput out, int count, int[] values, int offset)+ throws IOException {+ int k = 0;+ for (; k < count - BLOCK_SIZE_MINUS_ONE; k += BLOCK_SIZE) {+ int step = offset + k;+ for (int i = 0; i < 16; ++i) {+ long l =+ ((long) values[step + i] << 56)+ | ((long) values[step + 16 + i] << 48)+ | ((long) values[step + 32 + i] << 40)+ | ((long) values[step + 48 + i] << 32)+ | ((long) values[step + 64 + i] << 24)+ | ((long) values[step + 80 + i] << 16)+ | ((long) values[step + 96 + i] << 8)+ | (long) values[step + 112 + i];+ out.writeLong(Long.reverseBytes(l));+ }+ }+ for (; k < count; k++) {+ out.writeByte((byte) values[offset + k]);+ }+ }++ private static void writeInts16(DataOutput out, int count, int[] values, int offset)+ throws IOException {+ int k = 0;+ for (; k < count - BLOCK_SIZE_MINUS_ONE; k += BLOCK_SIZE) {+ int step = offset + k;+ for (int i = 0; i < 32; ++i) {+ long l =+ ((long) values[step + i] << 48)+ | ((long) values[step + 32 + i] << 32)+ | ((long) values[step + 64 + i] << 16)+ | (long) values[step + 96 + i];+ out.writeLong(Long.reverseBytes(l));+ }+ }+ for (; k < count; k++) {+ out.writeShort((short) values[offset + k]);+ }+ }++ private static void writeInts32(DataOutput out, int count, int[] values, int offset)+ throws IOException {+ int k = 0;+ for (; k < count - BLOCK_SIZE_MINUS_ONE; k += BLOCK_SIZE) {+ int step = offset + k;+ for (int i = 0; i < 64; ++i) {+ long l = ((long) values[step + i] << 32) | (long) values[step + 64 + i];+ out.writeLong(Long.reverseBytes(l));+ }+ }+ for (; k < count; k++) {+ out.writeInt(values[offset + k]);+ }+ }++ /** Read {@code count} integers into {@code values}. */+ static void readInts(IndexInput in, int count, long[] values, int offset) throws IOException {+ final int bpv = in.readByte();+ switch (bpv) {+ case 0:+ Arrays.fill(values, offset, offset + count, in.readVInt());+ break;+ case 8:+ readInts8(in, count, values, offset);+ break;+ case 16:+ readInts16(in, count, values, offset);+ break;+ case 32:+ readInts32(in, count, values, offset);+ break;+ default:+ throw new IOException("Unsupported number of bits per value: " + bpv);+ }+ }++ private static void readInts8(IndexInput in, int count, long[] values, int offset)+ throws IOException {+ int k = 0;+ for (; k < count - BLOCK_SIZE_MINUS_ONE; k += BLOCK_SIZE) {+ final int step = offset + k;+ in.readLELongs(values, step, 16);+ for (int i = 0; i < 16; ++i) {+ final long l = values[step + i];+ values[step + i] = (l >>> 56) & 0xFFL;+ values[step + 16 + i] = (l >>> 48) & 0xFFL;+ values[step + 32 + i] = (l >>> 40) & 0xFFL;+ values[step + 48 + i] = (l >>> 32) & 0xFFL;+ values[step + 64 + i] = (l >>> 24) & 0xFFL;+ values[step + 80 + i] = (l >>> 16) & 0xFFL;+ values[step + 96 + i] = (l >>> 8) & 0xFFL;+ values[step + 112 + i] = l & 0xFFL;+ }+ }+ for (; k < count; k++) {+ values[offset + k] = Byte.toUnsignedInt(in.readByte());+ }+ }++ private static void readInts16(IndexInput in, int count, long[] values, int offset)+ throws IOException {+ int k = 0;+ for (; k < count - BLOCK_SIZE_MINUS_ONE; k += BLOCK_SIZE) {+ int step = offset + k;+ in.readLELongs(values, step, 32);+ for (int i = 0; i < 32; ++i) {+ final long l = values[step + i];+ values[step + i] = (l >>> 48) & 0xFFFFL;+ values[step + 32 + i] = (l >>> 32) & 0xFFFFL;+ values[step + 64 + i] = (l >>> 16) & 0xFFFFL;+ values[step + 96 + i] = l & 0xFFFFL;+ }+ }+ for (; k < count; k++) {+ values[offset + k] = Short.toUnsignedInt(in.readShort());+ }+ }++ private static void readInts32(IndexInput in, int count, long[] values, int offset)+ throws IOException {+ int k = 0;+ for (; k < count - BLOCK_SIZE_MINUS_ONE; k += BLOCK_SIZE) {+ final int step = offset + k;+ in.readLELongs(values, step, 64);+ for (int i = 0; i < 64; ++i) {+ final long l = values[step + i];+ values[step + i] = l >>> 32;+ values[step + 64 + i] = l & 0xFFFFFFFFL;+ }+ }+ for (; k < count; k++) {+ values[offset + k] = in.readInt();+ }+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene90/compressing/TestStoredFieldsInt.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/compressing/TestStoredFieldsInt.javanew file mode 100644index 00000000000..fee31356700--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene90/compressing/TestStoredFieldsInt.java@@ -0,0 +1,75 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.codecs.lucene90.compressing;++import java.util.Arrays;+import org.apache.lucene.store.Directory;+import org.apache.lucene.store.IOContext;+import org.apache.lucene.store.IndexInput;+import org.apache.lucene.store.IndexOutput;+import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util.TestUtil;++public class TestStoredFieldsInt extends LuceneTestCase {++ public void testRandom() throws Exception {+ int numIters = atLeast(100);+ try (Directory dir = newDirectory()) {+ for (int iter = 0; iter < numIters; ++iter) {+ int[] values = new int[random().nextInt(5000) + 1];+ final int bpv = TestUtil.nextInt(random(), 1, 31);+ for (int i = 0; i < values.length; ++i) {+ values[i] = TestUtil.nextInt(random(), 0, (1 << bpv) - 1);+ }+ test(dir, values);+ }+ }+ }++ public void testAllEquals() throws Exception {+ try (Directory dir = newDirectory()) {+ int[] docIDs = new int[random().nextInt(5000) + 1];+ final int bpv = TestUtil.nextInt(random(), 1, 31);+ Arrays.fill(docIDs, TestUtil.nextInt(random(), 0, (1 << bpv) - 1));+ test(dir, docIDs);+ }+ }++ private void test(Directory dir, int[] ints) throws Exception {+ final long len;+ try (IndexOutput out = dir.createOutput("tmp", IOContext.DEFAULT)) {+ StoredFieldsInts.writeInts(ints, 0, ints.length, out);+ len = out.getFilePointer();+ if (random().nextBoolean()) {+ out.writeLong(0); // garbage+ }+ }++ try (IndexInput in = dir.openInput("tmp", IOContext.READONCE)) {+ final int offset = random().nextInt(5);+ long[] read = new long[ints.length + offset];+ StoredFieldsInts.readInts(in, ints.length, read, offset);+ int[] readInts = new int[ints.length];+ for (int i = 0; i < ints.length; i++) {+ readInts[i] = (int) read[offset + i];+ }+ assertArrayEquals(ints, readInts);+ assertEquals(len, in.getFilePointer());+ }+ dir.deleteFile("tmp");+ }+}