-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
quad_iterator.go
253 lines (227 loc) · 5.04 KB
/
quad_iterator.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
// Copyright 2016 The Cayley Authors. All rights reserved.
//
// 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 kv
import (
"context"
"fmt"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/iterator"
"github.com/cayleygraph/cayley/graph/proto"
)
type QuadIterator struct {
uid uint64
tags graph.Tagger
qs *QuadStore
ind QuadIndex
horizon int64
vals []uint64
size int64
tx BucketTx
b Bucket
it KVIterator
done bool
err error
off int
ids []uint64
buf []*proto.Primitive
prim *proto.Primitive
}
var _ graph.Iterator = &QuadIterator{}
func NewQuadIterator(qs *QuadStore, ind QuadIndex, vals []uint64) *QuadIterator {
return &QuadIterator{
qs: qs,
ind: ind,
horizon: qs.horizon(context.TODO()),
uid: iterator.NextUID(),
vals: vals,
size: -1,
}
}
func (it *QuadIterator) UID() uint64 {
return it.uid
}
func (it *QuadIterator) Reset() {
it.off = 0
it.ids = nil
it.buf = nil
it.done = false
if it.it != nil {
it.it.Close()
it.it = it.b.Scan(it.ind.Key(it.vals))
}
}
func (it *QuadIterator) Tagger() *graph.Tagger {
return &it.tags
}
func (it *QuadIterator) TagResults(dst map[string]graph.Value) {
it.tags.TagResult(dst, it.Result())
}
func (it *QuadIterator) Clone() graph.Iterator {
out := NewQuadIterator(it.qs, it.ind, it.vals)
out.tags.CopyFrom(it)
out.ids = it.ids
out.horizon = it.horizon
return out
}
func (it *QuadIterator) Close() error {
if it.it != nil {
if err := it.it.Close(); err != nil && it.err == nil {
it.err = err
}
if err := it.tx.Rollback(); err != nil && it.err == nil {
it.err = err
}
it.it = nil
it.tx = nil
it.b = nil
}
return it.err
}
func (it *QuadIterator) Err() error {
return it.err
}
func (it *QuadIterator) Result() graph.Value {
if it.off < 0 || it.prim == nil {
return nil
}
return it.prim
}
func (it *QuadIterator) ensureTx() bool {
if it.tx != nil {
return true
}
it.tx, it.err = it.qs.db.Tx(false)
if it.err != nil {
return false
}
it.b = it.tx.Bucket(it.ind.Bucket())
return true
}
func (it *QuadIterator) Next(ctx context.Context) bool {
it.prim = nil
if it.err != nil || it.done {
return false
}
if it.it == nil {
if !it.ensureTx() {
return false
}
it.it = it.b.Scan(it.ind.Key(it.vals))
if err := it.Err(); err != nil {
it.err = err
return false
}
}
for {
if len(it.buf) == 0 {
for len(it.ids[it.off:]) == 0 {
it.off = 0
it.ids = nil
it.buf = nil
if !it.it.Next(ctx) {
it.Close()
it.done = true
return false
}
it.ids, it.err = decodeIndex(it.it.Val())
if it.err != nil {
return false
}
}
ids := it.ids[it.off:]
if len(ids) > nextBatch {
ids = ids[:nextBatch]
}
it.buf, it.err = it.qs.getPrimitivesFromLog(ctx, it.tx, ids)
if it.err != nil {
return false
}
} else {
it.buf, it.off = it.buf[1:], it.off+1
}
for ; len(it.buf) > 0; it.buf, it.off = it.buf[1:], it.off+1 {
p := it.buf[0]
if p == nil || p.Deleted {
continue
}
it.prim = p
return true
}
}
}
func (it *QuadIterator) NextPath(ctx context.Context) bool {
return false
}
func (it *QuadIterator) Contains(ctx context.Context, v graph.Value) bool {
it.prim = nil
p, ok := v.(*proto.Primitive)
if !ok {
return false
}
for i, v := range it.vals {
if p.GetDirection(it.ind.Dirs[i]) != v {
return false
}
}
return true
}
func (it *QuadIterator) SubIterators() []graph.Iterator {
return nil
}
func (it *QuadIterator) Size() (int64, bool) {
if it.err != nil {
return 0, false
} else if it.size >= 0 {
return it.size, true
}
ctx := context.TODO()
if len(it.ind.Dirs) == len(it.vals) {
var ids []uint64
it.err = View(it.qs.db, func(tx BucketTx) error {
b := tx.Bucket(it.ind.Bucket())
vals, err := b.Get(ctx, [][]byte{it.ind.Key(it.vals)})
if err != nil {
return err
}
ids, err = decodeIndex(vals[0])
if err != nil {
return err
}
return nil
})
if it.err != nil {
return 0, false
}
it.size = int64(len(ids))
return it.size, true
}
return 1 + it.qs.Size()/2, false
}
func (it *QuadIterator) String() string {
return fmt.Sprintf("KVQuads(%v)", it.ind)
}
func (it *QuadIterator) Type() graph.Type { return "kv_quad" }
func (it *QuadIterator) Sorted() bool { return true }
func (it *QuadIterator) Optimize() (graph.Iterator, bool) {
return it, false
}
func (it *QuadIterator) Stats() graph.IteratorStats {
s, exact := it.Size()
return graph.IteratorStats{
ContainsCost: 1,
NextCost: 2,
Size: s,
ExactSize: exact,
}
}