forked from deepkaran/goforestdb
-
Notifications
You must be signed in to change notification settings - Fork 6
/
snapshot_marker_test.go
178 lines (153 loc) · 3.61 KB
/
snapshot_marker_test.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package forestdb
import (
"fmt"
"os"
"testing"
)
func TestSnapshotMarkersSingleKvs(t *testing.T) {
defer os.RemoveAll("test")
dbfile, err := Open("test", nil)
if err != nil {
t.Fatal(err)
}
defer dbfile.Close()
kvstore, err := dbfile.OpenKVStoreDefault(nil)
if err != nil {
t.Fatal(err)
}
defer kvstore.Close()
//insert first batch
for i := 0; i < 2; i++ {
doc, err := NewDoc([]byte(fmt.Sprintf("key-%d", i)), nil, []byte(fmt.Sprintf("value%d", i)))
if err != nil {
t.Error(err)
}
err = kvstore.Set(doc)
if err != nil {
t.Error(err)
}
doc.Close()
}
// commit changes
err = dbfile.Commit(COMMIT_NORMAL)
if err != nil {
t.Error(err)
}
//insert second batch
for i := 2; i < 4; i++ {
doc, err := NewDoc([]byte(fmt.Sprintf("key-%d", i)), nil, []byte(fmt.Sprintf("value%d", i)))
if err != nil {
t.Error(err)
}
err = kvstore.Set(doc)
if err != nil {
t.Error(err)
}
doc.Close()
}
// commit changes
err = dbfile.Commit(COMMIT_NORMAL)
if err != nil {
t.Error(err)
}
snap, err := dbfile.GetAllSnapMarkers()
if err != nil {
t.Error(err)
}
defer snap.FreeSnapMarkers()
snapList := snap.SnapInfoList()
if len(snapList) != 2 {
t.Errorf("expected num markers 2, got %v", len(snapList))
}
for i, s := range snapList {
snapMarker := s.GetSnapMarker()
if int(snapMarker.marker) == 0 {
t.Errorf("invalid snapshot marker %v", snapMarker.marker)
}
if s.GetNumKvsMarkers() != 1 {
t.Errorf("expected num_kvs_markers 1, got %v", s.GetNumKvsMarkers())
}
cm := s.GetKvsCommitMarkers()
for _, c := range cm {
if c.GetSeqNum() != SeqNum(4-(i*2)) {
t.Errorf("expected commit marker seqnum %v, got %v", 4-(i*2), c.GetSeqNum())
}
}
}
}
func TestSnapshotMarkersMultiKvs(t *testing.T) {
defer os.RemoveAll("test")
dbfile, err := Open("test", nil)
if err != nil {
t.Fatal(err)
}
defer dbfile.Close()
kvstore1, err := dbfile.OpenKVStore("test1", nil)
if err != nil {
t.Fatal(err)
}
defer kvstore1.Close()
kvstore2, err := dbfile.OpenKVStore("test2", nil)
if err != nil {
t.Fatal(err)
}
defer kvstore2.Close()
//insert first batch
for i := 0; i < 2; i++ {
doc, err := NewDoc([]byte(fmt.Sprintf("key-%d", i)), nil, []byte(fmt.Sprintf("value%d", i)))
if err != nil {
t.Error(err)
}
err = kvstore1.Set(doc)
if err != nil {
t.Error(err)
}
doc.Close()
}
//insert second kv store
for i := 0; i < 3; i++ {
doc, err := NewDoc([]byte(fmt.Sprintf("key-%d", i)), nil, []byte(fmt.Sprintf("value%d", i)))
if err != nil {
t.Error(err)
}
err = kvstore2.Set(doc)
if err != nil {
t.Error(err)
}
doc.Close()
}
// commit changes
err = dbfile.Commit(COMMIT_MANUAL_WAL_FLUSH)
if err != nil {
t.Error(err)
}
snap, err := dbfile.GetAllSnapMarkers()
if err != nil {
t.Error(err)
}
defer snap.FreeSnapMarkers()
snapList := snap.SnapInfoList()
if len(snapList) != 3 {
t.Errorf("expected num markers 3, got %v", len(snapList))
}
//first snap marker is the latest one
s0 := snapList[0]
snapMarker := s0.GetSnapMarker()
if int(snapMarker.marker) == 0 {
t.Errorf("invalid snapshot marker %v", snapMarker.marker)
}
if s0.GetNumKvsMarkers() != 2 {
t.Errorf("expected num_kvs_markers 2, got %v", s0.GetNumKvsMarkers())
}
cm := s0.GetKvsCommitMarkers()
if len(cm) != 2 {
t.Errorf("expected commit markers 2, got %v", len(cm))
}
for _, c := range cm {
if c.GetKvStoreName() == "test1" && c.GetSeqNum() != 2 {
t.Errorf("expected commit marker seqnum 2, got %v", c.GetSeqNum())
} else if c.GetKvStoreName() == "test2" && c.GetSeqNum() != 3 {
t.Errorf("expected commit marker seqnum 2, got %v", c.GetSeqNum())
}
}
}