forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterators.go
141 lines (128 loc) · 3.44 KB
/
iterators.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
package kv
import (
"context"
"fmt"
"github.com/aperturerobotics/cayley/kv"
"github.com/aperturerobotics/cayley/graph"
"github.com/aperturerobotics/cayley/graph/iterator"
"github.com/aperturerobotics/cayley/graph/refs"
"github.com/aperturerobotics/cayley/quad"
"github.com/aperturerobotics/cayley/query/shape"
)
func (qs *QuadStore) NodesAllIterator(ctx context.Context) iterator.Shape {
return qs.newAllIterator(true, nil)
}
func (qs *QuadStore) QuadsAllIterator(ctx context.Context) iterator.Shape {
return qs.newAllIterator(false, nil)
}
func (qs *QuadStore) indexSize(ctx context.Context, ind QuadIndex, vals []uint64) (refs.Size, error) {
var sz int64
err := kv.View(ctx, qs.db, func(tx kv.Tx) error {
val, err := tx.Get(ctx, ind.Key(vals))
if err != nil {
return err
}
sz, err = countIndex(val)
if err != nil {
return err
}
return nil
})
if err != nil {
return refs.Size{}, err
}
if len(ind.Dirs) == len(vals) {
return refs.Size{
Value: sz,
Exact: true,
}, nil
}
return refs.Size{
Value: 1 + sz/2,
Exact: false,
}, nil
}
func (qs *QuadStore) QuadIteratorSize(ctx context.Context, d quad.Direction, v graph.Ref) (refs.Size, error) {
vi, ok := v.(Int64Value)
if !ok {
return refs.Size{Value: 0, Exact: true}, nil
}
qs.indexes.RLock()
all := qs.indexes.all
qs.indexes.RUnlock()
for _, ind := range all {
if len(ind.Dirs) == 1 && ind.Dirs[0] == d {
return qs.indexSize(ctx, ind, []uint64{uint64(vi)})
}
}
st, err := qs.Stats(ctx, false)
if err != nil {
return refs.Size{}, err
}
return refs.Size{
Value: 1 + st.Quads.Value/2,
Exact: false,
}, nil
}
func (qs *QuadStore) QuadIterator(ctx context.Context, dir quad.Direction, v graph.Ref) iterator.Shape {
if v == nil {
return iterator.NewNull()
}
vi, ok := v.(Int64Value)
if !ok {
return iterator.NewError(fmt.Errorf("unexpected node type: %T", v))
}
// Find the best index for this direction.
if ind := qs.bestIndexes([]quad.Direction{dir}); len(ind) == 1 {
// this will scan the prefix automatically
return qs.newQuadIterator(ind[0], []uint64{uint64(vi)})
}
// Fallback: iterate all quads and check the corresponding direction.
return qs.newAllIterator(false, &constraint{
dir: dir,
val: vi,
})
}
func (qs *QuadStore) OptimizeShape(ctx context.Context, s shape.Shape) (shape.Shape, bool, error) {
switch s := s.(type) {
case shape.QuadsAction:
return qs.optimizeQuadsAction(s)
}
return s, false, nil
}
func (qs *QuadStore) optimizeQuadsAction(s shape.QuadsAction) (shape.Shape, bool, error) {
if len(s.Filter) == 0 {
return s, false, nil
}
dirs := make([]quad.Direction, 0, len(s.Filter))
for d := range s.Filter {
dirs = append(dirs, d)
}
ind := qs.bestIndexes(dirs)
if len(ind) != 1 {
return s, false, nil // TODO(dennwc): allow intersecting indexes
}
quads := IndexScan{Index: ind[0]}
for _, d := range ind[0].Dirs {
v, ok := s.Filter[d].(Int64Value)
if !ok {
return s, false, nil
}
quads.Values = append(quads.Values, uint64(v))
}
return s.SimplifyFrom(quads), true, nil
}
type IndexScan struct {
Index QuadIndex
Values []uint64
}
func (s IndexScan) BuildIterator(ctx context.Context, qs graph.QuadStore) iterator.Shape {
kqs, ok := qs.(*QuadStore)
if !ok {
return iterator.NewError(fmt.Errorf("expected KV quadstore, got: %T", qs))
}
return kqs.newQuadIterator(s.Index, s.Values)
}
func (s IndexScan) Optimize(ctx context.Context, r shape.Optimizer) (shape.Shape, bool, error) {
return s, false, nil
}