use rusqlite::Connection;
use serde_json::Value;
use super::document::{Analyzed, analyze};
use super::postings::{decode_keys, decode_lengths, encode_keys, encode_lengths};
use super::score::Part;
use super::settings::Settings;
use super::vectors::{self, Vector};
use crate::limits::Limit;
use crate::sqlite::{Tx, sql_error};
use crate::{Error, Result};
const FIND_DOC: &str = "select doc, keys, lengths from _tinystore_search_docs where index_id = ?1 and id = ?2";
const MARK_GONE: &str = "insert into _tinystore_search_gone (index_id, doc, keys) values (?1, ?2, ?3)";
const DROP_DOC: &str = "delete from _tinystore_search_docs where index_id = ?1 and doc = ?2";
const LEAVE_KEY: &str =
"update _tinystore_search_keys set documents = documents - 1 where id = ?1 returning part, documents";
const DROP_HEAD: &str = "delete from _tinystore_search_head where index_id = ?1 and key = ?2 and doc = ?3";
const ADD_TOKENS: &str = "insert into _tinystore_search_parts (index_id, part, tokens) values (?1, ?2, ?3)
on conflict (index_id, part) do update set tokens = tokens + excluded.tokens";
const LEAVE_INDEX: &str =
"update _tinystore_search_indexes set documents = documents - 1, gone_version = gone_version + 1,
words_version = words_version + ?2, vectors = vectors - ?3 where id = ?1";
const NEXT_DOC: &str = "select next_doc from _tinystore_search_indexes where id = ?1";
const JOIN_KEY: &str = "insert into _tinystore_search_keys (index_id, part, term, documents) values (?1, ?2, ?3, 1)
on conflict (index_id, part, term) do update set documents = documents + 1 returning id, documents";
const ADD_HEAD: &str = "insert into _tinystore_search_head (index_id, key, doc, tf, len) values (?1, ?2, ?3, ?4, ?5)";
const KEEP_DOC: &str =
"insert into _tinystore_search_docs (index_id, doc, id, value, keys, lengths) values (?1, ?2, ?3, ?4, ?5, ?6)";
const JOIN_INDEX: &str = "update _tinystore_search_indexes set next_doc = ?2 + 1, documents = documents + 1,
words_version = words_version + ?3, vectors = vectors + ?4 where id = ?1";
const LAST_DOC: i64 = u32::MAX as i64 - 1;
const DOCUMENT_NUMBERS: Limit = Limit { name: "document numbers", bound: LAST_DOC as u64 };
#[derive(Debug)]
pub(crate) struct Prepared {
pub(crate) index: i64,
pub(crate) id: String,
pub(crate) value: Vec<u8>,
pub(crate) analyzed: Analyzed,
pub(crate) vector: Option<Vector>,
pub(crate) clustered: bool,
}
#[derive(Debug)]
pub(crate) struct Incoming<'v> {
pub(crate) id: String,
pub(crate) tree: Value,
pub(crate) bytes: Vec<u8>,
pub(crate) vector: Option<Option<&'v [f32]>>,
}
impl Prepared {
pub(crate) fn new(index: i64, incoming: Incoming<'_>, settings: &Settings) -> Result<Prepared> {
let analyzed = analyze(&incoming.tree, &settings.fields, &settings.filters)?;
let vector = match (settings.vectors, incoming.vector) {
(None, Some(Some(_))) => return Err(Error::invalid("a vector for an index without vectors")),
(None, _) | (Some(_), Some(None)) => None,
(Some(_), None) => {
return Err(Error::invalid(
"the index keeps vectors: a set gives the document's vector, or says it has none",
));
}
(Some(vectors), Some(Some(values))) => Some(Vector::new(values, vectors.dimensions)?),
};
let clustered = settings.vectors.is_some_and(|vectors| vectors.recall.is_some());
Ok(Prepared { index, id: incoming.id, value: incoming.bytes, analyzed, vector, clustered })
}
pub(crate) fn bytes(&self) -> usize {
let words: usize = self.analyzed.terms.iter().map(|(_, term, _)| term.len() + 16).sum();
self.value.len() + words + self.vector.as_ref().map_or(0, Vector::size)
}
}
pub(crate) fn set(tx: &Tx<'_>, document: &Prepared) -> Result<()> {
let index = document.index;
forget(tx, index, &document.id)?;
let doc: i64 = tx
.prepare_cached(NEXT_DOC)
.and_then(|mut select| select.query_row([index], |row| row.get(0)))
.map_err(|error| sql_error("number a document", error))?;
if doc > LAST_DOC {
return Err(
DOCUMENT_NUMBERS.run_out(format!("the index gave its {LAST_DOC} numbers: a set takes a number each"))
);
}
let mut keys = Vec::with_capacity(document.analyzed.terms.len());
let mut new_words = false;
for (part, term, tf) in &document.analyzed.terms {
let (key, documents) = join_key(tx, index, *part, term)?;
new_words |= documents == 1 && part.is_written();
let len = document.analyzed.length(*part);
execute(tx, ADD_HEAD, (index, key, doc, tf, len), "write a posting")?;
keys.push(key);
}
for (part, len) in &document.analyzed.lengths {
execute(tx, ADD_TOKENS, (index, part.0, len), "count a part's words")?;
}
keys.sort_unstable();
let lengths: Vec<(u32, u32)> = document.analyzed.lengths.iter().map(|(part, len)| (part.0, *len)).collect();
let row = (index, doc, &document.id, &document.value, encode_keys(&keys), encode_lengths(&lengths));
execute(tx, KEEP_DOC, row, "keep a document")?;
if let Some(vector) = &document.vector {
vectors::keep(tx, index, doc as u32, vector)?;
if document.clustered {
vectors::join_head(tx, index, doc as u32)?;
}
}
let counted = (index, doc, i64::from(new_words), i64::from(document.vector.is_some()));
execute(tx, JOIN_INDEX, counted, "count a document").map(drop)
}
pub(crate) fn delete(tx: &Tx<'_>, index: i64, id: &str) -> Result<bool> {
forget(tx, index, id)
}
fn forget(tx: &Tx<'_>, index: i64, id: &str) -> Result<bool> {
let failed = |error| sql_error("find a document", error);
let found: Option<(i64, Vec<u8>, Vec<u8>)> = {
let mut select = tx.prepare_cached(FIND_DOC).map_err(failed)?;
let mut rows = select.query((index, id)).map_err(failed)?;
match rows.next().map_err(failed)? {
Some(row) => Some((row.get(0).map_err(failed)?, row.get(1).map_err(failed)?, row.get(2).map_err(failed)?)),
None => None,
}
};
let Some((doc, keys, lengths)) = found else { return Ok(false) };
let within = |error: Error| error.within(format!("document {doc}"));
let mut words_left = false;
for key in decode_keys(&keys).map_err(within)? {
let (part, documents) = leave_key(tx, key)?;
words_left |= documents == 0 && part.is_written();
execute(tx, DROP_HEAD, (index, key, doc), "drop a posting")?;
}
for (part, len) in decode_lengths(&lengths).map_err(within)? {
execute(tx, ADD_TOKENS, (index, part, -i64::from(len)), "count a part's words")?;
}
execute(tx, MARK_GONE, (index, doc, &keys), "mark a document gone")?;
execute(tx, DROP_DOC, (index, doc), "drop a document")?;
let had_vector = vectors::forget(tx, index, doc as u32)?;
execute(tx, LEAVE_INDEX, (index, i64::from(words_left), i64::from(had_vector)), "count a document")?;
Ok(true)
}
fn join_key(connection: &Connection, index: i64, part: Part, term: &str) -> Result<(i64, i64)> {
let mut upsert = connection.prepare_cached(JOIN_KEY).map_err(|error| sql_error("write a key", error))?;
let joined = upsert.query_row((index, part.0, term), |row| Ok((row.get(0)?, row.get(1)?)));
joined.map_err(|error| sql_error("write a key", error))
}
fn leave_key(connection: &Connection, key: i64) -> Result<(Part, i64)> {
let mut update = connection.prepare_cached(LEAVE_KEY).map_err(|error| sql_error("write a key", error))?;
let left = update.query_row([key], |row| Ok((Part(row.get(0)?), row.get(1)?)));
left.map_err(|error| sql_error(format!("write key {key}"), error))
}
pub(crate) use crate::sqlite::execute_cached as execute;