forked from ligato/cn-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.go
76 lines (65 loc) · 2.44 KB
/
broker.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
package filedb
import (
"path/filepath"
"strings"
"github.com/ligato/cn-infra/datasync"
"github.com/ligato/cn-infra/db/keyval"
"github.com/ligato/cn-infra/db/keyval/filedb/decoder"
)
// BrokerWatcher implements CoreBrokerWatcher and provides broker/watcher constructors with client
type BrokerWatcher struct {
*Client
prefix string
}
// Put calls client's 'Put' method
func (pdb *BrokerWatcher) Put(key string, data []byte, opts ...datasync.PutOption) error {
return pdb.Client.Put(pdb.prefixKey(key), data, opts...)
}
// NewTxn calls client's 'NewTxn' method
func (pdb *BrokerWatcher) NewTxn() keyval.BytesTxn {
return pdb.Client.NewTxn()
}
// GetValue calls client's 'GetValue' method
func (pdb *BrokerWatcher) GetValue(key string) (data []byte, found bool, revision int64, err error) {
return pdb.Client.GetValue(pdb.prefixKey(key))
}
// Delete calls client's 'Delete' method
func (pdb *BrokerWatcher) Delete(key string, opts ...datasync.DelOption) (existed bool, err error) {
return pdb.Client.Delete(pdb.prefixKey(key), opts...)
}
// ListValues returns a list of all database values for given key
func (pdb *BrokerWatcher) ListValues(key string) (keyval.BytesKeyValIterator, error) {
keyValues := pdb.db.GetDataForPrefix(pdb.prefixKey(key))
data := make([]*decoder.FileDataEntry, 0, len(keyValues))
for _, entry := range keyValues {
data = append(data, &decoder.FileDataEntry{
Key: strings.TrimPrefix(entry.Key, pdb.prefix),
Value: entry.Value,
})
}
return &bytesKeyValIterator{len: len(data), data: data}, nil
}
// ListKeys returns a list of all database keys for given prefix
func (pdb *BrokerWatcher) ListKeys(prefix string) (keyval.BytesKeyIterator, error) {
entries := pdb.Client.db.GetDataForPrefix(prefix)
var keys []string
for _, entry := range entries {
keys = append(keys, entry.Key)
}
return &bytesKeyIterator{len: len(keys), keys: keys, prefix: pdb.prefix}, nil
}
// Watch augments watcher's response and removes prefix from it
func (pdb *BrokerWatcher) Watch(resp func(keyval.BytesWatchResp), closeChan chan string, keys ...string) error {
var prefixedKeys []string
for _, key := range keys {
prefixedKeys = append(prefixedKeys, pdb.prefixKey(key))
}
return pdb.Client.Watch(func(origResp keyval.BytesWatchResp) {
r := origResp.(*watchResp)
r.Key = strings.TrimPrefix(r.Key, pdb.prefix)
resp(r)
}, closeChan, prefixedKeys...)
}
func (pdb *BrokerWatcher) prefixKey(key string) string {
return filepath.Join(pdb.prefix, key)
}