/
index.go
257 lines (191 loc) · 5.1 KB
/
index.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
package updog
import (
"bytes"
"encoding/binary"
"encoding/gob"
"sort"
"sync"
"github.com/RoaringBitmap/roaring"
"github.com/akrennmair/updog/internal/openfile"
"go.etcd.io/bbolt"
)
// OpenIndex opens an index file previously created using the IndexWriter.
func OpenIndex(file string, opts ...IndexOption) (*Index, error) {
db, err := bbolt.Open(file, 0644, &bbolt.Options{OpenFile: openfile.OpenFile(openfile.Options{FailIfFileDoesntExist: true})})
if err != nil {
return nil, err
}
return OpenIndexFromBoltDatabase(db, opts...)
}
// OpenIndexFromBoltDatabase opens an index directly from a bbolt database. For this
// to work correctly, the index should be created using the IndexWriter.
func OpenIndexFromBoltDatabase(db *bbolt.DB, opts ...IndexOption) (*Index, error) {
idx := &Index{}
idx.db = db
err := db.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte("data"))
schemaItem := bucket.Get(keySchema)
var sch schema
if err := gob.NewDecoder(bytes.NewReader(schemaItem)).Decode(&sch); err != nil {
return err
}
idx.schema = &sch
rowsItem := bucket.Get(keyNextRowID)
idx.nextRowID = binary.BigEndian.Uint32(rowsItem)
return nil
})
if err != nil {
db.Close()
return nil, err
}
idx.cache = &nullCache{}
idx.metrics = &IndexMetrics{}
for _, opt := range opts {
if err := opt(idx); err != nil {
return nil, err
}
}
if idx.values == nil {
idx.values = newOnDemandColGetter(idx.db)
}
return idx, nil
}
// Index represents an index to run queries on. You have to create objects using the OpenIndex
// or OpenIndexFromBoltDatabase constructor functions.
type Index struct {
mtx sync.RWMutex
schema *schema
nextRowID uint32
db *bbolt.DB
values colGetter
cache Cache
metrics *IndexMetrics
}
func (idx *Index) GetSchema() *Schema {
idx.mtx.RLock()
defer idx.mtx.RUnlock()
var cols []SchemaColumn
for colName, col := range idx.schema.Columns {
schCol := SchemaColumn{
Name: colName,
}
for v := range col.Values {
schCol.Values = append(schCol.Values, SchemaColumnValue{Value: v})
}
sort.Slice(schCol.Values, func(i, j int) bool { return schCol.Values[i].Value < schCol.Values[j].Value })
cols = append(cols, schCol)
}
sort.Slice(cols, func(i, j int) bool { return cols[i].Name < cols[j].Name })
return &Schema{
Columns: cols,
}
}
type Schema struct {
Columns []SchemaColumn
}
type SchemaColumn struct {
Name string
Values []SchemaColumnValue
}
type SchemaColumnValue struct {
Value string
}
type colGetter interface {
GetCol(key uint64) (*roaring.Bitmap, error)
}
type IndexOption func(idx *Index) error
// WithCache is an option for OpenIndex and OpenIndexFromBoltDatabase to set
// a cache for caching queries, including partial queries.
func WithCache(cache Cache) IndexOption {
return func(idx *Index) error {
idx.cache = cache
return nil
}
}
// Close closes the index, including the associated bbolt database.
func (idx *Index) Close() error {
if idx.db == nil {
return nil
}
err := idx.db.Close()
idx.db = nil
return err
}
func newOnDemandColGetter(db *bbolt.DB) colGetter {
return &onDemandColGetter{db: db}
}
type onDemandColGetter struct {
db *bbolt.DB
}
func (g *onDemandColGetter) GetCol(key uint64) (*roaring.Bitmap, error) {
var bm *roaring.Bitmap
err := g.db.View(func(tx *bbolt.Tx) error {
var keyBuf [8]byte
bucket := tx.Bucket([]byte("data"))
binary.BigEndian.PutUint64(keyBuf[:], key)
item := bucket.Get(append(keyPrefixValue, keyBuf[:]...))
bm = roaring.New()
if _, err := bm.FromBuffer(item); err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return bm, nil
}
// WithPreloadedData is an option for OpenIndex and OpenIndexFromBoltDatabase to preload
// all data into memory to allow for faster queries. Only use this if all data from
// the index file will fit into the available memory.
func WithPreloadedData() IndexOption {
return func(idx *Index) error {
cg, err := newPreloadedColGetter(idx.db)
if err != nil {
return err
}
idx.values = cg
return nil
}
}
// WithIndexMetrics is an option for OpenIndex and OpenIndexFromBoltDatabase to set
// an IndexMetrics object.
func WithIndexMetrics(metrics *IndexMetrics) IndexOption {
return func(c *Index) error {
c.metrics = metrics
return nil
}
}
type IndexMetrics struct {
ExecuteDuration HistogramMetric
}
type HistogramMetric interface {
Observe(float64)
}
func newPreloadedColGetter(db *bbolt.DB) (colGetter, error) {
cg := &preloadedColGetter{
values: map[uint64]*roaring.Bitmap{},
}
err := db.View(func(tx *bbolt.Tx) error {
c := tx.Bucket([]byte("data")).Cursor()
for k, v := c.Seek(keyPrefixValue); k != nil && bytes.HasPrefix(k, keyPrefixValue); k, v = c.Next() {
key := binary.BigEndian.Uint64(k[1:])
bm := roaring.New()
if _, err := bm.FromBuffer(v); err != nil {
return err
}
cg.values[key] = bm
}
return nil
})
if err != nil {
return nil, err
}
return cg, nil
}
type preloadedColGetter struct {
values map[uint64]*roaring.Bitmap
}
func (cg *preloadedColGetter) GetCol(key uint64) (*roaring.Bitmap, error) {
return cg.values[key], nil
}