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