-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
76 lines (59 loc) · 1.11 KB
/
db.go
File metadata and controls
76 lines (59 loc) · 1.11 KB
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
package fossil
import (
"errors"
"fmt"
"sync"
)
type DB struct {
mu sync.RWMutex
x, y *memtable
w *wal
d string
ss []*sstable
memSize uint64
flushCh chan struct{}
}
// Get
func (d *DB) Get(key []byte) ([]byte, error) {
if val, found := d.x.get(key); found {
return val, nil
}
d.mu.Lock()
y := d.y
d.mu.Unlock()
if y != nil {
if val, found := y.get(key); found {
return val, nil
}
}
for _, s := range d.ss {
if val, found := s.get(key); found {
return val, nil
}
}
return nil, errors.New("key not found")
}
// Put
func (d *DB) Put(key, val []byte) error {
d.mu.Lock()
// possible flush before writing
if d.x.getSize() >= d.memSize {
if d.y != nil {
d.mu.Unlock()
return errors.New("db saturated: flush in progress")
}
}
d.mu.Unlock()
if err := d.w.append(key, val); err != nil {
return fmt.Errorf("failed to append to wal file: %w", err)
}
d.x.put(key, val)
return nil
}
func (d *DB) flush() {
// TODO
// move x to y memtables
// create new wal file for new x memtable
// spawn goroutine to write y memtable to sstable
// discard y memtable and old wal
}