forked from cayleygraph/cayley
/
iterate.go
384 lines (359 loc) · 7.73 KB
/
iterate.go
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
package graph
import (
"context"
"encoding/json"
"fmt"
"github.com/cayleygraph/cayley/clog"
"github.com/cayleygraph/cayley/quad"
)
// IterateChain is a chain-enabled helper to setup iterator execution.
type IterateChain struct {
ctx context.Context
it Iterator
qs QuadStore
paths bool
optimize bool
limit int
n int
}
// Iterate is a set of helpers for iteration. Context may be used to cancel execution.
// Iterator will be optimized and closed after execution.
//
// By default, iteration has no limit and includes sub-paths.
func Iterate(ctx context.Context, it Iterator) *IterateChain {
if ctx == nil {
ctx = context.Background()
}
return &IterateChain{
ctx: ctx, it: it,
limit: -1, paths: true,
optimize: true,
}
}
func (c *IterateChain) next() bool {
select {
case <-c.ctx.Done():
return false
default:
}
ok := (c.limit < 0 || c.n < c.limit) && c.it.Next(c.ctx)
if ok {
c.n++
}
return ok
}
func (c *IterateChain) nextPath() bool {
select {
case <-c.ctx.Done():
return false
default:
}
ok := c.paths && (c.limit < 0 || c.n < c.limit) && c.it.NextPath(c.ctx)
if ok {
c.n++
}
return ok
}
func (c *IterateChain) start() {
if c.optimize {
c.it, _ = c.it.Optimize()
if c.qs != nil {
c.it, _ = c.qs.OptimizeIterator(c.it)
}
}
if !clog.V(2) {
return
}
if b, err := json.MarshalIndent(DescribeIterator(c.it), "", " "); err != nil {
clog.Infof("failed to format description: %v", err)
} else {
clog.Infof("%s", b)
}
}
func (c *IterateChain) end() {
c.it.Close()
if !clog.V(2) {
return
}
if b, err := json.MarshalIndent(DumpStats(c.it), "", " "); err != nil {
clog.Infof("failed to format stats: %v", err)
} else {
clog.Infof("%s", b)
}
}
// Limit limits a total number of results returned.
func (c *IterateChain) Limit(n int) *IterateChain {
c.limit = n
return c
}
// Paths switches iteration over sub-paths (with it.NextPath).
// Defaults to true.
func (c *IterateChain) Paths(enable bool) *IterateChain {
c.paths = enable
return c
}
// On sets a default quad store for iteration. If qs was set, it may be omitted in other functions.
func (c *IterateChain) On(qs QuadStore) *IterateChain {
c.qs = qs
return c
}
// UnOptimized disables iterator optimization.
func (c *IterateChain) UnOptimized() *IterateChain {
c.optimize = false
return c
}
// Each will run a provided callback for each result of the iterator.
func (c *IterateChain) Each(fnc func(Value)) error {
c.start()
defer c.end()
done := c.ctx.Done()
for c.next() {
select {
case <-done:
return c.ctx.Err()
default:
}
fnc(c.it.Result())
for c.nextPath() {
select {
case <-done:
return c.ctx.Err()
default:
}
fnc(c.it.Result())
}
}
return c.it.Err()
}
// All will return all results of an iterator.
func (c *IterateChain) Count() (int64, error) {
c.start()
defer c.end()
if err := c.it.Err(); err != nil {
return 0, err
}
if size, exact := c.it.Size(); exact {
return size, nil
}
done := c.ctx.Done()
var cnt int64
iteration:
for c.next() {
select {
case <-done:
break iteration
default:
}
cnt++
for c.nextPath() {
select {
case <-done:
break iteration
default:
}
cnt++
}
}
return cnt, c.it.Err()
}
// All will return all results of an iterator.
func (c *IterateChain) All() ([]Value, error) {
c.start()
defer c.end()
done := c.ctx.Done()
var out []Value
iteration:
for c.next() {
select {
case <-done:
break iteration
default:
}
out = append(out, c.it.Result())
for c.nextPath() {
select {
case <-done:
break iteration
default:
}
out = append(out, c.it.Result())
}
}
return out, c.it.Err()
}
// First will return a first result of an iterator. It returns nil if iterator is empty.
func (c *IterateChain) First() (Value, error) {
c.start()
defer c.end()
if !c.next() {
return nil, c.it.Err()
}
return c.it.Result(), nil
}
// Send will send each result of the iterator to the provided channel.
//
// Channel will NOT be closed when function returns.
func (c *IterateChain) Send(out chan<- Value) error {
c.start()
defer c.end()
done := c.ctx.Done()
for c.next() {
select {
case <-done:
return c.ctx.Err()
case out <- c.it.Result():
}
for c.nextPath() {
select {
case <-done:
return c.ctx.Err()
case out <- c.it.Result():
}
}
}
return c.it.Err()
}
// TagEach will run a provided tag map callback for each result of the iterator.
func (c *IterateChain) TagEach(fnc func(map[string]Value)) error {
c.start()
defer c.end()
done := c.ctx.Done()
mn := 0
for c.next() {
select {
case <-done:
return c.ctx.Err()
default:
}
tags := make(map[string]Value, mn)
c.it.TagResults(tags)
if n := len(tags); n > mn {
mn = n
}
fnc(tags)
for c.nextPath() {
select {
case <-done:
return c.ctx.Err()
default:
}
tags := make(map[string]Value, mn)
c.it.TagResults(tags)
if n := len(tags); n > mn {
mn = n
}
fnc(tags)
}
}
return c.it.Err()
}
var errNoQuadStore = fmt.Errorf("no quad store in Iterate")
// EachValue is an analog of Each, but it will additionally call NameOf
// for each graph.Value before passing it to a callback.
func (c *IterateChain) EachValue(qs QuadStore, fnc func(quad.Value)) error {
if qs != nil {
c.qs = qs
}
if c.qs == nil {
return errNoQuadStore
}
// TODO(dennwc): batch NameOf?
return c.Each(func(v Value) {
if nv := c.qs.NameOf(v); nv != nil {
fnc(nv)
}
})
}
// EachValuePair is an analog of Each, but it will additionally call NameOf
// for each graph.Value before passing it to a callback. Original value will be passed as well.
func (c *IterateChain) EachValuePair(qs QuadStore, fnc func(Value, quad.Value)) error {
if qs != nil {
c.qs = qs
}
if c.qs == nil {
return errNoQuadStore
}
// TODO(dennwc): batch NameOf?
return c.Each(func(v Value) {
if nv := c.qs.NameOf(v); nv != nil {
fnc(v, nv)
}
})
}
// AllValues is an analog of All, but it will additionally call NameOf
// for each graph.Value before returning the results slice.
func (c *IterateChain) AllValues(qs QuadStore) ([]quad.Value, error) {
var out []quad.Value
err := c.EachValue(qs, func(v quad.Value) {
out = append(out, v)
})
return out, err
}
// FirstValue is an analog of First, but it does lookup of a value in QuadStore.
func (c *IterateChain) FirstValue(qs QuadStore) (quad.Value, error) {
if qs != nil {
c.qs = qs
}
if c.qs == nil {
return nil, errNoQuadStore
}
v, err := c.First()
if err != nil || v == nil {
return nil, err
}
// TODO: return an error from NameOf once we have it exposed
return c.qs.NameOf(v), nil
}
// SendValues is an analog of Send, but it will additionally call NameOf
// for each graph.Value before sending it to a channel.
func (c *IterateChain) SendValues(qs QuadStore, out chan<- quad.Value) error {
if qs != nil {
c.qs = qs
}
if c.qs == nil {
return errNoQuadStore
}
c.start()
defer c.end()
done := c.ctx.Done()
send := func(v Value) error {
nv := c.qs.NameOf(c.it.Result())
if nv == nil {
return nil
}
select {
case <-done:
return c.ctx.Err()
case out <- c.qs.NameOf(c.it.Result()):
}
return nil
}
for c.next() {
if err := send(c.it.Result()); err != nil {
return err
}
for c.nextPath() {
if err := send(c.it.Result()); err != nil {
return err
}
}
}
return c.it.Err()
}
// TagValues is an analog of TagEach, but it will additionally call NameOf
// for each graph.Value before passing the map to a callback.
func (c *IterateChain) TagValues(qs QuadStore, fnc func(map[string]quad.Value)) error {
if qs != nil {
c.qs = qs
}
if c.qs == nil {
return errNoQuadStore
}
return c.TagEach(func(m map[string]Value) {
vm := make(map[string]quad.Value, len(m))
for k, v := range m {
vm[k] = c.qs.NameOf(v) // TODO(dennwc): batch NameOf?
}
fnc(vm)
})
}