-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
322 lines (254 loc) · 5.5 KB
/
db.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
package logdb
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"github.com/cespare/xxhash"
)
// TODO implement batch inserts.
// TODO build index in background.
// TODO implement compaction - remove duplicate keys and tombstones.
var ErrNotExists = errors.New("key does not exists")
type Database struct {
f *os.File // Used only for insert.
hashFunc func(b []byte) uint64
indexMu sync.RWMutex
index map[uint64]header
}
func Open(path string) (*Database, error) {
err := os.MkdirAll(filepath.Dir(path), 0700)
if err != nil {
return nil, fmt.Errorf("create dir %s: %q", filepath.Dir(path), err)
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return nil, fmt.Errorf("create file %s: %q", path, err)
}
db := &Database{
f: f,
hashFunc: xxhash.Sum64,
index: make(map[uint64]header),
}
if err := db.buildIndex(); err != nil {
return nil, fmt.Errorf("build index: %w", err)
}
return db, nil
}
func (d *Database) Set(key, value []byte) error {
return d.save(key, value, false, true)
}
func (d *Database) BatchSet(keys, values [][]byte) error {
if len(keys) != len(values) {
return fmt.Errorf("keys and values have different length")
}
var err error
for i := 0; i < len(keys); i++ {
err = d.save(keys[i], values[i], false, false)
}
if err != nil {
err = d.f.Sync()
}
return err
}
func (d *Database) Get(key []byte) ([]byte, error) {
e, err := d.get(key)
if err != nil {
return nil, err
}
return e.value, nil
}
func (d *Database) Delete(key []byte) error {
e, err := d.get(key)
if err != nil {
return err
}
return d.save(e.key, nil, true, true)
}
func (d *Database) Close() error {
err := d.f.Sync()
if err == nil {
err = d.f.Close()
}
return err
}
// get retrieves the entry using index.
func (d *Database) get(key []byte) (*entry, error) {
hashKey := d.hashFunc(key)
d.indexMu.RLock()
h, exists := d.index[hashKey]
d.indexMu.RUnlock()
if !exists {
return nil, ErrNotExists
}
if _, err := d.f.Seek(h.offset, io.SeekStart); err != nil {
return nil, err
}
buf := make([]byte, h.klen+h.vlen)
_, err := io.ReadFull(d.f, buf[:])
if err != nil {
if err == io.EOF {
return nil, fmt.Errorf("read entry, unexpected EOF: %w", err)
}
return nil, fmt.Errorf("read entry: %w", err)
}
e := &entry{}
e.key = buf[:h.klen]
e.value = buf[h.klen:]
return e, nil
}
// find retrieves the last entry of the key, without using index.
func (d *Database) find(key []byte) (*entry, error) {
f, err := os.OpenFile(d.f.Name(), os.O_RDONLY, 0600)
if err != nil {
return nil, err
}
defer f.Close()
r := bufio.NewReader(f)
var found *entry
var h header
for {
err := h.decodeFrom(r)
if err != nil {
if err == io.EOF {
if found == nil {
return nil, ErrNotExists
}
return found, nil
} else if err != nil {
return nil, err
}
}
e, _, err := decodeEntry(r, h, true)
if err != nil {
if err == io.EOF {
return nil, fmt.Errorf("read entry, unexpected EOF: %w", err)
}
return nil, fmt.Errorf("read entry: %w", err)
}
if bytes.Equal(e.key, key) {
found = e
}
}
}
// save writes the entry into disk.
//
// value can be nil.
// If isDelete is true, the value will not be saved.
func (d *Database) save(key []byte, value []byte, isDelete bool, fsync bool) error {
h := header{
klen: uint32(len(key)),
}
bufSize := MaxHeaderSize + len(key)
if isDelete {
h.meta |= 1
h.vlen = 0
} else {
h.vlen = uint32(len(value))
bufSize += len(value)
}
buf := make([]byte, bufSize)
// Encode the header.
hsize := h.encode(buf[:])
bufN := hsize
// Write the key.
copy(buf[bufN:], key)
bufN += len(key)
// Write the value.
// Don't save the entry's value if the entry is deleted.
if !isDelete {
copy(buf[bufN:], value)
bufN += len(value)
}
// Make sure the pointer is at the end.
offset, err := d.f.Seek(0, io.SeekEnd)
if err != nil {
return err
}
n, err := d.f.Write(buf[:bufN])
if err != nil {
return err
}
if n != bufN {
return fmt.Errorf("want n %d, got %d: %w", len(buf),
n, io.ErrShortWrite)
}
if fsync {
err := d.f.Sync()
if err != nil {
return err
}
}
keyHash := d.hashFunc(key)
h.offset = offset + int64(hsize)
d.indexMu.Lock()
if isDelete {
delete(d.index, keyHash)
} else {
d.index[keyHash] = h
}
d.indexMu.Unlock()
return nil
}
func (d *Database) buildIndex() error {
f, err := os.OpenFile(d.f.Name(), os.O_RDONLY, 0600)
if err != nil {
return fmt.Errorf("open file %s: %q", d.f.Name(), err)
}
defer f.Close()
index := make(map[uint64]header)
r := bufio.NewReader(f)
rc := &readerCount{
reader: r,
}
var h header
for {
err := h.decodeFrom(rc)
if err != nil {
if err == io.EOF {
break
}
return err
}
// Record the offset for entry.
offset := int64(rc.bytesRead)
e, _, err := decodeEntry(rc, h, false)
if err != nil {
if err == io.EOF {
return fmt.Errorf("unexpected EOF, expected entry: %w", err)
}
return err
}
h.offset = offset
index[d.hashFunc(e.key)] = h
}
d.indexMu.Lock()
d.index = index
d.indexMu.Unlock()
return nil
}
type readerCount struct {
reader *bufio.Reader
bytesRead int
}
func (r *readerCount) Read(p []byte) (n int, err error) {
n, err = r.reader.Read(p)
r.bytesRead += n
return
}
func (r *readerCount) ReadByte() (b byte, err error) {
b, err = r.reader.ReadByte()
if err == nil {
r.bytesRead++
}
return
}
func (r *readerCount) Discard(i int) (n int, err error) {
n, err = r.reader.Discard(i)
r.bytesRead += n
return
}