diff --git a/lib/src/doc/delete.rs b/lib/src/doc/delete.rs index 51096885..aa9ad245 100644 --- a/lib/src/doc/delete.rs +++ b/lib/src/doc/delete.rs @@ -24,8 +24,8 @@ impl<'a> Document<'a> { self.erase(ctx, opt, txn, stm).await?; // Purge index data self.index(ctx, opt, txn, stm).await?; - // Store record data - self.store(ctx, opt, txn, stm).await?; + // Purge record data + self.purge(ctx, opt, txn, stm).await?; // Run table queries self.table(ctx, opt, txn, stm).await?; // Run lives queries diff --git a/lib/src/doc/mod.rs b/lib/src/doc/mod.rs index a907e6df..23a0023f 100644 --- a/lib/src/doc/mod.rs +++ b/lib/src/doc/mod.rs @@ -20,6 +20,7 @@ mod lives; mod merge; mod perms; mod pluck; +mod purge; mod relate; mod select; mod store; diff --git a/lib/src/doc/purge.rs b/lib/src/doc/purge.rs new file mode 100644 index 00000000..8a4814db --- /dev/null +++ b/lib/src/doc/purge.rs @@ -0,0 +1,40 @@ +use crate::dbs::Options; +use crate::dbs::Runtime; +use crate::dbs::Statement; +use crate::dbs::Transaction; +use crate::doc::Document; +use crate::err::Error; + +impl<'a> Document<'a> { + pub async fn purge( + &self, + ctx: &Runtime, + opt: &Options, + txn: &Transaction, + _stm: &Statement, + ) -> Result<(), Error> { + // Check if forced + if !opt.force && !self.changed() { + return Ok(()); + } + // Check if the table is a view + if self.tb(ctx, opt, txn).await?.drop { + return Ok(()); + } + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Get the record id + let rid = self.id.as_ref().unwrap(); + // Purge the record data + let key = crate::key::thing::new(opt.ns(), opt.db(), &rid.tb, &rid.id); + run.del(key).await?; + // Remove the graph data + let beg = crate::key::graph::prefix(opt.ns(), opt.db(), &rid.tb, &rid.id); + let end = crate::key::graph::suffix(opt.ns(), opt.db(), &rid.tb, &rid.id); + run.delr(beg..end, u32::MAX).await?; + // Carry on + Ok(()) + } +} diff --git a/lib/src/doc/store.rs b/lib/src/doc/store.rs index 947d1638..4512e63b 100644 --- a/lib/src/doc/store.rs +++ b/lib/src/doc/store.rs @@ -4,19 +4,33 @@ use crate::dbs::Statement; use crate::dbs::Transaction; use crate::doc::Document; use crate::err::Error; -use crate::key::thing; impl<'a> Document<'a> { pub async fn store( &self, - _ctx: &Runtime, + ctx: &Runtime, opt: &Options, txn: &Transaction, _stm: &Statement, ) -> Result<(), Error> { - let md = self.id.as_ref().unwrap(); - let key = thing::new(opt.ns(), opt.db(), &md.tb, &md.id); - txn.clone().lock().await.set(key, self).await?; + // Check if forced + if !opt.force && !self.changed() { + return Ok(()); + } + // Check if the table is a view + if self.tb(ctx, opt, txn).await?.drop { + return Ok(()); + } + // Clone transaction + let run = txn.clone(); + // Claim transaction + let mut run = run.lock().await; + // Get the record id + let rid = self.id.as_ref().unwrap(); + // Store the record data + let key = crate::key::thing::new(opt.ns(), opt.db(), &rid.tb, &rid.id); + run.set(key, self).await?; + // Carry on Ok(()) } }