surrealpatch/core/src/doc/edges.rs

81 lines
2.3 KiB
Rust
Raw Normal View History

2022-05-30 15:32:26 +00:00
use crate::ctx::Context;
2024-05-28 10:43:45 +00:00
use crate::dbs::Options;
2022-05-30 15:32:26 +00:00
use crate::dbs::Statement;
use crate::dbs::Workable;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::paths::EDGE;
use crate::sql::paths::IN;
use crate::sql::paths::OUT;
use crate::sql::value::Value;
use crate::sql::Dir;
2024-08-22 15:25:20 +00:00
use crate::sql::Relation;
use crate::sql::TableType;
2022-05-30 15:32:26 +00:00
2024-08-15 16:01:02 +00:00
impl Document {
2022-05-30 15:32:26 +00:00
pub async fn edges(
&mut self,
2024-08-15 16:01:02 +00:00
ctx: &Context,
2022-05-30 15:32:26 +00:00
opt: &Options,
_stm: &Statement<'_>,
) -> Result<(), Error> {
2024-08-22 15:25:20 +00:00
// Get the table
let tb = self.tb(ctx, opt).await?;
2022-05-30 15:32:26 +00:00
// Check if the table is a view
2024-08-22 15:25:20 +00:00
if tb.drop {
2022-05-30 15:32:26 +00:00
return Ok(());
}
// Get the transaction
let txn = ctx.tx();
// Lock the transaction
let mut txn = txn.lock().await;
2022-05-30 15:32:26 +00:00
// Get the record id
let rid = self.id.as_ref().unwrap();
// Store the record edges
if let Workable::Relate(l, r, _) = &self.extras {
2024-08-22 15:25:20 +00:00
// For enforced relations, ensure that the edges exist
if matches!(
tb.kind,
TableType::Relation(Relation {
enforced: true,
..
})
) {
let key = crate::key::thing::new(opt.ns()?, opt.db()?, &l.tb, &l.id);
if !txn.exists(key).await? {
return Err(Error::IdNotFound {
value: l.to_string(),
});
}
let key = crate::key::thing::new(opt.ns()?, opt.db()?, &r.tb, &r.id);
if !txn.exists(key).await? {
return Err(Error::IdNotFound {
value: r.to_string(),
});
}
}
// Get temporary edge references
let (ref o, ref i) = (Dir::Out, Dir::In);
2022-05-30 15:32:26 +00:00
// Store the left pointer edge
let key = crate::key::graph::new(opt.ns()?, opt.db()?, &l.tb, &l.id, o, rid);
2024-08-22 22:34:33 +00:00
txn.set(key, vec![], None).await?;
2022-05-30 15:32:26 +00:00
// Store the left inner edge
let key = crate::key::graph::new(opt.ns()?, opt.db()?, &rid.tb, &rid.id, i, l);
2024-08-22 22:34:33 +00:00
txn.set(key, vec![], None).await?;
2022-05-30 15:32:26 +00:00
// Store the right inner edge
let key = crate::key::graph::new(opt.ns()?, opt.db()?, &rid.tb, &rid.id, o, r);
2024-08-22 22:34:33 +00:00
txn.set(key, vec![], None).await?;
2022-05-30 15:32:26 +00:00
// Store the right pointer edge
let key = crate::key::graph::new(opt.ns()?, opt.db()?, &r.tb, &r.id, i, rid);
2024-08-22 22:34:33 +00:00
txn.set(key, vec![], None).await?;
// Store the edges on the record
self.current.doc.to_mut().put(&*EDGE, Value::Bool(true));
self.current.doc.to_mut().put(&*IN, l.clone().into());
self.current.doc.to_mut().put(&*OUT, r.clone().into());
2022-05-30 15:32:26 +00:00
}
// Carry on
Ok(())
}
}