From adf73ff6bd49bd09701e8a960cdcfb492cd3c6e0 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Tue, 11 Dec 2018 10:11:04 +0100 Subject: [PATCH] test --- .../index/BinaryDocValuesFieldUpdates.java | 8 +++ .../lucene/index/DocValuesFieldUpdates.java | 56 +++++++++++++++---- .../index/NumericDocValuesFieldUpdates.java | 6 ++ 3 files changed, 60 insertions(+), 10 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesFieldUpdates.java b/lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesFieldUpdates.java index fa60929991c..67227ed5b0d 100644 --- a/lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesFieldUpdates.java +++ b/lucene/core/src/java/org/apache/lucene/index/BinaryDocValuesFieldUpdates.java @@ -107,6 +107,14 @@ final class BinaryDocValuesFieldUpdates extends DocValuesFieldUpdates { lengths.set(i, tmpLength); } + @Override + protected void copy(int i, int j) { + super.copy(i, j); + + offsets.set(j, offsets.get(i)); + lengths.set(j, lengths.get(i)); + } + @Override protected void grow(int size) { super.grow(size); diff --git a/lucene/core/src/java/org/apache/lucene/index/DocValuesFieldUpdates.java b/lucene/core/src/java/org/apache/lucene/index/DocValuesFieldUpdates.java index 464a7f2f219..71d9c7d2d1b 100644 --- a/lucene/core/src/java/org/apache/lucene/index/DocValuesFieldUpdates.java +++ b/lucene/core/src/java/org/apache/lucene/index/DocValuesFieldUpdates.java @@ -21,10 +21,10 @@ import org.apache.lucene.util.Accountable; import org.apache.lucene.util.BitSet; import org.apache.lucene.util.BitSetIterator; import org.apache.lucene.util.BytesRef; -import org.apache.lucene.util.InPlaceMergeSorter; import org.apache.lucene.util.PriorityQueue; import org.apache.lucene.util.RamUsageEstimator; import org.apache.lucene.util.SparseFixedBitSet; +import org.apache.lucene.util.TimSorter; import org.apache.lucene.util.packed.PackedInts; import org.apache.lucene.util.packed.PagedMutable; @@ -32,11 +32,11 @@ import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS; /** * Holds updates of a single DocValues field, for a set of documents within one segment. - * + * * @lucene.experimental */ abstract class DocValuesFieldUpdates implements Accountable { - + protected static final int PAGE_SIZE = 1024; private static final long HAS_VALUE_MASK = 1; private static final long HAS_NO_VALUE_MASK = 0; @@ -260,7 +260,7 @@ abstract class DocValuesFieldUpdates implements Accountable { final boolean getFinished() { return finished; } - + abstract void add(int doc, long value); abstract void add(int doc, BytesRef value); @@ -285,11 +285,17 @@ abstract class DocValuesFieldUpdates implements Accountable { throw new IllegalStateException("already finished"); } finished = true; - // shrink wrap - if (size < docs.size()) { - resize(size); - } - new InPlaceMergeSorter() { + + // Swaps and comparisons are quite costly with all these packed ints, + // so we use TimSorter instead of InPlaceMergeSorter, which has the nice + // property of doing fewer swaps/copies at the expense of a higher memory + // usage + final int tempSlotsOffset = size; + // we grow the current structures to get temporary storage + resize(Math.addExact(size, size / 2)); // 50% extra memory for temp slots + final int numTempSlots = Math.toIntExact(docs.size() - size); + new TimSorter(numTempSlots) { + @Override protected void swap(int i, int j) { DocValuesFieldUpdates.this.swap(i, j); @@ -302,7 +308,33 @@ abstract class DocValuesFieldUpdates implements Accountable { // stable and preserving original order so the last update to that docID wins return Long.compare(docs.get(i)>>>1, docs.get(j)>>>1); } + + @Override + protected void save(int i, int len) { + assert len <= numTempSlots; + for (int k = 0; k < len; ++k) { + copy(i + k, tempSlotsOffset + k); + } + } + + @Override + protected void restore(int src, int dest) { + copy(tempSlotsOffset + src, dest); + } + + @Override + protected void copy(int src, int dest) { + DocValuesFieldUpdates.this.copy(src, dest); + } + + @Override + protected int compareSaved(int i, int j) { + return compare(tempSlotsOffset + i, j); + } }.sort(0, size); + + // shrink wrap to save memory, this will also release temporary storage + resize(size); } /** Returns true if this instance contains any updates. */ @@ -350,6 +382,10 @@ abstract class DocValuesFieldUpdates implements Accountable { docs.set(i, tmpDoc); } + protected void copy(int i, int j) { + docs.set(j, docs.get(i)); + } + protected void grow(int size) { docs = docs.grow(size); } @@ -477,7 +513,7 @@ abstract class DocValuesFieldUpdates implements Accountable { } protected abstract BytesRef binaryValue(); - + protected abstract long longValue(); @Override diff --git a/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesFieldUpdates.java b/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesFieldUpdates.java index 550a86a7aea..05d5c1433bf 100644 --- a/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesFieldUpdates.java +++ b/lucene/core/src/java/org/apache/lucene/index/NumericDocValuesFieldUpdates.java @@ -99,6 +99,12 @@ final class NumericDocValuesFieldUpdates extends DocValuesFieldUpdates { values.set(i, tmpVal); } + @Override + protected void copy(int i, int j) { + super.copy(i, j); + values.set(j, values.get(i)); + } + @Override protected void grow(int size) { super.grow(size);