-
Notifications
You must be signed in to change notification settings - Fork 68
/
comparator_ts_test.go
61 lines (52 loc) · 1.43 KB
/
comparator_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
package grocksdb
import (
"bytes"
"runtime"
"testing"
"github.com/stretchr/testify/require"
)
func TestComparatorWithTS(t *testing.T) {
db, opts := newTestDBAndOpts(t, func(opts *Options) {
comp := newComparatorWithTimeStamp(
"rev",
func(a, b []byte) int {
return bytes.Compare(a, b) * -1
},
)
opts.SetComparator(comp)
})
defer func() {
db.Close()
opts.Destroy()
}()
runtime.GC()
// insert keys
givenKeys := [][]byte{[]byte("key1"), []byte("key2"), []byte("key3")}
givenTimes := [][]byte{marshalTimestamp(1), marshalTimestamp(2), marshalTimestamp(3)}
wo := NewDefaultWriteOptions()
for i, k := range givenKeys {
require.Nil(t, db.PutWithTS(wo, k, givenTimes[i], []byte("val")))
runtime.GC()
}
// create a iterator to collect the keys
ro := NewDefaultReadOptions()
ro.SetTimestamp(marshalTimestamp(4))
iter := db.NewIterator(ro)
defer iter.Close()
// we seek to the last key and iterate in reverse order
// to match given keys
var actualKeys, actualTimes [][]byte
for iter.SeekToLast(); iter.Valid(); iter.Prev() {
key := make([]byte, 4)
ts := make([]byte, timestampSize)
copy(key, iter.Key().Data())
copy(ts, iter.Timestamp().Data())
actualKeys = append(actualKeys, key)
actualTimes = append(actualTimes, ts)
runtime.GC()
}
require.Nil(t, iter.Err())
// ensure that the order is correct
require.EqualValues(t, actualKeys, givenKeys)
require.EqualValues(t, actualTimes, givenTimes)
}