This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
forked from linxGnu/grocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_batch_test.go
87 lines (71 loc) · 1.89 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 grocksdb
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestWriteBatch(t *testing.T) {
db := newTestDB(t, nil)
defer db.Close()
var (
givenKey1 = []byte("key1")
givenVal1 = []byte("val1")
givenKey2 = []byte("key2")
)
wo := NewDefaultWriteOptions()
require.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)
require.EqualValues(t, wb.Count(), 2)
// perform the batch
require.Nil(t, db.Write(wo, wb))
// check changes
ro := NewDefaultReadOptions()
v1, err := db.Get(ro, givenKey1)
defer v1.Free()
require.Nil(t, err)
require.EqualValues(t, v1.Data(), givenVal1)
v2, err := db.Get(ro, givenKey2)
defer v2.Free()
require.Nil(t, err)
require.True(t, v2.Data() == nil)
// DeleteRange test
wb.Clear()
wb.DeleteRange(givenKey1, givenKey2)
// perform the batch
require.Nil(t, db.Write(wo, wb))
v1, err = db.Get(ro, givenKey1)
defer v1.Free()
require.Nil(t, err)
require.True(t, v1.Data() == nil)
}
func TestWriteBatchIterator(t *testing.T) {
db := newTestDB(t, 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)
require.EqualValues(t, wb.Count(), 2)
// iterate over the batch
iter := wb.NewIterator()
require.True(t, iter.Next())
record := iter.Record()
require.EqualValues(t, record.Type, WriteBatchValueRecord)
require.EqualValues(t, record.Key, givenKey1)
require.EqualValues(t, record.Value, givenVal1)
require.True(t, iter.Next())
record = iter.Record()
require.EqualValues(t, record.Type, WriteBatchDeletionRecord)
require.EqualValues(t, record.Key, givenKey2)
// there shouldn't be any left
require.False(t, iter.Next())
}