-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcask_helper.go
101 lines (82 loc) · 2.19 KB
/
bitcask_helper.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
package bitcask
import (
"os"
"path"
"time"
"github.com/IslamWalid/bitcask/internal/datastore"
"github.com/IslamWalid/bitcask/internal/recfmt"
)
// parseUsrOpts fills an options struct with the passed user options.
func parseUsrOpts(opts []ConfigOpt) options {
usrOpts := options{
syncOption: SyncOnDemand,
accessPermission: ReadOnly,
}
for _, opt := range opts {
switch opt {
case SyncOnPut:
usrOpts.syncOption = SyncOnPut
case ReadWrite:
usrOpts.accessPermission = ReadWrite
}
}
return usrOpts
}
// listOldFiles prepares a list with all old files to be deleted after merge.
func (b *Bitcask) listOldFiles() ([]string, error) {
res := make([]string, 0)
dataStore, err := os.Open(b.dataStore.Path())
if err != nil {
return nil, err
}
defer dataStore.Close()
b.accessMu.Lock()
files, err := dataStore.Readdir(0)
b.accessMu.Unlock()
if err != nil {
return nil, err
}
for _, file := range files {
fileName := file.Name()
if fileName[0] != '.' && fileName != b.activeFile.Name() && fileName != "keydir" {
res = append(res, fileName)
}
}
return res, nil
}
// mergeWrite performs a writing to the created merge file.
// returns the new record about the written data
// returns error if the data is deleted and will not be written again or on any system failures.
func (b *Bitcask) mergeWrite(mergeFile *datastore.AppendFile, key string) (recfmt.KeyDirRec, error) {
rec := b.keyDir[key]
value, err := b.dataStore.ReadValueFromFile(rec.FileId, key, rec.ValuePos, rec.ValueSize)
if err != nil {
return recfmt.KeyDirRec{}, err
}
tstamp := time.Now().UnixMicro()
n, err := mergeFile.WriteData(key, value, tstamp)
if err != nil {
return recfmt.KeyDirRec{}, err
}
newRec := recfmt.KeyDirRec{
FileId: mergeFile.Name(),
ValuePos: uint32(n),
ValueSize: uint32(len(value)),
Tstamp: tstamp,
}
err = mergeFile.WriteHint(key, newRec)
if err != nil {
return recfmt.KeyDirRec{}, err
}
return newRec, nil
}
// deleteOldFiles deletes all files passed to it.
func (b *Bitcask) deleteOldFiles(files []string) error {
for _, file := range files {
err := os.Remove(path.Join(b.dataStore.Path(), file))
if err != nil {
return err
}
}
return nil
}