-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
262 lines (215 loc) · 5.85 KB
/
search.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
/*
* go-leia
* Copyright (C) 2021 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package leia
import (
"bytes"
"errors"
)
// ErrNoQuery is returned when an empty query is given
var ErrNoQuery = errors.New("no query given")
type jsonPath string
// NewJSONPath creates a JSON path query: "person.path" or "person.children.#.path"
// # is used to traverse arrays
func NewJSONPath(path string) QueryPath {
return jsonPath(path)
}
func (q jsonPath) Equals(other QueryPath) bool {
return q == other
}
// QueryPath is the interface for the query path given in queries
type QueryPath interface {
Equals(other QueryPath) bool
}
// iriPath represents a nested structure (or graph path) using the fully qualified IRIs
type iriPath struct {
// iris represent the nested structure from highest (index 0) to lowest
iris []string
}
// NewIRIPath creates a QueryPath of JSON-LD terms
func NewIRIPath(IRIs ...string) QueryPath {
return iriPath{iris: IRIs}
}
// IsEmpty returns true of no terms are in the list
func (tp iriPath) IsEmpty() bool {
return len(tp.iris) == 0
}
// Head returns the first IRI of the list or ""
func (tp iriPath) Head() string {
if len(tp.iris) == 0 {
return ""
}
return tp.iris[0]
}
// Tail returns the last terms of the list or an empty TermPath
func (tp iriPath) Tail() iriPath {
if len(tp.iris) <= 1 {
return iriPath{}
}
return iriPath{iris: tp.iris[1:]}
}
// Equals returns true if two TermPaths have the exact same Terms in the exact same order
func (tp iriPath) Equals(other QueryPath) bool {
otherIRIPath, ok := other.(iriPath)
if !ok {
return false
}
if len(tp.iris) != len(otherIRIPath.iris) {
return false
}
for i, iri := range tp.iris {
if iri != otherIRIPath.iris[i] {
return false
}
}
return true
}
type QueryPart interface {
QueryPathComparable
// Seek returns the key for cursor.Seek
Seek() Scalar
// Condition returns true if given key falls within this condition.
// The optional transform fn is applied to this query part before evaluation is done.
Condition(key Key, transform Transform) bool
}
// New creates a new query with an initial query part. Both begin and end are inclusive for the conditional check.
func New(part QueryPart) Query {
return Query{
parts: []QueryPart{part},
}
}
// Eq creates a query part for an exact match
func Eq(queryPath QueryPath, value Scalar) QueryPart {
return eqPart{
queryPath: queryPath,
value: value,
}
}
// Range creates a query part for a range query
func Range(queryPath QueryPath, begin Scalar, end Scalar) QueryPart {
return rangePart{
queryPath: queryPath,
begin: begin,
end: end,
}
}
// NotNil creates a query part where the value must exist.
// This is done by finding results between byte 0x0 and 0xff
func NotNil(queryPath QueryPath) QueryPart {
return notNilPart{
queryPath: queryPath,
}
}
// Prefix creates a query part for a partial match
// The beginning of a value is matched against the query.
func Prefix(queryPath QueryPath, value Scalar) QueryPart {
return prefixPart{
queryPath: queryPath,
value: value,
}
}
// Query represents a query with multiple arguments
type Query struct {
parts []QueryPart
}
func (q Query) And(part QueryPart) Query {
q.parts = append(q.parts, part)
return q
}
type eqPart struct {
queryPath QueryPath
value Scalar
}
func (e eqPart) Equals(other QueryPathComparable) bool {
return e.queryPath.Equals(other.QueryPath())
}
func (e eqPart) QueryPath() QueryPath {
return e.queryPath
}
func (e eqPart) Seek() Scalar {
return e.value
}
func (e eqPart) Condition(key Key, transform Transform) bool {
if transform != nil {
transformed := transform(e.value)
return bytes.Compare(key, transformed.Bytes()) == 0
}
return bytes.Compare(key, e.value.Bytes()) == 0
}
type rangePart struct {
queryPath QueryPath
begin Scalar
end Scalar
}
func (r rangePart) Equals(other QueryPathComparable) bool {
return r.queryPath.Equals(other.QueryPath())
}
func (r rangePart) QueryPath() QueryPath {
return r.queryPath
}
func (r rangePart) Seek() Scalar {
return r.begin
}
func (r rangePart) Condition(key Key, transform Transform) bool {
bTransformed := r.begin
eTransformed := r.end
if transform != nil {
bTransformed = transform(r.begin)
eTransformed = transform(r.end)
}
// the key becomes before the start
if bytes.Compare(key, bTransformed.Bytes()) < 0 {
return false
}
return bytes.Compare(key, eTransformed.Bytes()) <= 0
}
type prefixPart struct {
queryPath QueryPath
value Scalar
}
func (p prefixPart) Equals(other QueryPathComparable) bool {
return p.queryPath.Equals(other.QueryPath())
}
func (p prefixPart) QueryPath() QueryPath {
return p.queryPath
}
func (p prefixPart) Seek() Scalar {
return p.value
}
func (p prefixPart) Condition(key Key, transform Transform) bool {
transformed := p.value
if transform != nil {
transformed = transform(p.value)
}
return bytes.HasPrefix(key, transformed.Bytes())
}
type notNilPart struct {
queryPath QueryPath
}
func (p notNilPart) Equals(other QueryPathComparable) bool {
return p.queryPath.Equals(other.QueryPath())
}
func (p notNilPart) QueryPath() QueryPath {
return p.queryPath
}
func (p notNilPart) Seek() Scalar {
return bytesScalar{}
}
func (p notNilPart) Condition(key Key, _ Transform) bool {
return len(key) > 0
}