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