forked from cpuguy83/drax
/
client.go
156 lines (128 loc) · 3.35 KB
/
client.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package client
import (
"crypto/tls"
"errors"
"time"
"github.com/docker/docker/pkg/raftkv/api"
"github.com/docker/libkv/store"
)
type Client interface {
Get(key string) (*store.KVPair, error)
Put(key string, value []byte, options *store.WriteOptions) error
Delete(key string) error
Exists(key string) (bool, error)
List(prefix string) ([]*store.KVPair, error)
DeleteTree(dir string) error
Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error)
WatchTree(dir string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error)
NewLock(key string, options *store.LockOptions) (store.Locker, error)
AtomicPut(key string, value []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error)
AtomicDelete(key string, previous *store.KVPair) (bool, error)
}
var errNotImplemented = errors.New("Call not implemented in current backend")
type client struct {
addr string
tlsConfig *tls.Config
dialTimeout time.Duration
}
func New(addr string, tlsConfig *tls.Config, dialTimeout time.Duration) Client {
return &client{addr, tlsConfig, dialTimeout}
}
func (c *client) Get(key string) (*store.KVPair, error) {
req := &api.Request{
Action: api.Get,
Key: key,
}
res, err := c.do(req)
if err != nil {
return nil, err
}
return kvToLibKV(res.KV), nil
}
func (c *client) Put(key string, value []byte, options *store.WriteOptions) error {
req := &api.Request{
Action: api.Put,
Key: key,
Value: value,
TTL: options.TTL,
}
_, err := c.do(req)
return err
}
func (c *client) Delete(key string) error {
req := &api.Request{
Action: api.Delete,
Key: key,
}
_, err := c.do(req)
return err
}
func (c *client) Exists(key string) (bool, error) {
req := &api.Request{
Action: api.Exists,
Key: key,
}
res, err := c.do(req)
return res.Exists, err
}
func (c *client) List(prefix string) ([]*store.KVPair, error) {
req := &api.Request{
Action: api.List,
Key: prefix,
}
res, err := c.do(req)
if err != nil {
return nil, err
}
var ls []*store.KVPair
for _, kv := range res.List {
ls = append(ls, kvToLibKV(kv))
}
if len(ls) == 0 {
return nil, store.ErrKeyNotFound
}
return ls, nil
}
func (c *client) DeleteTree(dir string) error {
req := &api.Request{
Action: api.DeleteTree,
Key: dir,
}
_, err := c.do(req)
return err
}
func (c *client) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) {
return nil, errNotImplemented
}
func (c *client) WatchTree(dir string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) {
return nil, errNotImplemented
}
func (c *client) NewLock(key string, options *store.LockOptions) (store.Locker, error) {
return nil, errNotImplemented
}
func (c *client) AtomicPut(key string, value []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) {
req := api.Request{
Action: api.AtomicPut,
Key: key,
Value: value,
Previous: libkvToKV(previous),
TTL: options.TTL,
}
res, err := c.do(&req)
if err != nil {
return false, nil, err
}
return res.Completed, kvToLibKV(res.KV), nil
}
func (c *client) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
req := api.Request{
Action: api.AtomicDelete,
Key: key,
Previous: libkvToKV(previous),
}
res, err := c.do(&req)
if err != nil {
return false, err
}
return res.Completed, nil
}