Skip to content

Commit 645556c

Browse files
author
Matthieu Nottale
committed
Add support for memo backend.
Signed-off-by: Matthieu Nottale <[email protected]>
1 parent 93ab0e6 commit 645556c

File tree

3 files changed

+703
-0
lines changed

3 files changed

+703
-0
lines changed

store/memo/memo.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package memo
2+
3+
import (
4+
"github.com/docker/libkv"
5+
"github.com/docker/libkv/store"
6+
kvs "github.com/docker/libkv/store/memo/memo_kvs"
7+
"golang.org/x/net/context"
8+
"google.golang.org/grpc"
9+
"google.golang.org/grpc/codes"
10+
)
11+
12+
// Memo is the receiver type for the Store interface
13+
type Memo struct {
14+
kvs kvs.KeyValueStoreClient
15+
}
16+
17+
// Register registers memo to libkv
18+
func Register() {
19+
libkv.AddStore("memo", New)
20+
}
21+
22+
// New creates a new memo client
23+
func New(addrs []string, options *store.Config) (store.Store, error) {
24+
conn, err := grpc.Dial(addrs[0], grpc.WithInsecure())
25+
if err != nil {
26+
return nil, err
27+
}
28+
kvs := kvs.NewKeyValueStoreClient(conn)
29+
return &Memo{kvs: kvs}, nil
30+
}
31+
32+
// Get current value at "key".
33+
func (s *Memo) Get(key string) (*store.KVPair, error) {
34+
res, err := s.kvs.Fetch(context.Background(), &kvs.FetchRequest{Key: key})
35+
if err != nil {
36+
if grpc.Code(err) == codes.NotFound {
37+
return nil, store.ErrKeyNotFound
38+
}
39+
return nil, err
40+
}
41+
return &store.KVPair{
42+
Key: key,
43+
Value: res.Value,
44+
LastIndex: 0,
45+
}, nil
46+
}
47+
48+
// Put value at "key"
49+
func (s *Memo) Put(key string, value []byte, options *store.WriteOptions) error {
50+
_, err := s.kvs.Upsert(context.Background(),
51+
&kvs.UpsertRequest{Key: key, Value: value})
52+
return err
53+
}
54+
55+
// Delete value at "key"
56+
func (s *Memo) Delete(key string) error {
57+
_, err := s.kvs.Delete(context.Background(), &kvs.DeleteRequest{Key: key})
58+
return err
59+
}
60+
61+
// Existts checks if "key" is present in the store
62+
func (s *Memo) Exists(key string) (bool, error) {
63+
_, err := s.kvs.Fetch(context.Background(), &kvs.FetchRequest{Key: key})
64+
if err == nil {
65+
return true, nil
66+
}
67+
if grpc.Code(err) == codes.NotFound {
68+
return false, nil
69+
}
70+
return false, err
71+
}
72+
73+
// Watch for changes. Not supported by memo.
74+
func (s *Memo) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) {
75+
return nil, store.ErrCallNotSupported
76+
}
77+
78+
// WatchTree for changes. Not supported by memo.
79+
func (s *Memo) WatchTree(directory string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) {
80+
return nil, store.ErrCallNotSupported
81+
}
82+
83+
// NewLock is not supported by memo.
84+
func (s *Memo) NewLock(key string, options *store.LockOptions) (store.Locker, error) {
85+
return nil, store.ErrCallNotSupported
86+
}
87+
88+
// List keys with given prefix.
89+
func (s *Memo) List(directory string) ([]*store.KVPair, error) {
90+
keys, err := s.kvs.List(context.Background(),
91+
&kvs.ListRequest{Prefix: directory, MaxKeys: 1000000000})
92+
if err != nil {
93+
return nil, err
94+
}
95+
var res []*store.KVPair
96+
for _, k := range keys.Items {
97+
kv, err := s.Get(k.Key)
98+
if err != nil {
99+
return nil, err
100+
}
101+
res = append(res, kv)
102+
}
103+
return res, nil
104+
}
105+
106+
// DeleteTree deletes all entries with given prefix.
107+
func (s *Memo) DeleteTree(directory string) error {
108+
keys, err := s.kvs.List(context.Background(),
109+
&kvs.ListRequest{Prefix: directory, MaxKeys: 1000000000})
110+
if err != nil {
111+
return err
112+
}
113+
for _, k := range keys.Items {
114+
s.Delete(k.Key)
115+
}
116+
return nil
117+
}
118+
119+
// AtomicPut is not supported by memo.
120+
func (s *Memo) AtomicPut(key string, value []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) {
121+
return false, nil, store.ErrCallNotSupported
122+
}
123+
124+
// AtomicDelete is not supported by memo.
125+
func (s *Memo) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
126+
return false, store.ErrCallNotSupported
127+
}
128+
129+
// Close the connection
130+
func (s *Memo) Close() {
131+
}

0 commit comments

Comments
 (0)