2021-08-14 01:25:31 +00:00
|
|
|
//! Implements a [*dataspace*][crate::dataspace#GarnockJones2017]
|
|
|
|
//! entity.
|
2021-08-14 00:39:27 +00:00
|
|
|
//!
|
|
|
|
//! **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).
|
|
|
|
|
2021-09-19 14:53:37 +00:00
|
|
|
use super::language;
|
2019-10-20 21:25:01 +00:00
|
|
|
use super::skeleton;
|
2021-07-03 07:03:52 +00:00
|
|
|
use super::actor::*;
|
|
|
|
use super::schemas::dataspace::*;
|
2019-10-20 15:01:27 +00:00
|
|
|
|
2021-07-03 07:03:52 +00:00
|
|
|
use preserves::value::Map;
|
2021-09-19 14:53:37 +00:00
|
|
|
use preserves_schema::Codec;
|
2019-10-20 15:01:27 +00:00
|
|
|
|
2021-08-14 00:39: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,
|
2021-08-14 00:39:27 +00:00
|
|
|
/// Index over assertions placed in the dataspace; used to
|
|
|
|
/// efficiently route assertion changes and messages to observers.
|
2021-07-03 07:03:52 +00:00
|
|
|
pub index: skeleton::Index,
|
2021-08-14 00:39:27 +00:00
|
|
|
/// Local memory of assertions indexed by `Handle`, used to remove
|
|
|
|
/// assertions from the `index` when they are retracted.
|
2022-01-15 22:18:29 +00:00
|
|
|
pub handle_map: Map<Handle, _Any>,
|
2019-10-20 15:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Dataspace {
|
2021-08-14 00:39:27 +00:00
|
|
|
/// 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(),
|
2021-07-03 07:03:52 +00:00
|
|
|
handle_map: Map::new(),
|
2020-05-18 09:36:44 +00:00
|
|
|
}
|
2019-10-20 15:01:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 00:39:27 +00:00
|
|
|
/// Retrieve the current count of *distinct* assertions placed in
|
|
|
|
/// the dataspace.
|
2021-07-03 07:03:52 +00:00
|
|
|
pub fn assertion_count(&self) -> usize {
|
|
|
|
self.index.assertion_count()
|
2019-10-20 15:01:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 00:39:27 +00:00
|
|
|
/// Retrieve the current count of assertions, including
|
|
|
|
/// duplicates, placed in the dataspace.
|
2021-07-03 07:03:52 +00:00
|
|
|
pub fn endpoint_count(&self) -> isize {
|
|
|
|
self.index.endpoint_count()
|
2019-10-20 15:01:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 00:39:27 +00:00
|
|
|
/// Retrieve the current count of
|
|
|
|
/// [`Observe`][crate::schemas::dataspace::Observe] assertions in
|
|
|
|
/// the dataspace.
|
2021-07-03 07:03:52 +00:00
|
|
|
pub fn observer_count(&self) -> usize {
|
|
|
|
self.index.observer_count()
|
2019-10-20 15:01:27 +00:00
|
|
|
}
|
2021-07-03 07:03:52 +00:00
|
|
|
}
|
2019-10-20 15:01:27 +00:00
|
|
|
|
2021-07-22 14:53:56 +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 {
|
2022-01-15 22:18:29 +00:00
|
|
|
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");
|
2022-01-15 22:18:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2022-01-15 22:18:29 +00:00
|
|
|
|
|
|
|
self.handle_map.insert(h, a);
|
2019-10-20 15:01:27 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-10-24 19:05:39 +00:00
|
|
|
|
2021-07-03 07:03:52 +00:00
|
|
|
fn retract(&mut self, t: &mut Activation, h: Handle) -> ActorResult {
|
2022-01-15 22:18:29 +00:00
|
|
|
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"),
|
2022-01-15 22:18:29 +00:00
|
|
|
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");
|
2022-01-15 22:18:29 +00:00
|
|
|
|
|
|
|
if is_last {
|
|
|
|
if let Ok(o) = language().parse::<Observe>(&a) {
|
|
|
|
self.index.remove_observer(t, o.pattern, &o.observer);
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 07:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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");
|
2021-08-14 00:39:27 +00:00
|
|
|
self.index.send(t, &m);
|
2021-07-03 07:03:52 +00:00
|
|
|
Ok(())
|
2020-05-18 09:36:44 +00:00
|
|
|
}
|
2019-10-24 19:05:39 +00:00
|
|
|
}
|