-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwal_rewriter.go
More file actions
59 lines (49 loc) · 884 Bytes
/
wal_rewriter.go
File metadata and controls
59 lines (49 loc) · 884 Bytes
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
package bitcask
type WalRewriter struct {
wal *Wal
bufLen int
threshold int
}
func NewWalRewriter(wal *Wal, threshold int) *WalRewriter {
if threshold < 4*1024 {
threshold = 4 * 1024
}
wal.Ref()
return &WalRewriter{
wal: wal,
bufLen: 0,
threshold: threshold,
}
}
func (r *WalRewriter) Wal() *Wal {
return r.wal
}
func (r *WalRewriter) Close() error {
if r.bufLen != 0 {
if err := r.wal.Flush(); err != nil {
return err
}
}
r.wal.Unref()
return nil
}
func (r *WalRewriter) AppendRecord(record []byte) (off uint64, err error) {
off, err = r.wal.WriteRecord(record)
if err != nil {
return 0, err
}
r.bufLen += len(record)
if r.bufLen >= r.threshold {
err = r.Flush()
}
return
}
func (r *WalRewriter) Flush() error {
if r.bufLen != 0 {
if err := r.wal.Flush(); err != nil {
return err
}
r.bufLen = 0
}
return nil
}