-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.go
52 lines (44 loc) · 1.01 KB
/
example.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
package wal
import (
"fmt"
"time"
"go.uber.org/zap"
)
func main() {
log, err := zap.NewProduction()
if err != nil {
panic(err)
}
wal, err := NewWriteAheadLog(&WALOptions{
LogDir: "data/",
MaxLogSize: 40 * 1024 * 1024, // 40 MB (log rotation size)
MaxSegments: 2,
Log: log,
MaxWaitBeforeSync: 1 * time.Second,
SyncMaxBytes: 1000,
})
if err != nil {
fmt.Println("Error creating Write-Ahead Log:", err)
return
}
defer wal.Close()
for i := 0; i < 10000000; i++ {
_, err := wal.Write([]byte("Simulate some database changes and write them to the WAL"))
if err != nil {
panic(err)
}
}
// Sync all the logs to the file at the end so that we dont loose any data
err = wal.Sync()
if err != nil {
fmt.Println("Error in final sync", err)
}
startTime := time.Now()
var numLines int
wal.Replay(0, func(b []byte) error {
numLines++
return nil
})
fmt.Println("Total lines replayed:", numLines)
fmt.Println("time taken:", time.Since(startTime))
}