26
26
import org .apache .lucene .search .KnnCollector ;
27
27
import org .apache .lucene .search .VectorScorer ;
28
28
import org .apache .lucene .search .suggest .document .CompletionTerms ;
29
- import org .apache .lucene .util .BitSet ;
30
29
import org .apache .lucene .util .Bits ;
31
30
import org .apache .lucene .util .BytesRef ;
32
31
import org .apache .lucene .util .automaton .CompiledAutomaton ;
@@ -146,7 +145,7 @@ public void searchNearestVectors(String field, byte[] target, KnnCollector colle
146
145
in .searchNearestVectors (field , target , collector , acceptDocs );
147
146
return ;
148
147
}
149
- in .searchNearestVectors (field , target , collector , createTimeOutCheckingBits ( acceptDocs ) );
148
+ in .searchNearestVectors (field , target , new TimeOutCheckingKnnCollector ( collector ), acceptDocs );
150
149
}
151
150
152
151
@ Override
@@ -164,122 +163,25 @@ public void searchNearestVectors(String field, float[] target, KnnCollector coll
164
163
in .searchNearestVectors (field , target , collector , acceptDocs );
165
164
return ;
166
165
}
167
- in .searchNearestVectors (field , target , collector , createTimeOutCheckingBits ( acceptDocs ) );
166
+ in .searchNearestVectors (field , target , new TimeOutCheckingKnnCollector ( collector ), acceptDocs );
168
167
}
169
168
170
- private Bits createTimeOutCheckingBits (Bits acceptDocs ) {
171
- if (acceptDocs == null || acceptDocs instanceof BitSet ) {
172
- return new TimeOutCheckingBitSet ((BitSet ) acceptDocs );
173
- }
174
- return new TimeOutCheckingBits (acceptDocs );
175
- }
176
-
177
- private class TimeOutCheckingBitSet extends BitSet {
178
- private static final int MAX_CALLS_BEFORE_QUERY_TIMEOUT_CHECK = 10 ;
179
- private int calls ;
180
- private final BitSet inner ;
181
- private final int maxDoc ;
182
-
183
- private TimeOutCheckingBitSet (BitSet inner ) {
184
- this .inner = inner ;
185
- this .maxDoc = maxDoc ();
186
- }
187
-
188
- @ Override
189
- public void set (int i ) {
190
- throw new UnsupportedOperationException ("not supported on TimeOutCheckingBitSet" );
191
- }
192
-
193
- @ Override
194
- public boolean getAndSet (int i ) {
195
- throw new UnsupportedOperationException ("not supported on TimeOutCheckingBitSet" );
196
- }
197
-
198
- @ Override
199
- public void clear (int i ) {
200
- throw new UnsupportedOperationException ("not supported on TimeOutCheckingBitSet" );
201
- }
202
-
203
- @ Override
204
- public void clear (int startIndex , int endIndex ) {
205
- throw new UnsupportedOperationException ("not supported on TimeOutCheckingBitSet" );
206
- }
207
-
208
- @ Override
209
- public int cardinality () {
210
- if (inner == null ) {
211
- return maxDoc ;
212
- }
213
- return inner .cardinality ();
214
- }
215
-
216
- @ Override
217
- public int approximateCardinality () {
218
- if (inner == null ) {
219
- return maxDoc ;
220
- }
221
- return inner .approximateCardinality ();
222
- }
223
-
224
- @ Override
225
- public int prevSetBit (int index ) {
226
- throw new UnsupportedOperationException ("not supported on TimeOutCheckingBitSet" );
227
- }
228
-
229
- @ Override
230
- public int nextSetBit (int start , int end ) {
231
- throw new UnsupportedOperationException ("not supported on TimeOutCheckingBitSet" );
232
- }
233
-
234
- @ Override
235
- public long ramBytesUsed () {
236
- throw new UnsupportedOperationException ("not supported on TimeOutCheckingBitSet" );
237
- }
238
-
239
- @ Override
240
- public boolean get (int index ) {
241
- if (calls ++ % MAX_CALLS_BEFORE_QUERY_TIMEOUT_CHECK == 0 ) {
242
- queryCancellation .checkCancelled ();
243
- }
244
- if (inner == null ) {
245
- // if acceptDocs is null, we assume all docs are accepted
246
- return index >= 0 && index < maxDoc ;
247
- }
248
- return inner .get (index );
249
- }
250
-
251
- @ Override
252
- public int length () {
253
- if (inner == null ) {
254
- // if acceptDocs is null, we assume all docs are accepted
255
- return maxDoc ;
256
- }
257
- return 0 ;
258
- }
259
- }
260
-
261
- private class TimeOutCheckingBits implements Bits {
169
+ private class TimeOutCheckingKnnCollector extends KnnCollector .Decorator {
170
+ private final KnnCollector in ;
262
171
private static final int MAX_CALLS_BEFORE_QUERY_TIMEOUT_CHECK = 10 ;
263
- private final Bits updatedAcceptDocs ;
264
172
private int calls ;
265
173
266
- private TimeOutCheckingBits (Bits acceptDocs ) {
267
- // when acceptDocs is null due to no doc deleted, we will instantiate a new one that would
268
- // match all docs to allow timeout checking.
269
- this .updatedAcceptDocs = acceptDocs == null ? new Bits .MatchAllBits (maxDoc ()) : acceptDocs ;
174
+ private TimeOutCheckingKnnCollector (KnnCollector in ) {
175
+ super (in );
176
+ this .in = in ;
270
177
}
271
178
272
179
@ Override
273
- public boolean get (int index ) {
180
+ public boolean collect (int docId , float similarity ) {
274
181
if (calls ++ % MAX_CALLS_BEFORE_QUERY_TIMEOUT_CHECK == 0 ) {
275
182
queryCancellation .checkCancelled ();
276
183
}
277
- return updatedAcceptDocs .get (index );
278
- }
279
-
280
- @ Override
281
- public int length () {
282
- return updatedAcceptDocs .length ();
184
+ return in .collect (docId , similarity );
283
185
}
284
186
}
285
187
}
0 commit comments