1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::core::index::filtered_terms_enum::{
AcceptStatus, FilteredTermsEnum, FilteredTermsEnumBase,
};
use crate::core::index::terms_enum::TermsEnum;
use crate::core::index::{BytesRef, BytesRefBuilder};
use crate::core::util::SliceCopyOps;
use crate::core::util::array_util::ArrayUtil;
use crate::core::util::automation::compiled_automaton::{
AutomatonEnum, AutomatonType, CompiledAutomaton,
};
use crate::core::util::automation::transition::Transition;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::ints_ref_builder::IntsRefBuilder;
use std::borrow::Cow;
use std::sync::Arc;
/// A [`FilteredTermsEnum`] that enumerates terms based on what is accepted by a
/// DFA.
///
/// The algorithm works as follows:
///
/// 1. As long as matches are successful, keep reading sequentially.
/// 2. When a match fails, skip to the next string in lexicographic order that
/// does **not** enter a reject state.
///
/// Note:
/// - The algorithm does **not** attempt to skip directly to the next completely
/// accepted string, which is not possible when the language accepted by the
/// DFA is infinite (e.g., due to `*` operator).
pub struct AutomatonTermsEnum {
/// A tableized array-based form of the DFA.
/// array of sorted transitions for each state, indexed by state number
automaton: AutomatonEnum,
/// Common suffix of the automaton.
common_suffix_ref: Option<Arc<BytesRef<Vec<u8>>>>,
// true if the automaton accepts a finite language
finite: bool,
// Used for visited state tracking: each short records gen when we last
// visited the state; we use gens to avoid having to clear
visited: Vec<u16>,
cur_gen: u16,
// the reference used for seeking forwards through the term dictionary
seek_bytes_ref: BytesRefBuilder<Vec<u8>>,
// true if we are enumerating an infinite portion of the DFA.
// in this case it is faster to drive the query based on the terms dictionary.
// when this is true, linearUpperBound indicate the end of range
// of terms where we should simply do sequential reads instead.
linear: bool,
linear_upper_bound: BytesRef<Vec<u8>>,
transition: Transition,
saved_states: IntsRefBuilder<Vec<i32>>,
start_term: Option<BytesRef<Vec<u8>>>,
}
impl AutomatonTermsEnum {
pub fn new<TE>(tenum: TE, compiled: &CompiledAutomaton) -> Result<FilteredTermsEnum<TE, Self>>
where
TE: TermsEnum,
{
Self::with_start_term(tenum, compiled, None)
}
pub fn with_start_term<TE>(
tenum: TE,
compiled: &CompiledAutomaton,
start_term: Option<&BytesRef<Vec<u8>>>,
) -> Result<FilteredTermsEnum<TE, Self>>
where
TE: TermsEnum,
{
if compiled.type_ != AutomatonType::Normal {
return Err(LuceneError::illegal_argument(
"please use CompiledAutomaton.get_terms_enum instead",
));
}
let byte_runnable = compiled.get_automaton()?;
let visited = if compiled.finite {
Vec::new()
} else {
vec![0u16; byte_runnable.get_size()?]
};
let sub = Self {
// FilteredTermsEnum parent initialization — you'd handle this separately
automaton: byte_runnable,
common_suffix_ref: compiled.common_suffix_ref.clone(),
finite: compiled.finite,
visited,
cur_gen: 0,
seek_bytes_ref: BytesRefBuilder::new(),
linear: false,
linear_upper_bound: BytesRef::new(),
transition: Transition::default(),
saved_states: IntsRefBuilder::new(),
start_term: start_term.cloned(),
};
Ok(FilteredTermsEnum::new(tenum, sub))
}
/// Records the given state has been visited.
fn set_visited(&mut self, state: i32) -> Result<()> {
debug_assert!(state >= 0);
let state = state as usize;
if !self.finite {
if state >= self.visited.len() {
ArrayUtil::grow_with(&mut self.visited, state + 1, || 0)?;
}
self.visited[state] = self.cur_gen;
}
Ok(())
}
/// Indicates whether the given state has been visited.
fn is_visited(&self, state: i32) -> bool {
debug_assert!(state >= 0);
let state = state as usize;
!self.finite && state < self.visited.len() && self.visited[state] == self.cur_gen
}
/// Sets the enum to operate in linear fashion after detecting a looping
/// transition at a given position.
///
/// This sets an upper bound and behaves like a [`TermRangeQuery`](crate::core::search::term_range_query::TermRangeQuery) for this
/// portion of the term space.
fn set_linear(&mut self, position: usize) -> Result<()> {
debug_assert!(!self.linear);
let mut state = 0;
let mut max_interval = 0xFF;
for i in 0..position {
let byte = self.seek_bytes_ref.byte_at(i);
state = self.automaton.step(state, byte as i32)?;
debug_assert!(state >= 0, "state = {state}");
}
let num_transitions = self.automaton.get_num_transitions_with_state(state)?;
self
.automaton
.init_transition(state, &mut self.transition)?;
for _ in 0..num_transitions {
self.automaton.get_next_transition(&mut self.transition)?;
let ch = self.seek_bytes_ref.byte_at(position) as i32;
if self.transition.min <= ch && ch <= self.transition.max {
max_interval = self.transition.max as u8;
break;
}
}
// 0xff terms don't get the optimization... not worth the trouble.
max_interval = max_interval.saturating_add(1);
let length = position + 1;
if self.linear_upper_bound.bytes.len() < length {
ArrayUtil::grow_no_copy(&mut self.linear_upper_bound.bytes, length, || 0)?;
}
self.linear_upper_bound.bytes[..position]
.copy_from(&self.seek_bytes_ref.bytes().bytes[0..position], 0);
self.linear_upper_bound.bytes[position] = max_interval;
self.linear_upper_bound.length = length;
self.linear = true;
Ok(())
}
/// Increments the byte buffer to the next string in binary order after `s`
/// that will not put the machine into a reject state. If no such string
/// exists, returns `false`.
///
/// The correctness of this method depends on:
/// - The automaton being deterministic
/// - No transitions leading to dead states
///
/// Returns:
/// - `true` if more possible solutions exist for the DFA; otherwise,
/// `false`.
pub fn next_string(&mut self) -> Result<bool> {
let mut state;
let mut pos: usize = 0;
self.saved_states.grow(self.seek_bytes_ref.length() + 1)?;
self.saved_states.set_int_at(0, 0);
loop {
if !self.finite {
self.cur_gen = self.cur_gen.wrapping_add(1);
if self.cur_gen == 0 {
// Clear the visited states every time curGen wraps (so very infrequently to not
// impact average perf).
self.visited.fill(u16::MAX);
}
}
self.linear = false;
// walk the automaton until a character is rejected
state = self.saved_states.int_at(pos);
while pos < self.seek_bytes_ref.length() {
self.set_visited(state)?;
let byte = self.seek_bytes_ref.byte_at(pos) as i32;
let next_state = self.automaton.step(state, byte)?;
if next_state == -1 {
break;
}
self.saved_states.set_int_at(pos + 1, next_state);
if !self.linear && self.is_visited(next_state) {
self.set_linear(pos)?;
}
state = next_state;
pos += 1;
}
// take the useful portion, and the last non-reject state, and attempt to
// append characters that will match.
if self.next_string_with_position(state, pos)? {
return Ok(true);
} else {
/* no more solutions exist from this useful portion, backtrack */
let Some(previous_position) = self.backtrack(pos) else {
/* no more solutions at all */
return Ok(false);
};
pos = previous_position;
let prev_state = self.saved_states.int_at(pos);
let byte = self.seek_bytes_ref.byte_at(pos) as i32;
let new_state = self.automaton.step(prev_state, byte)?;
if new_state >= 0 && self.automaton.is_accept(new_state)? {
/* String is good to go as-is */
return Ok(true);
}
if !self.finite {
pos = 0;
}
}
}
}
/// Returns the next string in lexicographic order that will not put the
/// machine into a reject state.
///
/// This method traverses the DFA from the given position in the string,
/// starting at the given state.
///
/// If the machine cannot be satisfied from this position, returns `false`.
/// The traversal follows the minimal path in lexicographic order as far
/// as possible.
///
/// Note:
/// - If this method returns `false`, there might still be more solutions;
/// it is necessary to backtrack to continue the search.
///
/// Parameters:
/// - `state`: The current non-reject state.
/// - `position`: The portion of the string to use.
///
/// Returns:
/// - `true` if more possible solutions exist for the DFA from this
/// position.
fn next_string_with_position(&mut self, mut state: i32, position: usize) -> Result<bool> {
// The next lexicographic character must be greater than the existing character.
let mut c = 0;
if position < self.seek_bytes_ref.length() {
c = self.seek_bytes_ref.byte_at(position);
// if the next byte is 0xff and is not part of the useful portion,
// then by definition it puts us in a reject state, and therefore this
// path is dead. there cannot be any higher transitions. backtrack.
if c == 0xFF {
return Ok(false);
}
c += 1;
}
self.seek_bytes_ref.set_length(position);
self.set_visited(state)?;
let num_transitions = self.automaton.get_num_transitions_with_state(state)?;
self
.automaton
.init_transition(state, &mut self.transition)?;
// find the minimal path (lexicographic order) that is >= c
let c = c as i32;
for _ in 0..num_transitions {
self.automaton.get_next_transition(&mut self.transition)?;
if self.transition.max >= c {
let next_char = self.transition.min.max(c);
// append either the next sequential char, or the minimum transition
self.seek_bytes_ref.append_byte(next_char as u8)?;
state = self.transition.dest;
// as long as is possible, continue down the minimal path in
// lexicographic order. if a loop or accept state is encountered, stop.
// descend minimal lex path until loop or accept state
while !self.is_visited(state) && !self.automaton.is_accept(state)? {
self.set_visited(state)?;
// Note: we work with a DFA with no transitions to dead states.
// so the below is ok, if it is not an accept state,
// then there MUST be at least one transition.
self
.automaton
.init_transition(state, &mut self.transition)?;
self.automaton.get_next_transition(&mut self.transition)?;
state = self.transition.dest;
// append the minimum transition
self.seek_bytes_ref.append_byte(self.transition.min as u8)?;
// we found a loop, record it for faster enumeration
if !self.linear && self.is_visited(state) {
self.set_linear(self.seek_bytes_ref.length() - 1)?;
}
}
return Ok(true);
}
}
Ok(false)
}
/// Attempts to backtrack through the string after encountering a dead end
/// at the given position. Returns `None` if no more possible strings
/// can match.
///
/// Parameters:
/// - `position`: The current position in the input string.
///
/// Returns:
/// - A position if more possible solutions exist for the DFA;
/// otherwise, returns `None`.
fn backtrack(&mut self, mut position: usize) -> Option<usize> {
debug_assert!(position < i32::MAX as usize);
while position > 0 {
position -= 1;
let next_char = self.seek_bytes_ref.byte_at(position);
// if a character is 0xff it's a dead-end too,
// because there is no higher character in binary sort order.
if next_char != 0xFF {
self.seek_bytes_ref.set_byte_at(position, next_char + 1);
self.seek_bytes_ref.set_length(position + 1);
return Some(position);
}
}
None // all solutions exhausted
}
}
impl FilteredTermsEnumBase for AutomatonTermsEnum {
fn accept(&mut self, term: &BytesRef<&[u8]>, _ord: i64) -> Result<AcceptStatus> {
let suffix_ok = match &self.common_suffix_ref {
None => true,
Some(suffix) => term.as_byte_slice().ends_with(suffix.as_byte_slice()),
};
let v = if suffix_ok {
if self.automaton.run(term.bytes, term.offset, term.length)? {
if self.linear {
AcceptStatus::Yes
} else {
AcceptStatus::YesAndSeek
}
} else if self.linear && term.compare_to(&self.linear_upper_bound).is_lt() {
AcceptStatus::No
} else {
AcceptStatus::NoAndSeek
}
} else if self.linear && term.compare_to(&self.linear_upper_bound).is_lt() {
AcceptStatus::No
} else {
AcceptStatus::NoAndSeek
};
Ok(v)
}
fn next_seek_term(
&mut self,
term: Option<&BytesRef<&[u8]>>,
) -> Result<Option<Cow<'_, BytesRef<Vec<u8>>>>> {
if let Some(t) = term {
self.seek_bytes_ref.copy_bytes_from_ref(t)?;
} else {
match self.start_term {
Some(ref t) => self.seek_bytes_ref.copy_bytes_from_ref(t)?,
None => {
debug_assert_eq!(self.seek_bytes_ref.length(), 0);
// return the empty term, as it's valid
if self.automaton.is_accept(0)? {
return Ok(Some(Cow::Borrowed(self.seek_bytes_ref.get_bytes_ref())));
}
},
}
}
let v = if self.next_string()? {
Some(Cow::Borrowed(self.seek_bytes_ref.get_bytes_ref()))
} else {
None
};
Ok(v)
}
}