use serde::Serialize;
use super::index::{Given, open_as_it_is};
use crate::replication::{In, Out};
use crate::{Error, ErrorKind, Result, Store};
pub(crate) fn make(store: &Store) -> Result<()> {
super::engine::Search::of(store).map(drop)
}
pub(crate) use super::engine::FILE;
pub(super) struct Passed {
id: String,
json: Vec<u8>,
vector: Option<Option<Vec<f32>>>,
}
impl Passed {
pub(super) fn of<T: Serialize>(given: &Given<'_, T>) -> Result<Passed> {
let json = serde_json::to_vec(given.value)
.map_err(|error| Error::invalid(format!("a document that is no JSON: {error}")))?;
Ok(Passed { id: given.id.clone(), json, vector: given.vector.map(|vector| vector.map(<[f32]>::to_vec)) })
}
}
pub(super) fn pass_on_set(store: &Store, name: &str, documents: &[Passed]) -> Result<()> {
let mut out = Out(vec![crate::replication::SEARCH]);
out.text(name).byte(0).list(documents, |out, document| {
out.text(&document.id).bytes(&document.json);
match &document.vector {
None => out.byte(0),
Some(None) => out.byte(1),
Some(Some(vector)) => out.byte(2).list(vector, |out, x| {
out.word(x.to_bits());
}),
};
});
crate::replication::pass_on(store, FILE, out.0).map(drop)
}
pub(super) fn pass_on_delete(store: &Store, name: &str, id: &str) -> Result<bool> {
let mut out = Out(vec![crate::replication::SEARCH]);
out.text(name).byte(1).text(id);
let answer = crate::replication::pass_on(store, FILE, out.0)?;
In { bytes: &answer, at: 0 }.byte().map(|found| found == 1)
}
pub(crate) fn run(store: &Store, bytes: &[u8]) -> Result<Vec<u8>> {
let mut at = In { bytes, at: 0 };
let name = at.text()?;
let index = open_as_it_is::<serde_json::Value>(store, &name)?;
match at.byte()? {
0 => {
let documents = at.list(1 << 12, |at| {
let id = at.text()?;
let value: serde_json::Value =
serde_json::from_slice(at.bytes()?).map_err(|_| corrupt("a document's JSON"))?;
let vector = match at.byte()? {
0 => None,
1 => Some(None),
2 => Some(Some(at.list(1 << 16, |at| at.word().map(f32::from_bits))?)),
_ => return Err(corrupt("a document's vector")),
};
Ok((id, value, vector))
})?;
let given = documents
.iter()
.map(|(id, value, vector)| Given {
id: id.clone(),
value,
vector: vector.as_ref().map(|v| v.as_deref()),
})
.collect();
index.set_all(given)?;
Ok(Vec::new())
}
1 => {
let found = index.delete(at.text()?)?;
Ok(vec![u8::from(found)])
}
_ => Err(corrupt("a call")),
}
}
fn corrupt(what: &str) -> Error {
Error::new(ErrorKind::Corrupt, format!("a search write passed on: {what}"))
}