1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! 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;
use super::skeleton;
use super::actor::*;
use super::schemas::dataspace::*;
use super::schemas::dataspace::_Any;

use preserves::value::Map;
use preserves_schema::Codec;

/// A Dataspace object (entity).
#[derive(Debug)]
pub struct Dataspace {
    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>,
}

impl Dataspace {
    /// Construct a new, empty dataspace.
    pub fn new(name: Name) -> Self {
        Self {
            name,
            index: skeleton::Index::new(),
            handle_map: Map::new(),
        }
    }

    /// Retrieve the current count of *distinct* assertions placed in
    /// the dataspace.
    pub fn assertion_count(&self) -> usize {
        self.index.assertion_count()
    }

    /// Retrieve the current count of assertions, including
    /// duplicates, placed in the dataspace.
    pub fn endpoint_count(&self) -> isize {
        self.index.endpoint_count()
    }

    /// Retrieve the current count of
    /// [`Observe`][crate::schemas::dataspace::Observe] assertions in
    /// the dataspace.
    pub fn observer_count(&self) -> usize {
        self.index.observer_count()
    }
}

impl Entity<_Any> for Dataspace {
    fn assert(&mut self, t: &mut Activation, a: _Any, h: Handle) -> ActorResult {
        let is_new = self.index.insert(t, &a);
        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);
            }
        }

        self.handle_map.insert(h, a);
        Ok(())
    }

    fn retract(&mut self, t: &mut Activation, h: Handle) -> ActorResult {
        match self.handle_map.remove(&h) {
            None => tracing::warn!(dataspace = ?self.name, handle = ?h, "retract of unknown handle"),
            Some(a) => {
                let is_last = self.index.remove(t, &a);
                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(())
    }

    fn message(&mut self, t: &mut Activation, m: _Any) -> ActorResult {
        tracing::trace!(dataspace = ?self.name, body = ?m, "message");
        self.index.send(t, &m);
        Ok(())
    }
}