-
Notifications
You must be signed in to change notification settings - Fork 2
/
ezdb.go
252 lines (210 loc) · 4.97 KB
/
ezdb.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
package ezdb
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"os"
"sync"
"github.com/rs/zerolog"
lmdb "wellquite.org/golmdb"
)
const mode = os.FileMode(0644)
type Option func(option *options) error
type options struct {
numReaders *uint
numDbs *uint
batchSize *uint
log *zerolog.Logger
}
func WithNumReaders(numReaders uint) Option {
return func(option *options) error {
option.numReaders = &numReaders
return nil
}
}
func WithNumDBs(numDbs uint) Option {
return func(option *options) error {
option.numDbs = &numDbs
return nil
}
}
func WithBatchSize(batchSize uint) Option {
return func(option *options) error {
option.batchSize = &batchSize
return nil
}
}
func WithLogger(logger zerolog.Logger) Option {
return func(option *options) error {
option.log = &logger
return nil
}
}
type Client struct {
path string
options *options
db *lmdb.LMDBClient
initOnce sync.Once
}
func New(path string, opts ...Option) (*Client, error) {
o := &options{}
for _, opt := range opts {
err := opt(o)
if err != nil {
return nil, fmt.Errorf("failed to set options: %w", err)
}
}
// Default values
if o.numReaders == nil {
o.numReaders = new(uint)
*o.numReaders = 8
}
if o.numDbs == nil {
o.numDbs = new(uint)
*o.numDbs = 1
}
if o.batchSize == nil {
o.batchSize = new(uint)
*o.batchSize = 1
}
if o.log == nil {
o.log = new(zerolog.Logger)
*o.log = zerolog.Nop()
}
return &Client{
path: path,
options: o,
}, nil
}
func (db *Client) init() error {
// Check if directory exists, if not create it.
if _, err := os.Stat(db.path); os.IsNotExist(err) {
err = os.MkdirAll(db.path, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create db directory: %w", err)
}
}
// Open DB.
newDB, err := lmdb.NewLMDB(*db.options.log, db.path, mode, *db.options.numReaders, *db.options.numDbs, lmdb.EnvironmentFlag(0), *db.options.batchSize)
if err != nil {
return fmt.Errorf("failed to open db: %w", err)
}
db.db = newDB
return nil
}
func (db *Client) Close() {
db.db.TerminateSync()
}
type DBRef[K, V any] struct {
id string
ownerDB *Client
// TODO: reuse the gob encoder here.
// Also, since typeinfo is hardcoded here, maybe better to replace gob with raw bytes.
// Worth looking into go-bolt for their pure byte implementation.
}
func NewRef[K, V any](refID string, db *Client) (ref *DBRef[K, V], err error) {
ref = new(DBRef[K, V])
err = ref.init(refID, db)
if err != nil {
return nil, fmt.Errorf("failed to initialize db ref: %w", err)
}
return ref, nil
}
func (ref *DBRef[K, V]) init(refID string, db *Client) error {
var err error
db.initOnce.Do(func() {
err = db.init()
})
if err != nil {
return fmt.Errorf("failed to initialize database: %w", err)
}
err = db.db.Update(func(txn *lmdb.ReadWriteTxn) error {
_, err := txn.DBRef(refID, lmdb.DatabaseFlag(0x40000))
if err != nil {
return err
}
return nil
})
if err != nil {
return fmt.Errorf("failed to open db ref: %w", err)
}
*ref = DBRef[K, V]{
id: refID,
ownerDB: db,
}
return nil
}
func (ref *DBRef[K, V]) Put(key *K, val *V) (err error) {
err = ref.ownerDB.db.Update(func(txn *lmdb.ReadWriteTxn) error {
dbRef, err := txn.DBRef(ref.id, lmdb.DatabaseFlag(0))
if err != nil {
return fmt.Errorf("failed to get db ref: %w", err)
}
// Encode the key.
keyBuf, err := encode(key)
if err != nil {
return fmt.Errorf("failed to encode key: %w", err)
}
// Encode the value.
valBuf, err := encode(val)
if err != nil {
return fmt.Errorf("failed to encode value: %w", err)
}
err = txn.Put(dbRef, keyBuf.Bytes(), valBuf.Bytes(), lmdb.PutFlag(0))
if err != nil {
return fmt.Errorf("failed to put key/value pair: %w", err)
}
return nil
})
if err != nil {
return err
}
return nil
}
func (ref *DBRef[K, V]) Get(key *K) (val *V, err error) {
err = ref.ownerDB.db.View(func(txn *lmdb.ReadOnlyTxn) error {
dbRef, err := txn.DBRef(ref.id, lmdb.DatabaseFlag(0))
if err != nil {
return fmt.Errorf("failed to get db ref: %w", err)
}
// Encode the key.
keyBuf, err := encode(key)
if err != nil {
return fmt.Errorf("failed to encode key: %w", err)
}
// Get the value.
valBytes, err := txn.Get(dbRef, keyBuf.Bytes())
if err != nil {
return fmt.Errorf("failed to get key: %w", err)
}
// Decode the value.
err = decode(&val, bytes.NewReader(valBytes))
if err != nil {
return fmt.Errorf("failed to decode value: %w", err)
}
return nil
})
if err != nil {
return new(V), err
}
return val, nil
}
func encode[T any](val *T) (buf bytes.Buffer, err error) {
encoder := gob.NewEncoder(&buf)
err = encoder.Encode(val)
if err != nil {
return buf, err
}
return buf, nil
}
// Decodes a value from a reader into a pointer to a value.
// Will try and fail if the decoded type is not assignable to the thing we're decoding into.
func decode[T any](val *T, r io.Reader) error {
decoder := gob.NewDecoder(r)
err := decoder.Decode(val)
if err != nil {
return err
}
return nil
}