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_ts_test.go
99 lines (79 loc) · 2.5 KB
/
write_batch_ts_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
package grocksdb
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestWriteBatchWithTS(t *testing.T) {
db, cfs, cleanup := newTestDBMultiCF(t, []string{"default"}, func(opts *Options) {
opts.SetComparator(newDefaultComparatorWithTS())
})
defer cleanup()
defaultCF := cfs[0]
var (
givenKey1 = []byte("key1")
givenVal1 = []byte("val1")
givenKey2 = []byte("key2")
givenTs1 = marshalTimestamp(1)
givenTs2 = marshalTimestamp(2)
)
wo := NewDefaultWriteOptions()
require.Nil(t, db.PutWithTS(wo, givenKey2, givenTs1, []byte("foo")))
// create and fill the write batch
wb := NewWriteBatch()
defer wb.Destroy()
wb.PutCFWithTS(defaultCF, givenKey1, givenTs2, givenVal1)
wb.DeleteCFWithTS(defaultCF, givenKey2, givenTs2)
require.EqualValues(t, wb.Count(), 2)
// perform the batch
require.Nil(t, db.Write(wo, wb))
// check changes
ro := NewDefaultReadOptions()
ro.SetTimestamp(givenTs2)
v1, t1, err := db.GetWithTS(ro, givenKey1)
defer v1.Free()
require.Nil(t, err)
require.EqualValues(t, v1.Data(), givenVal1)
require.EqualValues(t, t1.Data(), givenTs2)
v2, t2, err := db.GetWithTS(ro, givenKey2)
defer v2.Free()
require.Nil(t, err)
require.True(t, v2.Data() == nil)
require.True(t, t2.Data() == nil)
wb.Clear()
// DeleteRange not supported for timestamp
}
func TestWriteBatchIteratorWithTS(t *testing.T) {
_, cfs, cleanup := newTestDBMultiCF(t, []string{"default"}, func(opts *Options) {
opts.SetComparator(newDefaultComparatorWithTS())
})
defer cleanup()
defaultCF := cfs[0]
var (
givenKey1 = []byte("key1")
givenVal1 = []byte("val1")
givenKey2 = []byte("key2")
givenTs1 = marshalTimestamp(1)
givenTs2 = marshalTimestamp(2)
expectedKeyWithTS1 = append(givenKey1, givenTs1...)
expectedKeyWithTS2 = append(givenKey2, givenTs2...)
)
// create and fill the write batch
wb := NewWriteBatch()
defer wb.Destroy()
wb.PutCFWithTS(defaultCF, givenKey1, givenTs1, givenVal1)
wb.DeleteCFWithTS(defaultCF, givenKey2, givenTs2)
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, expectedKeyWithTS1)
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, expectedKeyWithTS2)
// there shouldn't be any left
require.False(t, iter.Next())
}