use rusqlite::{Connection, OptionalExtension};
use super::packed::{Directory, decode_best};
use super::postings::{Posting, pack_piece, unpack_piece};
use super::write::execute;
use crate::sqlite::{Tx, sql_error};
use crate::{Error, Result};
pub(crate) const OPEN_DOCS: u64 = 1000;
pub(crate) const MERGE_AT: usize = 10;
pub(crate) const FOLDED_FROM: i64 = 2;
pub(crate) const BASE: i64 = 63;
const RUN_BITS: u32 = 24;
const READ_SEGMENTS: &str = "select id, level, first, past, docs, folded, merge, through
from _tinystore_search_segments where index_id = ?1 order by first, id";
const READ_NEWEST: &str = "select id, level, first, past, docs, folded, merge, through
from _tinystore_search_segments where index_id = ?1 order by first desc, id desc limit 1";
const ADD_SEGMENT: &str = "insert into _tinystore_search_segments (index_id, level, first, past, docs, folded)
values (?1, ?2, ?3, ?4, ?5, ?6)";
const GROW_SEGMENT: &str = "update _tinystore_search_segments set past = ?2, docs = docs + ?3 where id = ?1";
const READ_PIECE: &str = "select count, postings, directory, best from _tinystore_search_pieces
where segment = ?1 and key = ?2";
const ADD_PIECE: &str = "insert into _tinystore_search_pieces (segment, key, count, postings) values (?1, ?2, ?3, ?4)
on conflict (segment, key) do update set count = count + excluded.count,
postings = cast(postings || excluded.postings as blob)";
const PIECES: usize = 64;
static ADD_PIECES: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
let rows = vec!["(?, ?, ?, ?)"; PIECES].join(", ");
format!(
"insert into _tinystore_search_pieces (segment, key, count, postings) values {rows}
on conflict (segment, key) do update set count = count + excluded.count,
postings = cast(postings || excluded.postings as blob)"
)
});
pub(crate) const WRITE_PIECE: &str =
"insert or replace into _tinystore_search_pieces (segment, key, count, postings, directory, best)
values (?1, ?2, ?3, ?4, ?5, ?6)";
const DROP_PIECE: &str = "delete from _tinystore_search_pieces where segment = ?1 and key = ?2";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Segment {
pub(crate) id: i64,
pub(crate) level: i64,
pub(crate) first: u32,
pub(crate) past: u32,
pub(crate) docs: u64,
pub(crate) folded: bool,
pub(crate) merge: Option<i64>,
pub(crate) through: Option<i64>,
}
impl Segment {
fn of(row: &rusqlite::Row<'_>) -> rusqlite::Result<Segment> {
Ok(Segment {
id: row.get(0)?,
level: row.get(1)?,
first: row.get(2)?,
past: row.get(3)?,
docs: row.get::<_, i64>(4)?.max(0) as u64,
folded: row.get(5)?,
merge: row.get(6)?,
through: row.get(7)?,
})
}
fn open(&self) -> bool {
self.level == 0 && self.merge.is_none() && self.docs < OPEN_DOCS
}
pub(crate) fn runs(&self) -> std::ops::Range<i64> {
self.id << RUN_BITS..(self.id + 1) << RUN_BITS
}
}
pub(crate) fn of_index(connection: &Connection, index: i64) -> Result<Vec<Segment>> {
let failed = |error| sql_error("read an index's segments", error);
let mut select = connection.prepare_cached(READ_SEGMENTS).map_err(failed)?;
let rows = select.query_map([index], Segment::of);
rows.and_then(Iterator::collect).map_err(failed)
}
#[derive(Clone, Debug)]
pub(crate) enum Piece {
Short(Vec<Posting>),
Packed(Directory, Vec<Posting>),
}
pub(crate) fn piece(connection: &Connection, segment: &Segment, key: i64) -> Result<Option<(i64, Piece)>> {
let failed = |error| sql_error("read a piece", error);
let mut select = connection.prepare_cached(READ_PIECE).map_err(failed)?;
let row = select.query_row((segment.id, key), |row| {
Ok((row.get::<_, i64>(0)?, row.get::<_, Option<Vec<u8>>>(1)?, row.get::<_, Option<Vec<u8>>>(2)?, row.get(3)?))
});
let Some((count, postings, directory, best)) = row.optional().map_err(failed)? else { return Ok(None) };
piece_of(segment, (postings, directory, best)).map(|piece| Some((count, piece)))
}
pub(crate) type PieceBytes = (Option<Vec<u8>>, Option<Vec<u8>>, Option<Vec<u8>>);
pub(crate) fn piece_of(segment: &Segment, (postings, directory, best): PieceBytes) -> Result<Piece> {
let within = |error: Error| error.within(format!("segment {}", segment.id));
match (postings, directory) {
(Some(postings), None) => Ok(Piece::Short(unpack_piece(&postings, segment.first).map_err(within)?)),
(None, Some(directory)) => {
let best = decode_best(&best.unwrap_or_default()).map_err(within)?;
Ok(Piece::Packed(Directory::decode(&directory).map_err(within)?, best))
}
_ => Err(within(Error::corrupt("a piece holds neither postings nor a directory"))),
}
}
pub(crate) fn holding(connection: &Connection, segments: &[Segment], key: i64, documents: i64) -> Result<u64> {
let mut holding = documents;
for segment in segments.iter().filter(|segment| !segment.folded) {
holding += counted(connection, segment, key)?;
}
Ok(holding.max(0) as u64)
}
fn counted(connection: &Connection, segment: &Segment, key: i64) -> Result<i64> {
let failed = |error| sql_error("read a piece", error);
let mut select = connection.prepare_cached(READ_PIECE).map_err(failed)?;
let count = select.query_row((segment.id, key), |row| row.get::<_, i64>(0)).optional().map_err(failed)?;
Ok(count.unwrap_or(0))
}
pub(crate) fn write_set(
tx: &Tx<'_>,
index: i64,
(first, past, docs): (u32, u32, u64),
postings: &mut Vec<(i64, u32, u32, u32)>,
) -> Result<()> {
let segment = open(tx, index, (first, past, docs))?;
postings.sort_unstable();
let rows: Vec<(i64, i64, Vec<u8>)> = postings
.chunk_by(|a, b| a.0 == b.0)
.map(|run| {
let held: Vec<Posting> = run.iter().map(|&(_, doc, tf, len)| Posting { doc, tf, len }).collect();
(run[0].0, run.len() as i64, pack_piece(&held, segment.first))
})
.collect();
let failed = |error| sql_error("write postings", error);
let mut many = tx.prepare_cached(&ADD_PIECES).map_err(failed)?;
let (chunks, rest) = rows.as_chunks::<PIECES>();
for chunk in chunks {
let values: Vec<&dyn rusqlite::ToSql> = chunk
.iter()
.flat_map(|(key, count, bytes)| [&segment.id as &dyn rusqlite::ToSql, key, count, bytes])
.collect();
many.execute(values.as_slice()).map_err(failed)?;
}
for (key, count, bytes) in rest {
execute(tx, ADD_PIECE, (segment.id, key, count, bytes), "write postings")?;
}
postings.clear();
Ok(())
}
fn open(tx: &Tx<'_>, index: i64, (first, past, docs): (u32, u32, u64)) -> Result<Segment> {
if let Some(newest) = open_of(tx, index)? {
execute(tx, GROW_SEGMENT, (newest.id, past, docs as i64), "grow a segment")?;
return Ok(Segment { past, docs: newest.docs + docs, ..newest });
}
add(tx, index, (0, first, past), docs)
}
pub(crate) fn open_of(connection: &Connection, index: i64) -> Result<Option<Segment>> {
let failed = |error| sql_error("read the newest segment", error);
let newest = connection
.prepare_cached(READ_NEWEST)
.and_then(|mut select| select.query_row([index], Segment::of).optional())
.map_err(failed)?;
Ok(newest.filter(Segment::open))
}
pub(crate) fn take_out(connection: &Connection, segment: &Segment, key: i64, doc: u32) -> Result<bool> {
let Some((count, Piece::Short(mut postings))) = piece(connection, segment, key)? else { return Ok(false) };
let Ok(at) = postings.binary_search_by_key(&doc, |posting| posting.doc) else { return Ok(false) };
postings.remove(at);
if postings.is_empty() {
drop_piece(connection, segment, key)?;
} else {
write_piece(connection, (segment, key), count - 1, &Written::Short(postings))?;
}
Ok(true)
}
pub(crate) fn add(tx: &Tx<'_>, index: i64, (level, first, past): (i64, u32, u32), docs: u64) -> Result<Segment> {
let folded = level >= FOLDED_FROM;
execute(tx, ADD_SEGMENT, (index, level, first, past, docs as i64, folded), "add a segment")?;
let id = tx.last_insert_rowid();
Ok(Segment { id, level, first, past, docs, folded, merge: None, through: None })
}
pub(crate) fn write_piece(
connection: &Connection,
(segment, key): (&Segment, i64),
count: i64,
written: &Written,
) -> Result<()> {
let row = bytes_of(segment, written);
execute(connection, WRITE_PIECE, (segment.id, key, count, row.0, row.1, row.2), "write a piece").map(drop)
}
pub(crate) fn bytes_of(segment: &Segment, written: &Written) -> PieceBytes {
match written {
Written::Short(postings) => (Some(pack_piece(postings, segment.first)), None, None),
Written::Packed(directory, best) => (None, Some(directory.encode()), Some(best.clone())),
}
}
pub(crate) enum Written {
Short(Vec<Posting>),
Packed(Directory, Vec<u8>),
}
pub(crate) fn drop_piece(connection: &Connection, segment: &Segment, key: i64) -> Result<()> {
execute(connection, DROP_PIECE, (segment.id, key), "drop a piece").map(drop)
}