Apache Solr open-source search software
LUCENE-5721: Monotonic compression doesn't use zig-zag encoding anymore.
Adrien Grandb31ae698e5d1
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex ae34b94c4af..5e51573e32b 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -254,6 +254,9 @@ Optimizations* LUCENE-5703: BinaryDocValues producers don't allocate or copy bytes oneach access anymore. (Adrien Grand)+* LUCENE-5721: Monotonic compression doesn't use zig-zag encoding anymore.+ (Robert Muir, Adrien Grand)+Bug fixes* LUCENE-5673: MMapDirectory: Work around a "bug" in the JDK that throwsdiff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexReader.javaindex 5237f4a399b..62c1f611bc7 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexReader.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/FixedGapTermsIndexReader.java@@ -256,10 +256,10 @@ public class FixedGapTermsIndexReader extends TermsIndexReaderBase {termBytes.copy(clone, numTermBytes);// records offsets into main terms dict file- termsDictOffsets = new MonotonicBlockPackedReader(clone, packedIntsVersion, blocksize, numIndexTerms, false);+ termsDictOffsets = MonotonicBlockPackedReader.of(clone, packedIntsVersion, blocksize, numIndexTerms, false);// records offsets into byte[] term data- termOffsets = new MonotonicBlockPackedReader(clone, packedIntsVersion, blocksize, 1+numIndexTerms, false);+ termOffsets = MonotonicBlockPackedReader.of(clone, packedIntsVersion, blocksize, 1+numIndexTerms, false);} finally {clone.close();}diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/diskdv/DiskDocValuesProducer.java b/lucene/codecs/src/java/org/apache/lucene/codecs/diskdv/DiskDocValuesProducer.javaindex 82ce5968d41..879f90d76c0 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/diskdv/DiskDocValuesProducer.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/diskdv/DiskDocValuesProducer.java@@ -34,7 +34,7 @@ class DiskDocValuesProducer extends Lucene49DocValuesProducer {@Overrideprotected MonotonicBlockPackedReader getAddressInstance(IndexInput data, FieldInfo field, BinaryEntry bytes) throws IOException {data.seek(bytes.addressesOffset);- return new MonotonicBlockPackedReader(data.clone(), bytes.packedIntsVersion, bytes.blockSize, bytes.count, true);+ return MonotonicBlockPackedReader.of(data.clone(), bytes.packedIntsVersion, bytes.blockSize, bytes.count, true);}@Override@@ -45,6 +45,6 @@ class DiskDocValuesProducer extends Lucene49DocValuesProducer {@Overrideprotected MonotonicBlockPackedReader getOrdIndexInstance(IndexInput data, FieldInfo field, NumericEntry entry) throws IOException {data.seek(entry.offset);- return new MonotonicBlockPackedReader(data.clone(), entry.packedIntsVersion, entry.blockSize, entry.count, true);+ return MonotonicBlockPackedReader.of(data.clone(), entry.packedIntsVersion, entry.blockSize, entry.count, true);}}diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/MemoryDocValuesProducer.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/MemoryDocValuesProducer.javaindex e330bfb42d8..b9eaed16421 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/MemoryDocValuesProducer.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/MemoryDocValuesProducer.java@@ -330,7 +330,7 @@ class MemoryDocValuesProducer extends DocValuesProducer {ramBytesUsed.addAndGet(bytesAndAddresses.reader.ramBytesUsed());if (entry.minLength != entry.maxLength) {data.seek(data.getFilePointer() + entry.missingBytes);- bytesAndAddresses.addresses = new MonotonicBlockPackedReader(data, entry.packedIntsVersion, entry.blockSize, maxDoc, false);+ bytesAndAddresses.addresses = MonotonicBlockPackedReader.of(data, entry.packedIntsVersion, entry.blockSize, maxDoc, false);ramBytesUsed.addAndGet(bytesAndAddresses.addresses.ramBytesUsed());}return bytesAndAddresses;diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesProducer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesProducer.javaindex 19d7e43dbbf..566f0efb5dc 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesProducer.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene42/Lucene42DocValuesProducer.java@@ -300,7 +300,7 @@ class Lucene42DocValuesProducer extends DocValuesProducer {}};} else {- final MonotonicBlockPackedReader addresses = new MonotonicBlockPackedReader(data, entry.packedIntsVersion, entry.blockSize, maxDoc, false);+ final MonotonicBlockPackedReader addresses = MonotonicBlockPackedReader.of(data, entry.packedIntsVersion, entry.blockSize, maxDoc, false);ramBytesUsed.addAndGet(bytes.ramBytesUsed() + addresses.ramBytesUsed());return new BinaryDocValues() {diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene45/Lucene45DocValuesProducer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene45/Lucene45DocValuesProducer.javaindex 88024f98fc8..9ad611cc9e5 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene45/Lucene45DocValuesProducer.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene45/Lucene45DocValuesProducer.java@@ -412,7 +412,7 @@ public class Lucene45DocValuesProducer extends DocValuesProducer implements ClosMonotonicBlockPackedReader addrInstance = addressInstances.get(field.number);if (addrInstance == null) {data.seek(bytes.addressesOffset);- addrInstance = new MonotonicBlockPackedReader(data, bytes.packedIntsVersion, bytes.blockSize, bytes.count, false);+ addrInstance = MonotonicBlockPackedReader.of(data, bytes.packedIntsVersion, bytes.blockSize, bytes.count, false);addressInstances.put(field.number, addrInstance);ramBytesUsed.addAndGet(addrInstance.ramBytesUsed() + RamUsageEstimator.NUM_BYTES_INT);}@@ -461,7 +461,7 @@ public class Lucene45DocValuesProducer extends DocValuesProducer implements Clos} else {size = 1L + bytes.count / interval;}- addrInstance = new MonotonicBlockPackedReader(data, bytes.packedIntsVersion, bytes.blockSize, size, false);+ addrInstance = MonotonicBlockPackedReader.of(data, bytes.packedIntsVersion, bytes.blockSize, size, false);addressInstances.put(field.number, addrInstance);ramBytesUsed.addAndGet(addrInstance.ramBytesUsed() + RamUsageEstimator.NUM_BYTES_INT);}@@ -533,7 +533,7 @@ public class Lucene45DocValuesProducer extends DocValuesProducer implements ClosMonotonicBlockPackedReader ordIndexInstance = ordIndexInstances.get(field.number);if (ordIndexInstance == null) {data.seek(entry.offset);- ordIndexInstance = new MonotonicBlockPackedReader(data, entry.packedIntsVersion, entry.blockSize, entry.count, false);+ ordIndexInstance = MonotonicBlockPackedReader.of(data, entry.packedIntsVersion, entry.blockSize, entry.count, false);ordIndexInstances.put(field.number, ordIndexInstance);ramBytesUsed.addAndGet(ordIndexInstance.ramBytesUsed() + RamUsageEstimator.NUM_BYTES_INT);}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene49/Lucene49DocValuesProducer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene49/Lucene49DocValuesProducer.javaindex 7689e6840b3..3b9e2bf8cd0 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene49/Lucene49DocValuesProducer.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene49/Lucene49DocValuesProducer.java@@ -392,7 +392,7 @@ public class Lucene49DocValuesProducer extends DocValuesProducer implements ClosMonotonicBlockPackedReader addrInstance = addressInstances.get(field.number);if (addrInstance == null) {data.seek(bytes.addressesOffset);- addrInstance = new MonotonicBlockPackedReader(data, bytes.packedIntsVersion, bytes.blockSize, bytes.count, false);+ addrInstance = MonotonicBlockPackedReader.of(data, bytes.packedIntsVersion, bytes.blockSize, bytes.count, false);addressInstances.put(field.number, addrInstance);ramBytesUsed.addAndGet(addrInstance.ramBytesUsed() + RamUsageEstimator.NUM_BYTES_INT);}@@ -441,7 +441,7 @@ public class Lucene49DocValuesProducer extends DocValuesProducer implements Clos} else {size = 1L + bytes.count / interval;}- addrInstance = new MonotonicBlockPackedReader(data, bytes.packedIntsVersion, bytes.blockSize, size, false);+ addrInstance = MonotonicBlockPackedReader.of(data, bytes.packedIntsVersion, bytes.blockSize, size, false);addressInstances.put(field.number, addrInstance);ramBytesUsed.addAndGet(addrInstance.ramBytesUsed() + RamUsageEstimator.NUM_BYTES_INT);}@@ -511,7 +511,7 @@ public class Lucene49DocValuesProducer extends DocValuesProducer implements ClosMonotonicBlockPackedReader ordIndexInstance = ordIndexInstances.get(field.number);if (ordIndexInstance == null) {data.seek(entry.offset);- ordIndexInstance = new MonotonicBlockPackedReader(data, entry.packedIntsVersion, entry.blockSize, entry.count, false);+ ordIndexInstance = MonotonicBlockPackedReader.of(data, entry.packedIntsVersion, entry.blockSize, entry.count, false);ordIndexInstances.put(field.number, ordIndexInstance);ramBytesUsed.addAndGet(ordIndexInstance.ramBytesUsed() + RamUsageEstimator.NUM_BYTES_INT);}diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicAppendingLongBuffer.java b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicAppendingLongBuffer.javaindex bf04d16a8e2..c30da1c4791 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicAppendingLongBuffer.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicAppendingLongBuffer.java@@ -17,10 +17,12 @@ package org.apache.lucene.util.packed;* limitations under the License.*/-import org.apache.lucene.util.RamUsageEstimator;+import static org.apache.lucene.util.packed.MonotonicBlockPackedReader.expected;import java.util.Arrays;+import org.apache.lucene.util.RamUsageEstimator;+/*** Utility class to buffer signed longs in memory, which is optimized for the* case where the sequence is monotonic, although it can encode any sequence of@@ -30,14 +32,6 @@ import java.util.Arrays;*/public final class MonotonicAppendingLongBuffer extends AbstractAppendingLongBuffer {- static long zigZagDecode(long n) {- return ((n >>> 1) ^ -(n & 1));- }-- static long zigZagEncode(long n) {- return (n >> 63) ^ (n << 1);- }-float[] averages;long[] minValues;@@ -68,18 +62,12 @@ public final class MonotonicAppendingLongBuffer extends AbstractAppendingLongBufthis(16, 1024, acceptableOverheadRatio);}-@Overridelong get(int block, int element) {if (block == valuesOff) {return pending[element];} else {- final long base = minValues[block] + (long) (averages[block] * (long) element);- if (values[block] == null) {- return base;- } else {- return base + zigZagDecode(values[block].get(element));- }+ return expected(minValues[block], averages[block], element) + values[block].get(element);}}@@ -90,21 +78,11 @@ public final class MonotonicAppendingLongBuffer extends AbstractAppendingLongBufSystem.arraycopy(pending, element, arr, off, sysCopyToRead);return sysCopyToRead;} else {- if (values[block] == null) {- int toFill = Math.min(len, pending.length - element);- for (int r = 0; r < toFill; r++, off++, element++) {- arr[off] = minValues[block] + (long) (averages[block] * (long) element);- }- return toFill;- } else {-- /* packed block */- int read = values[block].get(element, arr, off, len);- for (int r = 0; r < read; r++, off++, element++) {- arr[off] = minValues[block] + (long) (averages[block] * (long) element) + zigZagDecode(arr[off]);- }- return read;+ int read = values[block].get(element, arr, off, len);+ for (int r = 0; r < read; r++, off++, element++) {+ arr[off] += expected(minValues[block], averages[block], element);}+ return read;}}@@ -118,11 +96,22 @@ public final class MonotonicAppendingLongBuffer extends AbstractAppendingLongBuf@Overridevoid packPendingValues() {assert pendingOff > 0;- minValues[valuesOff] = pending[0];- averages[valuesOff] = pendingOff == 1 ? 0 : (float) (pending[pendingOff - 1] - pending[0]) / (pendingOff - 1);+ final float average = pendingOff == 1 ? 0 : (float) (pending[pendingOff - 1] - pending[0]) / (pendingOff - 1);+ long minValue = pending[0];+ // adjust minValue so that all deltas will be positive+ for (int i = 1; i < pendingOff; ++i) {+ final long actual = pending[i];+ final long expected = expected(minValue, average, i);+ if (expected > actual) {+ minValue -= (expected - actual);+ }+ }++ minValues[valuesOff] = minValue;+ averages[valuesOff] = average;for (int i = 0; i < pendingOff; ++i) {- pending[i] = zigZagEncode(pending[i] - minValues[valuesOff] - (long) (averages[valuesOff] * (long) i));+ pending[i] = pending[i] - expected(minValue, average, i);}long maxDelta = 0;for (int i = 0; i < pendingOff; ++i) {diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.java b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.javaindex 6c27622d652..a96d85d33e0 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedReader.java@@ -35,16 +35,32 @@ import org.apache.lucene.util.RamUsageEstimator;* {@link MonotonicBlockPackedWriter}.* @lucene.internal*/-public final class MonotonicBlockPackedReader extends LongValues implements Accountable {+public class MonotonicBlockPackedReader extends LongValues implements Accountable {- private final int blockShift, blockMask;- private final long valueCount;- private final long[] minValues;- private final float[] averages;- private final PackedInts.Reader[] subReaders;+ static long expected(long origin, float average, int index) {+ return origin + (long) (average * (long) index);+ }++ final int blockShift, blockMask;+ final long valueCount;+ final long[] minValues;+ final float[] averages;+ final PackedInts.Reader[] subReaders;/** Sole constructor. */- public MonotonicBlockPackedReader(IndexInput in, int packedIntsVersion, int blockSize, long valueCount, boolean direct) throws IOException {+ public static MonotonicBlockPackedReader of(IndexInput in, int packedIntsVersion, int blockSize, long valueCount, boolean direct) throws IOException {+ if (packedIntsVersion < PackedInts.VERSION_MONOTONIC_WITHOUT_ZIGZAG) {+ return new MonotonicBlockPackedReader(in, packedIntsVersion, blockSize, valueCount, direct) {+ @Override+ protected long decodeDelta(long delta) {+ return zigZagDecode(delta);+ }+ };+ }+ return new MonotonicBlockPackedReader(in, packedIntsVersion, blockSize, valueCount, direct);+ }++ private MonotonicBlockPackedReader(IndexInput in, int packedIntsVersion, int blockSize, long valueCount, boolean direct) throws IOException {this.valueCount = valueCount;blockShift = checkBlockSize(blockSize, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE);blockMask = blockSize - 1;@@ -53,7 +69,11 @@ public final class MonotonicBlockPackedReader extends LongValues implements Accoaverages = new float[numBlocks];subReaders = new PackedInts.Reader[numBlocks];for (int i = 0; i < numBlocks; ++i) {- minValues[i] = in.readVLong();+ if (packedIntsVersion < PackedInts.VERSION_MONOTONIC_WITHOUT_ZIGZAG) {+ minValues[i] = in.readVLong();+ } else {+ minValues[i] = zigZagDecode(in.readVLong());+ }averages[i] = Float.intBitsToFloat(in.readInt());final int bitsPerValue = in.readVInt();if (bitsPerValue > 64) {@@ -79,7 +99,11 @@ public final class MonotonicBlockPackedReader extends LongValues implements Accoassert index >= 0 && index < valueCount;final int block = (int) (index >>> blockShift);final int idx = (int) (index & blockMask);- return minValues[block] + (long) (idx * averages[block]) + zigZagDecode(subReaders[block].get(idx));+ return expected(minValues[block], averages[block], idx) + decodeDelta(subReaders[block].get(idx));+ }++ protected long decodeDelta(long delta) {+ return delta;}/** Returns the number of values */diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedWriter.java b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedWriter.javaindex a875be9cc1b..21fb41407b1 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedWriter.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicBlockPackedWriter.java@@ -18,10 +18,12 @@ package org.apache.lucene.util.packed;*/import static org.apache.lucene.util.BitUtil.zigZagEncode;+import static org.apache.lucene.util.packed.MonotonicBlockPackedReader.expected;import java.io.IOException;import org.apache.lucene.store.DataOutput;+import org.apache.lucene.util.BitUtil;/*** A writer for large monotonically increasing sequences of positive longs.@@ -29,7 +31,7 @@ import org.apache.lucene.store.DataOutput;* The sequence is divided into fixed-size blocks and for each block, values* are modeled after a linear function f: x → A × x + B. The block* encodes deltas from the expected values computed from this function using as- * few bits as possible. Each block has an overhead between 6 and 14 bytes.+ * few bits as possible.* <p>* Format:* <ul>@@ -38,17 +40,16 @@ import org.apache.lucene.store.DataOutput;* <li>Block: <Header, (Ints)>* <li>Header: <B, A, BitsPerValue>* <li>B: the B from f: x → A × x + B using a- * {@link DataOutput#writeVLong(long) variable-length long}+ * {@link BitUtil#zigZagEncode(long) zig-zag encoded}+ * {@link DataOutput#writeVLong(long) vLong}* <li>A: the A from f: x → A × x + B encoded using* {@link Float#floatToIntBits(float)} on* {@link DataOutput#writeInt(int) 4 bytes}* <li>BitsPerValue: a {@link DataOutput#writeVInt(int) variable-length int}* <li>Ints: if BitsPerValue is <tt>0</tt>, then there is nothing to read and* all values perfectly match the result of the function. Otherwise, these- * are the- * <a href="https://developers.google.com/protocol-buffers/docs/encoding#types">zigzag-encoded</a>- * {@link PackedInts packed} deltas from the expected value (computed from- * the function) using exaclty BitsPerValue bits per value+ * are the {@link PackedInts packed} deltas from the expected value+ * (computed from the function) using exaclty BitsPerValue bits per value.* </ul>* @see MonotonicBlockPackedReader* @lucene.internal@@ -72,22 +73,29 @@ public final class MonotonicBlockPackedWriter extends AbstractBlockPackedWriterprotected void flush() throws IOException {assert off > 0;- // TODO: perform a true linear regression?- final long min = values[0];- final float avg = off == 1 ? 0f : (float) (values[off - 1] - min) / (off - 1);+ final float avg = off == 1 ? 0f : (float) (values[off - 1] - values[0]) / (off - 1);+ long min = values[0];+ // adjust min so that all deltas will be positive+ for (int i = 1; i < off; ++i) {+ final long actual = values[i];+ final long expected = expected(min, avg, i);+ if (expected > actual) {+ min -= (expected - actual);+ }+ }- long maxZigZagDelta = 0;+ long maxDelta = 0;for (int i = 0; i < off; ++i) {- values[i] = zigZagEncode(values[i] - min - (long) (avg * i));- maxZigZagDelta = Math.max(maxZigZagDelta, values[i]);+ values[i] = values[i] - expected(min, avg, i);+ maxDelta = Math.max(maxDelta, values[i]);}- out.writeVLong(min);+ out.writeVLong(zigZagEncode(min));out.writeInt(Float.floatToIntBits(avg));- if (maxZigZagDelta == 0) {+ if (maxDelta == 0) {out.writeVInt(0);} else {- final int bitsRequired = PackedInts.bitsRequired(maxZigZagDelta);+ final int bitsRequired = PackedInts.bitsRequired(maxDelta);out.writeVInt(bitsRequired);writeValues(bitsRequired);}diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.java b/lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.javaindex 86cc5ea4788..473d7cb727b 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/PackedInts.java@@ -67,7 +67,8 @@ public class PackedInts {public final static String CODEC_NAME = "PackedInts";public final static int VERSION_START = 0; // PackedInts were long-alignedpublic final static int VERSION_BYTE_ALIGNED = 1;- public final static int VERSION_CURRENT = VERSION_BYTE_ALIGNED;+ public static final int VERSION_MONOTONIC_WITHOUT_ZIGZAG = 2;+ public final static int VERSION_CURRENT = VERSION_MONOTONIC_WITHOUT_ZIGZAG;/*** Check the validity of a version number.diff --git a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.javaindex 3c4348d70eb..608b1fb7594 100644--- a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java+++ b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java@@ -1230,7 +1230,7 @@ public class TestPackedInts extends LuceneTestCase {out.close();final IndexInput in = dir.openInput("out.bin", IOContext.DEFAULT);- final MonotonicBlockPackedReader reader = new MonotonicBlockPackedReader(in, PackedInts.VERSION_CURRENT, blockSize, valueCount, random().nextBoolean());+ final MonotonicBlockPackedReader reader = MonotonicBlockPackedReader.of(in, PackedInts.VERSION_CURRENT, blockSize, valueCount, random().nextBoolean());assertEquals(fp, in.getFilePointer());for (int i = 0; i < valueCount; ++i) {assertEquals("i=" +i, values[i], reader.get(i));