2021-10-29 13:13:15 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 11:13:52 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-29 13:13:15 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:13:52 +08:00
|
|
|
//
|
2021-10-29 13:13:15 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-04-19 11:13:52 +08:00
|
|
|
|
2021-01-11 18:35:54 +08:00
|
|
|
package rocksdbkv_test
|
|
|
|
|
|
|
|
import (
|
2021-12-29 10:08:47 +08:00
|
|
|
"os"
|
2021-08-16 18:46:10 +08:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
2021-01-11 18:35:54 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-04-06 19:14:32 +08:00
|
|
|
|
|
|
|
rocksdbkv "github.com/milvus-io/milvus/internal/kv/rocksdb"
|
2021-01-11 18:35:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRocksdbKV(t *testing.T) {
|
|
|
|
name := "/tmp/rocksdb"
|
|
|
|
rocksdbKV, err := rocksdbkv.NewRocksdbKV(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-29 10:08:47 +08:00
|
|
|
defer os.RemoveAll(name)
|
2021-01-11 18:35:54 +08:00
|
|
|
defer rocksdbKV.Close()
|
|
|
|
// Need to call RemoveWithPrefix
|
|
|
|
defer rocksdbKV.RemoveWithPrefix("")
|
|
|
|
|
|
|
|
err = rocksdbKV.Save("abc", "123")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-11 18:35:54 +08:00
|
|
|
|
2022-02-25 17:19:53 +08:00
|
|
|
err = rocksdbKV.SaveBytes("abcd", []byte("1234"))
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-11 18:35:54 +08:00
|
|
|
|
|
|
|
val, err := rocksdbKV.Load("abc")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-11 18:35:54 +08:00
|
|
|
assert.Equal(t, val, "123")
|
2022-02-25 17:19:53 +08:00
|
|
|
value, err := rocksdbKV.LoadBytes("abc")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, value, []byte("123"))
|
|
|
|
|
|
|
|
_, err = rocksdbKV.Load("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = rocksdbKV.LoadBytes("")
|
|
|
|
assert.Error(t, err)
|
2021-01-11 18:35:54 +08:00
|
|
|
|
|
|
|
keys, vals, err := rocksdbKV.LoadWithPrefix("abc")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-11 18:35:54 +08:00
|
|
|
assert.Equal(t, len(keys), len(vals))
|
|
|
|
assert.Equal(t, len(keys), 2)
|
|
|
|
|
|
|
|
assert.Equal(t, keys[0], "abc")
|
|
|
|
assert.Equal(t, keys[1], "abcd")
|
|
|
|
assert.Equal(t, vals[0], "123")
|
|
|
|
assert.Equal(t, vals[1], "1234")
|
|
|
|
|
2022-02-25 17:19:53 +08:00
|
|
|
var values [][]byte
|
|
|
|
keys, values, err = rocksdbKV.LoadBytesWithPrefix("abc")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(keys), len(vals))
|
|
|
|
assert.Equal(t, len(keys), 2)
|
|
|
|
|
|
|
|
assert.Equal(t, keys[0], "abc")
|
|
|
|
assert.Equal(t, keys[1], "abcd")
|
|
|
|
assert.Equal(t, values[0], []byte("123"))
|
|
|
|
assert.Equal(t, values[1], []byte("1234"))
|
|
|
|
|
2021-01-11 18:35:54 +08:00
|
|
|
err = rocksdbKV.Save("key_1", "123")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-11 18:35:54 +08:00
|
|
|
err = rocksdbKV.Save("key_2", "456")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-11 18:35:54 +08:00
|
|
|
err = rocksdbKV.Save("key_3", "789")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-11 18:35:54 +08:00
|
|
|
|
|
|
|
keys = []string{"key_1", "key_2"}
|
|
|
|
vals, err = rocksdbKV.MultiLoad(keys)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-11 18:35:54 +08:00
|
|
|
assert.Equal(t, len(vals), len(keys))
|
|
|
|
assert.Equal(t, vals[0], "123")
|
|
|
|
assert.Equal(t, vals[1], "456")
|
2021-09-28 17:48:08 +08:00
|
|
|
|
2022-02-25 17:19:53 +08:00
|
|
|
values, err = rocksdbKV.MultiLoadBytes(keys)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-02-25 17:19:53 +08:00
|
|
|
assert.Equal(t, len(values), len(keys))
|
|
|
|
assert.Equal(t, values[0], []byte("123"))
|
|
|
|
assert.Equal(t, values[1], []byte("456"))
|
|
|
|
|
2021-09-28 17:48:08 +08:00
|
|
|
err = rocksdbKV.MultiRemove(keys)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
saves := map[string]string{
|
|
|
|
"s_1": "111",
|
|
|
|
"s_2": "222",
|
|
|
|
"s_3": "333",
|
|
|
|
}
|
|
|
|
removals := []string{"key_3"}
|
|
|
|
err = rocksdbKV.MultiSaveAndRemove(saves, removals)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = rocksdbKV.DeleteRange("s_1", "s_3")
|
|
|
|
assert.NoError(t, err)
|
2021-01-11 18:35:54 +08:00
|
|
|
}
|
2021-01-12 18:03:24 +08:00
|
|
|
|
|
|
|
func TestRocksdbKV_Prefix(t *testing.T) {
|
|
|
|
name := "/tmp/rocksdb"
|
|
|
|
rocksdbKV, err := rocksdbkv.NewRocksdbKV(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-29 10:08:47 +08:00
|
|
|
defer os.RemoveAll(name)
|
2021-01-12 18:03:24 +08:00
|
|
|
defer rocksdbKV.Close()
|
|
|
|
// Need to call RemoveWithPrefix
|
|
|
|
defer rocksdbKV.RemoveWithPrefix("")
|
|
|
|
|
2022-02-25 17:19:53 +08:00
|
|
|
kvs := map[string]string{
|
|
|
|
"abcd": "123",
|
|
|
|
"abdd": "1234",
|
|
|
|
"abddqqq": "1234555",
|
|
|
|
}
|
|
|
|
err = rocksdbKV.MultiSave(kvs)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-12 18:03:24 +08:00
|
|
|
|
2022-02-25 17:19:53 +08:00
|
|
|
keys, vals, err := rocksdbKV.LoadWithPrefix("abc")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-12 18:03:24 +08:00
|
|
|
|
2022-02-25 17:19:53 +08:00
|
|
|
assert.Equal(t, len(keys), 1)
|
|
|
|
assert.Equal(t, len(vals), 1)
|
|
|
|
assert.Equal(t, keys[0], "abcd")
|
|
|
|
assert.Equal(t, vals[0], "123")
|
2021-01-12 18:03:24 +08:00
|
|
|
|
2022-02-25 17:19:53 +08:00
|
|
|
bytesKvs := map[string][]byte{}
|
|
|
|
for k, v := range kvs {
|
|
|
|
rocksdbKV.Remove(k)
|
|
|
|
bytesKvs[k] = []byte(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rocksdbKV.MultiSaveBytes(bytesKvs)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-17 23:44:42 +08:00
|
|
|
|
2022-02-25 17:19:53 +08:00
|
|
|
var values [][]byte
|
|
|
|
keys, values, err = rocksdbKV.LoadBytesWithPrefix("abc")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-12 18:03:24 +08:00
|
|
|
assert.Equal(t, len(keys), 1)
|
2022-02-25 17:19:53 +08:00
|
|
|
assert.Equal(t, len(values), 1)
|
|
|
|
assert.Equal(t, keys[0], "abcd")
|
|
|
|
assert.Equal(t, values[0], []byte("123"))
|
2021-01-12 18:03:24 +08:00
|
|
|
|
|
|
|
err = rocksdbKV.RemoveWithPrefix("abc")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-12 18:03:24 +08:00
|
|
|
val, err := rocksdbKV.Load("abc")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-12 18:03:24 +08:00
|
|
|
assert.Equal(t, len(val), 0)
|
|
|
|
val, err = rocksdbKV.Load("abdd")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-12 18:03:24 +08:00
|
|
|
assert.Equal(t, val, "1234")
|
|
|
|
val, err = rocksdbKV.Load("abddqqq")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-01-12 18:03:24 +08:00
|
|
|
assert.Equal(t, val, "1234555")
|
2021-12-17 23:44:42 +08:00
|
|
|
|
|
|
|
// test remove ""
|
|
|
|
err = rocksdbKV.RemoveWithPrefix("")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-17 23:44:42 +08:00
|
|
|
|
2022-01-04 09:07:20 +08:00
|
|
|
// test remove from an empty cf
|
2021-12-17 23:44:42 +08:00
|
|
|
err = rocksdbKV.RemoveWithPrefix("")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-17 23:44:42 +08:00
|
|
|
|
|
|
|
val, err = rocksdbKV.Load("abddqqq")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-17 23:44:42 +08:00
|
|
|
assert.Equal(t, len(val), 0)
|
|
|
|
|
|
|
|
// test we can still save after drop
|
|
|
|
err = rocksdbKV.Save("abcd", "123")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-17 23:44:42 +08:00
|
|
|
|
|
|
|
val, err = rocksdbKV.Load("abcd")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-17 23:44:42 +08:00
|
|
|
assert.Equal(t, val, "123")
|
|
|
|
|
2021-01-12 18:03:24 +08:00
|
|
|
}
|
2021-08-16 18:46:10 +08:00
|
|
|
|
2022-05-16 19:17:55 +08:00
|
|
|
func TestRocksdbKV_Txn(t *testing.T) {
|
|
|
|
name := "/tmp/rocksdb"
|
|
|
|
rocksdbKV, err := rocksdbkv.NewRocksdbKV(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(name)
|
|
|
|
defer rocksdbKV.Close()
|
|
|
|
// Need to call RemoveWithPrefix
|
|
|
|
defer rocksdbKV.RemoveWithPrefix("")
|
|
|
|
|
|
|
|
kvs := map[string]string{
|
|
|
|
"abcd": "123",
|
|
|
|
"abdd": "1234",
|
|
|
|
"abddqqq": "1234555",
|
|
|
|
}
|
|
|
|
err = rocksdbKV.MultiSave(kvs)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
keys, vals, err := rocksdbKV.LoadWithPrefix("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(keys), 3)
|
|
|
|
assert.Equal(t, len(vals), 3)
|
|
|
|
|
|
|
|
removePrefix := []string{"abc", "abd"}
|
|
|
|
rocksdbKV.MultiRemoveWithPrefix(removePrefix)
|
|
|
|
|
|
|
|
keys, vals, err = rocksdbKV.LoadWithPrefix("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(keys), 0)
|
|
|
|
assert.Equal(t, len(vals), 0)
|
|
|
|
|
|
|
|
err = rocksdbKV.MultiSave(kvs)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
keys, vals, err = rocksdbKV.LoadWithPrefix("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(keys), 3)
|
|
|
|
assert.Equal(t, len(vals), 3)
|
|
|
|
|
|
|
|
// test delete the whole table
|
|
|
|
removePrefix = []string{"", "hello"}
|
|
|
|
rocksdbKV.MultiRemoveWithPrefix(removePrefix)
|
|
|
|
|
|
|
|
keys, vals, err = rocksdbKV.LoadWithPrefix("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(keys), 0)
|
|
|
|
assert.Equal(t, len(vals), 0)
|
|
|
|
|
|
|
|
err = rocksdbKV.MultiSave(kvs)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
keys, vals, err = rocksdbKV.LoadWithPrefix("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(keys), 3)
|
|
|
|
assert.Equal(t, len(vals), 3)
|
|
|
|
|
|
|
|
// test remove and save
|
|
|
|
removePrefix = []string{"abc", "abd"}
|
|
|
|
kvs2 := map[string]string{
|
|
|
|
"abfad": "12345",
|
|
|
|
}
|
|
|
|
rocksdbKV.MultiSaveAndRemoveWithPrefix(kvs2, removePrefix)
|
|
|
|
keys, vals, err = rocksdbKV.LoadWithPrefix("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(keys), 1)
|
|
|
|
assert.Equal(t, len(vals), 1)
|
|
|
|
}
|
|
|
|
|
2021-08-16 18:46:10 +08:00
|
|
|
func TestRocksdbKV_Goroutines(t *testing.T) {
|
|
|
|
name := "/tmp/rocksdb"
|
|
|
|
rocksdbkv, err := rocksdbkv.NewRocksdbKV(name)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-29 10:08:47 +08:00
|
|
|
defer os.RemoveAll(name)
|
2021-08-16 18:46:10 +08:00
|
|
|
defer rocksdbkv.Close()
|
|
|
|
defer rocksdbkv.RemoveWithPrefix("")
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
key := "key_" + strconv.Itoa(i)
|
|
|
|
val := "val_" + strconv.Itoa(i)
|
|
|
|
err := rocksdbkv.Save(key, val)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-08-16 18:46:10 +08:00
|
|
|
|
|
|
|
getVal, err := rocksdbkv.Load(key)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-08-16 18:46:10 +08:00
|
|
|
assert.Equal(t, getVal, val)
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2021-09-22 17:21:00 +08:00
|
|
|
|
2021-12-17 23:44:42 +08:00
|
|
|
func TestRocksdbKV_DummyDB(t *testing.T) {
|
2021-09-22 17:21:00 +08:00
|
|
|
name := "/tmp/rocksdb_dummy"
|
|
|
|
rocksdbkv, err := rocksdbkv.NewRocksdbKV(name)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-29 10:08:47 +08:00
|
|
|
defer os.RemoveAll(name)
|
2021-09-22 17:21:00 +08:00
|
|
|
defer rocksdbkv.Close()
|
|
|
|
defer rocksdbkv.RemoveWithPrefix("")
|
|
|
|
|
|
|
|
rocksdbkv.DB = nil
|
|
|
|
_, err = rocksdbkv.Load("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, _, err = rocksdbkv.LoadWithPrefix("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = rocksdbkv.MultiLoad(nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = rocksdbkv.Save("", "")
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = rocksdbkv.MultiSave(nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = rocksdbkv.RemoveWithPrefix("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = rocksdbkv.Remove("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = rocksdbkv.MultiRemove(nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = rocksdbkv.MultiSaveAndRemove(nil, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = rocksdbkv.DeleteRange("", "")
|
|
|
|
assert.Error(t, err)
|
2021-09-28 20:36:04 +08:00
|
|
|
|
|
|
|
rocksdbkv.ReadOptions = nil
|
|
|
|
_, err = rocksdbkv.Load("dummy")
|
|
|
|
assert.Error(t, err)
|
2021-09-22 17:21:00 +08:00
|
|
|
}
|
2021-12-17 23:44:42 +08:00
|
|
|
|
|
|
|
func TestRocksdbKV_CornerCase(t *testing.T) {
|
|
|
|
name := "/tmp/rocksdb_corner"
|
|
|
|
rocksdbkv, err := rocksdbkv.NewRocksdbKV(name)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-29 10:08:47 +08:00
|
|
|
defer os.RemoveAll(name)
|
2021-12-17 23:44:42 +08:00
|
|
|
defer rocksdbkv.Close()
|
|
|
|
defer rocksdbkv.RemoveWithPrefix("")
|
|
|
|
_, err = rocksdbkv.Load("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
keys, values, err := rocksdbkv.LoadWithPrefix("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, len(keys), 0)
|
|
|
|
assert.Equal(t, len(values), 0)
|
|
|
|
err = rocksdbkv.Save("", "")
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = rocksdbkv.Save("test", "")
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = rocksdbkv.Remove("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = rocksdbkv.DeleteRange("a", "a")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2023-07-10 19:40:27 +08:00
|
|
|
|
|
|
|
func TestHas(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
db, err := rocksdbkv.NewRocksdbKV(dir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
defer db.RemoveWithPrefix("")
|
|
|
|
|
|
|
|
has, err := db.Has("key1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, has)
|
|
|
|
err = db.Save("key1", "value1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
has, err = db.Has("key1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, has)
|
|
|
|
err = db.Remove("key1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
has, err = db.Has("key1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, has)
|
|
|
|
}
|
2023-07-11 10:28:28 +08:00
|
|
|
|
|
|
|
func TestHasPrefix(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
db, err := rocksdbkv.NewRocksdbKV(dir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
defer db.RemoveWithPrefix("")
|
|
|
|
|
|
|
|
has, err := db.HasPrefix("key")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, has)
|
|
|
|
|
|
|
|
err = db.Save("key1", "value1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
has, err = db.HasPrefix("key")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, has)
|
|
|
|
|
|
|
|
err = db.Remove("key1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
has, err = db.HasPrefix("key")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, has)
|
|
|
|
}
|