2021-10-29 13:09:33 +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:09:33 +08:00
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:13:52 +08:00
|
|
|
|
//
|
2021-10-29 13:09:33 +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
|
|
|
|
|
2020-12-07 15:22:20 +08:00
|
|
|
|
package memkv
|
2020-11-06 16:47:18 +08:00
|
|
|
|
|
|
|
|
|
import (
|
2021-10-09 19:33:03 +08:00
|
|
|
|
"fmt"
|
2021-01-22 19:43:27 +08:00
|
|
|
|
"strings"
|
2020-11-06 16:47:18 +08:00
|
|
|
|
"sync"
|
2020-11-12 12:04:12 +08:00
|
|
|
|
|
|
|
|
|
"github.com/google/btree"
|
2020-11-06 16:47:18 +08:00
|
|
|
|
)
|
|
|
|
|
|
2021-10-28 23:42:39 +08:00
|
|
|
|
// MemoryKV implements DataKV interface and relies on underling btree.BTree.
|
|
|
|
|
// As its name implies, all data is stored in memory.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
type MemoryKV struct {
|
|
|
|
|
sync.RWMutex
|
|
|
|
|
tree *btree.BTree
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewMemoryKV returns an in-memory kvBase for testing.
|
|
|
|
|
func NewMemoryKV() *MemoryKV {
|
|
|
|
|
return &MemoryKV{
|
|
|
|
|
tree: btree.New(2),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type memoryKVItem struct {
|
|
|
|
|
key, value string
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 13:23:10 +08:00
|
|
|
|
// Less returns true if the item is less than the given one.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (s memoryKVItem) Less(than btree.Item) bool {
|
|
|
|
|
return s.key < than.(memoryKVItem).key
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 23:54:55 +08:00
|
|
|
|
// Load loads an object with @key.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (kv *MemoryKV) Load(key string) (string, error) {
|
|
|
|
|
kv.RLock()
|
|
|
|
|
defer kv.RUnlock()
|
|
|
|
|
item := kv.tree.Get(memoryKVItem{key, ""})
|
2021-08-24 09:45:51 +08:00
|
|
|
|
// TODO,load unexisted key behavior is weird
|
2020-11-06 16:47:18 +08:00
|
|
|
|
if item == nil {
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
return item.(memoryKVItem).value, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 13:23:10 +08:00
|
|
|
|
// LoadWithDefault loads an object with @key. If the object does not exist, @defaultValue will be returned.
|
2021-10-28 23:16:40 +08:00
|
|
|
|
func (kv *MemoryKV) LoadWithDefault(key, defaultValue string) string {
|
2021-08-24 09:45:51 +08:00
|
|
|
|
kv.RLock()
|
|
|
|
|
defer kv.RUnlock()
|
|
|
|
|
item := kv.tree.Get(memoryKVItem{key, ""})
|
|
|
|
|
|
|
|
|
|
if item == nil {
|
2021-10-28 23:16:40 +08:00
|
|
|
|
return defaultValue
|
2021-08-24 09:45:51 +08:00
|
|
|
|
}
|
2021-10-28 23:16:40 +08:00
|
|
|
|
return item.(memoryKVItem).value
|
2021-08-24 09:45:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 13:23:10 +08:00
|
|
|
|
// LoadRange loads objects with range @startKey to @endKey with @limit number of objects.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (kv *MemoryKV) LoadRange(key, endKey string, limit int) ([]string, []string, error) {
|
|
|
|
|
kv.RLock()
|
|
|
|
|
defer kv.RUnlock()
|
|
|
|
|
keys := make([]string, 0, limit)
|
|
|
|
|
values := make([]string, 0, limit)
|
|
|
|
|
kv.tree.AscendRange(memoryKVItem{key, ""}, memoryKVItem{endKey, ""}, func(item btree.Item) bool {
|
|
|
|
|
keys = append(keys, item.(memoryKVItem).key)
|
|
|
|
|
values = append(values, item.(memoryKVItem).value)
|
|
|
|
|
if limit > 0 {
|
|
|
|
|
return len(keys) < limit
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return keys, values, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 19:27:27 +08:00
|
|
|
|
// Save object with @key to btree. Object value is @value.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (kv *MemoryKV) Save(key, value string) error {
|
|
|
|
|
kv.Lock()
|
|
|
|
|
defer kv.Unlock()
|
|
|
|
|
kv.tree.ReplaceOrInsert(memoryKVItem{key, value})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 19:27:27 +08:00
|
|
|
|
// Remove deletes an object with @key.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (kv *MemoryKV) Remove(key string) error {
|
|
|
|
|
kv.Lock()
|
|
|
|
|
defer kv.Unlock()
|
|
|
|
|
|
|
|
|
|
kv.tree.Delete(memoryKVItem{key, ""})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 23:54:55 +08:00
|
|
|
|
// MultiLoad loads objects with multi @keys.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (kv *MemoryKV) MultiLoad(keys []string) ([]string, error) {
|
|
|
|
|
kv.RLock()
|
|
|
|
|
defer kv.RUnlock()
|
|
|
|
|
result := make([]string, 0, len(keys))
|
2020-11-12 12:04:12 +08:00
|
|
|
|
for _, key := range keys {
|
2020-11-06 16:47:18 +08:00
|
|
|
|
item := kv.tree.Get(memoryKVItem{key, ""})
|
|
|
|
|
result = append(result, item.(memoryKVItem).value)
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 16:11:16 +08:00
|
|
|
|
// MultiSave saves given key-value pairs in MemoryKV atomicly.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (kv *MemoryKV) MultiSave(kvs map[string]string) error {
|
|
|
|
|
kv.Lock()
|
|
|
|
|
defer kv.Unlock()
|
|
|
|
|
for key, value := range kvs {
|
|
|
|
|
kv.tree.ReplaceOrInsert(memoryKVItem{key, value})
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 16:11:16 +08:00
|
|
|
|
// MultiRemove removes given @keys in MemoryKV atomicly.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (kv *MemoryKV) MultiRemove(keys []string) error {
|
|
|
|
|
kv.Lock()
|
|
|
|
|
defer kv.Unlock()
|
|
|
|
|
for _, key := range keys {
|
|
|
|
|
kv.tree.Delete(memoryKVItem{key, ""})
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 16:11:16 +08:00
|
|
|
|
// MultiSaveAndRemove saves and removes given key-value pairs in MemoryKV atomicly.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (kv *MemoryKV) MultiSaveAndRemove(saves map[string]string, removals []string) error {
|
|
|
|
|
kv.Lock()
|
|
|
|
|
defer kv.Unlock()
|
|
|
|
|
for key, value := range saves {
|
|
|
|
|
kv.tree.ReplaceOrInsert(memoryKVItem{key, value})
|
|
|
|
|
}
|
|
|
|
|
for _, key := range removals {
|
|
|
|
|
kv.tree.Delete(memoryKVItem{key, ""})
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 16:11:16 +08:00
|
|
|
|
// LoadWithPrefix returns all keys & values with given prefix.
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (kv *MemoryKV) LoadWithPrefix(key string) ([]string, []string, error) {
|
2021-01-22 19:43:27 +08:00
|
|
|
|
kv.Lock()
|
|
|
|
|
defer kv.Unlock()
|
|
|
|
|
|
2021-11-17 20:57:17 +08:00
|
|
|
|
var keys []string
|
|
|
|
|
var values []string
|
2021-04-09 09:55:04 +08:00
|
|
|
|
|
2021-01-22 19:43:27 +08:00
|
|
|
|
kv.tree.Ascend(func(i btree.Item) bool {
|
|
|
|
|
if strings.HasPrefix(i.(memoryKVItem).key, key) {
|
|
|
|
|
keys = append(keys, i.(memoryKVItem).key)
|
|
|
|
|
values = append(values, i.(memoryKVItem).value)
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return keys, values, nil
|
2020-11-06 16:47:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 16:11:16 +08:00
|
|
|
|
// Close dummy close
|
2020-11-06 16:47:18 +08:00
|
|
|
|
func (kv *MemoryKV) Close() {
|
2020-11-12 12:04:12 +08:00
|
|
|
|
}
|
2021-03-06 16:00:41 +08:00
|
|
|
|
|
2021-11-30 16:08:59 +08:00
|
|
|
|
// MultiRemoveWithPrefix not implemented
|
2021-03-06 16:00:41 +08:00
|
|
|
|
func (kv *MemoryKV) MultiRemoveWithPrefix(keys []string) error {
|
|
|
|
|
panic("not implement")
|
|
|
|
|
}
|
2021-11-30 16:08:59 +08:00
|
|
|
|
|
|
|
|
|
// MultiSaveAndRemoveWithPrefix saves key-value pairs in @saves, & remove key with prefix in @removals in MemoryKV atomicly.
|
2021-03-06 16:00:41 +08:00
|
|
|
|
func (kv *MemoryKV) MultiSaveAndRemoveWithPrefix(saves map[string]string, removals []string) error {
|
2021-08-24 15:51:51 +08:00
|
|
|
|
kv.Lock()
|
|
|
|
|
defer kv.Unlock()
|
|
|
|
|
|
2021-11-17 20:57:17 +08:00
|
|
|
|
var keys []memoryKVItem
|
2021-08-24 15:51:51 +08:00
|
|
|
|
for _, key := range removals {
|
|
|
|
|
kv.tree.Ascend(func(i btree.Item) bool {
|
|
|
|
|
if strings.HasPrefix(i.(memoryKVItem).key, key) {
|
|
|
|
|
keys = append(keys, i.(memoryKVItem))
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
for _, item := range keys {
|
|
|
|
|
kv.tree.Delete(item)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for key, value := range saves {
|
|
|
|
|
kv.tree.ReplaceOrInsert(memoryKVItem{key, value})
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2021-03-06 16:00:41 +08:00
|
|
|
|
}
|
2021-04-09 09:55:04 +08:00
|
|
|
|
|
2021-11-30 16:08:59 +08:00
|
|
|
|
// RemoveWithPrefix remove key of given prefix in MemoryKV atomicly.
|
2021-04-09 09:55:04 +08:00
|
|
|
|
func (kv *MemoryKV) RemoveWithPrefix(key string) error {
|
|
|
|
|
kv.Lock()
|
|
|
|
|
defer kv.Unlock()
|
|
|
|
|
|
2021-11-17 20:57:17 +08:00
|
|
|
|
var keys []btree.Item
|
2021-04-09 09:55:04 +08:00
|
|
|
|
|
|
|
|
|
kv.tree.Ascend(func(i btree.Item) bool {
|
|
|
|
|
if strings.HasPrefix(i.(memoryKVItem).key, key) {
|
|
|
|
|
keys = append(keys, i.(memoryKVItem))
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
for _, item := range keys {
|
|
|
|
|
kv.tree.Delete(item)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-10-09 19:33:03 +08:00
|
|
|
|
|
2021-12-09 10:49:25 +08:00
|
|
|
|
// LoadPartial item already in memory, just slice the value.
|
2021-10-09 19:33:03 +08:00
|
|
|
|
func (kv *MemoryKV) LoadPartial(key string, start, end int64) ([]byte, error) {
|
|
|
|
|
value, err := kv.Load(key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
switch {
|
|
|
|
|
case 0 <= start && start < end && end <= int64(len(value)):
|
|
|
|
|
return []byte(value[start:end]), nil
|
|
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("invalid range specified: start=%d end=%d",
|
|
|
|
|
start, end)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-26 15:34:21 +08:00
|
|
|
|
|
|
|
|
|
func (kv *MemoryKV) GetSize(key string) (int64, error) {
|
|
|
|
|
value, err := kv.Load(key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int64(len(value)), nil
|
|
|
|
|
}
|