forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_batch_test.go
87 lines (71 loc) · 1.9 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
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 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())
}