Files
tantivy/src/schema/bytes_options.rs
Paul Masurel 02cffa4dea Code simplification. (#1169)
Code simplification and Clippy
2021-10-07 14:11:44 +09:00

155 lines
4.0 KiB
Rust

use serde::{Deserialize, Serialize};
use std::ops::BitOr;
use super::flags::{FastFlag, IndexedFlag, SchemaFlagList, StoredFlag};
/// Define how an a bytes field should be handled by tantivy.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct BytesOptions {
indexed: bool,
fast: bool,
stored: bool,
}
impl BytesOptions {
/// Returns true iff the value is indexed.
pub fn is_indexed(&self) -> bool {
self.indexed
}
/// Returns true iff the value is a fast field.
pub fn is_fast(&self) -> bool {
self.fast
}
/// Returns true iff 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.
pub fn set_indexed(mut self) -> BytesOptions {
self.indexed = 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.
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.
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,
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,
stored: false,
fast: true,
}
}
}
impl From<StoredFlag> for BytesOptions {
fn from(_: StoredFlag) -> Self {
BytesOptions {
indexed: false,
stored: true,
fast: false,
}
}
}
impl From<IndexedFlag> for BytesOptions {
fn from(_: IndexedFlag) -> Self {
BytesOptions {
indexed: 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(), 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_fast(),
(INDEXED | FAST).into()
);
assert_eq!(
BytesOptions::default().set_stored().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().set_stored().is_stored());
assert!(BytesOptions::default().set_fast().is_fast());
assert!(BytesOptions::default().set_indexed().is_indexed());
}
}