-
Notifications
You must be signed in to change notification settings - Fork 5
/
basicquery.go
135 lines (103 loc) · 2.63 KB
/
basicquery.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
package tormenta
import (
"time"
"github.com/dgraph-io/badger"
"github.com/jpincas/gouuidv6"
)
type basicQuery struct {
// From and To dates
from, to gouuidv6.UUID
// Reverse?
reverse bool
// Name of the entity -> key root
keyRoot []byte
// Limit number of returned results
limit int
// Offet - start returning results N entities from the beginning
// offsetCounter used to track the offset
offset, offsetCounter int
// Ranges and comparision key
seekFrom, validTo, compareTo []byte
// Is already prepared?
prepared bool
}
func (b *basicQuery) prepare() {
b.setFromToIfEmpty()
b.setRanges()
// Mark as prepared
b.prepared = true
}
func (b *basicQuery) setFromToIfEmpty() {
t1 := time.Time{}
t2 := time.Now()
if b.from.IsNil() {
b.from = fromUUID(t1)
}
if b.to.IsNil() {
b.to = toUUID(t2)
}
}
func (b *basicQuery) setRanges() {
var seekFrom, validTo, compareTo []byte
// For reverse queries, flick-flack start/end and from/to
// to provide a standardised user API
if b.reverse {
tempTo := b.to
b.to = b.from
b.from = tempTo
}
seekFrom = newContentKey(b.keyRoot, b.from).bytes()
validTo = newContentKey(b.keyRoot).bytes()
compareTo = newContentKey(b.keyRoot, b.to).bytes()
// For reverse queries, append the byte 0xFF to get inclusive results
// See Badger issue: https://github.com/dgraph-io/badger/issues/347
if b.reverse {
seekFrom = append(seekFrom, 0xFF)
}
b.seekFrom = seekFrom
b.validTo = validTo
b.compareTo = compareTo
}
func (b *basicQuery) reset() {
b.offsetCounter = b.offset
}
func (b basicQuery) getIteratorOptions() badger.IteratorOptions {
options := badger.DefaultIteratorOptions
options.Reverse = b.reverse
options.PrefetchValues = false
return options
}
func (b basicQuery) endIteration(it *badger.Iterator, noIDsSoFar int) bool {
if it.ValidForPrefix(b.validTo) {
if b.isLimitMet(noIDsSoFar) || b.isEndOfRange(it) {
return false
}
return true
}
return false
}
func (b basicQuery) isEndOfRange(it *badger.Iterator) bool {
key := it.Item().Key()
return !b.to.IsNil() && compareKeyBytes(b.compareTo, key, b.reverse, false)
}
func (b basicQuery) isLimitMet(noIDsSoFar int) bool {
return b.limit > 0 && noIDsSoFar >= b.limit
}
func (b *basicQuery) queryIDs(txn *badger.Txn) (ids idList) {
if !b.prepared {
b.prepare()
}
b.reset()
it := txn.NewIterator(b.getIteratorOptions())
defer it.Close()
for it.Seek(b.seekFrom); b.endIteration(it, len(ids)); it.Next() {
// Skip the first N entities according to the specified offset
if b.offsetCounter > 0 {
b.offsetCounter--
continue
}
item := it.Item()
ids = append(ids, extractID(item.Key()))
}
return
}