Skip to main content

trellis_core/
read.rs

1use crate::collection::{
2    MapCollectionShape, SetCollectionShape, downcast_map, downcast_map_diff, downcast_set,
3    downcast_set_diff,
4};
5use crate::input::{downcast_input, value_type};
6use crate::{
7    CollectionNode, DerivedNode, Graph, GraphError, GraphResult, InputNode, MapDiff, NodeId,
8    NodeKind, SetDiff,
9};
10use std::collections::{BTreeMap, BTreeSet};
11
12impl<C, O> Graph<C, O> {
13    /// Returns the committed value for a typed input node.
14    pub fn input_value<T>(&self, input: InputNode<T>) -> GraphResult<Option<&T>>
15    where
16        T: Clone + PartialEq + Send + Sync + 'static,
17    {
18        self.input_value_by_id(input.id())
19    }
20
21    /// Returns the committed value for an input node id.
22    pub fn input_value_by_id<T>(&self, node: NodeId) -> GraphResult<Option<&T>>
23    where
24        T: Clone + PartialEq + Send + Sync + 'static,
25    {
26        self.validate_input_write::<T>(node)?;
27        Ok(self
28            .input_values
29            .get(&node)
30            .and_then(|value| downcast_input::<T>(value.as_ref())))
31    }
32
33    /// Returns the committed value for a typed derived node.
34    pub fn derived_value<T>(&self, derived: DerivedNode<T>) -> GraphResult<Option<&T>>
35    where
36        T: Clone + PartialEq + Send + Sync + 'static,
37    {
38        self.derived_value_by_id(derived.id())
39    }
40
41    /// Returns the committed value for a derived node id.
42    pub fn derived_value_by_id<T>(&self, node: NodeId) -> GraphResult<Option<&T>>
43    where
44        T: Clone + PartialEq + Send + Sync + 'static,
45    {
46        self.validate_derived_write::<T>(node)?;
47        Ok(self
48            .derived_values
49            .get(&node)
50            .and_then(|value| downcast_input::<T>(value.as_ref())))
51    }
52
53    /// Returns the committed map for a collection node.
54    pub fn map_collection<K, V>(
55        &self,
56        collection: CollectionNode<K, V>,
57    ) -> GraphResult<Option<&BTreeMap<K, V>>>
58    where
59        K: Clone + Ord + Send + Sync + 'static,
60        V: Clone + PartialEq + Send + Sync + 'static,
61    {
62        self.map_collection_by_id(collection.id())
63    }
64
65    /// Returns the committed set for a collection node.
66    pub fn set_collection<K>(
67        &self,
68        collection: CollectionNode<K, ()>,
69    ) -> GraphResult<Option<&BTreeSet<K>>>
70    where
71        K: Clone + Ord + Send + Sync + 'static,
72    {
73        self.set_collection_by_id(collection.id())
74    }
75
76    /// Returns the last committed map diff for the current transaction.
77    pub fn map_diff<K, V>(
78        &self,
79        collection: CollectionNode<K, V>,
80    ) -> GraphResult<Option<&MapDiff<K, V>>>
81    where
82        K: Clone + Ord + Send + Sync + 'static,
83        V: Clone + PartialEq + Send + Sync + 'static,
84    {
85        self.map_diff_by_id(collection.id())
86    }
87
88    /// Returns the last committed set diff for the current transaction.
89    pub fn set_diff<K>(&self, collection: CollectionNode<K, ()>) -> GraphResult<Option<&SetDiff<K>>>
90    where
91        K: Clone + Ord + Send + Sync + 'static,
92    {
93        self.set_diff_by_id(collection.id())
94    }
95
96    pub(crate) fn validate_input_write<T>(&self, node: NodeId) -> GraphResult<()>
97    where
98        T: 'static,
99    {
100        let meta = self.nodes.get(&node).ok_or(GraphError::UnknownNode(node))?;
101        if meta.kind() != NodeKind::Input {
102            return Err(GraphError::NotInputNode(node));
103        }
104        if meta.value_type() != Some(value_type::<T>()) {
105            return Err(GraphError::WrongInputType(node));
106        }
107        Ok(())
108    }
109
110    pub(crate) fn validate_derived_write<T>(&self, node: NodeId) -> GraphResult<()>
111    where
112        T: 'static,
113    {
114        let meta = self.nodes.get(&node).ok_or(GraphError::UnknownNode(node))?;
115        if meta.kind() != NodeKind::Derived {
116            return Err(GraphError::NotDerivedNode(node));
117        }
118        if meta.value_type() != Some(value_type::<T>()) {
119            return Err(GraphError::WrongDerivedType(node));
120        }
121        Ok(())
122    }
123
124    /// Returns the committed map for a collection node id.
125    pub fn map_collection_by_id<K, V>(&self, node: NodeId) -> GraphResult<Option<&BTreeMap<K, V>>>
126    where
127        K: Clone + Ord + Send + Sync + 'static,
128        V: Clone + PartialEq + Send + Sync + 'static,
129    {
130        self.validate_map_collection_read::<K, V>(node)?;
131        Ok(self
132            .collection_values
133            .get(&node)
134            .and_then(|value| downcast_map::<K, V>(value.as_ref())))
135    }
136
137    /// Returns the committed set for a collection node id.
138    pub fn set_collection_by_id<K>(&self, node: NodeId) -> GraphResult<Option<&BTreeSet<K>>>
139    where
140        K: Clone + Ord + Send + Sync + 'static,
141    {
142        self.validate_set_collection_read::<K>(node)?;
143        Ok(self
144            .collection_values
145            .get(&node)
146            .and_then(|value| downcast_set::<K>(value.as_ref())))
147    }
148
149    /// Returns the current transaction's map diff for a collection node id.
150    pub fn map_diff_by_id<K, V>(&self, node: NodeId) -> GraphResult<Option<&MapDiff<K, V>>>
151    where
152        K: Clone + Ord + Send + Sync + 'static,
153        V: Clone + PartialEq + Send + Sync + 'static,
154    {
155        self.validate_map_collection_read::<K, V>(node)?;
156        Ok(self
157            .collection_diffs
158            .get(&node)
159            .and_then(|value| downcast_map_diff::<K, V>(value.as_ref())))
160    }
161
162    /// Returns the current transaction's set diff for a collection node id.
163    pub fn set_diff_by_id<K>(&self, node: NodeId) -> GraphResult<Option<&SetDiff<K>>>
164    where
165        K: Clone + Ord + Send + Sync + 'static,
166    {
167        self.validate_set_collection_read::<K>(node)?;
168        Ok(self
169            .collection_diffs
170            .get(&node)
171            .and_then(|value| downcast_set_diff::<K>(value.as_ref())))
172    }
173
174    pub(crate) fn validate_map_collection_read<K, V>(&self, node: NodeId) -> GraphResult<()>
175    where
176        K: 'static,
177        V: 'static,
178    {
179        let meta = self.nodes.get(&node).ok_or(GraphError::UnknownNode(node))?;
180        if meta.kind() != NodeKind::Collection {
181            return Err(GraphError::NotCollectionNode(node));
182        }
183        if meta.value_type() != Some(value_type::<MapCollectionShape<K, V>>()) {
184            return Err(GraphError::WrongCollectionType(node));
185        }
186        Ok(())
187    }
188
189    pub(crate) fn validate_set_collection_read<K>(&self, node: NodeId) -> GraphResult<()>
190    where
191        K: 'static,
192    {
193        let meta = self.nodes.get(&node).ok_or(GraphError::UnknownNode(node))?;
194        if meta.kind() != NodeKind::Collection {
195            return Err(GraphError::NotCollectionNode(node));
196        }
197        if meta.value_type() != Some(value_type::<SetCollectionShape<K>>()) {
198            return Err(GraphError::WrongCollectionType(node));
199        }
200        Ok(())
201    }
202}