diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPointsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPointsWriter.java index 13494f5b348..33af33e688e 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPointsWriter.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPointsWriter.java @@ -158,11 +158,10 @@ class SimpleTextPointsWriter extends PointsWriter { } @Override - protected void writeLeafBlockPackedValue(IndexOutput out, int[] commonPrefixLengths, byte[] bytes) throws IOException { + protected void writeLeafBlockPackedValue(IndexOutput out, int[] commonPrefixLengths, byte[] bytes, int bytesOffset) 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, 0, bytes.length).toString()); + write(out, new BytesRef(bytes, bytesOffset, packedBytesLength).toString()); newline(out); } }) { 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.java index 6d3cf03c456..765b01c1a92 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 @@ -106,9 +106,9 @@ public class BKDWriter implements Closeable { final double maxMBSortInHeap; final byte[] scratchDiff; - final byte[] scratchPackedValue; final byte[] scratch1; final byte[] scratch2; + final BytesRef scratchBytesRef = new BytesRef(); final int[] commonPrefixLengths; protected final FixedBitSet docsSeen; @@ -152,7 +152,7 @@ public class BKDWriter implements Closeable { packedBytesLength = numDims * bytesPerDim; scratchDiff = new byte[bytesPerDim]; - scratchPackedValue = new byte[packedBytesLength]; + scratchBytesRef.length = packedBytesLength; scratch1 = new byte[packedBytesLength]; scratch2 = new byte[packedBytesLength]; commonPrefixLengths = new int[numDims]; @@ -455,7 +455,7 @@ public class BKDWriter implements Closeable { } System.arraycopy(reader.state.scratchPackedValue, 0, maxPackedValue, 0, packedBytesLength); - assert numDims > 1 || valueInOrder(valueCount, lastPackedValue, reader.state.scratchPackedValue); + assert numDims > 1 || valueInOrder(valueCount, lastPackedValue, reader.state.scratchPackedValue, 0); valueCount++; if (pointCount > totalPointCount) { throw new IllegalStateException("totalPointCount=" + totalPointCount + " was passed when we were created, but we just hit " + pointCount + " values"); @@ -502,7 +502,7 @@ public class BKDWriter implements Closeable { // Write the full values: for (int i=0;i 0) { + if (StringHelper.compare(bytesPerDim, packedValue.bytes, packedValue.offset + offset, maxPackedValue, offset) > 0) { return false; } } @@ -1060,16 +1060,35 @@ public class BKDWriter implements Closeable { } if (nodeID >= leafNodeOffset) { + // Leaf node: write block + for (int dim=0;dim= maxPointsInLeafNode, so we better be in heap at this point: HeapPointWriter heapSource = (HeapPointWriter) source.writer; @@ -1083,37 +1102,21 @@ public class BKDWriter implements Closeable { assert count > 0: "nodeID=" + nodeID + " leafNodeOffset=" + leafNodeOffset; writeLeafBlockDocs(out, heapSource.docIDs, Math.toIntExact(source.start), count); - // First pass: find the per-dim common prefix for all values in this block: - Arrays.fill(commonPrefixLengths, bytesPerDim); - for (int i=0;i 0 && StringHelper.compare(bytesPerDim, lastPackedValue, 0, packedValue, 0) > 0) { - throw new AssertionError("values out of order: last value=" + new BytesRef(lastPackedValue) + " current value=" + new BytesRef(packedValue) + " ord=" + ord); + private boolean valueInOrder(long ord, byte[] lastPackedValue, byte[] packedValue, int packedValueOffset) { + if (ord > 0 && StringHelper.compare(bytesPerDim, lastPackedValue, 0, packedValue, packedValueOffset) > 0) { + throw new AssertionError("values out of order: last value=" + new BytesRef(lastPackedValue) + " current value=" + new BytesRef(packedValue, packedValueOffset, packedBytesLength) + " ord=" + ord); } - System.arraycopy(packedValue, 0, lastPackedValue, 0, bytesPerDim); + System.arraycopy(packedValue, packedValueOffset, lastPackedValue, 0, bytesPerDim); return true; } diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointReader.java b/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointReader.java index b178f085f92..63c78693b33 100644 --- a/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointReader.java +++ b/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointReader.java @@ -16,7 +16,6 @@ */ package org.apache.lucene.util.bkd; - import java.util.List; import org.apache.lucene.util.PagedBytes; diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java b/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java index 3b043d0ace0..45bb59181f4 100644 --- a/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java +++ b/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.List; import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.BytesRef; final class HeapPointWriter implements PointWriter { int[] docIDs; @@ -72,6 +73,15 @@ final class HeapPointWriter implements PointWriter { System.arraycopy(blocks.get(block), blockIndex * packedBytesLength, bytes, 0, packedBytesLength); } + /** Returns a reference, in result, to the byte[] slice holding this value */ + void getPackedValueSlice(int index, BytesRef result) { + int block = index / valuesPerBlock; + int blockIndex = index % valuesPerBlock; + result.bytes = blocks.get(block); + result.offset = blockIndex * packedBytesLength; + assert result.length == packedBytesLength; + } + void writePackedValue(int index, byte[] bytes) { assert bytes.length == packedBytesLength; int block = index / valuesPerBlock;