2021-04-19 11:13:52 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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-01-11 18:35:54 +08:00
|
|
|
package rocksdbkv
|
|
|
|
|
|
|
|
import (
|
2021-09-22 17:21:00 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-01-11 18:35:54 +08:00
|
|
|
"github.com/tecbot/gorocksdb"
|
|
|
|
)
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// RocksdbKV is KV implemented by rocksdb
|
2021-01-11 18:35:54 +08:00
|
|
|
type RocksdbKV struct {
|
2021-08-16 18:46:10 +08:00
|
|
|
Opts *gorocksdb.Options
|
|
|
|
DB *gorocksdb.DB
|
|
|
|
WriteOptions *gorocksdb.WriteOptions
|
|
|
|
ReadOptions *gorocksdb.ReadOptions
|
2021-01-11 18:35:54 +08:00
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2021-09-30 22:40:14 +08:00
|
|
|
const (
|
|
|
|
// LRUCacheSize is the lru cache size of rocksdb, default 3 << 30
|
2021-10-13 20:28:34 +08:00
|
|
|
LRUCacheSize = 0
|
2021-09-30 22:40:14 +08:00
|
|
|
)
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// NewRocksdbKV returns a rockskv object
|
2021-01-11 18:35:54 +08:00
|
|
|
func NewRocksdbKV(name string) (*RocksdbKV, error) {
|
2021-09-22 17:21:00 +08:00
|
|
|
if name == "" {
|
|
|
|
return nil, errors.New("rocksdb name is nil")
|
|
|
|
}
|
2021-01-11 18:35:54 +08:00
|
|
|
bbto := gorocksdb.NewDefaultBlockBasedTableOptions()
|
2021-09-29 18:45:59 +08:00
|
|
|
bbto.SetCacheIndexAndFilterBlocks(true)
|
2021-10-08 10:19:06 +08:00
|
|
|
bbto.SetPinL0FilterAndIndexBlocksInCache(true)
|
2021-09-30 22:40:14 +08:00
|
|
|
bbto.SetBlockCache(gorocksdb.NewLRUCache(LRUCacheSize))
|
2021-01-11 18:35:54 +08:00
|
|
|
opts := gorocksdb.NewDefaultOptions()
|
|
|
|
opts.SetBlockBasedTableFactory(bbto)
|
|
|
|
opts.SetCreateIfMissing(true)
|
|
|
|
|
|
|
|
ro := gorocksdb.NewDefaultReadOptions()
|
|
|
|
ro.SetFillCache(false)
|
|
|
|
|
|
|
|
wo := gorocksdb.NewDefaultWriteOptions()
|
|
|
|
db, err := gorocksdb.OpenDb(opts, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &RocksdbKV{
|
2021-08-16 18:46:10 +08:00
|
|
|
Opts: opts,
|
|
|
|
DB: db,
|
|
|
|
WriteOptions: wo,
|
|
|
|
ReadOptions: ro,
|
2021-01-11 18:35:54 +08:00
|
|
|
name: name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// Close free resource of rocksdb
|
2021-01-11 18:35:54 +08:00
|
|
|
func (kv *RocksdbKV) Close() {
|
2021-09-22 17:21:00 +08:00
|
|
|
if kv.DB != nil {
|
|
|
|
kv.DB.Close()
|
|
|
|
}
|
2021-01-11 18:35:54 +08:00
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// GetName returns the name of this object
|
2021-01-11 18:35:54 +08:00
|
|
|
func (kv *RocksdbKV) GetName() string {
|
|
|
|
return kv.name
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// Load returns the value of specified key
|
2021-01-11 18:35:54 +08:00
|
|
|
func (kv *RocksdbKV) Load(key string) (string, error) {
|
2021-09-22 17:21:00 +08:00
|
|
|
if kv.DB == nil {
|
|
|
|
return "", fmt.Errorf("Rocksdb instance is nil when load %s", key)
|
|
|
|
}
|
|
|
|
|
2021-08-16 18:46:10 +08:00
|
|
|
value, err := kv.DB.Get(kv.ReadOptions, []byte(key))
|
2021-09-28 20:36:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-01-11 18:35:54 +08:00
|
|
|
defer value.Free()
|
2021-09-28 20:36:04 +08:00
|
|
|
return string(value.Data()), nil
|
2021-01-11 18:35:54 +08:00
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// LoadWithPrefix returns a batch values of keys with a prefix
|
2021-01-11 18:35:54 +08:00
|
|
|
func (kv *RocksdbKV) LoadWithPrefix(key string) ([]string, []string, error) {
|
2021-09-22 17:21:00 +08:00
|
|
|
if key == "" {
|
|
|
|
return nil, nil, errors.New("Key is nil in LoadWithPrefix")
|
|
|
|
}
|
|
|
|
if kv.DB == nil {
|
|
|
|
return nil, nil, fmt.Errorf("Rocksdb instance is nil when load %s", key)
|
|
|
|
}
|
2021-08-16 18:46:10 +08:00
|
|
|
kv.ReadOptions.SetPrefixSameAsStart(true)
|
|
|
|
kv.DB.Close()
|
|
|
|
kv.Opts.SetPrefixExtractor(gorocksdb.NewFixedPrefixTransform(len(key)))
|
2021-01-12 18:03:24 +08:00
|
|
|
var err error
|
2021-08-16 18:46:10 +08:00
|
|
|
kv.DB, err = gorocksdb.OpenDb(kv.Opts, kv.GetName())
|
2021-01-12 18:03:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-16 18:46:10 +08:00
|
|
|
iter := kv.DB.NewIterator(kv.ReadOptions)
|
2021-01-12 18:03:24 +08:00
|
|
|
defer iter.Close()
|
2021-01-11 18:35:54 +08:00
|
|
|
keys := make([]string, 0)
|
|
|
|
values := make([]string, 0)
|
|
|
|
iter.Seek([]byte(key))
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
key := iter.Key()
|
|
|
|
value := iter.Value()
|
|
|
|
keys = append(keys, string(key.Data()))
|
|
|
|
values = append(values, string(value.Data()))
|
|
|
|
key.Free()
|
|
|
|
value.Free()
|
|
|
|
}
|
|
|
|
if err := iter.Err(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return keys, values, nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// ResetPrefixLength will close rocksdb object and open a new rocksdb with new prefix length
|
2021-09-10 10:22:01 +08:00
|
|
|
func (kv *RocksdbKV) ResetPrefixLength(len int) error {
|
|
|
|
kv.DB.Close()
|
|
|
|
kv.Opts.SetPrefixExtractor(gorocksdb.NewFixedPrefixTransform(len))
|
|
|
|
var err error
|
|
|
|
kv.DB, err = gorocksdb.OpenDb(kv.Opts, kv.GetName())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// MultiLoad load a batch of values by keys
|
2021-01-11 18:35:54 +08:00
|
|
|
func (kv *RocksdbKV) MultiLoad(keys []string) ([]string, error) {
|
2021-09-22 17:21:00 +08:00
|
|
|
if kv.DB == nil {
|
|
|
|
return nil, errors.New("Rocksdb instance is nil when do MultiLoad")
|
|
|
|
}
|
2021-01-11 18:35:54 +08:00
|
|
|
values := make([]string, 0, len(keys))
|
|
|
|
for _, key := range keys {
|
2021-08-16 18:46:10 +08:00
|
|
|
value, err := kv.DB.Get(kv.ReadOptions, []byte(key))
|
2021-01-11 18:35:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
values = append(values, string(value.Data()))
|
2021-09-28 19:36:04 +08:00
|
|
|
value.Free()
|
2021-01-11 18:35:54 +08:00
|
|
|
}
|
|
|
|
return values, nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// Save a pair of key-value
|
2021-01-11 18:35:54 +08:00
|
|
|
func (kv *RocksdbKV) Save(key, value string) error {
|
2021-09-22 17:21:00 +08:00
|
|
|
if kv.DB == nil {
|
|
|
|
return errors.New("Rocksdb instance is nil when do save")
|
|
|
|
}
|
2021-08-16 18:46:10 +08:00
|
|
|
err := kv.DB.Put(kv.WriteOptions, []byte(key), []byte(value))
|
2021-01-11 18:35:54 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// MultiSave a batch of key-values
|
2021-01-11 18:35:54 +08:00
|
|
|
func (kv *RocksdbKV) MultiSave(kvs map[string]string) error {
|
2021-09-22 17:21:00 +08:00
|
|
|
if kv.DB == nil {
|
|
|
|
return errors.New("Rocksdb instance is nil when do MultiSave")
|
|
|
|
}
|
2021-01-11 18:35:54 +08:00
|
|
|
writeBatch := gorocksdb.NewWriteBatch()
|
2021-09-28 19:36:04 +08:00
|
|
|
defer writeBatch.Destroy()
|
2021-01-11 18:35:54 +08:00
|
|
|
for k, v := range kvs {
|
|
|
|
writeBatch.Put([]byte(k), []byte(v))
|
|
|
|
}
|
2021-08-16 18:46:10 +08:00
|
|
|
err := kv.DB.Write(kv.WriteOptions, writeBatch)
|
2021-01-11 18:35:54 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// RemoveWithPrefix removes a batch of key-values with specified prefix
|
2021-01-11 18:35:54 +08:00
|
|
|
func (kv *RocksdbKV) RemoveWithPrefix(prefix string) error {
|
2021-09-22 17:21:00 +08:00
|
|
|
if kv.DB == nil {
|
|
|
|
return errors.New("Rocksdb instance is nil when do RemoveWithPrefix")
|
|
|
|
}
|
2021-08-16 18:46:10 +08:00
|
|
|
kv.ReadOptions.SetPrefixSameAsStart(true)
|
|
|
|
kv.DB.Close()
|
|
|
|
kv.Opts.SetPrefixExtractor(gorocksdb.NewFixedPrefixTransform(len(prefix)))
|
2021-01-12 18:03:24 +08:00
|
|
|
var err error
|
2021-08-16 18:46:10 +08:00
|
|
|
kv.DB, err = gorocksdb.OpenDb(kv.Opts, kv.GetName())
|
2021-01-12 18:03:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-16 18:46:10 +08:00
|
|
|
iter := kv.DB.NewIterator(kv.ReadOptions)
|
2021-01-12 18:03:24 +08:00
|
|
|
defer iter.Close()
|
2021-01-11 18:35:54 +08:00
|
|
|
iter.Seek([]byte(prefix))
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
key := iter.Key()
|
2021-08-16 18:46:10 +08:00
|
|
|
err := kv.DB.Delete(kv.WriteOptions, key.Data())
|
2021-09-28 22:34:04 +08:00
|
|
|
key.Free()
|
2021-01-11 18:35:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := iter.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// Remove is used to remove a pair of key-value
|
2021-01-11 18:35:54 +08:00
|
|
|
func (kv *RocksdbKV) Remove(key string) error {
|
2021-09-22 17:21:00 +08:00
|
|
|
if kv.DB == nil {
|
|
|
|
return errors.New("Rocksdb instance is nil when do Remove")
|
|
|
|
}
|
2021-08-16 18:46:10 +08:00
|
|
|
err := kv.DB.Delete(kv.WriteOptions, []byte(key))
|
2021-01-11 18:35:54 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// MultiRemove is used to remove a batch of key-values
|
2021-01-11 18:35:54 +08:00
|
|
|
func (kv *RocksdbKV) MultiRemove(keys []string) error {
|
2021-09-22 17:21:00 +08:00
|
|
|
if kv.DB == nil {
|
|
|
|
return errors.New("Rocksdb instance is nil when do MultiRemove")
|
|
|
|
}
|
2021-01-11 18:35:54 +08:00
|
|
|
writeBatch := gorocksdb.NewWriteBatch()
|
2021-09-28 19:36:04 +08:00
|
|
|
defer writeBatch.Destroy()
|
2021-01-11 18:35:54 +08:00
|
|
|
for _, key := range keys {
|
|
|
|
writeBatch.Delete([]byte(key))
|
|
|
|
}
|
2021-08-16 18:46:10 +08:00
|
|
|
err := kv.DB.Write(kv.WriteOptions, writeBatch)
|
2021-01-11 18:35:54 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-02-09 15:57:10 +08:00
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// MultiSaveAndRemove provides a transaction to execute a batch of operators
|
2021-02-09 15:57:10 +08:00
|
|
|
func (kv *RocksdbKV) MultiSaveAndRemove(saves map[string]string, removals []string) error {
|
2021-09-22 17:21:00 +08:00
|
|
|
if kv.DB == nil {
|
|
|
|
return errors.New("Rocksdb instance is nil when do MultiSaveAndRemove")
|
|
|
|
}
|
2021-02-09 15:57:10 +08:00
|
|
|
writeBatch := gorocksdb.NewWriteBatch()
|
2021-09-28 19:36:04 +08:00
|
|
|
defer writeBatch.Destroy()
|
2021-02-09 15:57:10 +08:00
|
|
|
for k, v := range saves {
|
|
|
|
writeBatch.Put([]byte(k), []byte(v))
|
|
|
|
}
|
|
|
|
for _, key := range removals {
|
|
|
|
writeBatch.Delete([]byte(key))
|
|
|
|
}
|
2021-08-16 18:46:10 +08:00
|
|
|
err := kv.DB.Write(kv.WriteOptions, writeBatch)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// DeleteRange remove a batch of key-values from startKey to endKey
|
2021-08-16 18:46:10 +08:00
|
|
|
func (kv *RocksdbKV) DeleteRange(startKey, endKey string) error {
|
2021-09-22 17:21:00 +08:00
|
|
|
if kv.DB == nil {
|
|
|
|
return errors.New("Rocksdb instance is nil when do DeleteRange")
|
|
|
|
}
|
2021-08-16 18:46:10 +08:00
|
|
|
writeBatch := gorocksdb.NewWriteBatch()
|
2021-09-28 19:36:04 +08:00
|
|
|
defer writeBatch.Destroy()
|
2021-08-16 18:46:10 +08:00
|
|
|
if len(startKey) == 0 {
|
|
|
|
iter := kv.DB.NewIterator(kv.ReadOptions)
|
|
|
|
defer iter.Close()
|
|
|
|
iter.SeekToFirst()
|
|
|
|
startKey = string(iter.Key().Data())
|
|
|
|
}
|
|
|
|
|
|
|
|
writeBatch.DeleteRange([]byte(startKey), []byte(endKey))
|
|
|
|
err := kv.DB.Write(kv.WriteOptions, writeBatch)
|
2021-02-09 15:57:10 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-03-06 16:00:41 +08:00
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// MultiRemoveWithPrefix is used to remove a batch of key-values with the same prefix
|
2021-03-06 16:00:41 +08:00
|
|
|
func (kv *RocksdbKV) MultiRemoveWithPrefix(keys []string) error {
|
|
|
|
panic("not implement")
|
|
|
|
}
|
2021-08-16 18:46:10 +08:00
|
|
|
|
2021-09-28 08:28:02 +08:00
|
|
|
// MultiSaveAndRemoveWithPrefix is used to execute a batch operators with the same prefix
|
2021-03-06 16:00:41 +08:00
|
|
|
func (kv *RocksdbKV) MultiSaveAndRemoveWithPrefix(saves map[string]string, removals []string) error {
|
|
|
|
panic("not implement")
|
|
|
|
}
|