forked from FeatureBaseDB/pdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bolttranslator.go
193 lines (176 loc) · 4.97 KB
/
bolttranslator.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
package pdk
import (
"sync"
"time"
"encoding/binary"
"github.com/boltdb/bolt"
"github.com/pkg/errors"
)
var (
idBucket = []byte("idKey")
valBucket = []byte("valKey")
)
// BoltTranslator is a Translator which stores the two way val/id mapping in
// boltdb. It only accepts string values to map.
type BoltTranslator struct {
Db *bolt.DB
fmu sync.RWMutex
frames map[string]struct{}
}
func (bt *BoltTranslator) Close() error {
err := bt.Db.Sync()
if err != nil {
return errors.Wrap(err, "syncing db")
}
return bt.Db.Close()
}
func NewBoltTranslator(filename string, frames ...string) (bt *BoltTranslator, err error) {
bt = &BoltTranslator{
frames: make(map[string]struct{}),
}
bt.Db, err = bolt.Open(filename, 0600, &bolt.Options{Timeout: 1 * time.Second, InitialMmapSize: 50000000, NoGrowSync: true})
if err != nil {
return nil, errors.Wrapf(err, "opening db file '%v'", filename)
}
bt.Db.MaxBatchDelay = 400 * time.Microsecond
err = bt.Db.Update(func(tx *bolt.Tx) error {
ib, err := tx.CreateBucketIfNotExists(idBucket)
if err != nil {
return errors.Wrap(err, "creating idKey bucket")
}
vb, err := tx.CreateBucketIfNotExists(valBucket)
if err != nil {
return errors.Wrap(err, "creating valKey bucket")
}
for _, frame := range frames {
_, _, err = bt.addFrame(ib, vb, frame)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, errors.Wrap(err, "ensuring bucket existence")
}
return bt, nil
}
func (bt *BoltTranslator) addFrame(ib, vb *bolt.Bucket, frame string) (fib, fvb *bolt.Bucket, err error) {
fib, err = ib.CreateBucketIfNotExists([]byte(frame))
if err != nil {
return nil, nil, errors.Wrap(err, "adding "+frame+" to id bucket")
}
fvb, err = vb.CreateBucketIfNotExists([]byte(frame))
if err != nil {
return nil, nil, errors.Wrap(err, "adding "+frame+" to id bucket")
}
bt.fmu.Lock()
bt.frames[frame] = struct{}{}
bt.fmu.Unlock()
return fib, fvb, nil
}
// Get returns the previously mapped value to the monotonic id generated from
// GetID. For BoltTranslator, val will always be a []byte.
func (bt *BoltTranslator) Get(frame string, id uint64) (val interface{}) {
bt.fmu.RLock()
if _, ok := bt.frames[frame]; !ok {
panic(errors.Errorf("can't Get() with unknown frame '%v'", frame))
}
bt.fmu.RUnlock()
err := bt.Db.View(func(tx *bolt.Tx) error {
ib := tx.Bucket(idBucket)
fib := ib.Bucket([]byte(frame))
idBytes := make([]byte, 8)
binary.BigEndian.PutUint64(idBytes, id)
val = fib.Get(idBytes)
return nil
})
if err != nil {
panic(err)
}
return val
}
// GetID maps val (which must be a byte slice) to a monotonic id.
func (bt *BoltTranslator) GetID(frame string, val interface{}) (id uint64, err error) {
// ensure frame existence
bt.fmu.RLock()
_, ok := bt.frames[frame]
bt.fmu.RUnlock()
if !ok {
err = bt.Db.Update(func(tx *bolt.Tx) error {
ib := tx.Bucket(idBucket)
vb := tx.Bucket(valBucket)
_, _, err := bt.addFrame(ib, vb, frame)
return err
})
if err != nil {
return 0, errors.Wrap(err, "adding frames in GetID")
}
}
// check that val is of a supported type
bsval, ok := val.([]byte)
if !ok {
return 0, errors.Errorf("val %v of type %T for frame %v not supported by BoltTranslator - must be a []byte. ", val, val, frame)
}
// look up to see if this val is already mapped to an id
var ret []byte
err = bt.Db.View(func(tx *bolt.Tx) error {
vb := tx.Bucket(valBucket)
fvb := vb.Bucket([]byte(frame))
ret = fvb.Get(bsval)
return nil
})
if ret != nil && len(ret) == 8 {
return binary.BigEndian.Uint64(ret), nil
}
// get new id, and map it in both directions
err = bt.Db.Batch(func(tx *bolt.Tx) error {
fib := tx.Bucket(idBucket).Bucket([]byte(frame))
fvb := tx.Bucket(valBucket).Bucket([]byte(frame))
id, _ = fib.NextSequence()
keybytes := make([]byte, 8)
binary.BigEndian.PutUint64(keybytes, id)
err = fib.Put(keybytes, bsval)
if err != nil {
return errors.Wrap(err, "inserting into idKey bucket")
}
err = fvb.Put(bsval, keybytes)
if err != nil {
return errors.Wrap(err, "inserting into valKey bucket")
}
return nil
})
if err != nil {
return 0, err
}
return id, nil
}
func (bt *BoltTranslator) BulkAdd(frame string, values [][]byte) error {
var batchSize uint64 = 10000
var batch uint64 = 0
for batch*batchSize < uint64(len(values)) {
err := bt.Db.Batch(func(tx *bolt.Tx) error {
fib := tx.Bucket(idBucket).Bucket([]byte(frame))
fvb := tx.Bucket(valBucket).Bucket([]byte(frame))
for i := batch * batchSize; i < (batch+1)*batchSize && i < uint64(len(values)); i++ {
idBytes := make([]byte, 8)
binary.BigEndian.PutUint64(idBytes, i)
valBytes := values[i]
err := fib.Put(idBytes, valBytes)
if err != nil {
return errors.Wrap(err, "putting into idKey bucket")
}
err = fvb.Put(valBytes, idBytes)
if err != nil {
return errors.Wrap(err, "putting into valKey bucket")
}
}
return nil
})
if err != nil {
return errors.Wrap(err, "inserting batch")
}
batch++
}
return nil
}