-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
74 lines (65 loc) · 1.42 KB
/
log.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
package storage
import (
"io"
"os"
"sort"
)
// log represents the data and index for the storage engine
type readLog struct {
path string
index map[string]int64
}
type writeLog struct {
file *os.File
index map[string]int64
size int64
}
func initReadLogs(paths []string) ([]*readLog, error) {
// sorting is needed as the file names order puts 13 before 2, and we rely on the order of files in making the index
sort.Slice(paths, func(i, j int) bool {
return extractFileNumber(paths[i]) < extractFileNumber(paths[j])
})
logs := make([]*readLog, 0, len(paths))
for _, path := range paths {
log, err := extractReadLog(path)
if err != nil {
return nil, err
}
logs = append(logs, log)
}
return logs, nil
}
func extractReadLog(path string) (*readLog, error) {
log := &readLog{
path: path,
index: make(map[string]int64),
}
file, err := os.OpenFile(path, os.O_RDONLY, 0644) // todo: set right perm for the read only file
if err != nil {
return nil, err
}
defer file.Close()
for {
key, err := readDataFile(file)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
endOffset, err := file.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
log.index[key] = endOffset
// Intentionally reading value to move the file cursor to the next key
_, err = readDataFile(file)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
}
return log, nil
}