forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtransactiondb_test.go
342 lines (292 loc) · 9.65 KB
/
transactiondb_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package gorocksdb
import (
"fmt"
"io/ioutil"
"testing"
"github.com/facebookgo/ensure"
)
func TestOpenTransactionDb(t *testing.T) {
db := newTestTransactionDB(t, "TestOpenTransactionDb", nil)
defer db.Close()
}
func TestTransactionDbColumnFamilies(t *testing.T) {
test_cf_names := []string{"default", "cf1", "cf2"}
db, cf_handles := newTestTransactionDBColumnFamilies(t, "TestOpenTransactionDbColumnFamilies", test_cf_names)
ensure.True(t, 3 == len(cf_handles))
defer db.Close()
cf_names, err := ListColumnFamilies(NewDefaultOptions(), db.name)
ensure.Nil(t, err)
ensure.True(t, 3 == len(cf_names))
ensure.DeepEqual(t, cf_names, test_cf_names)
for idx, cf_name := range test_cf_names {
ensure.Nil(t, db.PutCF(NewDefaultWriteOptions(), cf_handles[idx], []byte(cf_name+"_key"), []byte(cf_name+"_value")))
}
for idx, cf_name := range test_cf_names {
val, err := db.GetCF(NewDefaultReadOptions(), cf_handles[idx], []byte(cf_name+"_key"))
ensure.Nil(t, err)
ensure.DeepEqual(t, val.Data(), []byte(cf_name+"_value"))
}
// Delete all keys in all column families
for idx, cf_name := range test_cf_names {
ensure.Nil(t, db.DeleteCF(NewDefaultWriteOptions(), cf_handles[idx], []byte(cf_name+"_key")))
}
for idx, cf_name := range test_cf_names {
val, err := db.GetCF(NewDefaultReadOptions(), cf_handles[idx], []byte(cf_name+"_key"))
ensure.Nil(t, err)
ensure.True(t, val.Size() == 0)
}
{
cf_handle, err := db.CreateColumnFamily(NewDefaultOptions(), "new_cf")
ensure.Nil(t, err)
ensure.NotNil(t, cf_handle)
cf_names, err := ListColumnFamilies(NewDefaultOptions(), db.name)
ensure.Nil(t, err)
ensure.True(t, 4 == len(cf_names))
}
}
func TestTransactionDBCRUD(t *testing.T) {
db := newTestTransactionDB(t, "TestTransactionDBGet", nil)
defer db.Close()
var (
givenKey = []byte("hello")
givenVal1 = []byte("world1")
givenVal2 = []byte("world2")
givenTxnKey = []byte("hello2")
givenTxnKey2 = []byte("hello3")
givenTxnVal1 = []byte("whatawonderful")
wo = NewDefaultWriteOptions()
ro = NewDefaultReadOptions()
to = NewDefaultTransactionOptions()
)
// create
ensure.Nil(t, db.Put(wo, givenKey, givenVal1))
// retrieve
v1, err := db.Get(ro, givenKey)
defer v1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v1.Data(), givenVal1)
// update
ensure.Nil(t, db.Put(wo, givenKey, givenVal2))
v2, err := db.Get(ro, givenKey)
defer v2.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v2.Data(), givenVal2)
// delete
ensure.Nil(t, db.Delete(wo, givenKey))
v3, err := db.Get(ro, givenKey)
defer v3.Free()
ensure.Nil(t, err)
ensure.True(t, v3.Data() == nil)
// transaction
txn := db.TransactionBegin(wo, to, nil)
defer txn.Destroy()
// create
ensure.Nil(t, txn.Put(givenTxnKey, givenTxnVal1))
v4, err := txn.Get(ro, givenTxnKey)
defer v4.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v4.Data(), givenTxnVal1)
ensure.Nil(t, txn.Commit())
v5, err := db.Get(ro, givenTxnKey)
defer v5.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v5.Data(), givenTxnVal1)
// transaction
txn2 := db.TransactionBegin(wo, to, nil)
defer txn2.Destroy()
// create
ensure.Nil(t, txn2.Put(givenTxnKey2, givenTxnVal1))
// rollback
ensure.Nil(t, txn2.Rollback())
v6, err := txn2.Get(ro, givenTxnKey2)
defer v6.Free()
ensure.Nil(t, err)
ensure.True(t, v6.Data() == nil)
// transaction
txn3 := db.TransactionBegin(wo, to, nil)
defer txn3.Destroy()
// delete
ensure.Nil(t, txn3.Delete(givenTxnKey))
ensure.Nil(t, txn3.Commit())
v7, err := db.Get(ro, givenTxnKey)
defer v7.Free()
ensure.Nil(t, err)
ensure.True(t, v7.Data() == nil)
}
func TestTransactionDBWriteBatchColumnFamilies(t *testing.T) {
test_cf_names := []string{"default", "cf1", "cf2"}
db, cf_handles := newTestTransactionDBColumnFamilies(t, "TestOpenTransactionDbColumnFamilies", test_cf_names)
ensure.True(t, len(cf_handles) == 3)
defer db.Close()
var (
wo = NewDefaultWriteOptions()
ro = NewDefaultReadOptions()
)
// WriteBatch PutCF
{
batch := NewWriteBatch()
for h_idx := 1; h_idx <= 2; h_idx++ {
for k_idx := 0; k_idx <= 2; k_idx++ {
batch.PutCF(cf_handles[h_idx], []byte(fmt.Sprintf("%s_key_%d", test_cf_names[h_idx], k_idx)),
[]byte(fmt.Sprintf("%s_value_%d", test_cf_names[h_idx], k_idx)))
}
}
ensure.Nil(t, db.Write(wo, batch))
batch.Destroy()
}
// Read back
{
for h_idx := 1; h_idx <= 2; h_idx++ {
for k_idx := 0; k_idx <= 2; k_idx++ {
data, err := db.GetCF(ro, cf_handles[h_idx], []byte(fmt.Sprintf("%s_key_%d", test_cf_names[h_idx], k_idx)))
ensure.Nil(t, err)
ensure.DeepEqual(t, data.Data(), []byte(fmt.Sprintf("%s_value_%d", test_cf_names[h_idx], k_idx)))
}
}
}
// WriteBatch DeleteCF (DeleteRangeCF not implemented)
{
batch := NewWriteBatch()
batch.DeleteCF(cf_handles[1], []byte(test_cf_names[1]+"_key_0"))
batch.DeleteCF(cf_handles[1], []byte(test_cf_names[1]+"_key_1"))
ensure.Nil(t, db.Write(wo, batch))
}
// Read back the remaining keys
{
// All keys on "cf2" are still there.
// Only key2 on "cf1" still remains
for h_idx := 1; h_idx <= 2; h_idx++ {
for k_idx := 0; k_idx <= 2; k_idx++ {
data, err := db.GetCF(ro, cf_handles[h_idx], []byte(fmt.Sprintf("%s_key_%d", test_cf_names[h_idx], k_idx)))
ensure.Nil(t, err)
if h_idx == 2 || k_idx == 2 {
ensure.DeepEqual(t, data.Data(), []byte(fmt.Sprintf("%s_value_%d", test_cf_names[h_idx], k_idx)))
} else {
ensure.True(t, len(data.Data()) == 0)
}
}
}
}
}
func TestTransactionDBCRUDColumnFamilies(t *testing.T) {
test_cf_names := []string{"default", "cf1", "cf2"}
db, cf_handles := newTestTransactionDBColumnFamilies(t, "TestOpenTransactionDbColumnFamilies", test_cf_names)
ensure.True(t, len(cf_handles) == 3)
defer db.Close()
var (
wo = NewDefaultWriteOptions()
ro = NewDefaultReadOptions()
to = NewDefaultTransactionOptions()
)
{
txn := db.TransactionBegin(wo, to, nil)
defer txn.Destroy()
// RYW.
for idx, cf_handle := range cf_handles {
ensure.Nil(t, txn.PutCF(cf_handle, []byte(test_cf_names[idx]+"_key"), []byte(test_cf_names[idx]+"_value")))
val, err := txn.GetCF(ro, cf_handle, []byte(test_cf_names[idx]+"_key"))
defer val.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, val.Data(), []byte(test_cf_names[idx]+"_value"))
}
txn.Commit()
}
// Read after commit
for idx, cf_handle := range cf_handles {
val, err := db.GetCF(ro, cf_handle, []byte(test_cf_names[idx]+"_key"))
defer val.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, val.Data(), []byte(test_cf_names[idx]+"_value"))
}
// Delete
{
txn := db.TransactionBegin(wo, to, nil)
defer txn.Destroy()
// RYW.
for idx, cf_handle := range cf_handles {
ensure.Nil(t, txn.DeleteCF(cf_handle, []byte(test_cf_names[idx]+"_key")))
}
txn.Commit()
}
// Read after delete commit
for idx, cf_handle := range cf_handles {
val, err := db.GetCF(ro, cf_handle, []byte(test_cf_names[idx]+"_key"))
defer val.Free()
ensure.Nil(t, err)
ensure.True(t, val.Size() == 0)
}
}
func TestTransactionDBGetForUpdate(t *testing.T) {
lockTimeoutMilliSec := int64(50)
applyOpts := func(opts *Options, transactionDBOpts *TransactionDBOptions) {
transactionDBOpts.SetTransactionLockTimeout(lockTimeoutMilliSec)
}
db := newTestTransactionDB(t, "TestOpenTransactionDb", applyOpts)
defer db.Close()
var (
givenKey = []byte("hello")
givenVal = []byte("world")
wo = NewDefaultWriteOptions()
ro = NewDefaultReadOptions()
to = NewDefaultTransactionOptions()
)
txn := db.TransactionBegin(wo, to, nil)
defer txn.Destroy()
v, err := txn.GetForUpdate(ro, givenKey)
defer v.Free()
ensure.Nil(t, err)
// expect lock timeout error to be thrown
if err := db.Put(wo, givenKey, givenVal); err == nil {
t.Error("expect locktime out error, got nil error")
}
}
func TestTransactionDBGetForUpdateColumnFamilies(t *testing.T) {
test_cf_names := []string{"default", "cf1", "cf2"}
db, cf_handles := newTestTransactionDBColumnFamilies(t, "TestOpenTransactionDbColumnFamilies", test_cf_names)
ensure.True(t, 3 == len(cf_handles))
defer db.Close()
var (
wo = NewDefaultWriteOptions()
ro = NewDefaultReadOptions()
to = NewDefaultTransactionOptions()
)
{
txn := db.TransactionBegin(wo, to, nil)
defer txn.Destroy()
val, err := txn.GetForUpdateCF(ro, cf_handles[1], []byte(test_cf_names[1]+"_key"))
defer val.Free()
ensure.Nil(t, err)
txn.PutCF(cf_handles[1], []byte(test_cf_names[1]+"_key"), []byte(test_cf_names[1]+"_value"))
ensure.Nil(t, txn.Commit())
}
// Read after update
val, err := db.GetCF(ro, cf_handles[1], []byte(test_cf_names[1]+"_key"))
ensure.Nil(t, err)
ensure.DeepEqual(t, val.Data(), []byte(test_cf_names[1]+"_value"))
}
func newTestTransactionDB(t *testing.T, name string, applyOpts func(opts *Options, transactionDBOpts *TransactionDBOptions)) *TransactionDB {
dir, err := ioutil.TempDir("", "gorockstransactiondb-"+name)
ensure.Nil(t, err)
opts := NewDefaultOptions()
opts.SetCreateIfMissing(true)
transactionDBOpts := NewDefaultTransactionDBOptions()
if applyOpts != nil {
applyOpts(opts, transactionDBOpts)
}
db, err := OpenTransactionDb(opts, transactionDBOpts, dir)
ensure.Nil(t, err)
return db
}
func newTestTransactionDBColumnFamilies(t *testing.T, name string, cfNames []string) (*TransactionDB, []*ColumnFamilyHandle) {
dir, err := ioutil.TempDir("", "gorockstransactiondb-"+name)
ensure.Nil(t, err)
opts := NewDefaultOptions()
opts.SetCreateIfMissing(true)
opts.SetCreateIfMissingColumnFamilies(true)
transactionDBOpts := NewDefaultTransactionDBOptions()
cfOpts := []*Options{opts, opts, opts}
db, cfHandles, err := OpenTransactionDbColumnFamilies(opts, transactionDBOpts, dir, cfNames, cfOpts)
ensure.Nil(t, err)
ensure.True(t, 3 == len(cfHandles))
return db, cfHandles
}