forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_batch_test.go
124 lines (102 loc) · 2.67 KB
/
write_batch_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
package gorocksdb
import (
"testing"
"github.com/facebookgo/ensure"
)
func TestWriteBatch(t *testing.T) {
db := newTestDB(t, "TestWriteBatch", nil)
defer db.Close()
var (
givenKey1 = []byte("key1")
givenVal1 = []byte("val1")
givenKey2 = []byte("key2")
)
wo := NewDefaultWriteOptions()
ensure.Nil(t, db.Put(wo, givenKey2, []byte("foo")))
// create and fill the write batch
wb := NewWriteBatch()
defer wb.Destroy()
wb.Put(givenKey1, givenVal1)
wb.Delete(givenKey2)
ensure.DeepEqual(t, wb.Count(), 2)
// perform the batch
ensure.Nil(t, db.Write(wo, wb))
// check changes
ro := NewDefaultReadOptions()
v1, err := db.Get(ro, givenKey1)
defer v1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v1.Data(), givenVal1)
v2, err := db.Get(ro, givenKey2)
defer v2.Free()
ensure.Nil(t, err)
ensure.True(t, v2.Data() == nil)
// DeleteRange test
wb.Clear()
wb.DeleteRange(givenKey1, givenKey2)
// perform the batch
ensure.Nil(t, db.Write(wo, wb))
v1, err = db.Get(ro, givenKey1)
defer v1.Free()
ensure.Nil(t, err)
ensure.True(t, v1.Data() == nil)
}
func TestWriteBatchPutMany(t *testing.T) {
db := newTestDB(t, "TestWriteBatchPutMany", nil)
defer db.Close()
var (
key1 = []byte("key1")
val1 = []byte("val1")
key2 = []byte("key22")
val2 = []byte("val22")
)
wo := NewDefaultWriteOptions()
defer wo.Destroy()
// create and fill the write batch
keys := [][]byte{key1, key2}
values := [][]byte{val1, val2}
wb := NewWriteBatch()
defer wb.Destroy()
wb.PutMany(keys, values)
// ensure.DeepEqual(t, wb.Count(), 2)
// perform the batch
ensure.Nil(t, db.Write(wo, wb))
// check changes
ro := NewDefaultReadOptions()
v1, err := db.Get(ro, key1)
defer v1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v1.Data(), val1)
v2, err := db.Get(ro, key2)
defer v2.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v2.Data(), val2)
}
func TestWriteBatchIterator(t *testing.T) {
db := newTestDB(t, "TestWriteBatchIterator", nil)
defer db.Close()
var (
givenKey1 = []byte("key1")
givenVal1 = []byte("val1")
givenKey2 = []byte("key2")
)
// create and fill the write batch
wb := NewWriteBatch()
defer wb.Destroy()
wb.Put(givenKey1, givenVal1)
wb.Delete(givenKey2)
ensure.DeepEqual(t, wb.Count(), 2)
// iterate over the batch
iter := wb.NewIterator()
ensure.True(t, iter.Next())
record := iter.Record()
ensure.DeepEqual(t, record.Type, WriteBatchValueRecord)
ensure.DeepEqual(t, record.Key, givenKey1)
ensure.DeepEqual(t, record.Value, givenVal1)
ensure.True(t, iter.Next())
record = iter.Record()
ensure.DeepEqual(t, record.Type, WriteBatchDeletionRecord)
ensure.DeepEqual(t, record.Key, givenKey2)
// there shouldn't be any left
ensure.False(t, iter.Next())
}