-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
295 lines (234 loc) · 6.46 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
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
package sod
import (
"errors"
"fmt"
"math"
"strings"
)
var (
ErrUnknownOperator = errors.New("unknown logical operator")
ErrNoObjectFound = errors.New("no object found")
ErrUnexpectedNumberOfResults = errors.New("unexpected number of results")
)
func IsNoObjectFound(err error) bool {
return errors.Is(err, ErrNoObjectFound)
}
// Search helper structure to easily build search queries on objects
// and retrieve the results
type Search struct {
db *DB
object Object
fields []*indexedField
limit uint64
reverse bool
err error
}
func newSearch(db *DB, o Object, f []*indexedField, err error) *Search {
return &Search{db: db, object: o, fields: f, limit: math.MaxUint, err: err}
}
// ExpectsZeroOrN checks that the number of results is the one expected or zero.
// If not, next call to s.Err must return an error and any subsbequent
// attempt to collect results must fail
func (s *Search) ExpectsZeroOrN(n int) *Search {
found := len(s.fields)
if s.err != nil {
return s
}
if found != 0 && found != n {
s.err = fmt.Errorf("%w expected %d, found %d", ErrUnexpectedNumberOfResults, n, found)
}
return s
}
// Expects checks that the number of results is the one expected
// if not, next call to s.Err must return an error and any subsbequent
// attempt to collect results must fail
func (s *Search) Expects(n int) *Search {
found := len(s.fields)
if s.err != nil {
return s
}
if found != n {
s.err = fmt.Errorf("%w expected %d, found %d", ErrUnexpectedNumberOfResults, n, found)
}
return s
}
// Operation performs a new Search while ANDing or ORing the results
// operator must be in ["and", "&&", "or", "||"]
func (s *Search) Operation(operator, field, comparator string, value interface{}) *Search {
op := strings.ToLower(operator)
if s.err != nil {
return s
}
switch op {
case "and", "&&":
return s.And(field, comparator, value)
case "or", "||":
return s.Or(field, comparator, value)
default:
s.err = fmt.Errorf("%w %s", ErrUnknownOperator, op)
}
return s
}
// And performs a new Search while "ANDing" search results
func (s *Search) And(field, operator string, value interface{}) *Search {
if s.err != nil {
return s
}
return s.db.search(s.object, field, operator, value, s.fields)
}
// Or performs a new Search while "ORing" search results
func (s *Search) Or(field, operator string, value interface{}) *Search {
if s.err != nil {
return s
}
new := s.db.search(s.object, field, operator, value, nil)
marked := make(map[uint64]bool)
// we mark the fields of the new search
for _, f := range new.fields {
marked[f.ObjectId] = true
}
for _, f := range s.fields {
// we concat the searches while deduplicating
if _, ok := marked[f.ObjectId]; !ok {
new.fields = append(new.fields, f)
}
}
return new
}
// Len returns the number of data returned by the search
func (s *Search) Len() int {
return len(s.fields)
}
// Iterator returns an Iterator convenient to iterate over
// the objects resulting from the search
func (s *Search) Iterator() (it *iterator, err error) {
var sch *Schema
if s.err != nil {
return nil, s.err
}
if sch, err = s.db.schema(s.object); err != nil {
return
}
// create a new iterator
it = newIterator(s.db, s.object, make([]string, 0, len(s.fields)))
for _, f := range s.fields {
it.uuids = append(it.uuids, sch.ObjectIndex.ObjectIds[f.ObjectId])
}
return
}
// Delete deletes the objects found by the search
func (s *Search) Delete() (err error) {
var it *iterator
if it, err = s.Iterator(); err != nil {
return
}
return s.db.DeleteObjects(it)
}
// Reverse the order the results are collected by Collect function
func (s *Search) Reverse() *Search {
s.reverse = true
return s
}
// Limit the number of results collected by Collect function
func (s *Search) Limit(limit uint64) *Search {
s.limit = limit
return s
}
// One returns the first result found calling Collect function.
// If no Object is found, ErrNoObjectFound is returned
func (s *Search) One() (o Object, err error) {
s.db.RLock()
defer s.db.RUnlock()
return s.one()
}
// AssignUnique checks there is only one result in search and assign it
// to target. If search retuns more than one result, ErrUnexpectedNumberOfResults
// is returned
func (s *Search) AssignUnique(target interface{}) (err error) {
s.ExpectsZeroOrN(1)
return s.AssignOne(target)
}
// AssignOne returns the first result found calling Collect function
// and assign the Object found to target. Target must be a *sod.Object
// otherwise the function panics. If no Object is found, ErrNoObjectFound
// is returned
func (s *Search) AssignOne(target interface{}) (err error) {
s.db.RLock()
defer s.db.RUnlock()
var o Object
if o, err = s.one(); err != nil {
return err
}
AssignOne(o, target)
return
}
// Assign returns results found calling Collect function
// and assign them to target. Target must be a *[]sod.Object
// otherwise the function panics. If no Object is found, ErrNoObjectFound
// is returned
func (s *Search) Assign(target interface{}) (err error) {
s.db.RLock()
defer s.db.RUnlock()
var objs []Object
if objs, err = s.collect(); err != nil {
return err
}
return Assign(objs, target)
}
// Collect all the objects resulting from the search.
// If a search has been made on an indexed field, results
// will be in descending order by default. If you want to change
// result order, call Reverse before.
// NB: only search on indexed field(s) will be garanteed to be
// ordered according to the last field searched.
func (s *Search) Collect() (out []Object, err error) {
s.db.RLock()
defer s.db.RUnlock()
return s.collect()
}
// Err return any error encountered while searching
func (s *Search) Err() error {
return s.err
}
/************** Private Methods ******************/
func (s *Search) one() (o Object, err error) {
var sr []Object
if s.err != nil {
err = s.err
return
}
if s.Len() == 0 {
err = ErrNoObjectFound
return
}
// prevent collecting all results and using only one
s.limit = 1
if sr, err = s.collect(); err != nil {
return
}
o = sr[0]
return
}
func (s *Search) collect() (out []Object, err error) {
var it *iterator
var o Object
if s.err != nil {
return nil, s.err
}
if it, err = s.Iterator(); err != nil {
return
}
if s.reverse {
it.reversed()
}
out = make([]Object, 0, it.len())
for o, err = it.next(); err == nil && err != ErrEOI && s.limit > 0; o, err = it.next() {
out = append(out, o)
s.limit--
}
// normal end of iterator
if err == ErrEOI {
err = nil
}
return
}