use crate::ctx::Context;
use crate::dbs::Options;
use crate::dbs::Statement;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::dir::Dir;
use crate::sql::edges::Edges;
use crate::sql::paths::EDGE;
use crate::sql::paths::IN;
use crate::sql::paths::OUT;
use crate::sql::statements::DeleteStatement;
use crate::sql::table::Tables;
use crate::sql::value::{Value, Values};
use reblessive::tree::Stk;
impl Document {
pub(super) async fn purge(
&self,
stk: &mut Stk,
ctx: &Context,
opt: &Options,
_stm: &Statement<'_>,
) -> Result<(), Error> {
if !self.changed() {
return Ok(());
}
let txn = ctx.tx();
let mut txn = txn.lock().await;
if let Some(rid) = &self.id {
let ns = opt.ns()?;
let db = opt.db()?;
let key = crate::key::thing::new(ns, db, &rid.tb, &rid.id);
txn.del(key).await?;
match (
self.initial.doc.as_ref().pick(&*EDGE),
self.initial.doc.as_ref().pick(&*IN),
self.initial.doc.as_ref().pick(&*OUT),
) {
(Value::Bool(true), Value::Thing(ref l), Value::Thing(ref r)) => {
let (ref o, ref i) = (Dir::Out, Dir::In);
let key = crate::key::graph::new(ns, db, &l.tb, &l.id, o, rid);
txn.del(key).await?;
let key = crate::key::graph::new(ns, db, &rid.tb, &rid.id, i, l);
txn.del(key).await?;
let key = crate::key::graph::new(ns, db, &rid.tb, &rid.id, o, r);
txn.del(key).await?;
let key = crate::key::graph::new(ns, db, &r.tb, &r.id, i, rid);
txn.del(key).await?;
}
_ => {
drop(txn);
let stm = DeleteStatement {
what: Values(vec![Value::from(Edges {
dir: Dir::Both,
from: rid.as_ref().clone(),
what: Tables::default(),
})]),
..DeleteStatement::default()
};
stm.compute(stk, ctx, opt, None).await?;
}
}
}
Ok(())
}
}