2020-05-15 16:59:37 +02:00
|
|
|
// +build kvdb_etcd
|
|
|
|
|
2020-02-18 19:35:53 +01:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
2021-02-09 20:19:31 +01:00
|
|
|
"context"
|
2020-02-18 19:35:53 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2020-06-29 14:53:38 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-02-18 19:35:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadCursorEmptyInterval(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
2021-02-09 20:19:31 +01:00
|
|
|
db, err := newEtcdBackend(context.TODO(), f.BackendConfig())
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
2020-06-29 15:09:35 +02:00
|
|
|
b, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
return nil
|
2020-10-26 14:06:32 +01:00
|
|
|
}, func() {})
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
err = db.View(func(tx walletdb.ReadTx) error {
|
2020-06-29 15:09:35 +02:00
|
|
|
b := tx.ReadBucket([]byte("apple"))
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NotNil(t, b)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
cursor := b.ReadCursor()
|
|
|
|
k, v := cursor.First()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
k, v = cursor.Next()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
k, v = cursor.Last()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
k, v = cursor.Prev()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
return nil
|
2020-10-20 16:18:40 +02:00
|
|
|
}, func() {})
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-02-18 19:35:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadCursorNonEmptyInterval(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
2021-02-09 20:19:31 +01:00
|
|
|
db, err := newEtcdBackend(context.TODO(), f.BackendConfig())
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
testKeyValues := []KV{
|
|
|
|
{"b", "1"},
|
|
|
|
{"c", "2"},
|
|
|
|
{"da", "3"},
|
|
|
|
{"e", "4"},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
2020-06-29 15:09:35 +02:00
|
|
|
b, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
for _, kv := range testKeyValues {
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, b.Put([]byte(kv.key), []byte(kv.val)))
|
2020-02-18 19:35:53 +01:00
|
|
|
}
|
|
|
|
return nil
|
2020-10-26 14:06:32 +01:00
|
|
|
}, func() {})
|
2020-02-18 19:35:53 +01:00
|
|
|
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
err = db.View(func(tx walletdb.ReadTx) error {
|
2020-06-29 15:09:35 +02:00
|
|
|
b := tx.ReadBucket([]byte("apple"))
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NotNil(t, b)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Iterate from the front.
|
|
|
|
var kvs []KV
|
|
|
|
cursor := b.ReadCursor()
|
|
|
|
k, v := cursor.First()
|
|
|
|
|
|
|
|
for k != nil && v != nil {
|
|
|
|
kvs = append(kvs, KV{string(k), string(v)})
|
|
|
|
k, v = cursor.Next()
|
|
|
|
}
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, testKeyValues, kvs)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Iterate from the back.
|
|
|
|
kvs = []KV{}
|
|
|
|
k, v = cursor.Last()
|
|
|
|
|
|
|
|
for k != nil && v != nil {
|
|
|
|
kvs = append(kvs, KV{string(k), string(v)})
|
|
|
|
k, v = cursor.Prev()
|
|
|
|
}
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, reverseKVs(testKeyValues), kvs)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Random access
|
|
|
|
perm := []int{3, 0, 2, 1}
|
|
|
|
for _, i := range perm {
|
|
|
|
k, v := cursor.Seek([]byte(testKeyValues[i].key))
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, []byte(testKeyValues[i].key), k)
|
|
|
|
require.Equal(t, []byte(testKeyValues[i].val), v)
|
2020-02-18 19:35:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to nonexisting key.
|
|
|
|
k, v = cursor.Seek(nil)
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
k, v = cursor.Seek([]byte("x"))
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
return nil
|
2020-10-20 16:18:40 +02:00
|
|
|
}, func() {})
|
2020-02-18 19:35:53 +01:00
|
|
|
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-02-18 19:35:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadWriteCursor(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
2021-02-09 20:19:31 +01:00
|
|
|
db, err := newEtcdBackend(context.TODO(), f.BackendConfig())
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
testKeyValues := []KV{
|
|
|
|
{"b", "1"},
|
|
|
|
{"c", "2"},
|
|
|
|
{"da", "3"},
|
|
|
|
{"e", "4"},
|
|
|
|
}
|
|
|
|
|
|
|
|
count := len(testKeyValues)
|
|
|
|
|
|
|
|
// Pre-store the first half of the interval.
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, db.Update(func(tx walletdb.ReadWriteTx) error {
|
2020-02-18 19:35:53 +01:00
|
|
|
b, err := tx.CreateTopLevelBucket([]byte("apple"))
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
for i := 0; i < count/2; i++ {
|
|
|
|
err = b.Put(
|
|
|
|
[]byte(testKeyValues[i].key),
|
|
|
|
[]byte(testKeyValues[i].val),
|
|
|
|
)
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-02-18 19:35:53 +01:00
|
|
|
}
|
|
|
|
return nil
|
2020-10-26 14:06:32 +01:00
|
|
|
}, func() {}))
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
err = db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
b := tx.ReadWriteBucket([]byte("apple"))
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NotNil(t, b)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Store the second half of the interval.
|
|
|
|
for i := count / 2; i < count; i++ {
|
|
|
|
err = b.Put(
|
|
|
|
[]byte(testKeyValues[i].key),
|
|
|
|
[]byte(testKeyValues[i].val),
|
|
|
|
)
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-02-18 19:35:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cursor := b.ReadWriteCursor()
|
|
|
|
|
|
|
|
// First on valid interval.
|
|
|
|
fk, fv := cursor.First()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, []byte("b"), fk)
|
|
|
|
require.Equal(t, []byte("1"), fv)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Prev(First()) = nil
|
|
|
|
k, v := cursor.Prev()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Last on valid interval.
|
|
|
|
lk, lv := cursor.Last()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, []byte("e"), lk)
|
|
|
|
require.Equal(t, []byte("4"), lv)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Next(Last()) = nil
|
|
|
|
k, v = cursor.Next()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Nil(t, k)
|
|
|
|
require.Nil(t, v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Delete first item, then add an item before the
|
|
|
|
// deleted one. Check that First/Next will "jump"
|
|
|
|
// over the deleted item and return the new first.
|
|
|
|
_, _ = cursor.First()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, cursor.Delete())
|
|
|
|
require.NoError(t, b.Put([]byte("a"), []byte("0")))
|
2020-02-18 19:35:53 +01:00
|
|
|
fk, fv = cursor.First()
|
|
|
|
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, []byte("a"), fk)
|
|
|
|
require.Equal(t, []byte("0"), fv)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
k, v = cursor.Next()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, []byte("c"), k)
|
|
|
|
require.Equal(t, []byte("2"), v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Similarly test that a new end is returned if
|
|
|
|
// the old end is deleted first.
|
|
|
|
_, _ = cursor.Last()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, cursor.Delete())
|
|
|
|
require.NoError(t, b.Put([]byte("f"), []byte("5")))
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
lk, lv = cursor.Last()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, []byte("f"), lk)
|
|
|
|
require.Equal(t, []byte("5"), lv)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
k, v = cursor.Prev()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, []byte("da"), k)
|
|
|
|
require.Equal(t, []byte("3"), v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Overwrite k/v in the middle of the interval.
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, b.Put([]byte("c"), []byte("3")))
|
2020-02-18 19:35:53 +01:00
|
|
|
k, v = cursor.Prev()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, []byte("c"), k)
|
|
|
|
require.Equal(t, []byte("3"), v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Insert new key/values.
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, b.Put([]byte("cx"), []byte("x")))
|
|
|
|
require.NoError(t, b.Put([]byte("cy"), []byte("y")))
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
k, v = cursor.Next()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, []byte("cx"), k)
|
|
|
|
require.Equal(t, []byte("x"), v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
k, v = cursor.Next()
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, []byte("cy"), k)
|
|
|
|
require.Equal(t, []byte("y"), v)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
expected := []KV{
|
|
|
|
{"a", "0"},
|
|
|
|
{"c", "3"},
|
|
|
|
{"cx", "x"},
|
|
|
|
{"cy", "y"},
|
|
|
|
{"da", "3"},
|
|
|
|
{"f", "5"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate from the front.
|
|
|
|
var kvs []KV
|
|
|
|
k, v = cursor.First()
|
|
|
|
|
|
|
|
for k != nil && v != nil {
|
|
|
|
kvs = append(kvs, KV{string(k), string(v)})
|
|
|
|
k, v = cursor.Next()
|
|
|
|
}
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, expected, kvs)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
// Iterate from the back.
|
|
|
|
kvs = []KV{}
|
|
|
|
k, v = cursor.Last()
|
|
|
|
|
|
|
|
for k != nil && v != nil {
|
|
|
|
kvs = append(kvs, KV{string(k), string(v)})
|
|
|
|
k, v = cursor.Prev()
|
|
|
|
}
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, reverseKVs(expected), kvs)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
return nil
|
2020-10-26 14:06:32 +01:00
|
|
|
}, func() {})
|
2020-02-18 19:35:53 +01:00
|
|
|
|
2020-06-29 14:53:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-02-18 19:35:53 +01:00
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
bkey("apple"): bval("apple"),
|
|
|
|
vkey("a", "apple"): "0",
|
|
|
|
vkey("c", "apple"): "3",
|
|
|
|
vkey("cx", "apple"): "x",
|
|
|
|
vkey("cy", "apple"): "y",
|
|
|
|
vkey("da", "apple"): "3",
|
|
|
|
vkey("f", "apple"): "5",
|
|
|
|
}
|
2020-06-29 14:53:38 +02:00
|
|
|
require.Equal(t, expected, f.Dump())
|
2020-02-18 19:35:53 +01:00
|
|
|
}
|
2020-06-26 20:08:08 +02:00
|
|
|
|
|
|
|
// TestReadWriteCursorWithBucketAndValue tests that cursors are able to iterate
|
|
|
|
// over both bucket and value keys if both are present in the iterated bucket.
|
|
|
|
func TestReadWriteCursorWithBucketAndValue(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := NewEtcdTestFixture(t)
|
|
|
|
defer f.Cleanup()
|
|
|
|
|
2021-02-09 20:19:31 +01:00
|
|
|
db, err := newEtcdBackend(context.TODO(), f.BackendConfig())
|
2020-06-26 20:08:08 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Pre-store the first half of the interval.
|
|
|
|
require.NoError(t, db.Update(func(tx walletdb.ReadWriteTx) error {
|
|
|
|
b, err := tx.CreateTopLevelBucket([]byte("apple"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b)
|
|
|
|
|
|
|
|
require.NoError(t, b.Put([]byte("key"), []byte("val")))
|
|
|
|
|
|
|
|
b1, err := b.CreateBucket([]byte("banana"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b1)
|
|
|
|
|
|
|
|
b2, err := b.CreateBucket([]byte("pear"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, b2)
|
|
|
|
|
|
|
|
return nil
|
2020-10-26 14:06:32 +01:00
|
|
|
}, func() {}))
|
2020-06-26 20:08:08 +02:00
|
|
|
|
|
|
|
err = db.View(func(tx walletdb.ReadTx) error {
|
|
|
|
b := tx.ReadBucket([]byte("apple"))
|
|
|
|
require.NotNil(t, b)
|
|
|
|
|
|
|
|
cursor := b.ReadCursor()
|
|
|
|
|
|
|
|
// First on valid interval.
|
|
|
|
k, v := cursor.First()
|
|
|
|
require.Equal(t, []byte("banana"), k)
|
|
|
|
require.Nil(t, v)
|
|
|
|
|
|
|
|
k, v = cursor.Next()
|
|
|
|
require.Equal(t, []byte("key"), k)
|
|
|
|
require.Equal(t, []byte("val"), v)
|
|
|
|
|
|
|
|
k, v = cursor.Last()
|
|
|
|
require.Equal(t, []byte("pear"), k)
|
|
|
|
require.Nil(t, v)
|
|
|
|
|
|
|
|
k, v = cursor.Seek([]byte("k"))
|
|
|
|
require.Equal(t, []byte("key"), k)
|
|
|
|
require.Equal(t, []byte("val"), v)
|
|
|
|
|
|
|
|
k, v = cursor.Seek([]byte("banana"))
|
|
|
|
require.Equal(t, []byte("banana"), k)
|
|
|
|
require.Nil(t, v)
|
|
|
|
|
|
|
|
k, v = cursor.Next()
|
|
|
|
require.Equal(t, []byte("key"), k)
|
|
|
|
require.Equal(t, []byte("val"), v)
|
|
|
|
|
|
|
|
return nil
|
2020-10-20 16:18:40 +02:00
|
|
|
}, func() {})
|
2020-06-26 20:08:08 +02:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
bkey("apple"): bval("apple"),
|
|
|
|
bkey("apple", "banana"): bval("apple", "banana"),
|
|
|
|
bkey("apple", "pear"): bval("apple", "pear"),
|
|
|
|
vkey("key", "apple"): "val",
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, f.Dump())
|
|
|
|
}
|