-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot_test.go
155 lines (128 loc) · 3.12 KB
/
snapshot_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
package leveldb_test
import (
"bytes"
"testing"
"github.com/robaho/leveldb"
)
func TestSnapshot_Get(t *testing.T) {
leveldb.Remove("test/mydb")
db, err := leveldb.Open("test/mydb", options)
if err != nil {
t.Fatal("unable to create database", err)
}
err = db.Put([]byte("mykey"), []byte("myvalue"))
if err != nil {
t.Fatal("unable to put key/value", err)
}
s, err := db.Snapshot()
if err != nil {
t.Fatal("unable to get snapshot", err)
}
err = db.Put([]byte("mykey1"), []byte("myvalue1"))
if err != nil {
t.Fatal("unable to put key/value", err)
}
val, err := s.Get([]byte("mykey"))
if err != nil {
t.Fatal("unable to get key/value", err)
}
if !bytes.Equal([]byte("myvalue"), val) {
t.Fatal("value does not match")
}
val, err = s.Get([]byte("mykey1"))
if val != nil || err != leveldb.KeyNotFound {
t.Fatal("should have return nil,KeyNotFound")
}
db.Close()
}
func TestSnapshot_Lookup(t *testing.T) {
leveldb.Remove("test/mydb")
db, err := leveldb.Open("test/mydb", options)
if err != nil {
t.Fatal("unable to create database", err)
}
err = db.Put([]byte("mykey"), []byte("myvalue"))
if err != nil {
t.Fatal("unable to put key/value", err)
}
s, err := db.Snapshot()
if err != nil {
t.Fatal("unable to get snapshot", err)
}
err = db.Put([]byte("mykey1"), []byte("myvalue1"))
if err != nil {
t.Fatal("unable to put key/value", err)
}
itr, err := s.Lookup(nil, nil)
if err != nil {
t.Fatal("unable to lookup() on snapshot", err)
}
k, v, err := itr.Next()
if err != nil {
t.Fatal("unable to Next()", err)
}
if !bytes.Equal([]byte("mykey"), k) {
t.Fatal("key does not match")
}
if !bytes.Equal([]byte("myvalue"), v) {
t.Fatal("key does not match")
}
_, _, err = itr.Next()
if err != leveldb.EndOfIterator {
t.Fatal("should of seen EndOfIterator")
}
db.Close()
}
func TestSnapshot_DbClose(t *testing.T) {
leveldb.Remove("test/mydb")
db, err := leveldb.Open("test/mydb", options)
if err != nil {
t.Fatal("unable to create database", err)
}
err = db.Put([]byte("mykey"), []byte("myvalue"))
if err != nil {
t.Fatal("unable to put key/value", err)
}
s, err := db.Snapshot()
if err != nil {
t.Fatal("unable to get snapshot", err)
}
_, err = s.Get([]byte("mykey"))
if err != nil {
t.Fatal("unable to get key/value", err)
}
db.Close()
_, err = s.Get([]byte("mykey"))
if err != leveldb.SnapshotClosed {
t.Fatal("should have been closed", err)
}
}
func TestSnapshot_Close(t *testing.T) {
leveldb.Remove("test/mydb")
db, err := leveldb.Open("test/mydb", options)
if err != nil {
t.Fatal("unable to create database", err)
}
err = db.Put([]byte("mykey"), []byte("myvalue"))
if err != nil {
t.Fatal("unable to put key/value", err)
}
s, err := db.Snapshot()
if err != nil {
t.Fatal("unable to get snapshot", err)
}
err = db.Put([]byte("mykey1"), []byte("myvalue1"))
if err != nil {
t.Fatal("unable to put key/value", err)
}
_, err = s.Get([]byte("mykey"))
if err != nil {
t.Fatal("unable to get key/value", err)
}
s.Close()
_, err = s.Get([]byte("mykey"))
if err != leveldb.SnapshotClosed {
t.Fatal("should have been closed", err)
}
db.Close()
}