forked from ligato/cn-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes_broker_api.go
120 lines (105 loc) · 4.75 KB
/
bytes_broker_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// 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.
package keyval
import (
"context"
"go.ligato.io/cn-infra/v2/datasync"
)
// BytesBroker allows storing, retrieving and removing data in a key-value form.
type BytesBroker interface {
// Put puts single key-value pair into etcd.
// The behavior of put can be adjusted using PutOptions.
Put(key string, data []byte, opts ...datasync.PutOption) error
// NewTxn creates a transaction.
NewTxn() BytesTxn
// GetValue retrieves one item under the provided key.
GetValue(key string) (data []byte, found bool, revision int64, err error)
// ListValues returns an iterator that enables to traverse all items stored
// under the provided <key>.
ListValues(key string) (BytesKeyValIterator, error)
// ListKeys returns an iterator that allows to traverse all keys from data
// store that share the given <prefix>.
ListKeys(prefix string) (BytesKeyIterator, error)
// Delete removes data stored under the <key>.
Delete(key string, opts ...datasync.DelOption) (existed bool, err error)
}
// BytesBrokerWithAtomic extends BytesBroker with atomic operations.
// Currently only etcd plugin supports atomic operations.
type BytesBrokerWithAtomic interface {
BytesBroker
// PutIfNotExists puts given key-value pair into the datastore if there is no value set for the key.
// If the put was successful, <succeeded> is returned as true. If the key already exists, <succeeded> is returned
// as false and the value for the key is untouched.
PutIfNotExists(key string, data []byte) (succeeded bool, err error)
// CompareAndSwap compares the value currently stored under the given key with the expected <oldData>,
// and only if the expected and actual data match, the value is then changed to <newData>. The comparison and the
// value change are executed together in a single transaction and cannot be interleaved with another operation for
// that key.
CompareAndSwap(key string, oldData, newData []byte) (swapped bool, err error)
// CompareAndDelete compares the value currently stored under the given key with the expected <data>,
// and only if the expected and actual data match, the value is then removed from the datastore. The comparison and
// the value removal are executed together in a single transaction and cannot be interleaved with another operation
// for that key.
CompareAndDelete(key string, data []byte) (deleted bool, err error)
}
// BytesTxn allows to group operations into the transaction.
// Transaction executes multiple operations in a more efficient way in contrast
// to executing them one by one.
type BytesTxn interface {
// Put adds put operation (write raw <data> under the given <key>) into
// the transaction.
Put(key string, data []byte) BytesTxn
// Delete adds delete operation (removal of <data> under the given <key>)
// into the transaction.
Delete(key string) BytesTxn
// Commit tries to execute all the operations of the transaction.
// In the end, either all of them have been successfully applied or none
// of them and an error is returned.
Commit(ctx context.Context) error
}
// BytesKvPair groups getters for a key-value pair.
type BytesKvPair interface {
// GetValue returns the value of the pair.
GetValue() []byte
// GetPrevValue returns the previous value of the pair.
GetPrevValue() []byte
datasync.WithKey
}
// BytesKeyVal represents a single item in data store.
type BytesKeyVal interface {
BytesKvPair
datasync.WithRevision
}
// BytesKeyValIterator is an iterator returned by ListValues call.
type BytesKeyValIterator interface {
// GetNext retrieves the following item from the context.
// When there are no more items to get, <stop> is returned as *true*
// and <kv> is simply *nil*.
GetNext() (kv BytesKeyVal, stop bool)
}
// BytesKeyIterator is an iterator returned by ListKeys call.
type BytesKeyIterator interface {
// GetNext retrieves the following key from the context.
// When there are no more keys to get, <stop> is returned as *true*
// and <key>, <rev> are default values.
GetNext() (key string, rev int64, stop bool)
}
// CoreBrokerWatcher defines methods for full datastore access.
type CoreBrokerWatcher interface {
BytesBroker
BytesWatcher
NewBroker(prefix string) BytesBroker
NewWatcher(prefix string) BytesWatcher
Close() error
}