mod flush;
mod inverted;
use crate::external_sort::{RunLocation, SortBudget};
use crate::index::lucene::codec::field_infos::DocValuesType;
use crate::index::lucene::codec::postings::IndexOptions;
use crate::index::lucene::codec::segment_info::SegmentDirectory;
pub use inverted::{IndexWriterStatistics, LuceneIndexWriter};
pub const MAXIMUM_TERM_LENGTH: usize = 32_766;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Token {
pub bytes: Vec<u8>,
pub position_increment: u32,
pub start_offset: u32,
pub end_offset: u32,
}
#[derive(Clone, Debug, PartialEq)]
pub enum StoredValue {
Text(String),
Binary(Vec<u8>),
Integer(i32),
Long(i64),
Float(f32),
Double(f64),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DocValue {
Numeric(i64),
Sorted(Vec<u8>),
SortedSet(Vec<Vec<u8>>),
}
impl DocValue {
const fn kind(&self) -> DocValuesType {
match self {
Self::Numeric(_) => DocValuesType::Numeric,
Self::Sorted(_) => DocValuesType::Sorted,
Self::SortedSet(_) => DocValuesType::SortedSet,
}
}
}
#[derive(Clone, Debug)]
pub struct Field {
pub name: String,
pub options: IndexOptions,
pub indexed: bool,
pub tokens: Vec<Token>,
pub final_position_increment: u32,
pub final_offset: u32,
pub stored: Option<StoredValue>,
pub doc_value: Option<DocValue>,
pub omit_norms: bool,
pub boost: f32,
}
impl Field {
#[must_use]
pub fn indexed(name: impl Into<String>, options: IndexOptions, tokens: Vec<Token>) -> Self {
let final_offset = tokens.last().map_or(0, |token| token.end_offset);
Self {
name: name.into(),
options,
indexed: true,
tokens,
final_position_increment: 0,
final_offset,
stored: None,
doc_value: None,
omit_norms: false,
boost: 1.0,
}
}
#[must_use]
pub fn stored(name: impl Into<String>, value: StoredValue) -> Self {
Self {
name: name.into(),
options: IndexOptions::Documents,
indexed: false,
tokens: Vec::new(),
final_position_increment: 0,
final_offset: 0,
stored: Some(value),
doc_value: None,
omit_norms: true,
boost: 1.0,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct Document {
pub fields: Vec<Field>,
}
impl Document {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with(mut self, field: Field) -> Self {
self.fields.push(field);
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WrittenIndex {
pub files: Vec<String>,
pub document_count: i32,
pub statistics: IndexWriterStatistics,
}
pub fn index_writer<Directory: SegmentDirectory>(
directory: Directory,
runs: RunLocation,
budget: SortBudget,
) -> LuceneIndexWriter<Directory> {
LuceneIndexWriter::new(directory, runs, budget)
}
pub(crate) fn borrow_stored(
value: &StoredValue,
) -> crate::index::lucene::codec::stored_fields::StoredValue<'_> {
use crate::index::lucene::codec::stored_fields::StoredValue as Borrowed;
match value {
StoredValue::Text(text) => Borrowed::Text(text),
StoredValue::Binary(bytes) => Borrowed::Binary(bytes),
StoredValue::Integer(value) => Borrowed::Integer(*value),
StoredValue::Long(value) => Borrowed::Long(*value),
StoredValue::Float(value) => Borrowed::Float(*value),
StoredValue::Double(value) => Borrowed::Double(*value),
}
}