forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
187 lines (152 loc) · 3.49 KB
/
store.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
package state
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"sync"
log "github.com/Sirupsen/logrus"
)
var (
ErrNotFound = errors.New("not found")
ErrAlreadyExists = errors.New("already exists")
ErrInvalidKey = errors.New("invalid key")
)
// A simple key<->RequestedState store.
type Store struct {
RootDir string
values map[string]*RequestedState
sync.RWMutex
}
func NewStore(rootdir string) *Store {
return &Store{
RootDir: rootdir,
values: make(map[string]*RequestedState),
}
}
// Initialize must be called before performing any operation on the store. It
// will attempt to restore the data from disk.
func (s *Store) Initialize() error {
s.Lock()
defer s.Unlock()
if err := os.MkdirAll(s.RootDir, 0700); err != nil && !os.IsNotExist(err) {
return err
}
if err := s.restore(); err != nil {
return err
}
return nil
}
func (s *Store) path(key string) string {
return path.Join(s.RootDir, key+".json")
}
func (s *Store) restore() error {
files, err := ioutil.ReadDir(s.RootDir)
if err != nil {
return err
}
for _, fileinfo := range files {
file := fileinfo.Name()
// Verify the file extension.
extension := filepath.Ext(file)
if extension != ".json" {
log.Errorf("invalid file extension for filename %s (%s)", file, extension)
continue
}
// Load the object back.
value, err := s.load(path.Join(s.RootDir, file))
if err != nil {
log.Errorf(err.Error())
continue
}
// Extract the key.
key := file[0 : len(file)-len(extension)]
if len(key) == 0 {
log.Errorf("invalid filename %s", file)
continue
}
// Store it back.
s.values[key] = value
}
return nil
}
func (s *Store) load(file string) (*RequestedState, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("unable to load %s: %v", file, err)
}
value := &RequestedState{}
if err := json.Unmarshal(data, value); err != nil {
return nil, err
}
return value, nil
}
// Retrieves an object from the store keyed by `key`.
func (s *Store) Get(key string) (*RequestedState, error) {
s.RLock()
defer s.RUnlock()
if value, ok := s.values[key]; ok {
return value, nil
}
return nil, ErrNotFound
}
// Return all objects of the store.
func (s *Store) All() []*RequestedState {
s.RLock()
defer s.RUnlock()
states := make([]*RequestedState, len(s.values))
i := 0
for _, state := range s.values {
states[i] = state
i = i + 1
}
return states
}
func (s *Store) set(key string, value *RequestedState) error {
if len(key) == 0 {
return ErrInvalidKey
}
data, err := json.MarshalIndent(value, "", " ")
if err != nil {
return err
}
if err := ioutil.WriteFile(s.path(key), data, 0600); err != nil {
return err
}
s.values[key] = value
return nil
}
// Add a new object on the store. `key` must be unique.
func (s *Store) Add(key string, value *RequestedState) error {
s.Lock()
defer s.Unlock()
if _, exists := s.values[key]; exists {
return ErrAlreadyExists
}
return s.set(key, value)
}
// Replaces an already existing object from the store.
func (s *Store) Replace(key string, value *RequestedState) error {
s.Lock()
defer s.Unlock()
if _, exists := s.values[key]; !exists {
return ErrNotFound
}
return s.set(key, value)
}
// Remove `key` from the store.
func (s *Store) Remove(key string) error {
s.Lock()
defer s.Unlock()
if _, exists := s.values[key]; !exists {
return ErrNotFound
}
if err := os.Remove(s.path(key)); err != nil {
return err
}
delete(s.values, key)
return nil
}