-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
106 lines (88 loc) · 2.04 KB
/
common.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
package main
import (
"errors"
"fmt"
"os"
"sort"
"strings"
"github.com/rfyiamcool/raft_kvs/consensus/snap"
"github.com/rfyiamcool/raft_kvs/consensus/utils/fileutil"
"github.com/rfyiamcool/raft_kvs/consensus/wal"
)
var (
snapSuffix = ".snap"
errBadWALName = errors.New("bad wal name")
)
// getSnapNames returns the filename of the snapshots in logical time order (from newest to oldest).
// If there is no available snapshots, an ErrNoSnapshot will be returned.
func getSnapNames(d string) ([]string, error) {
dir, err := os.Open(d)
if err != nil {
return nil, err
}
defer dir.Close()
names, err := dir.Readdirnames(-1)
if err != nil {
return nil, err
}
snaps := checkSuffix(names)
if len(snaps) == 0 {
return nil, snap.ErrNoSnapshot
}
sort.Sort(sort.Reverse(sort.StringSlice(snaps)))
return snaps, nil
}
func checkSuffix(names []string) []string {
snaps := []string{}
for i := range names {
if strings.HasSuffix(names[i], snapSuffix) {
snaps = append(snaps, names[i])
}
}
return snaps
}
func readWALNames(dirpath string) ([]string, error) {
names, err := fileutil.ReadDir(dirpath)
if err != nil {
return nil, err
}
wnames := checkWalNames(names)
if len(wnames) == 0 {
return nil, wal.ErrFileNotFound
}
return wnames, nil
}
func checkWalNames(names []string) []string {
wnames := make([]string, 0)
for _, name := range names {
_, _, err := parseWALName(name)
if err != nil {
continue
}
wnames = append(wnames, name)
}
return wnames
}
func parseWALName(str string) (seq, index uint64, err error) {
if !strings.HasSuffix(str, ".wal") {
return 0, 0, errBadWALName
}
_, err = fmt.Sscanf(str, "%016x-%016x.wal", &seq, &index)
return seq, index, err
}
func walName(seq, index uint64) string {
return fmt.Sprintf("%016x-%016x.wal", seq, index)
}
func searchIndex(names []string, index uint64) (int, bool) {
for i := len(names) - 1; i >= 0; i-- {
name := names[i]
_, curIndex, err := parseWALName(name)
if err != nil {
continue
}
if index >= curIndex {
return i, true
}
}
return -1, false
}