Apache Lucene open-source search software
LUCENE-6891: use prefix coding when writing dimensional values in each leaf block
Michael McCandless830fc1fbfba2
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex e5f260dc363..928b6148f6a 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -83,6 +83,12 @@ API ChangesIndexOutput.getName returns its name (Dawid Weiss, Robert Muir, MikeMcCandless)+Optimizations++* LUCENE-6891: Use prefix coding when writing dimensional values in+ each leaf block in the default codec, to reduce the index+ size (Mike McCandless)+Changes in Runtime Behavior* LUCENE-6789: IndexSearcher's default Similarity is changed to BM25Similarity.diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDReader.javaindex af60a0d1624..752154ecfd6 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDReader.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDReader.java@@ -63,7 +63,8 @@ class SimpleTextBKDReader extends BKDReader {}@Override- protected void visitDocValues(byte[] scratchPackedValue, IndexInput in, int[] docIDs, int count, IntersectVisitor visitor) throws IOException {+ protected void visitDocValues(int[] commonPrefixLengths, byte[] scratchPackedValue, IndexInput in, int[] docIDs, int count, IntersectVisitor visitor) throws IOException {+ // NOTE: we don't do prefix coding, so we ignore commonPrefixLengthsassert scratchPackedValue.length == packedBytesLength;BytesRefBuilder scratch = new BytesRefBuilder();for(int i=0;i<count;i++) {diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDimensionalWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDimensionalWriter.javaindex 22ede84b06f..8bb1e193804 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDimensionalWriter.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDimensionalWriter.java@@ -128,10 +128,16 @@ class SimpleTextDimensionalWriter extends DimensionalWriter {}@Override- protected void writeLeafBlockPackedValue(IndexOutput out, byte[] bytes, int offset, int length) throws IOException {- assert length == packedBytesLength;+ protected void writeCommonPrefixes(IndexOutput out, int[] commonPrefixLengths, byte[] packedValue) {+ // NOTE: we don't do prefix coding, so we ignore commonPrefixLengths+ }++ @Override+ protected void writeLeafBlockPackedValue(IndexOutput out, int[] commonPrefixLengths, byte[] bytes) throws IOException {+ // NOTE: we don't do prefix coding, so we ignore commonPrefixLengths+ assert bytes.length == packedBytesLength;write(out, BLOCK_VALUE);- write(out, new BytesRef(bytes, offset, length).toString());+ write(out, new BytesRef(bytes, 0, bytes.length).toString());newline(out);}};diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.javaindex 2866abe40bb..cefc4feb576 100644--- a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java+++ b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java@@ -85,21 +85,25 @@ public class BKDReader implements Accountable {final IndexInput in;final int[] scratchDocIDs;final byte[] scratchPackedValue;+ final int[] commonPrefixLengths;final IntersectVisitor visitor;- public IntersectState(IndexInput in, int packedBytesLength,+ public IntersectState(IndexInput in, int numDims,+ int packedBytesLength,int maxPointsInLeafNode,IntersectVisitor visitor) {this.in = in;this.visitor = visitor;+ this.commonPrefixLengths = new int[numDims];this.scratchDocIDs = new int[maxPointsInLeafNode];this.scratchPackedValue = new byte[packedBytesLength];}}public void intersect(IntersectVisitor visitor) throws IOException {- IntersectState state = new IntersectState(in.clone(), packedBytesLength,+ IntersectState state = new IntersectState(in.clone(), numDims,+ packedBytesLength,maxPointsInLeafNode,visitor);byte[] rootMinPacked = new byte[packedBytesLength];@@ -147,10 +151,21 @@ public class BKDReader implements Accountable {return count;}- protected void visitDocValues(byte[] scratchPackedValue, IndexInput in, int[] docIDs, int count, IntersectVisitor visitor) throws IOException {+ protected void visitDocValues(int[] commonPrefixLengths, byte[] scratchPackedValue, IndexInput in, int[] docIDs, int count, IntersectVisitor visitor) throws IOException {visitor.grow(count);+ for(int dim=0;dim<numDims;dim++) {+ int prefix = in.readVInt();+ commonPrefixLengths[dim] = prefix;+ if (prefix > 0) {+ in.readBytes(scratchPackedValue, dim*bytesPerDim, prefix);+ }+ //System.out.println("R: " + dim + " of " + numDims + " prefix=" + prefix);+ }for(int i=0;i<count;i++) {- in.readBytes(scratchPackedValue, 0, scratchPackedValue.length);+ for(int dim=0;dim<numDims;dim++) {+ int prefix = commonPrefixLengths[dim];+ in.readBytes(scratchPackedValue, dim*bytesPerDim + prefix, bytesPerDim - prefix);+ }visitor.visit(docIDs[i], scratchPackedValue);}}@@ -186,7 +201,7 @@ public class BKDReader implements Accountable {int count = readDocIDs(state.in, leafBlockFPs[nodeID-leafNodeOffset], state.scratchDocIDs);// Again, this time reading values and checking with the visitor- visitDocValues(state.scratchPackedValue, state.in, state.scratchDocIDs, count, state.visitor);+ visitDocValues(state.commonPrefixLengths, state.scratchPackedValue, state.in, state.scratchDocIDs, count, state.visitor);} else {diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.javaindex 970f920caf3..4691edb7a30 100644--- a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java+++ b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java@@ -104,6 +104,7 @@ public class BKDWriter implements Closeable {final byte[] scratchPackedValue;final byte[] scratch1;final byte[] scratch2;+ final int[] commonPrefixLengths;private OfflinePointWriter offlinePointWriter;private HeapPointWriter heapPointWriter;@@ -133,6 +134,7 @@ public class BKDWriter implements Closeable {scratchPackedValue = new byte[packedBytesLength];scratch1 = new byte[packedBytesLength];scratch2 = new byte[packedBytesLength];+ commonPrefixLengths = new int[numDims];// dimensional values (numDims * bytesPerDim) + ord (long) + docID (int)bytesPerDoc = packedBytesLength + RamUsageEstimator.NUM_BYTES_LONG + RamUsageEstimator.NUM_BYTES_INT;@@ -398,6 +400,7 @@ public class BKDWriter implements Closeable {innerNodeCount--;int numLeaves = (int) (innerNodeCount+1);+ //System.out.println("LEAVES: " + numLeaves);// NOTE: we could save the 1+ here, to use a bit less heap at search time, but then we'd need a somewhat costly check at each// step of the recursion to recompute the split dim:@@ -495,8 +498,19 @@ public class BKDWriter implements Closeable {}}- protected void writeLeafBlockPackedValue(IndexOutput out, byte[] bytes, int offset, int length) throws IOException {- out.writeBytes(bytes, 0, length);+ protected void writeLeafBlockPackedValue(IndexOutput out, int[] commonPrefixLengths, byte[] bytes) throws IOException {+ for(int dim=0;dim<numDims;dim++) {+ int prefix = commonPrefixLengths[dim];+ out.writeBytes(bytes, dim*bytesPerDim+prefix, bytesPerDim-prefix);+ }+ }++ protected void writeCommonPrefixes(IndexOutput out, int[] commonPrefixes, byte[] packedValue) throws IOException {+ for(int dim=0;dim<numDims;dim++) {+ out.writeVInt(commonPrefixes[dim]);+ //System.out.println(commonPrefixes[dim] + " of " + bytesPerDim);+ out.writeBytes(packedValue, dim*bytesPerDim, commonPrefixes[dim]);+ }}@Override@@ -646,14 +660,35 @@ public class BKDWriter implements Closeable {// TODO: we should delta compress / only write suffix bytes, like terms dict (the values will all be "close together" since we are at// a leaf cell):- // Now write the full values:+ // First pass: find the per-dim common prefix for all values in this block:+ Arrays.fill(commonPrefixLengths, bytesPerDim);+ for (int i=0;i<source.count;i++) {+ if (i == 0) {+ heapSource.readPackedValue(Math.toIntExact(source.start + i), scratch1);+ } else {+ heapSource.readPackedValue(Math.toIntExact(source.start + i), scratchPackedValue);+ for(int dim=0;dim<numDims;dim++) {+ int offset = dim * bytesPerDim;+ for(int j=0;j<commonPrefixLengths[dim];j++) {+ if (scratch1[offset+j] != scratchPackedValue[offset+j]) {+ commonPrefixLengths[dim] = j;+ break;+ }+ }+ }+ }+ }++ writeCommonPrefixes(out, commonPrefixLengths, scratch1);++ // Second pass: write the full values:for (int i=0;i<source.count;i++) {// TODO: we could do bulk copying here, avoiding the intermediate copy:heapSource.readPackedValue(Math.toIntExact(source.start + i), scratchPackedValue);// Make sure this value does in fact fall within this leaf cell:assert valueInBounds(scratchPackedValue, minPackedValue, maxPackedValue);- writeLeafBlockPackedValue(out, scratchPackedValue, 0, scratchPackedValue.length);+ writeLeafBlockPackedValue(out, commonPrefixLengths, scratchPackedValue);}} else {