kv.go 3.4 KB
Newer Older
1 2 3 4 5 6
// 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
Y
yukun 已提交
7 8
// with the License. You may obtain a copy of the License at
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
Y
yukun 已提交
10
//
11 12 13 14 15
// 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.
Y
yukun 已提交
16

C
cai.zhang 已提交
17
package kv
X
xige-16 已提交
18

19 20
import (
	"github.com/milvus-io/milvus/internal/util/typeutil"
21
	clientv3 "go.etcd.io/etcd/client/v3"
22 23
)

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// CompareFailedError is a helper type for checking MetaKv CompareAndSwap series func error type
type CompareFailedError struct {
	internalError error
}

// Error implements error interface
func (e *CompareFailedError) Error() string {
	return e.internalError.Error()
}

// NewCompareFailedError wraps error into NewCompareFailedError
func NewCompareFailedError(err error) error {
	return &CompareFailedError{internalError: err}
}

C
congqixia 已提交
39
// BaseKV contains base operations of kv. Include save, load and remove.
G
godchen 已提交
40
type BaseKV interface {
X
xige-16 已提交
41
	Load(key string) (string, error)
X
XuanYang-cn 已提交
42
	MultiLoad(keys []string) ([]string, error)
Z
zhenshan.cao 已提交
43
	LoadWithPrefix(key string) ([]string, []string, error)
X
xige-16 已提交
44
	Save(key, value string) error
X
XuanYang-cn 已提交
45
	MultiSave(kvs map[string]string) error
X
xige-16 已提交
46
	Remove(key string) error
X
XuanYang-cn 已提交
47
	MultiRemove(keys []string) error
S
sunby 已提交
48
	RemoveWithPrefix(key string) error
Z
zhenshan.cao 已提交
49

X
xige-16 已提交
50
	Close()
N
neza2017 已提交
51
}
Z
zhenshan.cao 已提交
52

53 54 55 56
// DataKV persists the data.
type DataKV interface {
	BaseKV
	LoadPartial(key string, start, end int64) ([]byte, error)
57
	GetSize(key string) (int64, error)
58 59
}

G
godchen 已提交
60
// TxnKV contains extra txn operations of kv. The extra operations is transactional.
G
godchen 已提交
61 62
type TxnKV interface {
	BaseKV
Z
zhenshan.cao 已提交
63
	MultiSaveAndRemove(saves map[string]string, removals []string) error
N
neza2017 已提交
64 65
	MultiRemoveWithPrefix(keys []string) error
	MultiSaveAndRemoveWithPrefix(saves map[string]string, removals []string) error
Z
zhenshan.cao 已提交
66
}
67

68
// MetaKv is TxnKV for metadata. It should save data with lease.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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
}

G
godchen 已提交
85
// SnapShotKV is TxnKV for snapshot data. It must save timestamp.
86
type SnapShotKV interface {
87
	Save(key string, value string, ts typeutil.Timestamp) error
88
	Load(key string, ts typeutil.Timestamp) (string, error)
89
	MultiSave(kvs map[string]string, ts typeutil.Timestamp) error
90
	LoadWithPrefix(key string, ts typeutil.Timestamp) ([]string, []string, error)
91
	MultiSaveAndRemoveWithPrefix(saves map[string]string, removals []string, ts typeutil.Timestamp) error
92
}