/
boltdb.go
325 lines (248 loc) · 6.86 KB
/
boltdb.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
package datastores
import (
"encoding/json"
"time"
"github.com/boltdb/bolt"
)
// The BoltDB database information
type BoltDB struct {
Database string
User string
Password string
}
const system_ids string = "system_ids"
// If we need to list applications, we can do so by listing buckets:
// https://github.com/boltdb/bolt/issues/295
func (store BoltDB) InitStore(overwrite bool) error {
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
defer db.Close()
return err
}
func (store BoltDB) Get(configItem ConfigItem) (ConfigItem, error) {
// Our return item:
retval := ConfigItem{}
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
defer db.Close()
if err != nil {
return retval, err
}
// If we have a machine name, append it in the key:
keyName := configItem.Name
if configItem.Machine != "" {
keyName = keyName + "|" + configItem.Machine
}
// Get the key based on the 'global' application
err = db.View(func(tx *bolt.Tx) error {
// Get the item from the bucket with the app name
b := tx.Bucket([]byte("*"))
if b != nil {
configBytes := b.Get([]byte(keyName))
// Need to make sure we got something back here before we try to unmarshal?
if len(configBytes) > 0 {
// Unmarshal data into our config item
if err := json.Unmarshal(configBytes, &retval); err != nil {
return err
}
}
}
return nil
})
// Get the key based on the application name
err = db.View(func(tx *bolt.Tx) error {
// Get the item from the bucket with the app name
b := tx.Bucket([]byte(configItem.Application))
if b != nil {
configBytes := b.Get([]byte(keyName))
// Need to make sure we got something back here before we try to unmarshal?
if len(configBytes) > 0 {
// Unmarshal data into our config item
if err := json.Unmarshal(configBytes, &retval); err != nil {
return err
}
}
}
return nil
})
return retval, err
}
func (store BoltDB) GetAllForApplication(application string) ([]ConfigItem, error) {
// Our return items:
retval := []ConfigItem{}
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
defer db.Close()
if err != nil {
return retval, err
}
// Get the global app data first:
err = db.View(func(tx *bolt.Tx) error {
// Get the items from the bucket with the app name
b := tx.Bucket([]byte("*"))
if b != nil {
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
// Unmarshal data into our config item
ci := ConfigItem{}
if err := json.Unmarshal(v, &ci); err != nil {
return err
}
// Add the item to our list of config items
retval = append(retval, ci)
}
}
return nil
})
// Get the key based on the application
err = db.View(func(tx *bolt.Tx) error {
// Get the items from the bucket with the app name
b := tx.Bucket([]byte(application))
if b != nil {
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
// Unmarshal data into our config item
ci := ConfigItem{}
if err := json.Unmarshal(v, &ci); err != nil {
return err
}
// Add the item to our list of config items
retval = append(retval, ci)
}
}
return nil
})
// TODO: get application + machine
return retval, err
}
func (store BoltDB) GetAll() ([]ConfigItem, error) {
// Our return items:
retval := []ConfigItem{}
var bucketList []string
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
defer db.Close()
if err != nil {
return retval, err
}
// Get a list of all buckets
err = db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, _ *bolt.Bucket) error {
bucketList = append(bucketList, string(name))
return nil
})
})
// For each bucket...
for _, bucketName := range bucketList {
// ... get a list of configitems for that bucket
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
if b != nil {
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
// Unmarshal data into our config item
ci := ConfigItem{}
if err := json.Unmarshal(v, &ci); err != nil {
return err
}
// Add the item to our list of config items
retval = append(retval, ci)
}
}
return nil
})
}
return retval, err
}
func (store BoltDB) GetAllApplications() ([]string, error) {
// Our return items:
var bucketList []string
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
defer db.Close()
if err != nil {
return bucketList, err
}
// Get a list of all buckets
err = db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, _ *bolt.Bucket) error {
// As long as the bucket name isn't the reserved 'system_ids' bucket
// return the bucket name as an application name
if string(name) != system_ids {
bucketList = append(bucketList, string(name))
}
return nil
})
})
return bucketList, err
}
func (store BoltDB) Set(configItem ConfigItem) (ConfigItem, error) {
// Our return item:
retval := ConfigItem{}
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
defer db.Close()
if err != nil {
return retval, err
}
// Update the database:
err = db.Update(func(tx *bolt.Tx) error {
// Put the item in the bucket with the app name
b, err := tx.CreateBucketIfNotExists([]byte(configItem.Application))
if err != nil {
return err
}
// If we don't have an id, generate an id for the configitem.
// This returns an error only if the Tx is closed or not writeable.
// That can't happen in an Update() call so I ignore the error check.
if configItem.Id == 0 {
bids, err := tx.CreateBucketIfNotExists([]byte(system_ids))
if err != nil {
return err
}
id, _ := bids.NextSequence()
configItem.Id = int64(id)
}
// Set the current datetime:
configItem.LastUpdated = time.Now()
// Serialize to JSON format
encoded, err := json.Marshal(configItem)
if err != nil {
return err
}
// If we have a machine name, append it in the key:
keyName := configItem.Name
if configItem.Machine != "" {
keyName = keyName + "|" + configItem.Machine
}
// Store it, with the 'name' as the key:
return b.Put([]byte(keyName), encoded)
})
// Set our return item:
retval = configItem
return retval, err
}
func (store BoltDB) Remove(configItem ConfigItem) error {
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
defer db.Close()
if err != nil {
return err
}
// Update the database:
err = db.Update(func(tx *bolt.Tx) error {
// Get the item from the bucket with the app name
b := tx.Bucket([]byte(configItem.Application))
if b != nil {
// If we have a machine name, append it in the key:
keyName := configItem.Name
if configItem.Machine != "" {
keyName = keyName + "|" + configItem.Machine
}
// Delete it, with the 'name' as the key:
return b.Delete([]byte(keyName))
}
return nil
})
return err
}