lb_tantivy_columnar/
lib.rs

1//! # Tantivy-Columnar
2//!
3//! `tantivy-columnar`provides a columnar storage for tantivy.
4//! The crate allows for efficient read operations on specific columns rather than entire records.
5//!
6//! ## Overview
7//!
8//! - **columnar**: Reading, writing, and merging multiple columns:
9//!   - **[ColumnarWriter]**: Makes it possible to create a new columnar.
10//!   - **[ColumnarReader]**: The ColumnarReader makes it possible to access a set of columns
11//!     associated to field names.
12//!   - **[merge_columnar]**: Contains the functionalities to merge multiple ColumnarReader or
13//!     segments into a single one.
14//!
15//! - **column**: A single column, which contains
16//!     - [column_index]: Resolves the rows for a document id. Manages the cardinality of the
17//!       column.
18//!     - [column_values]: Stores the values of a column in a dense format.
19
20#![cfg_attr(all(feature = "unstable", test), feature(test))]
21
22#[cfg(test)]
23#[macro_use]
24extern crate more_asserts;
25
26#[cfg(all(test, feature = "unstable"))]
27extern crate test;
28
29use std::fmt::Display;
30use std::io;
31
32mod block_accessor;
33mod column;
34pub mod column_index;
35pub mod column_values;
36mod columnar;
37mod dictionary;
38mod dynamic_column;
39mod iterable;
40pub(crate) mod utils;
41mod value;
42
43pub use block_accessor::ColumnBlockAccessor;
44pub use column::{BytesColumn, Column, StrColumn};
45pub use column_index::ColumnIndex;
46pub use column_values::{
47    ColumnValues, EmptyColumnValues, MonotonicallyMappableToU64, MonotonicallyMappableToU128,
48};
49pub use columnar::{
50    CURRENT_VERSION, ColumnType, ColumnarReader, ColumnarWriter, HasAssociatedColumnType,
51    MergeRowOrder, ShuffleMergeOrder, StackMergeOrder, Version, merge_columnar,
52};
53use sstable::VoidSSTable;
54pub use value::{NumericalType, NumericalValue};
55
56pub use self::dynamic_column::{DynamicColumn, DynamicColumnHandle};
57
58pub type RowId = u32;
59pub type DocId = u32;
60
61#[derive(Clone, Copy, Debug)]
62pub struct RowAddr {
63    pub segment_ord: u32,
64    pub row_id: RowId,
65}
66
67pub use sstable::Dictionary;
68pub type Streamer<'a> = sstable::Streamer<'a, VoidSSTable>;
69
70pub use common::DateTime;
71
72#[derive(Copy, Clone, Debug)]
73pub struct InvalidData;
74
75impl From<InvalidData> for io::Error {
76    fn from(_: InvalidData) -> Self {
77        io::Error::new(io::ErrorKind::InvalidData, "Invalid data")
78    }
79}
80
81/// Enum describing the number of values that can exist per document
82/// (or per row if you will).
83///
84/// The cardinality must fit on 2 bits.
85#[derive(Clone, Copy, Hash, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
86#[repr(u8)]
87pub enum Cardinality {
88    /// All documents contain exactly one value.
89    /// `Full` is the default for auto-detecting the Cardinality, since it is the most strict.
90    #[default]
91    Full = 0,
92    /// All documents contain at most one value.
93    Optional = 1,
94    /// All documents may contain any number of values.
95    Multivalued = 2,
96}
97
98impl Display for Cardinality {
99    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
100        let short_str = match self {
101            Cardinality::Full => "full",
102            Cardinality::Optional => "opt",
103            Cardinality::Multivalued => "mult",
104        };
105        write!(f, "{short_str}")
106    }
107}
108
109impl Cardinality {
110    pub fn is_optional(&self) -> bool {
111        matches!(self, Cardinality::Optional)
112    }
113    pub fn is_multivalue(&self) -> bool {
114        matches!(self, Cardinality::Multivalued)
115    }
116    pub fn is_full(&self) -> bool {
117        matches!(self, Cardinality::Full)
118    }
119    pub(crate) fn to_code(self) -> u8 {
120        self as u8
121    }
122    pub(crate) fn try_from_code(code: u8) -> Result<Cardinality, InvalidData> {
123        match code {
124            0 => Ok(Cardinality::Full),
125            1 => Ok(Cardinality::Optional),
126            2 => Ok(Cardinality::Multivalued),
127            _ => Err(InvalidData),
128        }
129    }
130}
131
132#[cfg(test)]
133mod tests;
134
135#[cfg(test)]
136mod compat_tests;