Skip to main content

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(test)]
21#[macro_use]
22extern crate more_asserts;
23
24use std::fmt::Display;
25use std::io;
26
27mod column;
28pub mod column_index;
29pub mod column_values;
30mod columnar;
31mod dictionary;
32mod dynamic_column;
33mod iterable;
34pub(crate) mod utils;
35mod value;
36
37pub use column::{BytesColumn, Column, StrColumn};
38pub use column_index::ColumnIndex;
39pub use column_values::{
40    ColumnValues, EmptyColumnValues, MonotonicallyMappableToU64, MonotonicallyMappableToU128,
41};
42pub use columnar::{
43    CURRENT_VERSION, ColumnType, ColumnarReader, ColumnarWriter, HasAssociatedColumnType,
44    MergeRowOrder, ShuffleMergeOrder, StackMergeOrder, Version, compute_merged_term_ord_mapping,
45    merge_columnar,
46};
47use sstable::VoidSSTable;
48pub use value::{NumericalType, NumericalValue};
49
50pub use self::dynamic_column::{ColumnSpaceUsage, DynamicColumn, DynamicColumnHandle};
51
52pub type RowId = u32;
53pub type DocId = u32;
54
55#[derive(Clone, Copy, Debug)]
56pub struct RowAddr {
57    pub segment_ord: u32,
58    pub row_id: RowId,
59}
60
61pub use sstable::{Dictionary, TermOrdHit};
62pub type Streamer<'a> = sstable::Streamer<'a, VoidSSTable>;
63
64pub use common::DateTime;
65
66#[derive(Copy, Clone, Debug)]
67pub struct InvalidData;
68
69impl From<InvalidData> for io::Error {
70    fn from(_: InvalidData) -> Self {
71        io::Error::new(io::ErrorKind::InvalidData, "Invalid data")
72    }
73}
74
75/// Enum describing the number of values that can exist per document
76/// (or per row if you will).
77///
78/// The cardinality must fit on 2 bits.
79#[derive(Clone, Copy, Hash, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
80#[repr(u8)]
81pub enum Cardinality {
82    /// All documents contain exactly one value.
83    /// `Full` is the default for auto-detecting the Cardinality, since it is the most strict.
84    #[default]
85    Full = 0,
86    /// All documents contain at most one value.
87    Optional = 1,
88    /// All documents may contain any number of values.
89    Multivalued = 2,
90}
91
92impl Display for Cardinality {
93    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
94        let short_str = match self {
95            Cardinality::Full => "full",
96            Cardinality::Optional => "opt",
97            Cardinality::Multivalued => "mult",
98        };
99        write!(f, "{short_str}")
100    }
101}
102
103impl Cardinality {
104    pub fn is_optional(&self) -> bool {
105        matches!(self, Cardinality::Optional)
106    }
107    pub fn is_multivalue(&self) -> bool {
108        matches!(self, Cardinality::Multivalued)
109    }
110    pub fn is_full(&self) -> bool {
111        matches!(self, Cardinality::Full)
112    }
113    pub(crate) fn to_code(self) -> u8 {
114        self as u8
115    }
116    pub(crate) fn try_from_code(code: u8) -> Result<Cardinality, InvalidData> {
117        match code {
118            0 => Ok(Cardinality::Full),
119            1 => Ok(Cardinality::Optional),
120            2 => Ok(Cardinality::Multivalued),
121            _ => Err(InvalidData),
122        }
123    }
124}
125
126#[cfg(test)]
127mod tests;
128
129#[cfg(test)]
130mod compat_tests;