Use MGet to query multi keys in RocksDB KV (#24362)

RocksDB can performance optimizations such as async-io by io_uring.
This would benefit the performance a lot when keys are lying in
different levels of the LSM tree.

Signed-off-by: git-hulk <hulk.website@gmail.com>
pull/24428/head
hulk 2023-05-26 11:07:27 +08:00 committed by GitHub
parent 165734de4e
commit 1aeff84293
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 15 deletions

View File

@ -202,16 +202,22 @@ func (kv *RocksdbKV) MultiLoad(keys []string) ([]string, error) {
if kv.DB == nil {
return nil, errors.New("rocksdb instance is nil when do MultiLoad")
}
keyInBytes := make([][]byte, 0, len(keys))
for _, key := range keys {
keyInBytes = append(keyInBytes, []byte(key))
}
values := make([]string, 0, len(keys))
option := gorocksdb.NewDefaultReadOptions()
defer option.Destroy()
for _, key := range keys {
value, err := kv.DB.Get(option, []byte(key))
if err != nil {
return nil, err
}
values = append(values, string(value.Data()))
value.Free()
valueSlice, err := kv.DB.MultiGet(option, keyInBytes...)
if err != nil {
return nil, err
}
for i := range valueSlice {
values = append(values, string(valueSlice[i].Data()))
valueSlice[i].Free()
}
return values, nil
}
@ -220,21 +226,25 @@ func (kv *RocksdbKV) MultiLoadBytes(keys []string) ([][]byte, error) {
if kv.DB == nil {
return nil, errors.New("rocksdb instance is nil when do MultiLoad")
}
keyInBytes := make([][]byte, 0, len(keys))
for _, key := range keys {
keyInBytes = append(keyInBytes, []byte(key))
}
values := make([][]byte, 0, len(keys))
option := gorocksdb.NewDefaultReadOptions()
defer option.Destroy()
for _, key := range keys {
value, err := kv.DB.Get(option, []byte(key))
if err != nil {
return nil, err
}
data := value.Data()
valueSlice, err := kv.DB.MultiGet(option, keyInBytes...)
if err != nil {
return nil, err
}
for i := range valueSlice {
data := valueSlice[i].Data()
v := make([]byte, len(data))
copy(v, data)
values = append(values, v)
value.Free()
valueSlice[i].Free()
}
return values, nil
}