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
/*
* 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::bytes_ref::BytesRefValueEnum;
use crate::core::search::doc_id_set_iterator::DocIdSetIterator;
use crate::core::util::error::lucene_error::Result;
/// Iterates through the postings.
/// NOTE: you must first call [`next_doc`](DocIdSetIterator::next_doc) before
/// using any of the per-doc methods.
pub trait PostingsEnum: DocIdSetIterator {
/// Returns term frequency in the current document, or 1 if the field was
/// indexed with [`DOCS`](crate::core::index::index_options::IndexOptions::Docs)
/// only. Do not call this before
/// [`DocIdSetIterator::next_doc`] is first called, nor after
/// [`DocIdSetIterator::next_doc`] returns
/// [`DocIdSetIterator#
/// NO_MORE_DOCS`](crate::core::search::doc_id_set_iterator::NO_MORE_DOCS)
///
/// NOTE: if this enum was obtained with [`NONE`], the result of this method
/// is undefined.
fn freq(&mut self) -> Result<i32>;
/// Returns the next position, or -1 if positions were not indexed.
/// Calling this more than [`freq()`](Self::freq) times is undefined.
fn next_position(&mut self) -> Result<i32>;
/// Returns start offset for the current position, or -1 if offsets were not
/// indexed.
fn start_offset(&self) -> Result<i32>;
/// Returns end offset for the current position, or -1 if offsets were not
/// indexed.
fn end_offset(&self) -> Result<i32>;
/// Returns the payload at this position, or None if no payload was indexed.
/// Do not modify the returned bytes.
fn get_payload(&self) -> Result<Option<BytesRefValueEnum<'_>>>;
/// Visits this borrowed cursor while retaining its concrete type.
/// The default preserves wrapper behavior. Overrides must preserve cursor
/// state, values, errors and capabilities, including mutations by the visitor.
fn visit<V: PostingsEnumVisitor>(&mut self, visitor: V) -> V::Output
where
Self: Sized,
{
visitor.visit(self)
}
}
/// An operation over a borrowed concrete postings cursor.
/// Its result cannot retain the cursor borrow.
pub trait PostingsEnumVisitor {
type Output;
fn visit<P: PostingsEnum>(self, postings: &mut P) -> Self::Output;
}
pub const NONE: i16 = 0;
pub const FREQS: i16 = 1 << 3;
pub const POSITIONS: i16 = FREQS | 1 << 4;
pub const OFFSETS: i16 = POSITIONS | 1 << 5;
pub const PAYLOADS: i16 = POSITIONS | 1 << 6;
pub const ALL: i16 = OFFSETS | PAYLOADS;
/// Returns true if the given feature is requested in the flags.
pub fn feature_requested(flags: i32, feature: i16) -> bool {
(flags & (feature as i32)) == (feature as i32)
}
macro_rules! define_postings_enum_enum {
(
$enum_name:ident,
[$($V:ident),+ $(,)?]
) => {
pub enum $enum_name<$($V),+> {
$($V($V)),+
}
impl<$($V),+> crate::core::search::doc_id_set_iterator::DocIdSetIteratorExtensions for $enum_name<$($V),+>
where
$($V: PostingsEnum,)+
{}
impl<$($V),+> crate::core::search::doc_id_set_iterator::BitSetIteratorAccess for $enum_name<$($V),+>
where
$($V: PostingsEnum,)+
{}
impl<$($V),+> DocIdSetIterator for $enum_name<$($V),+>
where
$($V: PostingsEnum,)+
{
fn doc_id(&self) -> i32 {
match self {
$(Self::$V(t) => t.doc_id(),)+
}
}
fn next_doc(&mut self) -> Result<i32> {
match self {
$(Self::$V(t) => t.next_doc(),)+
}
}
fn advance(&mut self, target: i32) -> Result<i32> {
match self {
$(Self::$V(t) => t.advance(target),)+
}
}
fn slow_advance(&mut self, target: i32) -> Result<i32> {
match self {
$(Self::$V(t) => t.slow_advance(target),)+
}
}
fn cost(&self) -> Result<i64> {
match self {
$(Self::$V(t) => t.cost(),)+
}
}
}
impl<$($V),+> PostingsEnum for $enum_name<$($V),+>
where
$($V: PostingsEnum,)+
{
fn freq(&mut self) -> Result<i32> {
match self {
$(Self::$V(t) => t.freq(),)+
}
}
fn next_position(&mut self) -> Result<i32> {
match self {
$(Self::$V(t) => t.next_position(),)+
}
}
fn start_offset(&self) -> Result<i32> {
match self {
$(Self::$V(t) => t.start_offset(),)+
}
}
fn end_offset(&self) -> Result<i32> {
match self {
$(Self::$V(t) => t.end_offset(),)+
}
}
fn get_payload(&self) -> Result<Option<BytesRefValueEnum<'_>>> {
match self {
$(Self::$V(t) => t.get_payload(),)+
}
}
fn visit<V: PostingsEnumVisitor>(&mut self, visitor: V) -> V::Output {
match self {
$(Self::$V(t) => t.visit(visitor),)+
}
}
}
};
}
define_postings_enum_enum!(PostingsEnumEnum2, [A, B]);