/
bolt.go
774 lines (738 loc) · 23.2 KB
/
bolt.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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package bwbolt contains the implementation of the BadWolf driver using
// BoltDB.
package bwbolt
import (
"bytes"
"context"
"fmt"
"reflect"
"time"
"github.com/google/badwolf/storage"
"github.com/google/badwolf/triple"
"github.com/google/badwolf/triple/literal"
"github.com/google/badwolf/triple/node"
"github.com/google/badwolf/triple/predicate"
"go.etcd.io/bbolt"
)
// driver implements BadWolf storage.Store for a fully compliant driver.
type driver struct {
path string
db *bbolt.DB
lb literal.Builder
}
// graphBucket contains the name of the bucket containing the graphs.
const graphBucket = "GRAPHS"
// New create a new BadWolf driver using BoltDB as a storage driver.
func New(path string, lb literal.Builder, timeOut time.Duration, readOnly bool) (storage.Store, *bbolt.DB, error) {
// Bolt open options.
opts := &bbolt.Options{
Timeout: timeOut,
ReadOnly: readOnly,
}
// Open the DB.
db, err := bbolt.Open(path, 0600, opts)
if err != nil {
return nil, nil, fmt.Errorf("bwbolt driver initialization failure for file %q %v", path, err)
}
// Create the graph bucket if it does not exist.
db.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(graphBucket))
if err != nil {
return fmt.Errorf("driver initialization failed to create bucket %s for path %s with error %v", graphBucket, path, err)
}
return nil
})
// Return the initilized driver.
return &driver{
path: path,
db: db,
lb: lb,
}, db, nil
}
// Name returns the ID of the backend being used.
func (d *driver) Name(ctx context.Context) string {
return fmt.Sprintf("bwbolt/%s", d.path)
}
// Version returns the version of the driver implementation.
func (d *driver) Version(ctx context.Context) string {
return "HEAD"
}
// NewGraph creates a new graph. Creating an already existing graph
// should return an error.
func (d *driver) NewGraph(ctx context.Context, id string) (storage.Graph, error) {
err := d.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
_, err := b.CreateBucket([]byte(id))
if err != nil {
return fmt.Errorf("failed to create new graph %q with error %v", id, err)
}
return nil
})
if err != nil {
return nil, err
}
return &graph{
id: id,
db: d.db,
lb: d.lb,
}, nil
}
// Graph returns an existing graph if available. Getting a non existing
// graph should return an error.
func (d *driver) Graph(ctx context.Context, id string) (storage.Graph, error) {
err := d.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
g := b.Bucket([]byte(id))
if g == nil {
return fmt.Errorf("graph %q does not exist", id)
}
return nil
})
if err != nil {
return nil, err
}
return &graph{
id: id,
db: d.db,
lb: d.lb,
}, nil
}
// DeleteGraph deletes an existing graph. Deleting a non existing graph
// should return an error.
func (d *driver) DeleteGraph(ctx context.Context, id string) error {
return d.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
err := b.DeleteBucket([]byte(id))
if err != nil {
return err
}
return nil
})
}
// GraphNames returns the current available graph names in the store.
func (d *driver) GraphNames(ctx context.Context, names chan<- string) error {
defer close(names)
return d.db.View(func(tx *bbolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
c := b.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
names <- string(k)
}
return nil
})
}
// graph implements BadWolf storage.Graph for a fully compliant driver.
type graph struct {
id string
db *bbolt.DB
lb literal.Builder
}
// ID returns the id for this graph.
func (g *graph) ID(ctx context.Context) string {
return g.id
}
// indexUpdate contains the update to perform to a given index.
type indexUpdate struct {
idx string
key []byte
value []byte
}
const (
idxSPO = "SPO"
idxSOP = "SOP"
idxPOS = "POS"
idxOPS = "OPS"
)
// Given a triple, returns the updates to perform to the indices.
func (g *graph) tripleToIndexUpdate(t *triple.Triple) []*indexUpdate {
var updates []*indexUpdate
s, p, o := t.Subject().String(), t.Predicate().String(), t.Object().String()
tt := []byte(t.String())
updates = append(updates,
&indexUpdate{
idx: idxSPO,
key: []byte(s),
value: nil,
},
&indexUpdate{
idx: idxSPO,
key: []byte(s + "\t" + p),
value: nil,
},
&indexUpdate{
idx: idxSPO,
key: tt,
value: tt,
},
&indexUpdate{
idx: idxSOP,
key: []byte(s),
value: nil,
},
&indexUpdate{
idx: idxSOP,
key: []byte(s + "\t" + o),
value: nil,
},
&indexUpdate{
idx: idxSOP,
key: []byte(s + "\t" + o + "\t" + p),
value: tt,
},
&indexUpdate{
idx: idxPOS,
key: []byte(p),
value: nil,
},
&indexUpdate{
idx: idxPOS,
key: []byte(p + "\t" + o),
value: nil,
},
&indexUpdate{
idx: idxPOS,
key: []byte(p + "\t" + o + "\t" + s),
value: tt,
},
&indexUpdate{
idx: idxOPS,
key: []byte(o),
value: nil,
},
&indexUpdate{
idx: idxOPS,
key: []byte(o + "\t" + p),
value: nil,
},
&indexUpdate{
idx: idxOPS,
key: []byte(o + "\t" + p + "\t" + s),
value: tt,
})
return updates
}
// AddTriples adds the triples to the storage. Adding a triple that already
// exists should not fail. Efficiently resolving all the operations below
// require proper indexing. This driver provides the follow indices:
//
// * SPO: Textual representation of the triple to allow range queries.
// * SOP: Conbination to allow efficient query of S + P queries.
// * POS: Conbination to allow efficient query of P + O queries.
// * OPS: Conbination to allow efficient query of O + P queries.
//
// The GUID index containst the fully serialized triple. The other indices
// only contains as a value the GUID of the triple.
func (g *graph) AddTriples(ctx context.Context, ts []*triple.Triple) error {
return g.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
for _, t := range ts {
for _, iu := range g.tripleToIndexUpdate(t) {
b, err := gb.CreateBucketIfNotExists([]byte(iu.idx))
if err != nil {
return fmt.Errorf("failed to create bucket %s for graph %s with error %v", iu.idx, g.id, err)
}
err = b.Put(iu.key, iu.value)
if err != nil {
return err
}
}
}
return nil
})
}
// RemoveTriples removes the trilpes from the storage. Removing triples that
// are not present on the store should not fail.
func (g *graph) RemoveTriples(ctx context.Context, ts []*triple.Triple) error {
return g.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
for _, t := range ts {
for _, iu := range g.tripleToIndexUpdate(t) {
b, err := gb.CreateBucketIfNotExists([]byte(iu.idx))
if err != nil {
return fmt.Errorf("failed to create bucket %s for graph %s with error %v", iu.idx, g.id, err)
}
b.Delete(iu.key)
if err != nil {
if b.Get(iu.key) != nil {
return err
}
}
}
}
return nil
})
}
// shouldAccept returns is the triple should be accepted
func (g *graph) shouldAccept(cnt int, t *triple.Triple, lo *storage.LookupOptions) bool {
if lo.MaxElements > 0 && lo.MaxElements < cnt {
return false
}
p := t.Predicate()
if p.Type() == predicate.Temporal {
if lo.LowerAnchor != nil {
ta, err := t.Predicate().TimeAnchor()
if err != nil {
panic(fmt.Errorf("should have never failed to retrieve time anchor from triple %s with error %v", t.String(), err))
}
if lo.LowerAnchor.After(*ta) {
return false
}
}
if lo.UpperAnchor != nil {
ta, err := t.Predicate().TimeAnchor()
if err != nil {
panic(fmt.Errorf("should have never failed to retrieve time anchor from triple %s with error %v", t.String(), err))
}
if lo.UpperAnchor.Before(*ta) {
return false
}
}
}
return true
}
// Objects pushes to the provided channel the objects for the given object and
// predicate. The function does not return immediately but spawns a goroutine
// to satisfy elements in the channel.
func (g *graph) Objects(ctx context.Context, s *node.Node, p *predicate.Predicate, lo *storage.LookupOptions, objs chan<- *triple.Object) error {
defer close(objs)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
spo := gb.Bucket([]byte(idxSPO))
if spo == nil {
return fmt.Errorf("failed to load bucket SPO for graph %s", g.id)
}
cnt, c, prefix := 0, spo.Cursor(), []byte(s.String()+"\t"+p.String())
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index SPO failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
objs <- t.Object()
cnt++
}
}
return nil
})
}
// Subjects pushes to the provided channel the subjects for the give predicate
// and object. The function does not return immediately but spawns a
// goroutine to satisfy elements in the channel.
func (g *graph) Subjects(ctx context.Context, p *predicate.Predicate, o *triple.Object, lo *storage.LookupOptions, subs chan<- *node.Node) error {
defer close(subs)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
pos := gb.Bucket([]byte(idxPOS))
if pos == nil {
return fmt.Errorf("failed to load bucket POS for graph %s", g.id)
}
cnt, c, prefix := 0, pos.Cursor(), []byte(p.String()+"\t"+o.String())
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index POS failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
subs <- t.Subject()
cnt++
}
}
return nil
})
}
// PredicatesForSubject pushes to the provided channel all the predicates
// known for the given subject. The function does not return immediately but
// spawns a goroutine to satisfy elements in the channel.
func (g *graph) PredicatesForSubject(ctx context.Context, s *node.Node, lo *storage.LookupOptions, prds chan<- *predicate.Predicate) error {
defer close(prds)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
spo := gb.Bucket([]byte(idxSPO))
if spo == nil {
return fmt.Errorf("failed to load bucket SPO for graph %s", g.id)
}
cnt, c, prefix := 0, spo.Cursor(), []byte(s.String())
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index SPO failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
prds <- t.Predicate()
cnt++
}
}
return nil
})
}
// PredicatesForObject pushes to the provided channel all the predicates known
// for the given object. The function returns immediately and spawns a go
// routine to satisfy elements in the channel.
func (g *graph) PredicatesForObject(ctx context.Context, o *triple.Object, lo *storage.LookupOptions, prds chan<- *predicate.Predicate) error {
defer close(prds)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
ops := gb.Bucket([]byte(idxOPS))
if ops == nil {
return fmt.Errorf("failed to load bucket OPS for graph %s", g.id)
}
cnt, c, prefix := 0, ops.Cursor(), []byte(o.String())
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index OPS failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
prds <- t.Predicate()
cnt++
}
}
return nil
})
}
// PredicatesForSubjectAndObject pushes to the provided channel all predicates
// available for the given subject and object. The function does not return
// immediately but spawns a goroutine to satisfy elements in the channel.
func (g *graph) PredicatesForSubjectAndObject(ctx context.Context, s *node.Node, o *triple.Object, lo *storage.LookupOptions, prds chan<- *predicate.Predicate) error {
defer close(prds)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
sop := gb.Bucket([]byte(idxSOP))
if sop == nil {
return fmt.Errorf("failed to load bucket SOP for graph %s", g.id)
}
cnt, c, prefix := 0, sop.Cursor(), []byte(s.String()+"\t"+o.String())
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index SOP failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
prds <- t.Predicate()
cnt++
}
}
return nil
})
}
// TriplesForSubject pushes to the provided channel all triples available for
// the given subect. The function does not return immediately but spawns a
// goroutine to satisfy elements in the channel.
func (g *graph) TriplesForSubject(ctx context.Context, s *node.Node, lo *storage.LookupOptions, trpls chan<- *triple.Triple) error {
defer close(trpls)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
spo := gb.Bucket([]byte(idxSPO))
if spo == nil {
return fmt.Errorf("failed to load bucket SPO for graph %s", g.id)
}
cnt, c, prefix := 0, spo.Cursor(), []byte(s.String())
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index SPO failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
trpls <- t
cnt++
}
}
return nil
})
}
// TriplesForPredicate pushes to the provided channel all triples available
// for the given predicate.The function does not return immediately but spawns
// a goroutine to satisfy elements in the channel.
func (g *graph) TriplesForPredicate(ctx context.Context, p *predicate.Predicate, lo *storage.LookupOptions, trpls chan<- *triple.Triple) error {
defer close(trpls)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
pos := gb.Bucket([]byte(idxPOS))
if pos == nil {
return fmt.Errorf("failed to load bucket POS for graph %s", g.id)
}
cnt, c, prefix := 0, pos.Cursor(), []byte(p.String())
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index POS failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
trpls <- t
cnt++
}
}
return nil
})
}
// TriplesForObject pushes to the provided channel all triples available for
// the given object. The function does not return immediately but spawns a
// goroutine to satisfy elements in the channel.
func (g *graph) TriplesForObject(ctx context.Context, o *triple.Object, lo *storage.LookupOptions, trpls chan<- *triple.Triple) error {
defer close(trpls)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
pos := gb.Bucket([]byte(idxOPS))
if pos == nil {
return fmt.Errorf("failed to load bucket POS for graph %s", g.id)
}
cnt, c, prefix := 0, pos.Cursor(), []byte(o.String())
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index POS failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
trpls <- t
cnt++
}
}
return nil
})
}
// TriplesForSubjectAndPredicate pushes to the provided channel all triples
// available for the given subject and predicate. The function does not return
// immediately but spawns a goroutine to satisfy elements in the channel.
func (g *graph) TriplesForSubjectAndPredicate(ctx context.Context, s *node.Node, p *predicate.Predicate, lo *storage.LookupOptions, trpls chan<- *triple.Triple) error {
defer close(trpls)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
spo := gb.Bucket([]byte(idxSPO))
if spo == nil {
return fmt.Errorf("failed to load bucket SPO for graph %s", g.id)
}
cnt, c, prefix := 0, spo.Cursor(), []byte(s.String()+"\t"+p.String())
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index SPO failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
trpls <- t
cnt++
}
}
return nil
})
}
// TriplesForPredicateAndObject pushes to the provided channel all triples
// available for the given predicate and object. The function does not return
// immediately but spawns a goroutine to satisfy elements in the channel.
func (g *graph) TriplesForPredicateAndObject(ctx context.Context, p *predicate.Predicate, o *triple.Object, lo *storage.LookupOptions, trpls chan<- *triple.Triple) error {
defer close(trpls)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
pos := gb.Bucket([]byte(idxPOS))
if pos == nil {
return fmt.Errorf("failed to load bucket SPO for graph %s", g.id)
}
cnt, c, prefix := 0, pos.Cursor(), []byte(p.String()+"\t"+o.String())
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index POS failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
trpls <- t
cnt++
}
}
return nil
})
}
// Exist checks if the provided triple exists on the store.
func (g *graph) Exist(ctx context.Context, t *triple.Triple) (bool, error) {
res := false
err := g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
spo := gb.Bucket([]byte(idxSPO))
if spo == nil {
return fmt.Errorf("failed to load bucket SPO for graph %s", g.id)
}
if spo.Get([]byte(t.String())) != nil {
res = true
}
return nil
})
return res, err
}
// Triples pushes to the provided channel all available triples in the graph.
// The function does not return immediately but spawns a goroutine to satisfy
// elements in the channel.
func (g *graph) Triples(ctx context.Context, lo *storage.LookupOptions, trpls chan<- *triple.Triple) error {
defer close(trpls)
return g.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(graphBucket))
if b == nil {
return fmt.Errorf("inconsistent driver failed to retrieve top bucket %q", graphBucket)
}
gb := b.Bucket([]byte(g.id))
if gb == nil {
return fmt.Errorf("graph %q does not exist", g.id)
}
spo := gb.Bucket([]byte(idxSPO))
if spo == nil {
return fmt.Errorf("failed to load bucket SPO for graph %s", g.id)
}
cnt, c := 0, spo.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
if reflect.DeepEqual(v, []byte{}) {
continue
}
t, err := triple.Parse(string(v), g.lb)
if err != nil {
return fmt.Errorf("corrupt index SPO failing to parse %q with error %v", string(v), err)
}
if g.shouldAccept(cnt, t, lo) {
trpls <- t
cnt++
}
}
return nil
})
}