forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock_client.go
158 lines (141 loc) · 3.65 KB
/
mock_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
157
158
package discovery
import (
"fmt"
"path"
"strings"
"sync"
"time"
"github.com/pachyderm/pachyderm/src/client/pkg/uuid"
)
type record struct {
directory bool
data string
expires time.Time
}
type mockClient struct {
records map[string]record
lock sync.RWMutex
}
func newMockClient() *mockClient {
return &mockClient{
make(map[string]record),
sync.RWMutex{},
}
}
func (c *mockClient) Close() error {
return nil
}
func (c *mockClient) Get(key string) (string, bool, error) {
c.lock.RLock()
defer c.lock.RUnlock()
now := time.Now()
record, ok := c.records[key]
if !ok {
return "", false, nil
}
if record.directory {
return "", false, fmt.Errorf("pachyderm: key %s is directory", key)
}
if (record.expires != time.Time{}) && now.After(record.expires) {
delete(c.records, key)
return "", false, nil
}
return record.data, true, nil
}
func (c *mockClient) GetAll(keyPrefix string) (map[string]string, error) {
c.lock.RLock()
defer c.lock.RUnlock()
now := time.Now()
result := make(map[string]string)
for key, record := range c.records {
if (record.expires != time.Time{}) && now.After(record.expires) {
delete(c.records, key)
}
if strings.HasPrefix(key, keyPrefix) && !record.directory {
result[key] = record.data
}
}
return result, nil
}
func (c *mockClient) Watch(key string, cancel chan bool, callBack func(string) error) error {
// TODO jdoliner
return nil
}
func (c *mockClient) WatchAll(key string, cancel chan bool, callBack func(map[string]string) error) error {
// TODO jdoliner
return nil
}
func (c *mockClient) Set(key string, value string, ttl uint64) error {
c.lock.Lock()
defer c.lock.Unlock()
return c.unsafeSet(key, value, ttl)
}
func (c *mockClient) Create(key string, value string, ttl uint64) error {
c.lock.Lock()
defer c.lock.Unlock()
_, ok := c.records[key]
if ok {
return fmt.Errorf("pachyderm: key %s already exists", key)
}
return c.unsafeSet(key, value, ttl)
}
func (c *mockClient) CreateInDir(dir string, value string, ttl uint64) error {
c.lock.Lock()
defer c.lock.Unlock()
key := path.Join(dir, uuid.NewWithoutDashes())
return c.unsafeSet(key, value, ttl)
}
func (c *mockClient) Delete(key string) error {
c.lock.Lock()
defer c.lock.Unlock()
oldRecord, ok := c.records[key]
if !ok {
return nil
}
if oldRecord.directory {
return fmt.Errorf("pachyderm: can't delete directory %s", key)
}
delete(c.records, key)
return nil
}
func (c *mockClient) CheckAndSet(key string, value string, ttl uint64, oldValue string) error {
c.lock.Lock()
defer c.lock.Unlock()
oldRecord, ok := c.records[key]
if !ok {
return fmt.Errorf("pachyderm: key %s not found", key)
}
if oldRecord.directory {
return fmt.Errorf("pachyderm: can't set directory %s", key)
}
if oldRecord.data != oldValue {
return fmt.Errorf("pachyderm: precondition not met for %s", key)
}
return c.unsafeSet(key, value, ttl)
}
func (c *mockClient) unsafeSet(key string, value string, ttl uint64) error {
var expires time.Time
if ttl != 0 {
expires = time.Now().Add(time.Second * time.Duration(ttl))
}
parts := strings.Split(key, "/")
for i := range parts[:len(parts)-1] {
oldRecord, ok := c.records["/"+path.Join(parts[:i]...)]
if ok && !oldRecord.directory {
return fmt.Errorf("pachyderm: key %s not found", key)
}
if !ok {
c.records["/"+path.Join(parts[:i]...)] = record{true, "", expires}
}
}
oldRecord, ok := c.records[key]
if ok && oldRecord.directory {
return fmt.Errorf("pachyderm: can't set directory %s", key)
}
c.records[key] = record{false, value, expires}
return nil
}
func (c *mockClient) Hold(key string, value string, oldValue string, cancel chan bool) error {
// TODO jdoliner
return nil
}