Allow users to delete documents

This commit is contained in:
Kerollmops 2025-04-19 12:22:37 +02:00
parent 446b9c142c
commit c8b7822d0d
No known key found for this signature in database
GPG Key ID: F250A4C4E3AE5F5F
2 changed files with 39 additions and 16 deletions

View File

@ -412,7 +412,7 @@ impl<'doc> Versions<'doc> {
engine: &rhai::Engine,
edit_function: &rhai::AST,
doc_alloc: &'doc bumpalo::Bump,
) -> Result<Option<Self>> {
) -> Result<Option<Option<Self>>> {
let Some(data) = versions.next() else { return Ok(None) };
let mut doc = doc.unwrap_or_default();
@ -431,7 +431,9 @@ impl<'doc> Versions<'doc> {
let _ = engine.eval_ast_with_scope::<rhai::Dynamic>(&mut scope, edit_function).unwrap();
data = RawMap::with_hasher_in(FxBuildHasher, doc_alloc);
for (key, value) in scope.get_value::<rhai::Map>("doc").unwrap() {
match scope.get_value::<Option<rhai::Map>>("doc").unwrap() {
Some(map) => {
for (key, value) in map {
let mut vec = bumpalo::collections::Vec::new_in(doc_alloc);
serde_json::to_writer(&mut vec, &value).unwrap();
let key = doc_alloc.alloc_str(key.as_str());
@ -439,6 +441,11 @@ impl<'doc> Versions<'doc> {
data.insert(key, raw_value);
}
}
// In case the deletes the document and it's not the last change
// we simply set the document to an empty one and await the next change.
None => (),
}
}
// We must also run the code after the last change
let mut scope = rhai::Scope::new();
@ -449,15 +456,19 @@ impl<'doc> Versions<'doc> {
let _ = engine.eval_ast_with_scope::<rhai::Dynamic>(&mut scope, edit_function).unwrap();
data = RawMap::with_hasher_in(FxBuildHasher, doc_alloc);
for (key, value) in scope.get_value::<rhai::Map>("doc").unwrap() {
match scope.get_value::<Option<rhai::Map>>("doc").unwrap() {
Some(map) => {
for (key, value) in map {
let mut vec = bumpalo::collections::Vec::new_in(doc_alloc);
serde_json::to_writer(&mut vec, &value).unwrap();
let key = doc_alloc.alloc_str(key.as_str());
let raw_value = serde_json::from_slice(vec.into_bump_slice()).unwrap();
data.insert(key, raw_value);
}
Ok(Some(Self::single(data)))
Ok(Some(Some(Self::single(data))))
}
None => Ok(Some(None)),
}
}
pub fn single(version: RawMap<'doc, FxBuildHasher>) -> Self {

View File

@ -510,6 +510,7 @@ impl<'pl> PayloadOperations<'pl> {
}
/// Returns only the most recent version of a document based on the updates from the payloads.
#[allow(clippy::too_many_arguments)]
fn merge<'doc>(
&self,
rtxn: &heed::RoTxn,
@ -587,7 +588,18 @@ impl<'pl> PayloadOperations<'pl> {
.get(rtxn, &self.docid)?
.map(|obkv| obkv_to_rhaimap(obkv, fidmap))
.transpose()?;
Versions::multiple_with_edits(doc, versions, engine, ast, doc_alloc)?
match Versions::multiple_with_edits(doc, versions, engine, ast, doc_alloc)?
{
Some(Some(versions)) => Some(versions),
Some(None) if self.is_new => return Ok(None),
Some(None) => {
return Ok(Some(DocumentChange::Deletion(Deletion::create(
self.docid,
external_doc,
))));
}
None => None,
}
}
None => Versions::multiple(versions)?,
};