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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use crate::graph::GraphStorage;
use crate::*;
use std::sync::Arc;
impl<'g, S: GraphStorage + 'g> GraphTraversalSource<'g, S> {
/// Starts the traversal with a named side effect holding the given value.
pub fn with_side_effect<T: Into<ElementProperty>>(mut self, name: &str, value: T) -> Self {
self.side_effect.insert(
CompactString::new(name),
crate::traversal::side_effect::SideEffectValue::seed(value.into()),
);
self
}
/// Names an explicit target graph for `subgraph(name)`: `withSideEffect("sg", graph)`.
/// `subgraph("sg")` then writes the selected edges, their endpoint vertices, ids, labels and
/// properties into `target` (through the [`GraphStorage`] mutation API) when the step has
/// run, and `cap("sg")` returns `target` itself. A vertex or edge id the target already holds
/// is reused or skipped, so repeated writes are idempotent; the source's stored schema is
/// copied only when the target stores none.
///
/// The target must not be the graph being traversed (it is locked then and the step fails
/// instead of deadlocking).
///
/// ```
/// use graphersal::prelude::*;
/// use std::sync::Arc;
/// let graph = GraphSource::tinkerpop_modern();
/// let target = Arc::new(GraphSource::empty());
/// let g = graph.read();
/// g.traversal()
/// .with_side_effect_graph("sg", target.clone())
/// .e(None)
/// .subgraph("sg")
/// .to_list()
/// .unwrap();
/// assert_eq!(target.read().edge_count(), 6);
/// ```
pub fn with_side_effect_graph(mut self, name: &str, target: Arc<crate::graph::Graph>) -> Self {
self.side_effect.insert(
CompactString::new(name),
crate::traversal::side_effect::SideEffectValue::Subgraph {
edges: Default::default(),
target: Some(target),
},
);
self
}
/// Declares a side effect with a **reducer**, `withSideEffect(name, initial, Operator)`:
/// `aggregate(name)`/`store(name)` combine into the value with `operator` instead of
/// collecting a list, and `cap(name)` reads the result.
///
/// One `aggregate()` execution folds the values it collected in stream order, except that
/// `Operator::Assign` and `Operator::AddAll` receive them as one list. `store()` folds after
/// every element. An empty execution leaves the value unchanged. Deviations from TinkerPop
/// 3.8.2 (book page "Sack and Operators", `tinkerpop_deviations.md`): the collection rule
/// above is inferred from the feature files (3.7.2 itself cannot run `sum` here); integer
/// overflow is an `OperatorFailed` error; an array seed makes a reduced value, never a list.
///
/// ```
/// use graphersal::prelude::*;
/// let graph = GraphSource::tinkerpop_modern();
/// let g = graph.read();
/// let g = g.traversal();
/// let sum = g
/// .with_side_effect_reduced("a", 1, Operator::Sum)
/// .v(None::<()>)
/// .aggregate("a")
/// .by("age")
/// .cap("a")
/// .next()
/// .unwrap();
/// assert_eq!(sum.and_then(|v| v.as_i64()), Some(124));
/// ```
pub fn with_side_effect_reduced<T: Into<ElementProperty>>(
mut self,
name: &str,
value: T,
operator: Operator,
) -> Self {
self.side_effect.insert(
CompactString::new(name),
crate::traversal::side_effect::SideEffectValue::seed_reduced(value.into(), operator),
);
self
}
/// Declares a side effect as a **set**: `aggregate(name)`/`store(name)` into it keep each value
/// once (first-seen order), and `cap(name)`/`select(name)` return it as a deduplicated list.
///
/// Duplicates in `items` collapse. Bulk is invisible: merging equal traversers never changes
/// the set. A set is a property of the side-effect bucket only; Graphersal has no set value
/// type (`toSet()`, `GType.SET` are not implemented).
///
/// Deviations from TinkerPop (book page "Set Side Effects"): members keep first-seen order
/// (a TinkerPop set has an unspecified one); members are compared by value, so `1` and `1.0`
/// differ, and a property value from `values()` counts by its value, not by
/// the element it came from (a `properties()` element counts by element and key; an edge property too, where TinkerPop
/// compares key and value);
/// `cap`/`select` return a plain array.
///
/// ```
/// use graphersal::prelude::*;
/// let graph = GraphSource::tinkerpop_modern();
/// let g = graph.read();
/// let g = g.traversal();
/// let names = g
/// .with_side_effect_set("a", Vec::<ElementProperty>::new())
/// .v(None::<()>)
/// .both(None)
/// .values("name")
/// .aggregate("a")
/// .cap("a")
/// .next()
/// .unwrap();
/// assert!(names.is_some());
/// ```
pub fn with_side_effect_set<I: IntoIterator<Item = ElementProperty>>(
mut self,
name: &str,
items: I,
) -> Self {
self.side_effect.insert(
CompactString::new(name),
crate::traversal::side_effect::SideEffectValue::seed_set(items),
);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple() {
let graph = GraphSource::tinkerpop_modern();
let g_lock = graph.read();
let g = g_lock.traversal();
let result = g
.with_side_effect("x", "value")
.v("1")
.select("x")
.next()
.unwrap()
.unwrap();
assert_eq!(result.as_string(), Some("value"));
}
#[test]
fn simple_str_array() {
let graph = GraphSource::tinkerpop_modern();
let g_lock = graph.read();
let g = g_lock.traversal();
let result = g
.with_side_effect("x", vec!["hello", "world"])
.v("1")
.select("x")
.next()
.unwrap()
.unwrap();
match result {
ElementProperty::Array(result) => {
assert_eq!(result[0].as_string(), Some("hello"));
assert_eq!(result[1].as_string(), Some("world"));
}
_ => panic!(),
}
}
#[test]
fn simple_i64_array() {
let graph = GraphSource::tinkerpop_modern();
let g_lock = graph.read();
let g = g_lock.traversal();
let result = g
.with_side_effect("x", vec![1_i64, 2_i64])
.v("1")
.select("x")
.next()
.unwrap()
.unwrap();
match result {
ElementProperty::Array(result) => {
assert_eq!(result[0].as_i64(), Some(1_i64));
assert_eq!(result[1].as_i64(), Some(2_i64));
}
_ => panic!(),
}
}
#[test]
fn simple_f64_array() {
let graph = GraphSource::tinkerpop_modern();
let g_lock = graph.read();
let g = g_lock.traversal();
let result = g
.with_side_effect("x", vec![1.1_f64, 2.2_f64])
.v("1")
.select("x")
.next()
.unwrap()
.unwrap();
match result {
ElementProperty::Array(result) => {
assert_eq!(result[0].as_f64(), Some(1.1_f64));
assert_eq!(result[1].as_f64(), Some(2.2_f64));
}
_ => panic!(),
}
}
#[test]
fn number() {
let graph = GraphSource::tinkerpop_modern();
let g_lock = graph.read();
let g = g_lock.traversal();
let result = g
.with_side_effect("x", 1_usize)
.v("1")
.select("x")
.next()
.unwrap()
.unwrap();
assert_eq!(result.as_i64(), Some(1_i64));
}
#[test]
fn double() {
let graph = GraphSource::tinkerpop_modern();
let g_lock = graph.read();
let g = g_lock.traversal();
let result = g
.with_side_effect("x", "hello")
.with_side_effect("y", "world")
.v("1")
.select(vec!["x", "y"])
.next()
.unwrap()
.unwrap();
match result {
ElementProperty::Object(result) => {
assert_eq!(result["x"].as_string(), Some("hello"));
assert_eq!(result["y"].as_string(), Some("world"));
}
_ => panic!(),
}
}
#[test]
fn alias_after_v() {
let graph = GraphSource::tinkerpop_modern();
let g_lock = graph.read();
let g = g_lock.traversal();
let result = g
.with_side_effect("x", "hello")
.v("1")
.id()
.as_("z")
.with_side_effect("y", "world")
.select(vec!["x", "y", "z"])
.next()
.unwrap()
.unwrap();
match result {
ElementProperty::Object(result) => {
assert_eq!(result["x"].as_string(), Some("hello"));
assert_eq!(result["y"].as_string(), Some("world"));
assert_eq!(result["z"].as_string(), Some("1"));
}
_ => panic!(),
}
}
}