forked from src-d/go-mysql-server
/
mapping.go
393 lines (316 loc) · 6.85 KB
/
mapping.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// +build !windows
package pilosa
import (
"bytes"
"encoding/binary"
"encoding/gob"
"fmt"
"io"
"sort"
"sync"
"github.com/xsec-lab/go-mysql-server/sql"
bolt "go.etcd.io/bbolt"
)
// mapping
// buckets:
// - index name: columndID uint64 -> location []byte
// - field name: value []byte (gob encoding) -> rowID uint64
type mapping struct {
path string
mut sync.RWMutex
db *bolt.DB
// in create mode there's only one transaction closed explicitly by
// commit function
create bool
tx *bolt.Tx
clientMut sync.Mutex
clients int
}
func newMapping(path string) *mapping {
return &mapping{path: path}
}
func (m *mapping) open() error {
return m.openCreate(false)
}
// openCreate opens and sets creation mode in the database.
func (m *mapping) openCreate(create bool) error {
m.clientMut.Lock()
defer m.clientMut.Unlock()
m.mut.Lock()
defer m.mut.Unlock()
if m.clients == 0 && m.db == nil {
var err error
m.db, err = bolt.Open(m.path, 0640, nil)
if err != nil {
return err
}
}
m.clients++
m.create = create
return nil
}
func (m *mapping) close() error {
m.clientMut.Lock()
defer m.clientMut.Unlock()
m.mut.Lock()
defer m.mut.Unlock()
if m.clients > 1 {
m.clients--
return nil
}
m.clients = 0
if m.db != nil {
if err := m.db.Close(); err != nil {
return err
}
m.db = nil
}
return nil
}
func (m *mapping) rowID(fieldName string, value interface{}) (uint64, error) {
val, err := m.get(fieldName, value)
if err != nil {
return 0, err
}
if val == nil {
return 0, io.EOF
}
return binary.LittleEndian.Uint64(val), err
}
// commit saves current transaction, if cont is true a new transaction will be
// created again in the next query. Only for create mode.
func (m *mapping) commit(cont bool) error {
m.clientMut.Lock()
defer m.clientMut.Unlock()
var err error
if m.create && m.tx != nil {
err = m.tx.Commit()
}
m.create = cont
m.tx = nil
return err
}
func (m *mapping) rollback() error {
m.clientMut.Lock()
defer m.clientMut.Unlock()
var err error
if m.create && m.tx != nil {
err = m.tx.Rollback()
}
m.create = false
m.tx = nil
return err
}
func (m *mapping) transaction(writable bool, f func(*bolt.Tx) error) error {
m.clientMut.Lock()
defer m.clientMut.Unlock()
var tx *bolt.Tx
var err error
if m.create {
if m.tx == nil {
m.tx, err = m.db.Begin(true)
if err != nil {
return err
}
}
tx = m.tx
} else {
tx, err = m.db.Begin(writable)
if err != nil {
return err
}
}
err = f(tx)
if m.create {
return err
}
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
func (m *mapping) getRowID(fieldName string, value interface{}) (uint64, error) {
var id uint64
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(value)
if err != nil {
return 0, err
}
err = m.transaction(true, func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(fieldName))
if err != nil {
return err
}
key := buf.Bytes()
val := b.Get(key)
if val != nil {
id = binary.LittleEndian.Uint64(val)
return nil
}
// the first NextSequence is 1 so the first id will be 1
// this can only fail if the transaction is closed
id, _ = b.NextSequence()
val = make([]byte, 8)
binary.LittleEndian.PutUint64(val, id)
err = b.Put(key, val)
return err
})
if err != nil {
return 0, err
}
return id, err
}
func (m *mapping) getMaxRowID(fieldName string) (uint64, error) {
var id uint64
err := m.transaction(true, func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(fieldName))
if b == nil {
return nil
}
id = b.Sequence()
return nil
})
return id, err
}
func (m *mapping) putLocation(
indexName string,
colID uint64,
location []byte,
) error {
return m.transaction(true, func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(indexName))
if err != nil {
return err
}
key := make([]byte, 8)
binary.LittleEndian.PutUint64(key, colID)
return b.Put(key, location)
})
}
func (m *mapping) sortedLocations(
indexName string,
cols []uint64,
reverse bool,
) ([][]byte, error) {
var result [][]byte
m.mut.RLock()
defer m.mut.RUnlock()
err := m.db.View(func(tx *bolt.Tx) error {
bucket := []byte(indexName)
b := tx.Bucket(bucket)
if b == nil {
return fmt.Errorf("bucket %s not found", bucket)
}
for _, col := range cols {
key := make([]byte, 8)
binary.LittleEndian.PutUint64(key, col)
val := b.Get(key)
// val will point to mmap addresses, so we need to copy the slice
dst := make([]byte, len(val))
copy(dst, val)
result = append(result, dst)
}
return nil
})
if err != nil {
return nil, err
}
if reverse {
sort.Stable(sort.Reverse(byBytes(result)))
} else {
sort.Stable(byBytes(result))
}
return result, nil
}
type byBytes [][]byte
func (b byBytes) Len() int { return len(b) }
func (b byBytes) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byBytes) Less(i, j int) bool { return bytes.Compare(b[i], b[j]) < 0 }
func (m *mapping) getLocation(
indexName string,
colID uint64,
) ([]byte, error) {
var location []byte
err := m.transaction(true, func(tx *bolt.Tx) error {
bucket := []byte(indexName)
b := tx.Bucket(bucket)
if b == nil {
return fmt.Errorf("bucket %s not found", bucket)
}
key := make([]byte, 8)
binary.LittleEndian.PutUint64(key, colID)
location = b.Get(key)
return nil
})
return location, err
}
func (m *mapping) getLocationFromBucket(
bucket *bolt.Bucket,
colID uint64,
) ([]byte, error) {
key := make([]byte, 8)
binary.LittleEndian.PutUint64(key, colID)
return bucket.Get(key), nil
}
func (m *mapping) getBucket(
indexName string,
writable bool,
) (*bolt.Bucket, error) {
var bucket *bolt.Bucket
tx, err := m.db.Begin(writable)
if err != nil {
return nil, err
}
bu := []byte(indexName)
bucket = tx.Bucket(bu)
if bucket == nil {
_ = tx.Rollback()
return nil, fmt.Errorf("bucket %s not found", bu)
}
return bucket, err
}
func (m *mapping) get(name string, key interface{}) ([]byte, error) {
var value []byte
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(key)
if err != nil {
return nil, err
}
err = m.transaction(true, func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
if b != nil {
value = b.Get(buf.Bytes())
return nil
}
return fmt.Errorf("%s not found", name)
})
return value, err
}
func (m *mapping) filter(name string, fn func([]byte) (bool, error)) ([]uint64, error) {
var result []uint64
m.mut.RLock()
defer m.mut.RUnlock()
err := m.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
if b == nil {
return nil
}
return b.ForEach(func(k, v []byte) error {
ok, err := fn(k)
if err != nil {
return err
}
if ok {
result = append(result, binary.LittleEndian.Uint64(v))
}
return nil
})
})
return result, err
}
func mappingKey(p sql.Partition) string {
return fmt.Sprintf("%x", p.Key())
}