-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwal.go
More file actions
100 lines (77 loc) · 1.88 KB
/
Copy pathwal.go
File metadata and controls
100 lines (77 loc) · 1.88 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"encoding/binary"
"fmt"
"github.com/huandu/skiplist"
"os"
"strconv"
"strings"
"sync"
"time"
)
type WriteAheadLog struct {
writeLock sync.Mutex
}
func (w *WriteAheadLog) RenameCurrentWAL(newName string) {
w.writeLock.Lock()
defer w.writeLock.Unlock()
os.Rename("wal-current.bin", newName)
}
func (w *WriteAheadLog) Append(key string, value string, timestamp int64, tombstone bool) {
w.writeLock.Lock()
defer w.writeLock.Unlock()
binKey := []byte(key)
binKeySize := binary.Size(binKey)
binValue := []byte(value)
binValueSize := binary.Size(binValue)
walEntry := WALEntry{
keySize: uint32(binKeySize),
valueSize: uint32(binValueSize),
timestamp: uint64(timestamp),
tombstone: tombstone,
key: key,
value: value,
}
//TODO: write using O_DIRECT to skip fs cache
walFile, err := os.OpenFile("wal-current.bin", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
//TODO: handle error
}
WriteWALRow(walFile, &walEntry)
}
func Recover() {
timestamp := time.Now().UnixNano()
walFilePath := fmt.Sprintf("wal-%d.bin", timestamp)
os.Rename("wal-current.bin", walFilePath)
dir, err := os.Open("./")
if err != nil {
//TODO: handle error
return
}
walFiles := GetFilenamesByPredicate(dir, IsWALFile)
dir.Close()
for _, f := range walFiles {
timestampString := strings.ReplaceAll(strings.ReplaceAll(f, "wal-", ""), ".bin", "")
walTimestamp, err := strconv.Atoi(timestampString)
walCurrent, err := os.Open(f)
if err != nil {
return
}
memtable := skiplist.New(skiplist.StringAsc)
for {
walEntry := ReadWALRow(walCurrent)
if walEntry != nil {
m := MemtableEntry{
value: walEntry.value,
tombstone: walEntry.tombstone,
timestamp: walEntry.timestamp,
}
memtable.Set(walEntry.key, m)
} else {
break
}
}
walCurrent.Close()
FlushToFile(int64(walTimestamp), memtable)
}
}