use std::collections::HashMap;
use super::rank::{Best, Facts, Stats};
use super::{Excerpt, Hit, Order, Page, Passage, Request, matching, searchable};
use crate::store::{Document, error::Error, time::Stamp};
use serde_json::json;
struct NewestKey {
path: Box<str>,
updated: i64,
}
#[derive(Default)]
struct Paths {
blocks: Vec<String>,
}
struct PathRef {
start: usize,
len: usize,
}
impl Paths {
const BLOCK_BYTES: usize = 64 * 1024;
fn keep(&mut self, path: &str) -> PathRef {
if self
.blocks
.last()
.is_none_or(|block| block.len() + path.len() > Self::BLOCK_BYTES)
{
self.blocks.push(String::with_capacity(Self::BLOCK_BYTES));
}
let index = self.blocks.len() - 1;
let block = &mut self.blocks[index];
let start = index * Self::BLOCK_BYTES + block.len();
block.push_str(path);
PathRef {
start,
len: path.len(),
}
}
fn get(&self, key: &PathRef) -> &str {
let block = &self.blocks[key.start / Self::BLOCK_BYTES];
let offset = key.start % Self::BLOCK_BYTES;
&block[offset..offset + key.len]
}
}
pub struct Scan<'a> {
scope: &'a str,
request: &'a Request,
order: Order,
after: String,
terms: Vec<String>,
now: Stamp,
bounds: (Option<i64>, Option<i64>),
page: Page,
stats: Stats,
paths: Paths,
facts: Facts<PathRef>,
newest: Best<NewestKey>,
winners: Vec<(String, Option<u64>)>,
built: HashMap<String, Hit>,
}
impl<'a> Scan<'a> {
pub fn new(scope: &'a str, request: &'a Request) -> Result<Self, Error> {
let terms = matching::terms(&request.query);
let order = request.order.unwrap_or(Order::Path);
let maximum = if order == Order::Relevance {
crate::limits::search_candidates()?
} else {
0
};
Ok(Self {
scope,
request,
order,
after: request.scope_after(scope)?,
bounds: request.bounds().map_err(Error::Value)?,
stats: Stats::new(terms.len()),
paths: Paths::default(),
facts: Facts::new(terms.len(), maximum),
newest: Best::new(request.limit),
terms,
now: Stamp::now(),
page: Page::empty(request.order),
winners: Vec::new(),
built: HashMap::new(),
})
}
pub fn ranked(&self) -> bool {
self.order.ranked()
}
fn eligible(&self, path: &str, document: &Document) -> bool {
let request = self.request;
let updated = document.envelope.updated_at.instant();
path > self.after.as_str()
&& path.starts_with(&request.prefix)
&& searchable(path)
&& !document.envelope.redacted
&& self.bounds.0.is_none_or(|since| updated >= since)
&& self.bounds.1.is_none_or(|before| updated < before)
&& crate::knowledge::governance::eligible(
request.knowledge_policy,
self.scope,
path,
&document.content,
self.now,
)
}
fn hit(
&self,
path: String,
document: &Document,
found: (Excerpt, Vec<String>),
passages: Option<Vec<Passage>>,
) -> Hit {
let envelope = &document.envelope;
Hit {
scope: self.scope.into(),
path,
version: envelope.version,
created_at: envelope.created_at.utc().z(),
updated_at: envelope.updated_at.utc().z(),
actor: envelope.actor_value(),
matched_fields: found.1,
excerpt: found.0,
score: None,
passages,
reference: None,
source: None,
workspace_id: None,
}
}
pub fn push(&mut self, path: String, document: Document) -> Result<bool, Error> {
if !self.eligible(&path, &document) {
return Ok(true);
}
let request = self.request;
match self.order {
Order::Relevance => {
let tally =
matching::tally(self.scope, &path, &document, request, &self.terms, true)?;
self.stats.add(&tally.counts, tally.length);
if tally.counts.iter().any(|c| *c > 0) {
let key = self.paths.keep(&path);
self.facts
.keep(key, &tally.counts, tally.length)
.map_err(|broad| {
Error::Value(crate::limits::broad(
broad.0,
"documents",
"the words, the path or the update dates",
))
})?;
}
Ok(true)
}
Order::Newest => {
let tally =
matching::tally(self.scope, &path, &document, request, &self.terms, false)?;
if tally.all {
let key = NewestKey {
path: path.into_boxed_str(),
updated: document.envelope.updated_at.instant(),
};
self.newest.offer(key, newest);
}
Ok(true)
}
Order::Path => {
let measured =
matching::measure(self.scope, &path, &document, request, &self.terms, false)?;
let Some(found) = measured.found.filter(|_| measured.all) else {
return Ok(true);
};
if self.page.hits.len() == request.limit {
self.page.next_cursor = Some(request.cursor_for(
&json!(self.scope),
"memory-search-v1",
&self.page.hits.last().expect("nonempty").path,
));
return Ok(false);
}
let hit = self.hit(path, &document, found, measured.passages);
self.page.hits.push(hit);
Ok(true)
}
}
}
pub fn winners(&mut self) -> Vec<String> {
let facts = std::mem::replace(&mut self.facts, Facts::new(0, 0));
let newest = std::mem::replace(&mut self.newest, Best::new(0));
let paths = std::mem::take(&mut self.paths);
self.winners = match self.order {
Order::Relevance => facts
.best(&self.stats, self.request.limit, |a, b| {
paths.get(a).cmp(paths.get(b))
})
.into_iter()
.map(|(key, score)| (paths.get(&key).to_owned(), Some(score)))
.collect(),
Order::Newest => newest
.into_vec(self::newest)
.into_iter()
.map(|k| (k.path.into_string(), None))
.collect(),
Order::Path => Vec::new(),
};
self.winners.iter().map(|(path, _)| path.clone()).collect()
}
pub fn build(&mut self, path: String, document: Document) -> Result<(), Error> {
let Some(score) = self
.winners
.iter()
.find(|(winner, _)| *winner == path)
.map(|(_, score)| *score)
else {
return Ok(());
};
let any = self.order == Order::Relevance;
let measured =
matching::measure(self.scope, &path, &document, self.request, &self.terms, any)?;
let found = measured
.found
.ok_or_else(|| std::io::Error::other("a search winner changed while it was read"))?;
let mut hit = self.hit(path.clone(), &document, found, measured.passages);
hit.score = score;
self.built.insert(path, hit);
Ok(())
}
pub fn finish(mut self) -> Result<Page, Error> {
if !self.ranked() {
return Ok(self.page);
}
let mut hits = Vec::with_capacity(self.winners.len());
for (path, _) in &self.winners {
let hit = self.built.remove(path).ok_or_else(|| {
std::io::Error::other("a search winner vanished while it was read")
})?;
hits.push(hit);
}
self.page.hits = hits;
Ok(self.page)
}
}
fn newest(a: &NewestKey, b: &NewestKey) -> std::cmp::Ordering {
(b.updated, &a.path).cmp(&(a.updated, &b.path))
}
pub fn scan(
scope: &str,
request: &Request,
documents: impl Iterator<Item = Result<(String, Document), Error>>,
mut load: impl FnMut(&str) -> Result<Document, Error>,
) -> Result<Page, Error> {
let mut scan = Scan::new(scope, request)?;
for row in documents {
let (path, document) = row?;
if !scan.push(path, document)? {
break;
}
}
for path in scan.winners() {
let document = load(&path)?;
scan.build(path, document)?;
}
scan.finish()
}