35 lines
1.3 KiB
Rust
35 lines
1.3 KiB
Rust
//! Persistence boundary. A read view must represent a single committed revision.
|
|
use crate::{Document, DocumentId};
|
|
use std::{borrow::Cow, collections::BTreeMap};
|
|
|
|
pub type Postings = BTreeMap<DocumentId, [usize; 2]>;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct IndexedDocument {
|
|
pub document: Document,
|
|
pub title: String,
|
|
pub body: String,
|
|
pub lengths: [usize; 2],
|
|
pub frequencies: BTreeMap<String, [usize; 2]>,
|
|
}
|
|
|
|
pub trait IndexReader {
|
|
type Error;
|
|
/// Document count and total token counts in the short and body fields.
|
|
fn statistics(&self) -> Result<(usize, [usize; 2]), Self::Error>;
|
|
fn postings(&self, term: &str) -> Result<Option<Cow<'_, Postings>>, Self::Error>;
|
|
/// IDs come from this snapshot's posting lists and must resolve.
|
|
fn document(&self, id: DocumentId) -> Result<Cow<'_, IndexedDocument>, Self::Error>;
|
|
}
|
|
|
|
pub trait IndexStore {
|
|
type Error;
|
|
type Reader<'a>: IndexReader<Error = Self::Error>
|
|
where
|
|
Self: 'a;
|
|
fn snapshot(&self) -> Result<Self::Reader<'_>, Self::Error>;
|
|
/// Atomically replace the old document, its postings and field statistics.
|
|
fn replace(&mut self, entry: IndexedDocument) -> Result<(), Self::Error>;
|
|
fn delete(&mut self, id: DocumentId) -> Result<bool, Self::Error>;
|
|
}
|