2021-10-29 13:15:04 +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:15:04 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:13:52 +08:00
|
|
|
//
|
2021-10-29 13:15:04 +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-11-16 21:10:43 +08:00
|
|
|
package kv
|
2020-09-07 21:07:36 +08:00
|
|
|
|
2021-05-18 14:18:02 +08:00
|
|
|
import (
|
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2021-08-24 09:45:51 +08:00
|
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
2021-05-18 14:18:02 +08:00
|
|
|
)
|
|
|
|
|
2021-10-29 00:16:38 +08:00
|
|
|
// Value is interface for kv-value, needed to support string and byte slice
|
|
|
|
type Value interface {
|
|
|
|
Serialize() []byte
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringValue type alias for string to implement Value
|
|
|
|
type StringValue string
|
|
|
|
|
2021-11-02 21:40:12 +08:00
|
|
|
// Serialize serialize the StringValue to byte array.
|
2021-10-29 00:16:38 +08:00
|
|
|
func (s StringValue) Serialize() []byte {
|
|
|
|
return []byte(s)
|
|
|
|
}
|
|
|
|
|
2021-11-02 21:40:12 +08:00
|
|
|
// String return the value of StringValue.
|
2021-10-29 00:16:38 +08:00
|
|
|
func (s StringValue) String() string {
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BytesValue type alias for byte slice to implement value
|
|
|
|
type BytesValue []byte
|
|
|
|
|
2021-11-02 21:40:12 +08:00
|
|
|
// Serialize return the byte array.
|
2021-10-29 00:16:38 +08:00
|
|
|
func (s BytesValue) Serialize() []byte {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-11-02 21:40:12 +08:00
|
|
|
// String return the string of byte array.
|
2021-10-29 00:16:38 +08:00
|
|
|
func (s BytesValue) String() string {
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValueKV example usage to change KV interface to
|
|
|
|
type ValueKV interface {
|
|
|
|
Save(key string, value Value) error
|
|
|
|
Load(key string) (Value, error)
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:39:59 +08:00
|
|
|
// BaseKV contains base operations of kv. Include save, load and remove.
|
2021-04-12 18:09:28 +08:00
|
|
|
type BaseKV interface {
|
2020-09-07 21:07:36 +08:00
|
|
|
Load(key string) (string, error)
|
2020-11-03 15:08:50 +08:00
|
|
|
MultiLoad(keys []string) ([]string, error)
|
2020-11-06 16:47:18 +08:00
|
|
|
LoadWithPrefix(key string) ([]string, []string, error)
|
2020-09-07 21:07:36 +08:00
|
|
|
Save(key, value string) error
|
2020-11-03 15:08:50 +08:00
|
|
|
MultiSave(kvs map[string]string) error
|
2020-09-07 21:07:36 +08:00
|
|
|
Remove(key string) error
|
2020-11-03 15:08:50 +08:00
|
|
|
MultiRemove(keys []string) error
|
2021-04-09 09:55:04 +08:00
|
|
|
RemoveWithPrefix(key string) error
|
2020-12-07 15:22:20 +08:00
|
|
|
|
2020-12-07 14:37:42 +08:00
|
|
|
Close()
|
2020-12-05 17:39:58 +08:00
|
|
|
}
|
2020-12-07 15:22:20 +08:00
|
|
|
|
2021-10-09 19:33:03 +08:00
|
|
|
// DataKV persists the data.
|
|
|
|
type DataKV interface {
|
|
|
|
BaseKV
|
|
|
|
LoadPartial(key string, start, end int64) ([]byte, error)
|
2021-10-26 15:34:21 +08:00
|
|
|
GetSize(key string) (int64, error)
|
2021-10-09 19:33:03 +08:00
|
|
|
}
|
|
|
|
|
2021-09-29 18:39:59 +08:00
|
|
|
// TxnKV contains extra txn operations of kv. The extra operations is transactional.
|
2021-04-12 18:09:28 +08:00
|
|
|
type TxnKV interface {
|
|
|
|
BaseKV
|
2020-12-07 15:22:20 +08:00
|
|
|
MultiSaveAndRemove(saves map[string]string, removals []string) error
|
2021-03-06 16:00:41 +08:00
|
|
|
MultiRemoveWithPrefix(keys []string) error
|
|
|
|
MultiSaveAndRemoveWithPrefix(saves map[string]string, removals []string) error
|
2020-12-07 15:22:20 +08:00
|
|
|
}
|
2021-05-18 14:18:02 +08:00
|
|
|
|
2021-09-29 18:39:59 +08:00
|
|
|
// MetaKv is TxnKV for meta data. It should save data with lease.
|
2021-08-24 09:45:51 +08:00
|
|
|
type MetaKv interface {
|
|
|
|
TxnKV
|
|
|
|
GetPath(key string) string
|
|
|
|
LoadWithPrefix(key string) ([]string, []string, error)
|
|
|
|
LoadWithPrefix2(key string) ([]string, []string, []int64, error)
|
|
|
|
LoadWithRevision(key string) ([]string, []string, int64, error)
|
|
|
|
Watch(key string) clientv3.WatchChan
|
|
|
|
WatchWithPrefix(key string) clientv3.WatchChan
|
|
|
|
WatchWithRevision(key string, revision int64) clientv3.WatchChan
|
|
|
|
SaveWithLease(key, value string, id clientv3.LeaseID) error
|
|
|
|
Grant(ttl int64) (id clientv3.LeaseID, err error)
|
|
|
|
KeepAlive(id clientv3.LeaseID) (<-chan *clientv3.LeaseKeepAliveResponse, error)
|
|
|
|
CompareValueAndSwap(key, value, target string, opts ...clientv3.OpOption) error
|
|
|
|
CompareVersionAndSwap(key string, version int64, target string, opts ...clientv3.OpOption) error
|
|
|
|
}
|
|
|
|
|
2021-09-29 18:39:59 +08:00
|
|
|
// SnapShotKV is TxnKV for snapshot data. It must save timestamp.
|
2021-05-18 14:18:02 +08:00
|
|
|
type SnapShotKV interface {
|
2021-08-18 14:36:10 +08:00
|
|
|
Save(key string, value string, ts typeutil.Timestamp) error
|
2021-05-18 14:18:02 +08:00
|
|
|
Load(key string, ts typeutil.Timestamp) (string, error)
|
2021-10-13 15:54:33 +08:00
|
|
|
MultiSave(kvs map[string]string, ts typeutil.Timestamp) error
|
2021-05-18 14:18:02 +08:00
|
|
|
LoadWithPrefix(key string, ts typeutil.Timestamp) ([]string, []string, error)
|
2021-10-13 15:54:33 +08:00
|
|
|
MultiSaveAndRemoveWithPrefix(saves map[string]string, removals []string, ts typeutil.Timestamp) error
|
2021-05-18 14:18:02 +08:00
|
|
|
}
|