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
/
local.go
248 lines (231 loc) · 4.57 KB
/
local.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
package store
import (
"bytes"
"fmt"
"os"
"path/filepath"
"github.com/boltdb/bolt"
)
type Bolt struct {
storeBase
Path string
db *bolt.DB
Bucket []byte
}
func (b *Bolt) Type() string {
return "bolt"
}
func (b *Bolt) MetaData() map[string]string {
res := map[string]string{}
b.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte("$metadata"))
if bucket == nil {
return nil
}
bucket.ForEach(func(k, v []byte) error {
if v == nil {
return nil
}
res[string(k)] = string(v)
return nil
})
return nil
})
return res
}
func (b *Bolt) SetMetaData(vals map[string]string) error {
return b.db.Update(func(tx *bolt.Tx) error {
tx.DeleteBucket([]byte("$metadata"))
bucket, err := tx.CreateBucket([]byte("$metadata"))
if err != nil {
return err
}
for k, v := range vals {
if err := bucket.Put([]byte(k), []byte(v)); err != nil {
return err
}
}
if n, ok := vals["Name"]; ok {
b.name = n
}
return nil
})
}
func (b *Bolt) getBucket(tx *bolt.Tx) (res *bolt.Bucket) {
for _, part := range bytes.Split(b.Bucket, []byte("/")) {
if res == nil {
res = tx.Bucket(part)
} else {
res = res.Bucket(part)
}
if res == nil {
panic(fmt.Sprintf("Bucket %s does not exist", string(b.Bucket)))
}
}
return
}
func (b *Bolt) createBucket() error {
return b.db.Update(func(tx *bolt.Tx) error {
var bukkit *bolt.Bucket
var err error
for _, part := range bytes.Split(b.Bucket, []byte("/")) {
if bukkit == nil {
bukkit, err = tx.CreateBucketIfNotExists(part)
} else {
bukkit, err = bukkit.CreateBucketIfNotExists(part)
}
if err != nil {
return err
}
}
return nil
})
}
func (b *Bolt) MakeSub(loc string) (Store, error) {
b.Lock()
defer b.Unlock()
b.panicIfClosed()
if res, ok := b.subStores[loc]; ok {
return res, nil
}
res := &Bolt{db: b.db, Bucket: bytes.Join([][]byte{b.Bucket, []byte(loc)}, []byte("/"))}
if err := res.Open(b.Codec); err != nil {
return nil, err
}
res.closer = func() {
res.db = nil
}
addSub(b, res, loc)
return res, nil
}
func (l *Bolt) loadSubs() error {
if err := l.createBucket(); err != nil {
return err
}
subs := [][]byte{}
err := l.db.View(func(tx *bolt.Tx) error {
bucket := l.getBucket(tx)
bucket.ForEach(func(k, v []byte) error {
if v == nil {
subs = append(subs, k)
}
return nil
})
return nil
})
if err != nil {
return err
}
for _, sub := range subs {
if _, err := l.MakeSub(string(sub)); err != nil {
return err
}
}
return nil
}
func (b *Bolt) Open(codec Codec) error {
if b.Bucket == nil {
b.Bucket = []byte(`Default`)
}
if codec == nil {
codec = DefaultCodec
}
b.Codec = codec
if b.db == nil {
if b.Path == "" {
return fmt.Errorf("Cannot store data in ''")
}
finalLoc := filepath.Clean(b.Path)
if err := os.MkdirAll(finalLoc, 0755); err != nil {
return err
}
db, err := bolt.Open(filepath.Join(finalLoc, "bolt.db"), 0600, nil)
if err != nil {
return err
}
b.db = db
}
b.opened = true
if err := b.loadSubs(); err != nil {
return err
}
b.closer = func() {
b.db.Close()
b.db = nil
}
md := b.MetaData()
if n, ok := md["Name"]; ok {
b.name = n
}
return nil
}
func (b *Bolt) Keys() ([]string, error) {
b.panicIfClosed()
res := []string{}
err := b.db.View(func(tx *bolt.Tx) error {
bucket := b.getBucket(tx)
bucket.ForEach(func(k, v []byte) error {
if v != nil {
res = append(res, string(k))
}
return nil
})
return nil
})
return res, err
}
func (b *Bolt) Load(key string, val interface{}) error {
b.panicIfClosed()
var res []byte
err := b.db.View(func(tx *bolt.Tx) error {
bucket := b.getBucket(tx)
res = bucket.Get([]byte(key))
if res == nil {
return os.ErrNotExist
}
return nil
})
if err != nil {
return err
}
if err := b.Decode(res, val); err != nil {
return err
}
if ro, ok := val.(ReadOnlySetter); ok {
ro.SetReadOnly(b.ReadOnly())
}
if bb, ok := val.(BundleSetter); ok {
n := b.Name()
if n != "" {
bb.SetBundle(n)
}
}
return nil
}
func (b *Bolt) Save(key string, val interface{}) error {
b.panicIfClosed()
if b.ReadOnly() {
return UnWritable(key)
}
buf, err := b.Encode(val)
if err != nil {
return err
}
return b.db.Update(func(tx *bolt.Tx) error {
bucket := b.getBucket(tx)
return bucket.Put([]byte(key), buf)
})
}
func (b *Bolt) Remove(key string) error {
b.panicIfClosed()
if b.ReadOnly() {
return UnWritable(key)
}
return b.db.Update(func(tx *bolt.Tx) error {
bucket := b.getBucket(tx)
if res := bucket.Get([]byte(key)); res == nil {
return os.ErrNotExist
}
return bucket.Delete([]byte(key))
})
}