Skip to content

Commit a2bc5fa

Browse files
committed
Add doc values tests
1 parent e026a5c commit a2bc5fa

File tree

2 files changed

+181
-4
lines changed

2 files changed

+181
-4
lines changed

x-pack/plugin/logsdb/src/main/java/org/elasticsearch/xpack/logsdb/patternedtext/PatternedTextDocValues.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ public int docID() {
6868
@Override
6969
public int nextDoc() throws IOException {
7070
int templateNext = templateDocValues.nextDoc();
71-
int argsNext = argsDocValues.nextDoc();
72-
assert templateNext == argsNext;
71+
var argsAdvance = argsDocValues.advance(templateNext);
72+
assert argsAdvance >= templateNext;
7373
return templateNext;
7474
}
7575

7676
@Override
7777
public int advance(int i) throws IOException {
7878
int templateAdvance = templateDocValues.advance(i);
79-
int argAdvance = argsDocValues.advance(i);
80-
assert templateAdvance == argAdvance;
79+
var argsAdvance = argsDocValues.advance(templateAdvance);
80+
assert argsAdvance >= templateAdvance;
8181
return templateAdvance;
8282
}
8383

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.logsdb.patternedtext;
9+
10+
import org.apache.lucene.index.SortedSetDocValues;
11+
import org.apache.lucene.util.BytesRef;
12+
import org.elasticsearch.test.ESTestCase;
13+
14+
import java.io.IOException;
15+
import java.util.ArrayList;
16+
import java.util.Arrays;
17+
import java.util.HashSet;
18+
import java.util.List;
19+
import java.util.Objects;
20+
import java.util.stream.Collectors;
21+
22+
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
23+
24+
public class PatternTextDocValuesTests extends ESTestCase {
25+
26+
private static PatternedTextDocValues makeDocValueSparseArgs() {
27+
var template = new SimpleSortedSetDocValues("%W dog", "cat", "%W mouse %W", "hat %W");
28+
var args = new SimpleSortedSetDocValues("1", null, "2 3", "4");
29+
return new PatternedTextDocValues(template, args);
30+
}
31+
32+
private static PatternedTextDocValues makeDocValuesDenseArgs() {
33+
var template = new SimpleSortedSetDocValues("%W moose", "%W goose %W", "%W mouse %W", "%W house");
34+
var args = new SimpleSortedSetDocValues("1", "4 5", "2 3", "7");
35+
return new PatternedTextDocValues(template, args);
36+
}
37+
38+
private static PatternedTextDocValues makeDocValueMissingValues() {
39+
var template = new SimpleSortedSetDocValues("%W cheddar", "cat", null, "%W cheese");
40+
var args = new SimpleSortedSetDocValues("1", null, null, "4");
41+
return new PatternedTextDocValues(template, args);
42+
}
43+
44+
public void testNextDoc() throws IOException {
45+
var docValues = randomBoolean() ? makeDocValueSparseArgs() : makeDocValuesDenseArgs();
46+
assertEquals(-1, docValues.docID());
47+
assertEquals(0, docValues.nextDoc());
48+
assertEquals(1, docValues.nextDoc());
49+
assertEquals(2, docValues.nextDoc());
50+
assertEquals(3, docValues.nextDoc());
51+
assertEquals(NO_MORE_DOCS, docValues.nextDoc());
52+
}
53+
54+
public void testNextDocMissing() throws IOException {
55+
var docValues = makeDocValueMissingValues();
56+
assertEquals(-1, docValues.docID());
57+
assertEquals(0, docValues.nextDoc());
58+
assertEquals(1, docValues.nextDoc());
59+
assertEquals(3, docValues.nextDoc());
60+
assertEquals(NO_MORE_DOCS, docValues.nextDoc());
61+
}
62+
63+
public void testAdvance1() throws IOException {
64+
var docValues = randomBoolean() ? makeDocValueSparseArgs() : makeDocValuesDenseArgs();
65+
assertEquals(-1, docValues.docID());
66+
assertEquals(0, docValues.nextDoc());
67+
assertEquals(1, docValues.advance(1));
68+
assertEquals(2, docValues.advance(2));
69+
assertEquals(3, docValues.advance(3));
70+
assertEquals(NO_MORE_DOCS, docValues.advance(4));
71+
}
72+
73+
public void testAdvanceFarther() throws IOException {
74+
var docValues = randomBoolean() ? makeDocValueSparseArgs() : makeDocValuesDenseArgs();
75+
assertEquals(2, docValues.advance(2));
76+
// repeats says on value
77+
assertEquals(2, docValues.advance(2));
78+
}
79+
80+
public void testAdvanceSkipsValuesIfMissing() throws IOException {
81+
var docValues = makeDocValueMissingValues();
82+
assertEquals(3, docValues.advance(2));
83+
}
84+
85+
public void testAdvanceExactMissing() throws IOException {
86+
var docValues = makeDocValueMissingValues();
87+
assertTrue(docValues.advanceExact(1));
88+
assertFalse(docValues.advanceExact(2));
89+
assertEquals(3, docValues.docID());
90+
}
91+
92+
public void testValueAll() throws IOException {
93+
var docValues = makeDocValuesDenseArgs();
94+
assertEquals(0, docValues.nextDoc());
95+
assertEquals("1 moose", docValues.binaryValue().utf8ToString());
96+
assertEquals(1, docValues.nextDoc());
97+
assertEquals("4 goose 5", docValues.binaryValue().utf8ToString());
98+
assertEquals(2, docValues.nextDoc());
99+
assertEquals("2 mouse 3", docValues.binaryValue().utf8ToString());
100+
assertEquals(3, docValues.nextDoc());
101+
assertEquals("7 house", docValues.binaryValue().utf8ToString());
102+
}
103+
104+
public void testValueMissing() throws IOException {
105+
var docValues = makeDocValueMissingValues();
106+
assertEquals(0, docValues.nextDoc());
107+
assertEquals("1 cheddar", docValues.binaryValue().utf8ToString());
108+
assertEquals(1, docValues.nextDoc());
109+
assertEquals("cat", docValues.binaryValue().utf8ToString());
110+
assertEquals(3, docValues.nextDoc());
111+
assertEquals("4 cheese", docValues.binaryValue().utf8ToString());
112+
}
113+
114+
static class SimpleSortedSetDocValues extends SortedSetDocValues {
115+
116+
private final List<String> ordToValues;
117+
private final List<Integer> docToOrds;
118+
private int currDoc = -1;
119+
120+
SimpleSortedSetDocValues(String... docIdToValue) {
121+
ordToValues = Arrays.stream(docIdToValue).filter(Objects::nonNull)
122+
.collect(Collectors.toSet())
123+
.stream().sorted().toList();
124+
docToOrds = Arrays.stream(docIdToValue).map(v -> v == null ? null : ordToValues.indexOf(v)).toList();
125+
}
126+
127+
@Override
128+
public long nextOrd() {
129+
return docToOrds.get(currDoc);
130+
}
131+
132+
@Override
133+
public int docValueCount() {
134+
return 1;
135+
}
136+
137+
@Override
138+
public BytesRef lookupOrd(long ord) {
139+
return new BytesRef(ordToValues.get((int) ord));
140+
}
141+
142+
@Override
143+
public long getValueCount() {
144+
return ordToValues.size();
145+
}
146+
147+
@Override
148+
public boolean advanceExact(int target) {
149+
return advance(target) == target;
150+
}
151+
152+
@Override
153+
public int docID() {
154+
return currDoc >= docToOrds.size() ? NO_MORE_DOCS : currDoc;
155+
}
156+
157+
@Override
158+
public int nextDoc() throws IOException {
159+
return advance(currDoc + 1);
160+
}
161+
162+
@Override
163+
public int advance(int target) {
164+
for (currDoc = target; currDoc < docToOrds.size(); currDoc++) {
165+
if (docToOrds.get(currDoc) != null) {
166+
return currDoc;
167+
}
168+
}
169+
return NO_MORE_DOCS;
170+
}
171+
172+
@Override
173+
public long cost() {
174+
return 1;
175+
}
176+
}
177+
}

0 commit comments

Comments
 (0)