This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
352 lines (324 loc) · 7.69 KB
/
common.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package store
import (
"fmt"
"net/url"
"os"
"path"
"sync"
)
func safeReplace(name string, contents []byte) error {
tmpName := path.Join(path.Dir(name), ".new."+path.Base(name))
f, err := os.OpenFile(tmpName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)
if err != nil {
return err
}
func() {
defer f.Close()
if _, err = f.Write(contents); err == nil {
err = f.Sync()
}
}()
if err != nil {
return err
}
return os.Rename(tmpName, name)
}
// Open a store via URI style locator. Locators have the following formats:
//
// storeType:path?codec=codecType&ro=false&option=foo for stores
// that always refer to something local, and
//
// storeType://host:port/path?codec=codecType&ro=false&option=foo for stores
// that need to talk over the network.
//
// All store types take codec and ro as optional parameters
//
// The following storeTypes are known:
// * file, in which path refers to a single local file.
// * directory, in which path refers to a top-level directory
// * consul, in which path refers to the top key in the kv store.
// * bolt, in which path refers to the directory where the Bolt database
// is located. bolt also takes an optional bucket parameter to specify the
// top-level bucket data is stored in.
// * memory, in which path does not mean anything.
//
func Open(locator string) (Store, error) {
uri, err := url.Parse(locator)
if err != nil {
return nil, err
}
params := uri.Query()
codec := DefaultCodec
readOnly := false
codecParam := params.Get("codec")
switch codecParam {
case "yaml":
codec = YamlCodec
case "json":
codec = JsonCodec
case "", "default":
codec = DefaultCodec
default:
return nil, fmt.Errorf("Unknown codec %s", codecParam)
}
roParam := params.Get("ro")
switch roParam {
case "true", "yes", "1":
readOnly = true
case "false", "no", "0", "":
readOnly = false
default:
return nil, fmt.Errorf("Unknown ro value %s. Try true or false", roParam)
}
var res Store
path := uri.Opaque
if path == "" {
path = uri.Path
}
switch uri.Scheme {
case "stack":
res = &StackedStore{}
case "file":
res = &File{Path: path}
case "directory":
res = &Directory{Path: path}
case "bolt":
res = &Bolt{Path: path}
bucketParam := params.Get("bucket")
if bucketParam != "" {
res.(*Bolt).Bucket = []byte(bucketParam)
}
case "consul":
res = &Consul{BaseKey: path}
case "memory":
res = &Memory{}
}
if res == nil {
return nil, fmt.Errorf("Unknown schema type: %s", uri.Scheme)
}
if err := res.Open(codec); err != nil {
return nil, err
}
if readOnly {
res.SetReadOnly()
}
return res, nil
}
// Store provides an interface for some very basic key/value
// storage needs. Each Store (including ones created with MakeSub()
// should operate as seperate, flat key/value stores.
type Store interface {
sync.Locker
RLock()
RUnlock()
// Open opens the store for use.
Open(Codec) error
// GetCodec returns the codec that the open store uses for marshalling and unmarshalling data
GetCodec() Codec
// GetSub fetches an already-existing substore. nil means there is no such substore.
GetSub(string) Store
// MakeSub returns a Store that is subordinate to this one.
// What exactly that means depends on the simplestore in question,
// but it should wind up sharing the same backing store (directory,
// database, etcd cluster, whatever)
MakeSub(string) (Store, error)
// Parent fetches the parent of this store, if any.
Parent() Store
// Keys returns the list of keys that this store has in no
// particular order.
Keys() ([]string, error)
// Subs returns a map all of the substores for this store.
Subs() map[string]Store
// Load the data for a particular key
Load(string, interface{}) error
// Save data for a key
Save(string, interface{}) error
// Remove a key/value pair.
Remove(string) error
// ReadOnly returns whether a store is set to be read-only.
ReadOnly() bool
// SetReadOnly sets the store into read-only mode. This is a
// one-way operation -- once a store is set to read-only, it
// cannot be changed back to read-write while the store is open.
SetReadOnly() bool
// Close closes the store. Attempting to perfrom operations on
// a closed store will panic.
Close()
// Closed returns whether or not a store is Closed
Closed() bool
// Type is the type of Store this is.
Type() string
// Name is the name of Store this is.
Name() string
}
// MetaSaver is a Store that is capable of of recording
// metadata about itself.
type MetaSaver interface {
Store
MetaData() map[string]string
SetMetaData(map[string]string) error
}
// Copy copies all of the contents from src to dest, including substores and
// metadata. If dst starts out empty, then dst will wind up being a clone of src.
func Copy(dst, src Store) error {
src.RLock()
defer src.RUnlock()
dmeta, dok := dst.(MetaSaver)
smeta, sok := src.(MetaSaver)
if dok && sok {
if err := dmeta.SetMetaData(smeta.MetaData()); err != nil {
return err
}
}
keys, err := src.Keys()
if err != nil {
return err
}
for _, key := range keys {
var val interface{}
if err := src.Load(key, &val); err != nil {
return err
}
if err := dst.Save(key, val); err != nil {
return err
}
}
for k, sub := range src.Subs() {
subDst, err := dst.MakeSub(k)
if err != nil {
return err
}
if err := Copy(subDst, sub); err != nil {
return err
}
}
return nil
}
type parentSetter interface {
setParent(Store)
}
type childSetter interface {
addChild(string, Store)
}
type forceCloser interface {
forceClose()
}
// NotFound is the "key not found" error type.
type NotFound string
func (n NotFound) Error() string {
return fmt.Sprintf("key %s: not found", string(n))
}
type UnWritable string
func (u UnWritable) Error() string {
return fmt.Sprintf("readonly: %s", string(u))
}
type storeBase struct {
sync.RWMutex
Codec
readOnly bool
opened bool
subStores map[string]Store
parentStore Store
closer func()
name string
}
func (s *storeBase) Name() string {
if s.parentStore != nil {
return s.parentStore.Name()
}
return s.name
}
func (s *storeBase) forceClose() {
s.Lock()
defer s.Unlock()
if !s.opened {
return
}
if s.closer != nil {
s.closer()
}
s.opened = false
}
func (s *storeBase) Close() {
s.Lock()
if s.parentStore == nil {
s.Unlock()
s.forceClose()
for _, sub := range s.subStores {
sub.(forceCloser).forceClose()
}
return
}
parent := s.parentStore
s.Unlock()
parent.Close()
return
}
func (s *storeBase) GetCodec() Codec {
return s.Codec
}
func (s *storeBase) panicIfClosed() {
if !s.opened {
panic("Operation on closed store")
}
}
func (s *storeBase) ReadOnly() bool {
s.RLock()
defer s.RUnlock()
s.panicIfClosed()
return s.readOnly
}
func (s *storeBase) SetReadOnly() bool {
s.Lock()
defer s.Unlock()
s.panicIfClosed()
if s.readOnly {
return false
}
s.readOnly = true
for _, sub := range s.subStores {
sub.SetReadOnly()
}
return true
}
func (s *storeBase) GetSub(name string) Store {
s.RLock()
defer s.RUnlock()
s.panicIfClosed()
if s.subStores == nil {
return nil
}
return s.subStores[name]
}
func (s *storeBase) Subs() map[string]Store {
s.RLock()
defer s.RUnlock()
s.panicIfClosed()
res := map[string]Store{}
for k, v := range s.subStores {
res[k] = v
}
return res
}
func (s *storeBase) Parent() Store {
s.RLock()
defer s.RUnlock()
s.panicIfClosed()
return s.parentStore.(Store)
}
func (s *storeBase) Closed() bool {
return !s.opened
}
func (s *storeBase) setParent(p Store) {
s.parentStore = p
}
func (s *storeBase) addChild(name string, c Store) {
if s.subStores == nil {
s.subStores = map[string]Store{}
}
s.subStores[name] = c
}
func addSub(parent, child Store, name string) {
parent.(childSetter).addChild(name, child)
child.(parentSetter).setParent(parent)
}