forked from deepkaran/goforestdb
-
Notifications
You must be signed in to change notification settings - Fork 6
/
commit_test.go
110 lines (97 loc) · 2.1 KB
/
commit_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
package forestdb
import (
"os"
"testing"
)
func TestSnapshotAndRollback(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()
// put a new key
doc, err := NewDoc([]byte("key1"), nil, []byte("value1"))
if err != nil {
t.Error(err)
}
err = kvstore.Set(doc)
if err != nil {
t.Error(err)
}
snapshotSeqNum := doc.SeqNum()
doc.Close()
// commit changes
err = dbfile.Commit(COMMIT_NORMAL)
if err != nil {
t.Error(err)
}
// get a snapshot
dbSnapshot, err := kvstore.SnapshotOpen(snapshotSeqNum)
if err != nil {
t.Fatal(err)
}
defer dbSnapshot.Close()
// update the original key
doc, err = NewDoc([]byte("key1"), nil, []byte("value1-updated"))
if err != nil {
t.Error(err)
}
err = kvstore.Set(doc)
if err != nil {
t.Error(err)
}
doc.Close()
// get the document using the regular db handle
doc, err = NewDoc([]byte("key1"), nil, nil)
if err != nil {
t.Error(err)
}
err = kvstore.Get(doc)
if err != nil {
t.Error(err)
}
// verify we get the updated version
if string(doc.Body()) != "value1-updated" {
t.Errorf("expected value1-updated, got %s", doc.Body())
}
doc.Close()
// get the document, using the snapshot before the update
doc, err = NewDoc([]byte("key1"), nil, nil)
if err != nil {
t.Error(err)
}
err = dbSnapshot.Get(doc)
if err != nil {
t.Error(err)
}
// verify we get the version before we took the snapshot
if string(doc.Body()) != "value1" {
t.Errorf("expected value1, got %s", doc.Body())
}
doc.Close()
// now ask the db to rollback to the snapshot seqnum
err = kvstore.Rollback(snapshotSeqNum)
if err != nil {
t.Error(err)
}
// get the document using the regular db handle again
doc, err = NewDoc([]byte("key1"), nil, nil)
if err != nil {
t.Error(err)
}
err = kvstore.Get(doc)
if err != nil {
t.Error(err)
}
// verify we get the non-updated version
if string(doc.Body()) != "value1" {
t.Errorf("expected value1, got %s", doc.Body())
}
doc.Close()
}