-
Notifications
You must be signed in to change notification settings - Fork 2
/
wal_helper.go
81 lines (72 loc) · 1.34 KB
/
wal_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
package storage
type WalHelper struct {
w *Wal
filename string
l LogFormat
writeCount int
count int
}
func (w *WalHelper) Init(filename string, l LogFormat, writeCount int) {
w.filename = filename
w.l = l
w.writeCount = writeCount
w.count = 0
}
func (w *WalHelper) Close() {
if w.w != nil {
err := w.w.Close()
if err != nil {
logger.Error("close wal failed[%v]", err)
w.w = nil
}
w.w = nil
}
}
func (w *WalHelper) check() {
if w.count >= w.writeCount {
if w.w != nil {
if err := w.w.Flush(); err != nil {
logger.Error("flush wal failed[%v]", err)
return
}
if err := w.w.Close(); err != nil {
logger.Error("close wal failed[%v]", err)
return
}
w.w = nil
}
w.count = 0
}
}
func (w *WalHelper) getW() (*Wal, error) {
if w.w == nil {
wal := Wal{}
err := wal.Init(w.filename, w.l, false)
if err != nil {
return nil, err
}
w.w = &wal
return w.w, nil
}
return w.w, nil
}
func (w *WalHelper) Append(logOp ...*LogOperation) (string, int64, error) {
wal, err := w.getW()
if err != nil {
return "", 0, err
}
gid, num, err := wal.Append(logOp...)
if err != nil {
return "", 0, err
}
w.count++
w.check()
return gid, num, nil
}
func (w *WalHelper) Offset() (int64, error) {
wal, err := w.getW()
if err != nil {
return 0, err
}
return wal.Offset(), nil
}