forked from quickwit-oss/tantivy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes_options.rs
294 lines (268 loc) · 7.85 KB
/
bytes_options.rs
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
use std::ops::BitOr;
use serde::{Deserialize, Serialize};
use super::flags::{FastFlag, IndexedFlag, SchemaFlagList, StoredFlag};
/// Define how an a bytes field should be handled by tantivy.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "BytesOptionsDeser")]
pub struct BytesOptions {
indexed: bool,
fieldnorms: bool,
fast: bool,
stored: bool,
}
/// For backward compability we add an intermediary to interpret the
/// lack of fieldnorms attribute as "true" if and only if indexed.
///
/// (Downstream, for the moment, this attribute is not used if not indexed...)
/// Note that: newly serialized NumericOptions will include the new attribute.
#[derive(Deserialize)]
struct BytesOptionsDeser {
indexed: bool,
#[serde(default)]
fieldnorms: Option<bool>,
fast: bool,
stored: bool,
}
impl From<BytesOptionsDeser> for BytesOptions {
fn from(deser: BytesOptionsDeser) -> Self {
BytesOptions {
indexed: deser.indexed,
fieldnorms: deser.fieldnorms.unwrap_or(deser.indexed),
fast: deser.fast,
stored: deser.stored,
}
}
}
impl BytesOptions {
/// Returns true if the value is indexed.
pub fn is_indexed(&self) -> bool {
self.indexed
}
/// Returns true if and only if the value is normed.
pub fn fieldnorms(&self) -> bool {
self.fieldnorms
}
/// Returns true if the value is a fast field.
pub fn is_fast(&self) -> bool {
self.fast
}
/// Returns true if the value is stored.
pub fn is_stored(&self) -> bool {
self.stored
}
/// Set the field as indexed.
///
/// Setting an integer as indexed will generate
/// a posting list for each value taken by the integer.
#[must_use]
pub fn set_indexed(mut self) -> BytesOptions {
self.indexed = true;
self
}
/// Set the field as normed.
///
/// Setting an integer as normed will generate
/// the fieldnorm data for it.
#[must_use]
pub fn set_fieldnorms(mut self) -> BytesOptions {
self.fieldnorms = true;
self
}
/// Set the field as a single-valued fast field.
///
/// Fast fields are designed for random access.
/// Access time are similar to a random lookup in an array.
/// If more than one value is associated to a fast field, only the last one is
/// kept.
#[must_use]
pub fn set_fast(mut self) -> BytesOptions {
self.fast = true;
self
}
/// Set the field as stored.
///
/// Only the fields that are set as *stored* are
/// persisted into the Tantivy's store.
#[must_use]
pub fn set_stored(mut self) -> BytesOptions {
self.stored = true;
self
}
}
impl<T: Into<BytesOptions>> BitOr<T> for BytesOptions {
type Output = BytesOptions;
fn bitor(self, other: T) -> BytesOptions {
let other = other.into();
BytesOptions {
indexed: self.indexed | other.indexed,
fieldnorms: self.fieldnorms | other.fieldnorms,
stored: self.stored | other.stored,
fast: self.fast | other.fast,
}
}
}
impl From<()> for BytesOptions {
fn from(_: ()) -> Self {
Self::default()
}
}
impl From<FastFlag> for BytesOptions {
fn from(_: FastFlag) -> Self {
BytesOptions {
indexed: false,
fieldnorms: false,
stored: false,
fast: true,
}
}
}
impl From<StoredFlag> for BytesOptions {
fn from(_: StoredFlag) -> Self {
BytesOptions {
indexed: false,
fieldnorms: false,
stored: true,
fast: false,
}
}
}
impl From<IndexedFlag> for BytesOptions {
fn from(_: IndexedFlag) -> Self {
BytesOptions {
indexed: true,
fieldnorms: true,
stored: false,
fast: false,
}
}
}
impl<Head, Tail> From<SchemaFlagList<Head, Tail>> for BytesOptions
where
Head: Clone,
Tail: Clone,
Self: BitOr<Output = Self> + From<Head> + From<Tail>,
{
fn from(head_tail: SchemaFlagList<Head, Tail>) -> Self {
Self::from(head_tail.head) | Self::from(head_tail.tail)
}
}
#[cfg(test)]
mod tests {
use crate::schema::{BytesOptions, FAST, INDEXED, STORED};
#[test]
fn test_bytes_option_fast_flag() {
assert_eq!(BytesOptions::default().set_fast(), FAST.into());
assert_eq!(
BytesOptions::default().set_indexed().set_fieldnorms(),
INDEXED.into()
);
assert_eq!(BytesOptions::default().set_stored(), STORED.into());
}
#[test]
fn test_bytes_option_fast_flag_composition() {
assert_eq!(
BytesOptions::default().set_fast().set_stored(),
(FAST | STORED).into()
);
assert_eq!(
BytesOptions::default()
.set_indexed()
.set_fieldnorms()
.set_fast(),
(INDEXED | FAST).into()
);
assert_eq!(
BytesOptions::default()
.set_stored()
.set_fieldnorms()
.set_indexed(),
(STORED | INDEXED).into()
);
}
#[test]
fn test_bytes_option_fast_() {
assert!(!BytesOptions::default().is_stored());
assert!(!BytesOptions::default().is_fast());
assert!(!BytesOptions::default().is_indexed());
assert!(!BytesOptions::default().fieldnorms());
assert!(BytesOptions::default().set_stored().is_stored());
assert!(BytesOptions::default().set_fast().is_fast());
assert!(BytesOptions::default().set_indexed().is_indexed());
assert!(BytesOptions::default().set_fieldnorms().fieldnorms());
}
#[test]
fn test_bytes_options_deser_if_fieldnorm_missing_indexed_true() {
let json = r#"{
"indexed": true,
"fast": false,
"stored": false
}"#;
let bytes_options: BytesOptions = serde_json::from_str(json).unwrap();
assert_eq!(
&bytes_options,
&BytesOptions {
indexed: true,
fieldnorms: true,
fast: false,
stored: false
}
);
}
#[test]
fn test_bytes_options_deser_if_fieldnorm_missing_indexed_false() {
let json = r#"{
"indexed": false,
"stored": false,
"fast": false
}"#;
let bytes_options: BytesOptions = serde_json::from_str(json).unwrap();
assert_eq!(
&bytes_options,
&BytesOptions {
indexed: false,
fieldnorms: false,
fast: false,
stored: false
}
);
}
#[test]
fn test_bytes_options_deser_if_fieldnorm_false_indexed_true() {
let json = r#"{
"indexed": true,
"fieldnorms": false,
"fast": false,
"stored": false
}"#;
let bytes_options: BytesOptions = serde_json::from_str(json).unwrap();
assert_eq!(
&bytes_options,
&BytesOptions {
indexed: true,
fieldnorms: false,
fast: false,
stored: false
}
);
}
#[test]
fn test_bytes_options_deser_if_fieldnorm_true_indexed_false() {
// this one is kind of useless, at least at the moment
let json = r#"{
"indexed": false,
"fieldnorms": true,
"fast": false,
"stored": false
}"#;
let bytes_options: BytesOptions = serde_json::from_str(json).unwrap();
assert_eq!(
&bytes_options,
&BytesOptions {
indexed: false,
fieldnorms: true,
fast: false,
stored: false
}
);
}
}