Skip to content

Commit 5f83743

Browse files
committed
more iterators
1 parent 541e953 commit 5f83743

File tree

3 files changed

+232
-1
lines changed

3 files changed

+232
-1
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
package org.elasticsearch.lucene.queries;
10+
11+
import org.apache.lucene.index.DocValuesSkipper;
12+
import org.apache.lucene.index.NumericDocValues;
13+
import org.apache.lucene.search.DocIdSetIterator;
14+
15+
import java.io.IOException;
16+
17+
// NOT USED
18+
final class TimestampIterator2 extends DocIdSetIterator {
19+
20+
final NumericDocValues timestamps;
21+
22+
final DocValuesSkipper timestampSkipper;
23+
final DocValuesSkipper primaryFieldSkipper;
24+
final long minTimestamp;
25+
final long maxTimestamp;
26+
27+
int docID = -1;
28+
boolean skipperMatch;
29+
int primaryFieldUpTo = -1;
30+
int timestampFieldUpTo = -1;
31+
32+
TimestampIterator2(
33+
NumericDocValues timestamps,
34+
DocValuesSkipper timestampSkipper,
35+
DocValuesSkipper primaryFieldSkipper,
36+
long minTimestamp,
37+
long maxTimestamp
38+
) {
39+
this.timestamps = timestamps;
40+
this.timestampSkipper = timestampSkipper;
41+
this.primaryFieldSkipper = primaryFieldSkipper;
42+
this.minTimestamp = minTimestamp;
43+
this.maxTimestamp = maxTimestamp;
44+
}
45+
46+
@Override
47+
public int docID() {
48+
return docID;
49+
}
50+
51+
@Override
52+
public int nextDoc() throws IOException {
53+
return advance(docID + 1);
54+
}
55+
56+
static int one;
57+
static int two;
58+
static int three;
59+
static int bla;
60+
61+
@Override
62+
public int advance(int target) throws IOException {
63+
skipperMatch = false;
64+
target = timestamps.advance(target);
65+
if (target == DocIdSetIterator.NO_MORE_DOCS) {
66+
return docID = target;
67+
}
68+
while (true) {
69+
if (target > timestampFieldUpTo) {
70+
timestampSkipper.advance(target);
71+
timestampFieldUpTo = timestampSkipper.maxDocID(0);
72+
long minValue = timestampSkipper.minValue(0);
73+
long maxValue = timestampSkipper.maxValue(0);
74+
if (minValue > maxTimestamp || maxValue < minTimestamp) {
75+
for (int level = 1; level < timestampSkipper.numLevels(); level++) {
76+
minValue = timestampSkipper.minValue(level);
77+
maxValue = timestampSkipper.maxValue(level);
78+
if (minValue > maxTimestamp || maxValue < minTimestamp) {
79+
timestampFieldUpTo = timestampSkipper.maxDocID(level);
80+
} else {
81+
break;
82+
}
83+
}
84+
85+
int upTo = timestampFieldUpTo;
86+
if (maxValue < minTimestamp) {
87+
primaryFieldSkipper.advance(target);
88+
primaryFieldUpTo = primaryFieldSkipper.maxDocID(0);
89+
if (primaryFieldSkipper.minValue(0) == primaryFieldSkipper.maxValue(0)) {
90+
for (int level = 1; level < primaryFieldSkipper.numLevels(); level++) {
91+
if (primaryFieldSkipper.minValue(level) == primaryFieldSkipper.maxValue(level)) {
92+
primaryFieldUpTo = primaryFieldSkipper.maxDocID(level);
93+
} else {
94+
break;
95+
}
96+
}
97+
}
98+
bla++;
99+
if (primaryFieldUpTo > upTo) {
100+
upTo = primaryFieldUpTo;
101+
}
102+
}
103+
104+
one++;
105+
target = timestamps.advance(upTo + 1);
106+
if (target == DocIdSetIterator.NO_MORE_DOCS) {
107+
return docID = target;
108+
}
109+
} else if (minValue >= minTimestamp && maxValue <= maxTimestamp) {
110+
assert timestampSkipper.docCount(0) == timestampSkipper.maxDocID(0) - timestampSkipper.minDocID(0) + 1;
111+
skipperMatch = true;
112+
two++;
113+
return docID = target;
114+
}
115+
}
116+
117+
long value = timestamps.longValue();
118+
if (value < minTimestamp && target > primaryFieldUpTo) {
119+
primaryFieldSkipper.advance(target);
120+
primaryFieldUpTo = primaryFieldSkipper.maxDocID(0);
121+
if (primaryFieldSkipper.minValue(0) == primaryFieldSkipper.maxValue(0)) {
122+
for (int level = 1; level < primaryFieldSkipper.numLevels(); level++) {
123+
if (primaryFieldSkipper.minValue(level) == primaryFieldSkipper.maxValue(level)) {
124+
primaryFieldUpTo = primaryFieldSkipper.maxDocID(level);
125+
} else {
126+
break;
127+
}
128+
}
129+
three++;
130+
target = timestamps.advance(primaryFieldUpTo + 1);
131+
if (target == DocIdSetIterator.NO_MORE_DOCS) {
132+
return docID = target;
133+
}
134+
} else {
135+
target = timestamps.nextDoc();
136+
if (target == DocIdSetIterator.NO_MORE_DOCS) {
137+
return docID = target;
138+
}
139+
}
140+
} else if (value >= minTimestamp && value <= maxTimestamp) {
141+
return docID = target;
142+
} else {
143+
target = timestamps.nextDoc();
144+
if (target == DocIdSetIterator.NO_MORE_DOCS) {
145+
return docID = target;
146+
}
147+
}
148+
}
149+
}
150+
151+
@Override
152+
public int docIDRunEnd() throws IOException {
153+
if (skipperMatch) {
154+
return timestampFieldUpTo + 1;
155+
}
156+
return super.docIDRunEnd();
157+
}
158+
159+
@Override
160+
public long cost() {
161+
return timestamps.cost();
162+
}
163+
164+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
package org.elasticsearch.lucene.queries;
10+
11+
import org.apache.lucene.index.NumericDocValues;
12+
import org.apache.lucene.search.DocIdSetIterator;
13+
14+
import java.io.IOException;
15+
16+
/**
17+
* If doc values skippers aren't helpful, then just fallback to doc values. (linear execution)
18+
*/
19+
// NOT USED
20+
final class TimestampIterator3 extends DocIdSetIterator {
21+
22+
final NumericDocValues timestamps;
23+
24+
final long minTimestamp;
25+
final long maxTimestamp;
26+
27+
int docID = -1;
28+
29+
TimestampIterator3(NumericDocValues timestamps, long minTimestamp, long maxTimestamp) {
30+
this.timestamps = timestamps;
31+
this.minTimestamp = minTimestamp;
32+
this.maxTimestamp = maxTimestamp;
33+
}
34+
35+
@Override
36+
public int docID() {
37+
return docID;
38+
}
39+
40+
@Override
41+
public int nextDoc() throws IOException {
42+
return advance(docID + 1);
43+
}
44+
45+
@Override
46+
public int advance(int target) throws IOException {
47+
target = timestamps.advance(target);
48+
if (target == DocIdSetIterator.NO_MORE_DOCS) {
49+
return docID = target;
50+
}
51+
while (true) {
52+
long value = timestamps.longValue();
53+
if (value >= minTimestamp && value <= maxTimestamp) {
54+
return docID = target;
55+
} else {
56+
target = timestamps.nextDoc();
57+
if (target == DocIdSetIterator.NO_MORE_DOCS) {
58+
return docID = target;
59+
}
60+
}
61+
}
62+
}
63+
64+
@Override
65+
public long cost() {
66+
return timestamps.cost();
67+
}
68+
}

server/src/main/java/org/elasticsearch/lucene/queries/TimestampQuery.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOExcepti
9494
var primaryFieldSkipper = reader.getDocValuesSkipper(primarySortField);
9595
var iterator = new TimestampIterator(timestamps, timestampSkipper, primaryFieldSkipper, minTimestamp, maxTimestamp);
9696
return ConstantScoreScorerSupplier.fromIterator(TwoPhaseIterator.asDocIdSetIterator(iterator), score(), scoreMode, maxDoc);
97-
// var primaryFieldSkipper = reader.getDocValuesSkipper(primarySortField);
9897
// var iterator = new TimestampIterator2(timestamps, timestampSkipper, primaryFieldSkipper, minTimestamp, maxTimestamp);
9998
// return ConstantScoreScorerSupplier.fromIterator(iterator, score(), scoreMode, maxDoc);
10099
}

0 commit comments

Comments
 (0)