-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgremlindocs-side-effects.expected
526 lines (526 loc) · 10.9 KB
/
gremlindocs-side-effects.expected
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
node > // gremlindocs-side-effects
node > // See http://gremlindocs.com/#side-effect
node >
node > // ### http://gremlindocs.com/#side-effect/aggregate
node > // Emits input, but adds input in collection, where provided closure processes input prior to
node > // insertion (greedy). In being "greedy", 'aggregate' will exhaust all the items that come to
node > // it from previous steps before emitting the next element.
node > x = new ArrayList
'[]'
node > g.V('id', '1').out().order('{it -> it.a.id <=> it.b.id}').aggregate(x).out().except(x)
[
{
_id: '5',
_type: 'vertex',
lang: 'java',
name: 'ripple'
}
]
node > x
'[v[2], v[3], v[4]]'
node >
node > // ### http://gremlindocs.com/#side-effect/as
node > // Emits input, but names the previous step.
node > g.V().as('x').outE('knows').inV().has('age', T.gt, 30).back('x').property('age').order()
[
29
]
node >
node > // ### http://gremlindocs.com/#side-effect/groupby
node > // Emits input, but groups input after processing it by provided key-closure and value-closure.
node > // It is also possible to supply an optional reduce-closure.
node > g.V().order('{it -> it.a.id <=> it.b.id}').groupBy('{it -> it}', '{it -> it.out}').cap()
[
{
1: {
_key: {
_id: '1',
_type: 'vertex',
age: 29,
name: 'marko'
},
_value: [
{
_id: '2',
_type: 'vertex',
age: 27,
name: 'vadas'
},
{
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
{
_id: '4',
_type: 'vertex',
age: 32,
name: 'josh'
}
]
},
2: {
_key: {
_id: '2',
_type: 'vertex',
age: 27,
name: 'vadas'
},
_value: [
]
},
3: {
_key: {
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
_value: [
]
},
4: {
_key: {
_id: '4',
_type: 'vertex',
age: 32,
name: 'josh'
},
_value: [
{
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
{
_id: '5',
_type: 'vertex',
lang: 'java',
name: 'ripple'
}
]
},
5: {
_key: {
_id: '5',
_type: 'vertex',
lang: 'java',
name: 'ripple'
},
_value: [
]
},
6: {
_key: {
_id: '6',
_type: 'vertex',
age: 35,
name: 'peter'
},
_value: [
{
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
}
]
}
}
]
node > g.V().order('{it -> it.a.id <=> it.b.id}').groupBy('{it -> it}', '{it -> it.out}', '{it->it.size()}').cap()
[
{
1: {
_key: {
_id: '1',
_type: 'vertex',
age: 29,
name: 'marko'
},
_value: 3
},
2: {
_key: {
_id: '2',
_type: 'vertex',
age: 27,
name: 'vadas'
},
_value: 0
},
3: {
_key: {
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
_value: 0
},
4: {
_key: {
_id: '4',
_type: 'vertex',
age: 32,
name: 'josh'
},
_value: 2
},
5: {
_key: {
_id: '5',
_type: 'vertex',
lang: 'java',
name: 'ripple'
},
_value: 0
},
6: {
_key: {
_id: '6',
_type: 'vertex',
age: 35,
name: 'peter'
},
_value: 1
}
}
]
node > m = new HashMap
'{}'
node > g.V().order('{it -> it.a.id <=> it.b.id}').groupBy(m, '{it -> it}', '{it -> it.out}')
[
{
_id: '1',
_type: 'vertex',
age: 29,
name: 'marko'
},
{
_id: '2',
_type: 'vertex',
age: 27,
name: 'vadas'
},
{
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
{
_id: '4',
_type: 'vertex',
age: 32,
name: 'josh'
},
{
_id: '5',
_type: 'vertex',
lang: 'java',
name: 'ripple'
},
{
_id: '6',
_type: 'vertex',
age: 35,
name: 'peter'
}
]
node > // Normally, one can do the following command to see the contents of the HashMap.
node > // However, the order the HashMap is printed is not deterministic, so can't be used in a unit test.
node > // m
node >
node > // ### http://gremlindocs.com/#side-effect/groupcount
node > // Emits input, but updates a map for each input, where closures provides generic map update.
node > m = new HashMap
'{}'
node > g.V().out().order('{it -> it.a.id <=> it.b.id}').groupCount(m)
[
{
_id: '2',
_type: 'vertex',
age: 27,
name: 'vadas'
},
{
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
{
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
{
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
{
_id: '4',
_type: 'vertex',
age: 32,
name: 'josh'
},
{
_id: '5',
_type: 'vertex',
lang: 'java',
name: 'ripple'
}
]
node > // m
node > m = new HashMap
'{}'
node > g.V('id', '1').out().order('{it -> it.a.id <=> it.b.id}').groupCount(m, '{it->it}', '{it->it.b+1.0}').out().order('{it -> it.a.id <=> it.b.id}').groupCount(m, '{it->it}', '{it->it.b+0.5}')
[
{
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
{
_id: '5',
_type: 'vertex',
lang: 'java',
name: 'ripple'
}
]
node > // m
node >
node > // ### http://gremlindocs.com/#side-effect/optional
node > // Behaves similar to back except that it does not filter. It will go down a particular path and back
node > // up to where it left off. As such, its useful for yielding a side-effect down a particular branch.
node > g.V().as('x').outE('knows').inV().has('age', T.gt, 30).back('x')
[
{
_id: '1',
_type: 'vertex',
age: 29,
name: 'marko'
}
]
node > g.V().as('x').outE('knows').inV().has('age', T.gt, 30).optional('x')
[
{
_id: '1',
_type: 'vertex',
age: 29,
name: 'marko'
},
{
_id: '2',
_type: 'vertex',
age: 27,
name: 'vadas'
},
{
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
{
_id: '4',
_type: 'vertex',
age: 32,
name: 'josh'
},
{
_id: '5',
_type: 'vertex',
lang: 'java',
name: 'ripple'
},
{
_id: '6',
_type: 'vertex',
age: 35,
name: 'peter'
}
]
node >
node > // ### http://gremlindocs.com/#side-effect/sideeffect
node > // Actually, the example at gremlindocs.com can't be done here.
node > // But there is a good example of sideEffect at http://markorodriguez.com/2011/08/03/on-the-nature-of-pipes/
node > // Note that x stores the value of the iterator (a vertex) in the sideEffect step, and then x is used
node > // in the final filter step.
node > g.V('id', '1').sideEffect('{it -> x=it}')
[
{
_id: '1',
_type: 'vertex',
age: 29,
name: 'marko'
}
]
node > g.V('id', '1').sideEffect('{it -> x=it}').out('created')
[
{
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
}
]
node > g.V('id', '1').sideEffect('{it -> x=it}').out('created').in('created')
[
{
_id: '1',
_type: 'vertex',
age: 29,
name: 'marko'
},
{
_id: '4',
_type: 'vertex',
age: 32,
name: 'josh'
},
{
_id: '6',
_type: 'vertex',
age: 35,
name: 'peter'
}
]
node > g.V('id', '1').sideEffect('{it -> x=it}').out('created').in('created').filter('{it -> it != x}')
[
{
_id: '4',
_type: 'vertex',
age: 32,
name: 'josh'
},
{
_id: '6',
_type: 'vertex',
age: 35,
name: 'peter'
}
]
node >
node > // ### http://gremlindocs.com/#side-effect/store
node > // Emits input, but adds input to collection, where provided closure processes input prior to
node > // insertion (lazy). In being "lazy", 'store' will keep element as they are being requested.
node > x = new ArrayList
'[]'
node > g.V('id', '1').out().order('{it -> it.a.id <=> it.b.id}').store(x).map()
[
{
age: 27,
name: 'vadas'
},
{
age: 32,
name: 'josh'
},
{
lang: 'java',
name: 'lop'
}
]
node > x
'[v[2], v[3], v[4]]'
node > x = new ArrayList
'[]'
node > g.V('id', '1').out().order('{it -> it.a.id <=> it.b.id}').store(x).next(function (err, val) { })
node > // We'd like to immediately check the value of x here, but there is a race condition since
node > // the above statement uses an async callback. As a hack, we execute something else first
node > // and then check the value of x. This is not absolutely guaranteed to work, but should with very probability.
node > g.V().out().out().out().id()
[
]
node > // Ok, now check value of x
node > x
'[v[2]]'
node >
node > // ### http://gremlindocs.com/#side-effect/table
node > // Emits input, but stores row of as values (constrained by column names if provided) in a table.
node > // Accepts an optional set of closures that are applied in round-robin fashion to each column of the table.
node > t = new g.Table
'[]'
node > g.V().order('{it -> it.a.id <=> it.b.id}').as('x').property('name').as('name').back('x').property('age').as('age').table(t)
[
27,
29,
32,
35,
null,
null
]
node > t
'[[x:v[1], name:marko, age:29], [x:v[2], name:vadas, age:27], [x:v[3], name:lop, age:null], [x:v[4], name:josh, age:32], [x:v[5], name:ripple, age:null], [x:v[6], name:peter, age:35]]'
node > t = new g.Table
'[]'
node > g.V().order('{it -> it.a.id <=> it.b.id}').has('age').as('x').property('name').as('name').back('x').property('age').as('age').table(t, '{it->it}', '{it->it}', '{it->it>30 ? "over thirty" : "under thirty"}')
[
27,
29,
32,
35
]
node > t
'[[x:v[1], name:marko, age:under thirty], [x:v[2], name:vadas, age:under thirty], [x:v[4], name:josh, age:over thirty], [x:v[6], name:peter, age:over thirty]]'
node >
node > // ### http://gremlindocs.com/#side-effect/tree
node > // Emit input, but stores the tree formed by the traversal as a map. Accepts an optional set of closures
node > // to be applied in round-robin fashion over each level of the tree.
node > g.V('id', '1').out().out().tree().cap()
[
{
1: {
_key: {
_id: '1',
_type: 'vertex',
age: 29,
name: 'marko'
},
_value: {
4: {
_key: {
_id: '4',
_type: 'vertex',
age: 32,
name: 'josh'
},
_value: {
3: {
_key: {
_id: '3',
_type: 'vertex',
lang: 'java',
name: 'lop'
},
_value: {
}
},
5: {
_key: {
_id: '5',
_type: 'vertex',
lang: 'java',
name: 'ripple'
},
_value: {
}
}
}
}
}
}
}
]
node >
node > 'goodbye'
'goodbye'
node >