You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "iverase (via GitHub)" <gi...@apache.org> on 2024/07/11 14:44:31 UTC
[PR] Add levels to DocValues skipper index [lucene]
iverase opened a new pull request, #13563:
URL: https://github.com/apache/lucene/pull/13563
Currently the DocValues skipper index collects the stats every 4096 documents that allow implementors to used them to decide if they want to process those documents or they can be skipped. The idea of adding levels is to be able to skip several of those block (called intervals in the code) in one step by collecting the stats of the blocks and adding them to the index.
This implementation collects the stats of 8 of those intervals. For the first level, the first interval gets a new level with the stats from the next 8 intervals, then the 9th interval gets another level with the stats from the following 8 intervals and so on. For the second level, the first interval gets a new level with the stats from the next 8 level1 intervals, which is the same as the stats from the first 64 level 0 intervals and so on.
I run some basic experiments and I must say I did not see much change on the performance, still it feels the right thing to do, therefore I opened this PR.
relates https://github.com/apache/lucene/issues/11432
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1681112760
##########
lucene/test-framework/src/java/org/apache/lucene/tests/index/AssertingLeafReader.java:
##########
@@ -1194,24 +1194,27 @@ public int numLevels() {
@Override
public int minDocID(int level) {
assertThread("Doc values skipper", creationThread);
- Objects.checkIndex(level, numLevels());
int minDocID = in.minDocID(level);
assert minDocID <= in.maxDocID(level);
- if (level > 0) {
- assert minDocID <= in.minDocID(level - 1);
+ if (minDocID != -1 && minDocID != DocIdSetIterator.NO_MORE_DOCS) {
+ Objects.checkIndex(level, numLevels());
+ }
+ for (int i = 0; i < level; i++) {
+ assert minDocID >= in.minDocID(i);
Review Comment:
yes, done and revert changes here
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1680751732
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,88 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
Review Comment:
I modified the implementation so we will have access to higher levels at any point. Let me know what you think.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1682671882
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,127 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ assert minDocID <= other.minDocID && maxDocID < other.maxDocID;
+ maxDocID = other.maxDocID;
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<SkipAccumulator> accumulators = new ArrayList<>();
SkipAccumulator accumulator = null;
- int counter = 0;
+ final int maxAccumulators = 1 << (SKIP_INDEX_LEVEL_SHIFT * (SKIP_INDEX_MAX_LEVEL - 1));
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size() == maxAccumulators) {
Review Comment:
yes, but probably not frequent enough so I change the range interval for the test to use smaller values but still keeping a reasonable performance on the test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1682694746
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,127 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ assert minDocID <= other.minDocID && maxDocID < other.maxDocID;
+ maxDocID = other.maxDocID;
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<SkipAccumulator> accumulators = new ArrayList<>();
SkipAccumulator accumulator = null;
- int counter = 0;
+ final int maxAccumulators = 1 << (SKIP_INDEX_LEVEL_SHIFT * (SKIP_INDEX_MAX_LEVEL - 1));
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size() == maxAccumulators) {
+ writeLevels(accumulators);
+ accumulators.clear();
+ }
}
}
- if (counter > 0) {
- globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
- globalMinValue = Math.min(globalMinValue, accumulator.minValue);
- globalDocCount += accumulator.docCount;
- maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
+ if (accumulators.isEmpty() == false) {
+ if (accumulator != null) {
+ globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
+ globalMinValue = Math.min(globalMinValue, accumulator.minValue);
+ globalDocCount += accumulator.docCount;
+ maxDocId = accumulator.maxDocID;
+ }
+ writeLevels(accumulators);
}
meta.writeLong(start); // record the start in meta
meta.writeLong(data.getFilePointer() - start); // record the length
+ assert globalDocCount == 0 || globalMaxValue >= globalMinValue;
meta.writeLong(globalMaxValue);
meta.writeLong(globalMinValue);
+ assert globalDocCount <= maxDocId + 1;
meta.writeInt(globalDocCount);
meta.writeInt(maxDocId);
}
+ private void writeLevels(List<SkipAccumulator> accumulators) throws IOException {
+ final List<List<SkipAccumulator>> accumulatorsLevels = new ArrayList<>(SKIP_INDEX_MAX_LEVEL);
+ accumulatorsLevels.add(accumulators);
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL - 1; i++) {
+ accumulatorsLevels.add(buildLevel(accumulatorsLevels.get(i)));
+ }
+ int totalAccumulators = accumulators.size();
+ for (int index = 0; index < totalAccumulators; index++) {
+ // compute how many levels we need to write for the current accumulator
+ final int levels = getLevels(index, totalAccumulators);
+ // write the number of levels
+ data.writeByte((byte) levels);
+ // write intervals in reverse order. This is done so we don't
+ // need to read all of them in case of slipping
+ for (int level = levels - 1; level >= 0; level--) {
+ final SkipAccumulator accumulator =
+ accumulatorsLevels.get(level).get(index >> (SKIP_INDEX_LEVEL_SHIFT * level));
+ data.writeInt(accumulator.maxDocID);
+ data.writeInt(accumulator.minDocID);
+ data.writeLong(accumulator.maxValue);
+ data.writeLong(accumulator.minValue);
+ data.writeInt(accumulator.docCount);
+ }
+ }
+ }
+
+ private static List<SkipAccumulator> buildLevel(List<SkipAccumulator> accumulators) {
+ final int levelSize = 1 << SKIP_INDEX_LEVEL_SHIFT;
+ final List<SkipAccumulator> collector = new ArrayList<>();
+ for (int i = 0; i < accumulators.size() - levelSize + 1; i += levelSize) {
+ collector.add(SkipAccumulator.merge(accumulators, i, levelSize));
+ }
+ return collector;
+ }
+
+ private int getLevels(int index, int size) {
+ final int left = size - index;
+ for (int level = SKIP_INDEX_MAX_LEVEL - 1; level > 0; level--) {
+ final int numberIntervals = 1 << (SKIP_INDEX_LEVEL_SHIFT * level);
+ if (left >= numberIntervals && index % numberIntervals == 0) {
+ return level + 1;
+ }
+ }
+ return 1;
Review Comment:
I haven't been able to find a more elegant solution yet!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1681076840
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,133 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ minDocID = Math.min(minDocID, other.minDocID);
+ maxDocID = Math.max(maxDocID, other.maxDocID);
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<List<SkipAccumulator>> accumulators = new ArrayList<>(SKIP_INDEX_MAX_LEVEL);
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ accumulators.add(new ArrayList<>());
+ }
SkipAccumulator accumulator = null;
- int counter = 0;
+ final int maxAccumulators = 1 << (SKIP_INDEX_LEVEL_SHIFT * (SKIP_INDEX_MAX_LEVEL - 1));
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.get(0).add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size() == maxAccumulators) {
Review Comment:
I removed the List of List so this is not an issue any more
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1677204200
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesFormat.java:
##########
@@ -194,5 +194,34 @@ public DocValuesProducer fieldsProducer(SegmentReadState state) throws IOExcepti
static final int TERMS_DICT_REVERSE_INDEX_SIZE = 1 << TERMS_DICT_REVERSE_INDEX_SHIFT;
static final int TERMS_DICT_REVERSE_INDEX_MASK = TERMS_DICT_REVERSE_INDEX_SIZE - 1;
+ // number of documents in an interval
private static final int DEFAULT_SKIP_INDEX_INTERVAL_SIZE = 4096;
+ // number of intervals represented as a shift to create a new level, this is 1 << 3 == 8
+ // intervals.
+ static final int SKIP_INDEX_LEVEL_SHIFT = 3;
+ // max number of levels
+ // Increasing this number, it increases how much heap we need at index time.
+ // we currently need (1 * 8 * 8 * 8) = 512 accumulators on heap
+ static final int SKIP_INDEX_MAX_LEVEL = 4;
+ // how many intervals at level 0 are in each level (1 << (SKIP_INDEX_LEVEL_SHIFT * level)).
+ static int[] SKIP_INDEX_NUMBER_INTERVALS_PER_LEVEL = new int[SKIP_INDEX_MAX_LEVEL];
+ // number of bytes to skip when skipping a level. It does not take into account the
+ // current interval that is being read.
+ static long[] SKIP_INDEX_JUMP_LENGTH_PER_LEVEL = new long[SKIP_INDEX_MAX_LEVEL];
+
+ static {
+ for (int level = 0; level < SKIP_INDEX_MAX_LEVEL; level++) {
+ SKIP_INDEX_NUMBER_INTERVALS_PER_LEVEL[level] = 1 << (SKIP_INDEX_LEVEL_SHIFT * level);
Review Comment:
Nit: It's so cheap to compute that I wonder if we should really use a lookup table vs. recomputing it all the time.
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,88 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
Review Comment:
I'm a bit confused by this, because this `levels` variable feels like the number of lower levels that are getting updated, while some highel levels may still be valid? But then it would not be correct to return `levels` in `numLevels()` as we'd be missing these higher levels that are still valid?
E.g. if there are 8 intervals, there are two levels. But when we read the second interval, then `levels` is 1 because only the lower level needs updating, but there are still 2 valid levels?
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,120 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ maxDocID = other.maxDocID;
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ List<SkipAccumulator> accumulators = new ArrayList<>();
SkipAccumulator accumulator = null;
- int counter = 0;
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size()
+ == SKIP_INDEX_NUMBER_INTERVALS_PER_LEVEL[SKIP_INDEX_MAX_LEVEL - 1]) {
+ writeLevels(accumulators);
+ accumulators.clear();
+ }
}
}
- if (counter > 0) {
- globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
- globalMinValue = Math.min(globalMinValue, accumulator.minValue);
- globalDocCount += accumulator.docCount;
- maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
+ if (accumulators.isEmpty() == false) {
+ if (accumulator != null) {
+ globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
+ globalMinValue = Math.min(globalMinValue, accumulator.minValue);
+ globalDocCount += accumulator.docCount;
+ maxDocId = accumulator.maxDocID;
+ }
+ writeLevels(accumulators);
}
meta.writeLong(start); // record the start in meta
meta.writeLong(data.getFilePointer() - start); // record the length
+ assert globalDocCount == 0 || globalMaxValue >= globalMinValue;
meta.writeLong(globalMaxValue);
meta.writeLong(globalMinValue);
+ assert globalDocCount <= maxDocId + 1;
meta.writeInt(globalDocCount);
meta.writeInt(maxDocId);
}
+ private void writeLevels(List<SkipAccumulator> accumulators) throws IOException {
+ for (int index = 0; index < accumulators.size(); index++) {
+ // compute how many levels we need to write for the current accumulator
+ final int levels = getLevels(index, accumulators.size());
+ // build the levels
+ final SkipAccumulator[] accLevels = new SkipAccumulator[levels];
+ for (int level = 0; level < levels; level++) {
+ accLevels[level] =
+ SkipAccumulator.merge(
+ accumulators, index, SKIP_INDEX_NUMBER_INTERVALS_PER_LEVEL[level]);
Review Comment:
It would be a bit more efficient to compute the accumulators on a level based on the accumulator of the previous level rather than using accumulators at level 0 all the time?
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesFormat.java:
##########
@@ -194,5 +194,34 @@ public DocValuesProducer fieldsProducer(SegmentReadState state) throws IOExcepti
static final int TERMS_DICT_REVERSE_INDEX_SIZE = 1 << TERMS_DICT_REVERSE_INDEX_SHIFT;
static final int TERMS_DICT_REVERSE_INDEX_MASK = TERMS_DICT_REVERSE_INDEX_SIZE - 1;
+ // number of documents in an interval
private static final int DEFAULT_SKIP_INDEX_INTERVAL_SIZE = 4096;
+ // number of intervals represented as a shift to create a new level, this is 1 << 3 == 8
+ // intervals.
+ static final int SKIP_INDEX_LEVEL_SHIFT = 3;
+ // max number of levels
+ // Increasing this number, it increases how much heap we need at index time.
+ // we currently need (1 * 8 * 8 * 8) = 512 accumulators on heap
+ static final int SKIP_INDEX_MAX_LEVEL = 4;
+ // how many intervals at level 0 are in each level (1 << (SKIP_INDEX_LEVEL_SHIFT * level)).
+ static int[] SKIP_INDEX_NUMBER_INTERVALS_PER_LEVEL = new int[SKIP_INDEX_MAX_LEVEL];
+ // number of bytes to skip when skipping a level. It does not take into account the
+ // current interval that is being read.
+ static long[] SKIP_INDEX_JUMP_LENGTH_PER_LEVEL = new long[SKIP_INDEX_MAX_LEVEL];
+
+ static {
+ for (int level = 0; level < SKIP_INDEX_MAX_LEVEL; level++) {
+ SKIP_INDEX_NUMBER_INTERVALS_PER_LEVEL[level] = 1 << (SKIP_INDEX_LEVEL_SHIFT * level);
+ if (level > 0) {
+ SKIP_INDEX_JUMP_LENGTH_PER_LEVEL[level] =
+ // jump from previous level
+ SKIP_INDEX_JUMP_LENGTH_PER_LEVEL[level - 1]
+ // add nodes added by new level minus first one
+ + (SKIP_INDEX_NUMBER_INTERVALS_PER_LEVEL[level] - 1) * 29L
Review Comment:
Can you add a constant for this `29` and explain how it's computed?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1682502283
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,127 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ assert minDocID <= other.minDocID && maxDocID < other.maxDocID;
+ maxDocID = other.maxDocID;
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<SkipAccumulator> accumulators = new ArrayList<>();
SkipAccumulator accumulator = null;
- int counter = 0;
+ final int maxAccumulators = 1 << (SKIP_INDEX_LEVEL_SHIFT * (SKIP_INDEX_MAX_LEVEL - 1));
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size() == maxAccumulators) {
+ writeLevels(accumulators);
+ accumulators.clear();
+ }
}
}
- if (counter > 0) {
- globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
- globalMinValue = Math.min(globalMinValue, accumulator.minValue);
- globalDocCount += accumulator.docCount;
- maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
+ if (accumulators.isEmpty() == false) {
+ if (accumulator != null) {
+ globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
+ globalMinValue = Math.min(globalMinValue, accumulator.minValue);
+ globalDocCount += accumulator.docCount;
+ maxDocId = accumulator.maxDocID;
+ }
+ writeLevels(accumulators);
}
meta.writeLong(start); // record the start in meta
meta.writeLong(data.getFilePointer() - start); // record the length
+ assert globalDocCount == 0 || globalMaxValue >= globalMinValue;
meta.writeLong(globalMaxValue);
meta.writeLong(globalMinValue);
+ assert globalDocCount <= maxDocId + 1;
meta.writeInt(globalDocCount);
meta.writeInt(maxDocId);
}
+ private void writeLevels(List<SkipAccumulator> accumulators) throws IOException {
+ final List<List<SkipAccumulator>> accumulatorsLevels = new ArrayList<>(SKIP_INDEX_MAX_LEVEL);
+ accumulatorsLevels.add(accumulators);
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL - 1; i++) {
+ accumulatorsLevels.add(buildLevel(accumulatorsLevels.get(i)));
+ }
+ int totalAccumulators = accumulators.size();
+ for (int index = 0; index < totalAccumulators; index++) {
+ // compute how many levels we need to write for the current accumulator
+ final int levels = getLevels(index, totalAccumulators);
+ // write the number of levels
+ data.writeByte((byte) levels);
+ // write intervals in reverse order. This is done so we don't
+ // need to read all of them in case of slipping
+ for (int level = levels - 1; level >= 0; level--) {
+ final SkipAccumulator accumulator =
+ accumulatorsLevels.get(level).get(index >> (SKIP_INDEX_LEVEL_SHIFT * level));
+ data.writeInt(accumulator.maxDocID);
+ data.writeInt(accumulator.minDocID);
+ data.writeLong(accumulator.maxValue);
+ data.writeLong(accumulator.minValue);
+ data.writeInt(accumulator.docCount);
+ }
+ }
+ }
+
+ private static List<SkipAccumulator> buildLevel(List<SkipAccumulator> accumulators) {
+ final int levelSize = 1 << SKIP_INDEX_LEVEL_SHIFT;
+ final List<SkipAccumulator> collector = new ArrayList<>();
+ for (int i = 0; i < accumulators.size() - levelSize + 1; i += levelSize) {
+ collector.add(SkipAccumulator.merge(accumulators, i, levelSize));
+ }
+ return collector;
+ }
+
+ private int getLevels(int index, int size) {
+ final int left = size - index;
+ for (int level = SKIP_INDEX_MAX_LEVEL - 1; level > 0; level--) {
+ final int numberIntervals = 1 << (SKIP_INDEX_LEVEL_SHIFT * level);
+ if (left >= numberIntervals && index % numberIntervals == 0) {
+ return level + 1;
+ }
+ }
+ return 1;
Review Comment:
There must be a way to do this in constant time rather than linearly with SKIP_INDEX_MAX_LEVEL?
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,91 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
Review Comment:
This, or we can compute it based on maxDoc?
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,91 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels = 1;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
+ assert levels <= SKIP_INDEX_MAX_LEVEL && levels > 0
+ : "level out of range [" + levels + "]";
+ boolean competitive = true;
+ // check if current interval is competitive or we can jump to the next position
+ for (int level = levels - 1; level >= 0; level--) {
+ if ((maxDocID[level] = input.readInt()) < target) {
+ input.skipBytes(SKIP_INDEX_JUMP_LENGTH_PER_LEVEL[level]); // the jump for the level
+ competitive = false;
+ break;
+ }
+ minDocID[level] = input.readInt();
+ maxValue[level] = input.readLong();
+ minValue[level] = input.readLong();
+ docCount[level] = input.readInt();
+ }
+ if (competitive) {
+ // adjust levels
+ while (levels < SKIP_INDEX_MAX_LEVEL) {
+ if (maxDocID[levels] < target) {
+ break;
+ }
+ levels++;
+ }
Review Comment:
Let's combine both conditions under the while condition?
```java
while (levels < SKIP_INDEX_MAX_LEVEL && maxDocIDS[levels] < target) {
levels++;
}
```
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,127 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ assert minDocID <= other.minDocID && maxDocID < other.maxDocID;
+ maxDocID = other.maxDocID;
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<SkipAccumulator> accumulators = new ArrayList<>();
SkipAccumulator accumulator = null;
- int counter = 0;
+ final int maxAccumulators = 1 << (SKIP_INDEX_LEVEL_SHIFT * (SKIP_INDEX_MAX_LEVEL - 1));
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size() == maxAccumulators) {
Review Comment:
Presumably this line is covered testing wise by your recent change that introduced testing for small blocks?
##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -3301,17 +3301,17 @@ private static void checkDocValueSkipper(FieldInfo fi, DocValuesSkipper skipper)
if (skipper.maxDocID(0) == NO_MORE_DOCS) {
break;
}
+ if (skipper.minDocID(0) < doc) {
+ throw new CheckIndexException(
+ "skipper dv iterator for field: "
+ + fieldName
+ + " reports wrong minDocID, got "
+ + skipper.minDocID(0)
+ + " < "
+ + doc);
+ }
int levels = skipper.numLevels();
for (int level = 0; level < levels; level++) {
- if (skipper.minDocID(level) < doc) {
- throw new CheckIndexException(
- "skipper dv iterator for field: "
- + fieldName
- + " reports wrong minDocID, got "
- + skipper.minDocID(level)
- + " < "
- + doc);
- }
Review Comment:
This makes sense.
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,91 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels = 1;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
+ assert levels <= SKIP_INDEX_MAX_LEVEL && levels > 0
+ : "level out of range [" + levels + "]";
+ boolean competitive = true;
+ // check if current interval is competitive or we can jump to the next position
Review Comment:
I got a bit confused at first by the use of `competitive`. Maybe `valid`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1677636214
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,88 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
Review Comment:
I see. I need to think more about it. It makes sense to me for top-level queries which would visit the full doc ID range anyway. But if the query is part of a conjunction, then the leading clause of the conjunction could advance this clause to an arbitrary doc in the doc ID space, and I wonder if we're losing potential efficiency by not making the higher levels visible.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1678955876
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,88 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
Review Comment:
FWIW the call site I have in mind is this one: https://github.com/apache/lucene/blob/5e52b8094aca4dab2898c7dd17084fc7cf7fd016/lucene/core/src/java/org/apache/lucene/document/DocValuesRangeIterator.java#L89. How could it know that it should advance to an earlier doc ID if it wanted to have access to the higher levels?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1681096883
##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -3301,17 +3301,17 @@ private static void checkDocValueSkipper(FieldInfo fi, DocValuesSkipper skipper)
if (skipper.maxDocID(0) == NO_MORE_DOCS) {
break;
}
+ if (skipper.minDocID(0) < doc) {
+ throw new CheckIndexException(
+ "skipper dv iterator for field: "
+ + fieldName
+ + " reports wrong minDocID, got "
+ + skipper.minDocID(0)
+ + " < "
+ + doc);
+ }
int levels = skipper.numLevels();
for (int level = 0; level < levels; level++) {
- if (skipper.minDocID(level) < doc) {
- throw new CheckIndexException(
- "skipper dv iterator for field: "
- + fieldName
- + " reports wrong minDocID, got "
- + skipper.minDocID(level)
- + " < "
- + doc);
- }
Review Comment:
The current condition is `skipper.minDocID(level) < doc` where the doc is the provided doc on advance. So the provided doc needs to be lower than or equal to the minDocID not to trigger the error. I
It checks we have advance to the closer interval forward. This check is strange as it is only valid for implementations with exact bounds and it is clearly not valid for levels above 0.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1681097434
##########
lucene/test-framework/src/java/org/apache/lucene/tests/index/AssertingLeafReader.java:
##########
@@ -1194,24 +1194,27 @@ public int numLevels() {
@Override
public int minDocID(int level) {
assertThread("Doc values skipper", creationThread);
- Objects.checkIndex(level, numLevels());
int minDocID = in.minDocID(level);
assert minDocID <= in.maxDocID(level);
- if (level > 0) {
- assert minDocID <= in.minDocID(level - 1);
+ if (minDocID != -1 && minDocID != DocIdSetIterator.NO_MORE_DOCS) {
+ Objects.checkIndex(level, numLevels());
+ }
+ for (int i = 0; i < level; i++) {
+ assert minDocID >= in.minDocID(i);
Review Comment:
Could we make `numLevels` return 1 to avoid this issue?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1681081567
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,133 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ minDocID = Math.min(minDocID, other.minDocID);
+ maxDocID = Math.max(maxDocID, other.maxDocID);
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<List<SkipAccumulator>> accumulators = new ArrayList<>(SKIP_INDEX_MAX_LEVEL);
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ accumulators.add(new ArrayList<>());
+ }
SkipAccumulator accumulator = null;
- int counter = 0;
+ final int maxAccumulators = 1 << (SKIP_INDEX_LEVEL_SHIFT * (SKIP_INDEX_MAX_LEVEL - 1));
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.get(0).add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size() == maxAccumulators) {
+ writeLevels(accumulators);
+ for (List<SkipAccumulator> accumulatorList : accumulators) {
+ accumulatorList.clear();
+ }
+ }
}
}
- if (counter > 0) {
- globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
- globalMinValue = Math.min(globalMinValue, accumulator.minValue);
- globalDocCount += accumulator.docCount;
- maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
+ if (accumulators.isEmpty() == false) {
+ if (accumulator != null) {
+ globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
+ globalMinValue = Math.min(globalMinValue, accumulator.minValue);
+ globalDocCount += accumulator.docCount;
+ maxDocId = accumulator.maxDocID;
+ }
+ writeLevels(accumulators);
}
meta.writeLong(start); // record the start in meta
meta.writeLong(data.getFilePointer() - start); // record the length
+ assert globalDocCount == 0 || globalMaxValue >= globalMinValue;
meta.writeLong(globalMaxValue);
meta.writeLong(globalMinValue);
+ assert globalDocCount <= maxDocId + 1;
meta.writeInt(globalDocCount);
meta.writeInt(maxDocId);
}
+ private void writeLevels(List<List<SkipAccumulator>> accumulators) throws IOException {
+ for (int i = 1; i < accumulators.size(); i++) {
+ buildLevel(accumulators.get(i), accumulators.get(i - 1));
+ }
+ int totalAccumulators = accumulators.get(0).size();
+ for (int index = 0; index < totalAccumulators; index++) {
+ // compute how many levels we need to write for the current accumulator
+ final int levels = getLevels(index, totalAccumulators);
+ // build the levels
+ final SkipAccumulator[] accLevels = new SkipAccumulator[levels];
Review Comment:
yes, I did not realize when I modified the way we were writing the levels
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1681082271
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,91 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
+ assert levels <= SKIP_INDEX_MAX_LEVEL && levels > 0
+ : "level out of range [" + levels + "]";
+ boolean competitive = true;
+ // check if current interval is competitive or we can jump to the next position
+ for (int level = levels - 1; level >= 0; level--) {
+ if ((maxDocID[level] = input.readInt()) < target) {
+ input.skipBytes(SKIP_INDEX_JUMP_LENGTH_PER_LEVEL[level]); // the jump for the level
+ competitive = false;
+ break;
+ }
+ minDocID[level] = input.readInt();
+ maxValue[level] = input.readLong();
+ minValue[level] = input.readLong();
+ docCount[level] = input.readInt();
+ }
+ if (competitive) {
+ // adjust levels
+ while (levels < SKIP_INDEX_MAX_LEVEL) {
+ if (maxDocID[levels] == -1 || maxDocID[levels] < target) {
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1680897686
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,133 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ minDocID = Math.min(minDocID, other.minDocID);
+ maxDocID = Math.max(maxDocID, other.maxDocID);
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<List<SkipAccumulator>> accumulators = new ArrayList<>(SKIP_INDEX_MAX_LEVEL);
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ accumulators.add(new ArrayList<>());
+ }
SkipAccumulator accumulator = null;
- int counter = 0;
+ final int maxAccumulators = 1 << (SKIP_INDEX_LEVEL_SHIFT * (SKIP_INDEX_MAX_LEVEL - 1));
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.get(0).add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size() == maxAccumulators) {
+ writeLevels(accumulators);
+ for (List<SkipAccumulator> accumulatorList : accumulators) {
+ accumulatorList.clear();
+ }
+ }
}
}
- if (counter > 0) {
- globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
- globalMinValue = Math.min(globalMinValue, accumulator.minValue);
- globalDocCount += accumulator.docCount;
- maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
+ if (accumulators.isEmpty() == false) {
+ if (accumulator != null) {
+ globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
+ globalMinValue = Math.min(globalMinValue, accumulator.minValue);
+ globalDocCount += accumulator.docCount;
+ maxDocId = accumulator.maxDocID;
+ }
+ writeLevels(accumulators);
}
meta.writeLong(start); // record the start in meta
meta.writeLong(data.getFilePointer() - start); // record the length
+ assert globalDocCount == 0 || globalMaxValue >= globalMinValue;
meta.writeLong(globalMaxValue);
meta.writeLong(globalMinValue);
+ assert globalDocCount <= maxDocId + 1;
meta.writeInt(globalDocCount);
meta.writeInt(maxDocId);
}
+ private void writeLevels(List<List<SkipAccumulator>> accumulators) throws IOException {
+ for (int i = 1; i < accumulators.size(); i++) {
+ buildLevel(accumulators.get(i), accumulators.get(i - 1));
+ }
+ int totalAccumulators = accumulators.get(0).size();
+ for (int index = 0; index < totalAccumulators; index++) {
+ // compute how many levels we need to write for the current accumulator
+ final int levels = getLevels(index, totalAccumulators);
+ // build the levels
+ final SkipAccumulator[] accLevels = new SkipAccumulator[levels];
+ for (int level = 0; level < levels; level++) {
+ accLevels[level] =
+ accumulators.get(level).get(index / (1 << (SKIP_INDEX_LEVEL_SHIFT * level)));
Review Comment:
Nit: I believe that this could be simplified to:
```suggestion
accumulators.get(level).get(index >> (SKIP_INDEX_LEVEL_SHIFT * level));
```
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,133 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ minDocID = Math.min(minDocID, other.minDocID);
+ maxDocID = Math.max(maxDocID, other.maxDocID);
Review Comment:
Nit: It looks like we always accumulate in doc ID order, so we could just do `maxDocID = other.maxDocID` (and assert that `minDocID < other.minDocId && maxDocId < other.maxDocID`)?
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,91 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
+ assert levels <= SKIP_INDEX_MAX_LEVEL && levels > 0
+ : "level out of range [" + levels + "]";
+ boolean competitive = true;
+ // check if current interval is competitive or we can jump to the next position
+ for (int level = levels - 1; level >= 0; level--) {
+ if ((maxDocID[level] = input.readInt()) < target) {
+ input.skipBytes(SKIP_INDEX_JUMP_LENGTH_PER_LEVEL[level]); // the jump for the level
+ competitive = false;
+ break;
+ }
+ minDocID[level] = input.readInt();
+ maxValue[level] = input.readLong();
+ minValue[level] = input.readLong();
+ docCount[level] = input.readInt();
+ }
+ if (competitive) {
+ // adjust levels
+ while (levels < SKIP_INDEX_MAX_LEVEL) {
+ if (maxDocID[levels] == -1 || maxDocID[levels] < target) {
Review Comment:
I believe that the second condition is a superset of the first condition, so we could skip the first one?
```suggestion
if (maxDocID[levels] < target) {
```
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,133 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ minDocID = Math.min(minDocID, other.minDocID);
+ maxDocID = Math.max(maxDocID, other.maxDocID);
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<List<SkipAccumulator>> accumulators = new ArrayList<>(SKIP_INDEX_MAX_LEVEL);
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ accumulators.add(new ArrayList<>());
+ }
SkipAccumulator accumulator = null;
- int counter = 0;
+ final int maxAccumulators = 1 << (SKIP_INDEX_LEVEL_SHIFT * (SKIP_INDEX_MAX_LEVEL - 1));
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.get(0).add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size() == maxAccumulators) {
+ writeLevels(accumulators);
+ for (List<SkipAccumulator> accumulatorList : accumulators) {
+ accumulatorList.clear();
+ }
+ }
}
}
- if (counter > 0) {
- globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
- globalMinValue = Math.min(globalMinValue, accumulator.minValue);
- globalDocCount += accumulator.docCount;
- maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
+ if (accumulators.isEmpty() == false) {
+ if (accumulator != null) {
+ globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
+ globalMinValue = Math.min(globalMinValue, accumulator.minValue);
+ globalDocCount += accumulator.docCount;
+ maxDocId = accumulator.maxDocID;
+ }
+ writeLevels(accumulators);
}
meta.writeLong(start); // record the start in meta
meta.writeLong(data.getFilePointer() - start); // record the length
+ assert globalDocCount == 0 || globalMaxValue >= globalMinValue;
meta.writeLong(globalMaxValue);
meta.writeLong(globalMinValue);
+ assert globalDocCount <= maxDocId + 1;
meta.writeInt(globalDocCount);
meta.writeInt(maxDocId);
}
+ private void writeLevels(List<List<SkipAccumulator>> accumulators) throws IOException {
+ for (int i = 1; i < accumulators.size(); i++) {
+ buildLevel(accumulators.get(i), accumulators.get(i - 1));
+ }
+ int totalAccumulators = accumulators.get(0).size();
+ for (int index = 0; index < totalAccumulators; index++) {
+ // compute how many levels we need to write for the current accumulator
+ final int levels = getLevels(index, totalAccumulators);
+ // build the levels
+ final SkipAccumulator[] accLevels = new SkipAccumulator[levels];
Review Comment:
Nit: it looks like we don't actually need to copy them to an array and we could instead dynamically compute the index to look up in the for loop that writes levels?
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,133 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ minDocID = Math.min(minDocID, other.minDocID);
+ maxDocID = Math.max(maxDocID, other.maxDocID);
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<List<SkipAccumulator>> accumulators = new ArrayList<>(SKIP_INDEX_MAX_LEVEL);
Review Comment:
Nit: For simplicity, maybe this could be just a list of accumulators (instead of a list of lists) and we'd create the list of lists in `writeLevels` since lists at indexes 1 or more are only used in `writeLevels`?
##########
lucene/core/src/java/org/apache/lucene/index/CheckIndex.java:
##########
@@ -3301,17 +3301,17 @@ private static void checkDocValueSkipper(FieldInfo fi, DocValuesSkipper skipper)
if (skipper.maxDocID(0) == NO_MORE_DOCS) {
break;
}
+ if (skipper.minDocID(0) < doc) {
+ throw new CheckIndexException(
+ "skipper dv iterator for field: "
+ + fieldName
+ + " reports wrong minDocID, got "
+ + skipper.minDocID(0)
+ + " < "
+ + doc);
+ }
int levels = skipper.numLevels();
for (int level = 0; level < levels; level++) {
- if (skipper.minDocID(level) < doc) {
- throw new CheckIndexException(
- "skipper dv iterator for field: "
- + fieldName
- + " reports wrong minDocID, got "
- + skipper.minDocID(level)
- + " < "
- + doc);
- }
Review Comment:
What was wrong with having this check on every level?
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,133 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ minDocID = Math.min(minDocID, other.minDocID);
+ maxDocID = Math.max(maxDocID, other.maxDocID);
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<List<SkipAccumulator>> accumulators = new ArrayList<>(SKIP_INDEX_MAX_LEVEL);
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ accumulators.add(new ArrayList<>());
+ }
SkipAccumulator accumulator = null;
- int counter = 0;
+ final int maxAccumulators = 1 << (SKIP_INDEX_LEVEL_SHIFT * (SKIP_INDEX_MAX_LEVEL - 1));
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.get(0).add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size() == maxAccumulators) {
Review Comment:
Should it actually be `accumulators.get(0).size() == maxAccumulators`?
##########
lucene/test-framework/src/java/org/apache/lucene/tests/index/AssertingLeafReader.java:
##########
@@ -1194,24 +1194,27 @@ public int numLevels() {
@Override
public int minDocID(int level) {
assertThread("Doc values skipper", creationThread);
- Objects.checkIndex(level, numLevels());
int minDocID = in.minDocID(level);
assert minDocID <= in.maxDocID(level);
- if (level > 0) {
- assert minDocID <= in.minDocID(level - 1);
+ if (minDocID != -1 && minDocID != DocIdSetIterator.NO_MORE_DOCS) {
+ Objects.checkIndex(level, numLevels());
+ }
+ for (int i = 0; i < level; i++) {
+ assert minDocID >= in.minDocID(i);
Review Comment:
The check made a bit more sense to me before. Is the problem that we return a `numLevels()` of 0 when the iterator is unpositioned or exhausted?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1681080747
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,133 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ minDocID = Math.min(minDocID, other.minDocID);
+ maxDocID = Math.max(maxDocID, other.maxDocID);
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<List<SkipAccumulator>> accumulators = new ArrayList<>(SKIP_INDEX_MAX_LEVEL);
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ accumulators.add(new ArrayList<>());
+ }
SkipAccumulator accumulator = null;
- int counter = 0;
+ final int maxAccumulators = 1 << (SKIP_INDEX_LEVEL_SHIFT * (SKIP_INDEX_MAX_LEVEL - 1));
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.get(0).add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size() == maxAccumulators) {
+ writeLevels(accumulators);
+ for (List<SkipAccumulator> accumulatorList : accumulators) {
+ accumulatorList.clear();
+ }
+ }
}
}
- if (counter > 0) {
- globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
- globalMinValue = Math.min(globalMinValue, accumulator.minValue);
- globalDocCount += accumulator.docCount;
- maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
+ if (accumulators.isEmpty() == false) {
+ if (accumulator != null) {
+ globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
+ globalMinValue = Math.min(globalMinValue, accumulator.minValue);
+ globalDocCount += accumulator.docCount;
+ maxDocId = accumulator.maxDocID;
+ }
+ writeLevels(accumulators);
}
meta.writeLong(start); // record the start in meta
meta.writeLong(data.getFilePointer() - start); // record the length
+ assert globalDocCount == 0 || globalMaxValue >= globalMinValue;
meta.writeLong(globalMaxValue);
meta.writeLong(globalMinValue);
+ assert globalDocCount <= maxDocId + 1;
meta.writeInt(globalDocCount);
meta.writeInt(maxDocId);
}
+ private void writeLevels(List<List<SkipAccumulator>> accumulators) throws IOException {
+ for (int i = 1; i < accumulators.size(); i++) {
+ buildLevel(accumulators.get(i), accumulators.get(i - 1));
+ }
+ int totalAccumulators = accumulators.get(0).size();
+ for (int index = 0; index < totalAccumulators; index++) {
+ // compute how many levels we need to write for the current accumulator
+ final int levels = getLevels(index, totalAccumulators);
+ // build the levels
+ final SkipAccumulator[] accLevels = new SkipAccumulator[levels];
+ for (int level = 0; level < levels; level++) {
+ accLevels[level] =
+ accumulators.get(level).get(index / (1 << (SKIP_INDEX_LEVEL_SHIFT * level)));
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1681080290
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,133 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ minDocID = Math.min(minDocID, other.minDocID);
+ maxDocID = Math.max(maxDocID, other.maxDocID);
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ final List<List<SkipAccumulator>> accumulators = new ArrayList<>(SKIP_INDEX_MAX_LEVEL);
Review Comment:
yes, this list of list is awful. I was thinking in reusing ti but it creates more issues that it helps.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1677637583
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,120 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ maxDocID = other.maxDocID;
+ minValue = Math.min(minValue, other.minValue);
+ maxValue = Math.max(maxValue, other.maxValue);
+ docCount += other.docCount;
+ }
+
void nextDoc(int docID) {
maxDocID = docID;
++docCount;
}
- void writeTo(DataOutput output) throws IOException {
- output.writeInt(maxDocID);
- output.writeInt(minDocID);
- output.writeLong(maxValue);
- output.writeLong(minValue);
- output.writeInt(docCount);
+ public static SkipAccumulator merge(List<SkipAccumulator> list, int index, int length) {
+ SkipAccumulator acc = new SkipAccumulator(list.get(index).minDocID);
+ for (int i = 0; i < length; i++) {
+ acc.accumulate(list.get(index + i));
+ }
+ return acc;
}
}
private void writeSkipIndex(FieldInfo field, DocValuesProducer valuesProducer)
throws IOException {
assert field.hasDocValuesSkipIndex();
- // TODO: This disk compression once we introduce levels
- long start = data.getFilePointer();
- SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
+ final long start = data.getFilePointer();
+ final SortedNumericDocValues values = valuesProducer.getSortedNumeric(field);
long globalMaxValue = Long.MIN_VALUE;
long globalMinValue = Long.MAX_VALUE;
int globalDocCount = 0;
int maxDocId = -1;
+ List<SkipAccumulator> accumulators = new ArrayList<>();
SkipAccumulator accumulator = null;
- int counter = 0;
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
- if (counter == 0) {
+ if (accumulator == null) {
accumulator = new SkipAccumulator(doc);
+ accumulators.add(accumulator);
}
accumulator.nextDoc(doc);
for (int i = 0, end = values.docValueCount(); i < end; ++i) {
accumulator.accumulate(values.nextValue());
}
- if (++counter == skipIndexIntervalSize) {
+ if (accumulator.docCount == skipIndexIntervalSize) {
globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
globalMinValue = Math.min(globalMinValue, accumulator.minValue);
globalDocCount += accumulator.docCount;
maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
- counter = 0;
+ accumulator = null;
+ if (accumulators.size()
+ == SKIP_INDEX_NUMBER_INTERVALS_PER_LEVEL[SKIP_INDEX_MAX_LEVEL - 1]) {
+ writeLevels(accumulators);
+ accumulators.clear();
+ }
}
}
- if (counter > 0) {
- globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
- globalMinValue = Math.min(globalMinValue, accumulator.minValue);
- globalDocCount += accumulator.docCount;
- maxDocId = accumulator.maxDocID;
- accumulator.writeTo(data);
+ if (accumulators.isEmpty() == false) {
+ if (accumulator != null) {
+ globalMaxValue = Math.max(globalMaxValue, accumulator.maxValue);
+ globalMinValue = Math.min(globalMinValue, accumulator.minValue);
+ globalDocCount += accumulator.docCount;
+ maxDocId = accumulator.maxDocID;
+ }
+ writeLevels(accumulators);
}
meta.writeLong(start); // record the start in meta
meta.writeLong(data.getFilePointer() - start); // record the length
+ assert globalDocCount == 0 || globalMaxValue >= globalMinValue;
meta.writeLong(globalMaxValue);
meta.writeLong(globalMinValue);
+ assert globalDocCount <= maxDocId + 1;
meta.writeInt(globalDocCount);
meta.writeInt(maxDocId);
}
+ private void writeLevels(List<SkipAccumulator> accumulators) throws IOException {
+ for (int index = 0; index < accumulators.size(); index++) {
+ // compute how many levels we need to write for the current accumulator
+ final int levels = getLevels(index, accumulators.size());
+ // build the levels
+ final SkipAccumulator[] accLevels = new SkipAccumulator[levels];
+ for (int level = 0; level < levels; level++) {
+ accLevels[level] =
+ SkipAccumulator.merge(
+ accumulators, index, SKIP_INDEX_NUMBER_INTERVALS_PER_LEVEL[level]);
Review Comment:
I was hoping this approach would be a bit more heap friendly as we will only have to create those accumulators for a short period of time and not having a list per level.
But yes, it might make sense to build all the levels upfront using previous levels.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1682656013
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,91 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
Review Comment:
I have plans to allow some intervals to have different number of documents depending on the unique values in the interval so we would not be able to compute the number of levels by looking at the number of docs.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1681083206
##########
lucene/test-framework/src/java/org/apache/lucene/tests/index/AssertingLeafReader.java:
##########
@@ -1194,24 +1194,27 @@ public int numLevels() {
@Override
public int minDocID(int level) {
assertThread("Doc values skipper", creationThread);
- Objects.checkIndex(level, numLevels());
int minDocID = in.minDocID(level);
assert minDocID <= in.maxDocID(level);
- if (level > 0) {
- assert minDocID <= in.minDocID(level - 1);
+ if (minDocID != -1 && minDocID != DocIdSetIterator.NO_MORE_DOCS) {
+ Objects.checkIndex(level, numLevels());
+ }
+ for (int i = 0; i < level; i++) {
+ assert minDocID >= in.minDocID(i);
Review Comment:
exactly
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase merged PR #13563:
URL: https://github.com/apache/lucene/pull/13563
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1677624311
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,88 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
Review Comment:
This is done in purpose as I am thinking in this data structure as a skip list, no a tree, so any given interval at any level should only be evaluated once and therefore only appear once while reading the index.
The idea is that users only see new levels the first time they get updated. At that point, user can decide if they want to skip it or not. If they don't skip it, then they go inside that level but they should not have to evaluate it again and therefore it should not appear in lower levels.
I think otherwise, we will be evaluating the same interval many times?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1677661302
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,88 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
Review Comment:
mmm, advancing doc ids is different to exposing levels through the API. It is related to the implementation of `#advance(int docID)`. We are currently reading the next interval when we advance which can be inefficient on advancing to an arbitrary doc ID.
We can probably do better there
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1681079162
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesConsumer.java:
##########
@@ -207,65 +210,133 @@ void accumulate(long value) {
maxValue = Math.max(maxValue, value);
}
+ void accumulate(SkipAccumulator other) {
+ minDocID = Math.min(minDocID, other.minDocID);
+ maxDocID = Math.max(maxDocID, other.maxDocID);
Review Comment:
Done. The assert need to be `minDocID <= other.minDocId && maxDocId < other.maxDocID)` because we initializr the accumulator with the minDocID
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Add levels to DocValues skipper index [lucene]
Posted by "iverase (via GitHub)" <gi...@apache.org>.
iverase commented on code in PR #13563:
URL: https://github.com/apache/lucene/pull/13563#discussion_r1682654554
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90DocValuesProducer.java:
##########
@@ -1792,61 +1794,91 @@ public DocValuesSkipper getSkipper(FieldInfo field) throws IOException {
if (input.length() > 0) {
input.prefetch(0, 1);
}
+ // TODO: should we write to disk the actual max level for this segment?
return new DocValuesSkipper() {
- int minDocID = -1;
- int maxDocID = -1;
- long minValue, maxValue;
- int docCount;
+ final int[] minDocID = new int[SKIP_INDEX_MAX_LEVEL];
+ final int[] maxDocID = new int[SKIP_INDEX_MAX_LEVEL];
+
+ {
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = -1;
+ }
+ }
+
+ final long[] minValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final long[] maxValue = new long[SKIP_INDEX_MAX_LEVEL];
+ final int[] docCount = new int[SKIP_INDEX_MAX_LEVEL];
+ int levels = 1;
@Override
public void advance(int target) throws IOException {
if (target > entry.maxDocId) {
- minDocID = DocIdSetIterator.NO_MORE_DOCS;
- maxDocID = DocIdSetIterator.NO_MORE_DOCS;
+ // skipper is exhausted
+ for (int i = 0; i < SKIP_INDEX_MAX_LEVEL; i++) {
+ minDocID[i] = maxDocID[i] = DocIdSetIterator.NO_MORE_DOCS;
+ }
} else {
+ // find next interval
+ assert target > maxDocID[0] : "target must be bigger that current interval";
while (true) {
- maxDocID = input.readInt();
- if (maxDocID >= target) {
- minDocID = input.readInt();
- maxValue = input.readLong();
- minValue = input.readLong();
- docCount = input.readInt();
+ levels = input.readByte();
+ assert levels <= SKIP_INDEX_MAX_LEVEL && levels > 0
+ : "level out of range [" + levels + "]";
+ boolean competitive = true;
+ // check if current interval is competitive or we can jump to the next position
+ for (int level = levels - 1; level >= 0; level--) {
+ if ((maxDocID[level] = input.readInt()) < target) {
+ input.skipBytes(SKIP_INDEX_JUMP_LENGTH_PER_LEVEL[level]); // the jump for the level
+ competitive = false;
+ break;
+ }
+ minDocID[level] = input.readInt();
+ maxValue[level] = input.readLong();
+ minValue[level] = input.readLong();
+ docCount[level] = input.readInt();
+ }
+ if (competitive) {
+ // adjust levels
+ while (levels < SKIP_INDEX_MAX_LEVEL) {
+ if (maxDocID[levels] < target) {
+ break;
+ }
+ levels++;
+ }
Review Comment:
of course, the condition looks more like:
```java
while (levels < SKIP_INDEX_MAX_LEVEL && maxDocID[levels] >= target) {
levels++;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org