forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_comparison.go
293 lines (260 loc) · 6.51 KB
/
value_comparison.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
// Copyright 2014 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 iterator
// "Value Comparison" is a unary operator -- a filter across the values in the
// relevant subiterator.
//
// This is hugely useful for things like label, but value ranges in general
// come up from time to time. At *worst* we're as big as our underlying iterator.
// At best, we're the null iterator.
//
// This is ripe for backend-side optimization. If you can run a value iterator,
// from a sorted set -- some sort of value index, then go for it.
//
// In MQL terms, this is the [{"age>=": 21}] concept.
import (
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/quad"
"time"
)
type Operator int
const (
CompareLT Operator = iota
CompareLTE
CompareGT
CompareGTE
// Why no Equals? Because that's usually an AndIterator.
)
type Comparison struct {
uid uint64
tags graph.Tagger
subIt graph.Iterator
op Operator
val quad.Value
qs graph.QuadStore
result graph.Value
err error
}
func NewComparison(sub graph.Iterator, op Operator, val quad.Value, qs graph.QuadStore) *Comparison {
return &Comparison{
uid: NextUID(),
subIt: sub,
op: op,
val: val,
qs: qs,
}
}
func (it *Comparison) UID() uint64 {
return it.uid
}
func (it *Comparison) Value() quad.Value { return it.val }
func (it *Comparison) Operator() Operator { return it.op }
// Here's the non-boilerplate part of the ValueComparison iterator. Given a value
// and our operator, determine whether or not we meet the requirement.
func (it *Comparison) doComparison(val graph.Value) bool {
qval := it.qs.NameOf(val)
switch cVal := it.val.(type) {
case quad.Raw:
return RunStrOp(quad.StringOf(qval), it.op, string(cVal))
case quad.Int:
if cVal2, ok := qval.(quad.Int); ok {
return RunIntOp(cVal2, it.op, cVal)
}
return false
case quad.Float:
if cVal2, ok := qval.(quad.Float); ok {
return RunFloatOp(cVal2, it.op, cVal)
}
return false
case quad.String:
if cVal2, ok := qval.(quad.String); ok {
return RunStrOp(string(cVal2), it.op, string(cVal))
}
return false
case quad.BNode:
if cVal2, ok := qval.(quad.BNode); ok {
return RunStrOp(string(cVal2), it.op, string(cVal))
}
return false
case quad.IRI:
if cVal2, ok := qval.(quad.IRI); ok {
return RunStrOp(string(cVal2), it.op, string(cVal))
}
return false
case quad.Time:
if cVal2, ok := qval.(quad.Time); ok {
return RunTimeOp(time.Time(cVal2), it.op, time.Time(cVal))
}
return false
default:
return RunStrOp(quad.StringOf(qval), it.op, quad.StringOf(it.val))
}
}
func (it *Comparison) Close() error {
return it.subIt.Close()
}
func RunIntOp(a quad.Int, op Operator, b quad.Int) bool {
switch op {
case CompareLT:
return a < b
case CompareLTE:
return a <= b
case CompareGT:
return a > b
case CompareGTE:
return a >= b
default:
panic("Unknown operator type")
}
}
func RunFloatOp(a quad.Float, op Operator, b quad.Float) bool {
switch op {
case CompareLT:
return a < b
case CompareLTE:
return a <= b
case CompareGT:
return a > b
case CompareGTE:
return a >= b
default:
panic("Unknown operator type")
}
}
func RunStrOp(a string, op Operator, b string) bool {
switch op {
case CompareLT:
return a < b
case CompareLTE:
return a <= b
case CompareGT:
return a > b
case CompareGTE:
return a >= b
default:
panic("Unknown operator type")
}
}
func RunTimeOp(a time.Time, op Operator, b time.Time) bool {
switch op {
case CompareLT:
return a.Before(b)
case CompareLTE:
return !a.After(b)
case CompareGT:
return a.After(b)
case CompareGTE:
return !a.Before(b)
default:
panic("Unknown operator type")
}
}
func (it *Comparison) Reset() {
it.subIt.Reset()
it.err = nil
it.result = nil
}
func (it *Comparison) Tagger() *graph.Tagger {
return &it.tags
}
func (it *Comparison) Clone() graph.Iterator {
out := NewComparison(it.subIt.Clone(), it.op, it.val, it.qs)
out.tags.CopyFrom(it)
return out
}
func (it *Comparison) Next() bool {
for it.subIt.Next() {
val := it.subIt.Result()
if it.doComparison(val) {
it.result = val
return true
}
}
it.err = it.subIt.Err()
return false
}
func (it *Comparison) Err() error {
return it.err
}
func (it *Comparison) Result() graph.Value {
return it.result
}
func (it *Comparison) NextPath() bool {
for {
hasNext := it.subIt.NextPath()
if !hasNext {
it.err = it.subIt.Err()
return false
}
if it.doComparison(it.subIt.Result()) {
break
}
}
it.result = it.subIt.Result()
return true
}
func (it *Comparison) SubIterators() []graph.Iterator {
return []graph.Iterator{it.subIt}
}
func (it *Comparison) Contains(val graph.Value) bool {
if !it.doComparison(val) {
return false
}
ok := it.subIt.Contains(val)
if !ok {
it.err = it.subIt.Err()
}
return ok
}
// If we failed the check, then the subiterator should not contribute to the result
// set. Otherwise, go ahead and tag it.
func (it *Comparison) TagResults(dst map[string]graph.Value) {
for _, tag := range it.tags.Tags() {
dst[tag] = it.Result()
}
for tag, value := range it.tags.Fixed() {
dst[tag] = value
}
it.subIt.TagResults(dst)
}
// Registers the value-comparison iterator.
func (it *Comparison) Type() graph.Type { return graph.Comparison }
func (it *Comparison) Describe() graph.Description {
primary := it.subIt.Describe()
return graph.Description{
UID: it.UID(),
Type: it.Type(),
Iterator: &primary,
}
}
// There's nothing to optimize, locally, for a value-comparison iterator.
// Replace the underlying iterator if need be.
// potentially replace it.
func (it *Comparison) Optimize() (graph.Iterator, bool) {
newSub, changed := it.subIt.Optimize()
if changed {
it.subIt.Close()
it.subIt = newSub
}
return it, false
}
// We're only as expensive as our subiterator.
// Again, optimized value comparison iterators should do better.
func (it *Comparison) Stats() graph.IteratorStats {
return it.subIt.Stats()
}
func (it *Comparison) Size() (int64, bool) {
return 0, false
}
var _ graph.Iterator = &Comparison{}