syndicate-rs/syndicate/src/dataspace.rs

99 lines
3.1 KiB
Rust
Raw Permalink Normal View History

2021-08-14 01:25:31 +00:00
//! Implements a [*dataspace*][crate::dataspace#GarnockJones2017]
//! entity.
//!
//! **References.**
//!
//! - Garnock-Jones, Tony. <a name="GarnockJones2017">“Conversational
//! Concurrency.”</a> PhD, Northeastern University, 2017. [Available
//! on the web](https://syndicate-lang.org/tonyg-dissertation/).
//! [PDF](https://syndicate-lang.org/papers/conversational-concurrency-201712310922.pdf).
use super::language;
2019-10-20 21:25:01 +00:00
use super::skeleton;
use super::actor::*;
use super::schemas::dataspace::*;
2019-10-20 15:01:27 +00:00
use preserves::value::Map;
use preserves_schema::Codec;
2019-10-20 15:01:27 +00:00
/// A Dataspace object (entity).
2019-10-20 15:01:27 +00:00
#[derive(Debug)]
pub struct Dataspace {
2022-01-19 13:40:50 +00:00
pub name: Name,
/// Index over assertions placed in the dataspace; used to
/// efficiently route assertion changes and messages to observers.
pub index: skeleton::Index,
/// Local memory of assertions indexed by `Handle`, used to remove
/// assertions from the `index` when they are retracted.
pub handle_map: Map<Handle, _Any>,
2019-10-20 15:01:27 +00:00
}
impl Dataspace {
/// Construct a new, empty dataspace.
2022-01-19 13:40:50 +00:00
pub fn new(name: Name) -> Self {
2020-05-18 09:36:44 +00:00
Self {
2023-01-28 21:45:48 +00:00
name,
2020-05-18 09:36:44 +00:00
index: skeleton::Index::new(),
handle_map: Map::new(),
2020-05-18 09:36:44 +00:00
}
2019-10-20 15:01:27 +00:00
}
/// Retrieve the current count of *distinct* assertions placed in
/// the dataspace.
pub fn assertion_count(&self) -> usize {
self.index.assertion_count()
2019-10-20 15:01:27 +00:00
}
/// Retrieve the current count of assertions, including
/// duplicates, placed in the dataspace.
pub fn endpoint_count(&self) -> isize {
self.index.endpoint_count()
2019-10-20 15:01:27 +00:00
}
/// Retrieve the current count of
/// [`Observe`][crate::schemas::dataspace::Observe] assertions in
/// the dataspace.
pub fn observer_count(&self) -> usize {
self.index.observer_count()
2019-10-20 15:01:27 +00:00
}
}
2019-10-20 15:01:27 +00:00
impl Entity<_Any> for Dataspace {
2021-07-15 07:13:31 +00:00
fn assert(&mut self, t: &mut Activation, a: _Any, h: Handle) -> ActorResult {
let is_new = self.index.insert(t, &a);
2022-01-19 13:40:50 +00:00
tracing::trace!(dataspace = ?self.name, assertion = ?a, handle = ?h, ?is_new, "assert");
if is_new {
if let Ok(o) = language().parse::<Observe>(&a) {
self.index.add_observer(t, &o.pattern, &o.observer);
}
2019-10-20 15:01:27 +00:00
}
self.handle_map.insert(h, a);
2019-10-20 15:01:27 +00:00
Ok(())
}
fn retract(&mut self, t: &mut Activation, h: Handle) -> ActorResult {
match self.handle_map.remove(&h) {
2022-01-19 13:40:50 +00:00
None => tracing::warn!(dataspace = ?self.name, handle = ?h, "retract of unknown handle"),
Some(a) => {
let is_last = self.index.remove(t, &a);
2022-01-19 13:40:50 +00:00
tracing::trace!(dataspace = ?self.name, assertion = ?a, handle = ?h, ?is_last, "retract");
if is_last {
if let Ok(o) = language().parse::<Observe>(&a) {
self.index.remove_observer(t, o.pattern, &o.observer);
}
}
}
}
Ok(())
2020-05-18 09:36:44 +00:00
}
2021-07-15 07:13:31 +00:00
fn message(&mut self, t: &mut Activation, m: _Any) -> ActorResult {
2022-01-19 13:40:50 +00:00
tracing::trace!(dataspace = ?self.name, body = ?m, "message");
self.index.send(t, &m);
Ok(())
2020-05-18 09:36:44 +00:00
}
}