-
Notifications
You must be signed in to change notification settings - Fork 7
/
bolt.go
239 lines (196 loc) · 5.46 KB
/
bolt.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
package pkgmirror
import (
"errors"
"fmt"
"os"
"time"
log "github.com/Sirupsen/logrus"
"github.com/boltdb/bolt"
)
func OpenDatabaseWithBucket(basePath string, bucket []byte) (db *bolt.DB, err error) {
if err = os.MkdirAll(basePath, 0755); err != nil {
return
}
path := fmt.Sprintf("%s/%s.db", basePath, bucket)
db, err = bolt.Open(path, 0600, &bolt.Options{
Timeout: 1 * time.Second,
ReadOnly: false,
})
if err != nil {
return
}
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(bucket)
return err
})
return
}
// adapted from : https://github.com/boltdb/bolt/blob/master/cmd/bolt/main.go
var (
// ErrPathRequired is returned when the path to a Bolt database is not specified.
ErrPathRequired = errors.New("path required")
// ErrFileNotFound is returned when a Bolt database does not exist.
ErrFileNotFound = errors.New("file not found")
ErrUnableToCloseDatabase = errors.New("Unable to close the database")
)
type BoltCompacter struct {
TxMaxSize int64
Logger *log.Logger
}
// Run executes the command.
func (bc *BoltCompacter) Compact(srcPath string) (err error) {
now := time.Now()
bckPath := fmt.Sprintf("%s.%d-%d-%d-%d.backup", srcPath, now.Year(), now.Month(), now.Day(), now.Unix())
dstPath := fmt.Sprintf("%s.%d-%d-%d-%d.compacted", srcPath, now.Year(), now.Month(), now.Day(), now.Unix())
logger := bc.Logger.WithFields(log.Fields{
"method": "compacter",
"srcPath": srcPath,
"dstPath": dstPath,
})
// Require database paths.
if srcPath == "" {
logger.Error("srcPath is not defined")
return ErrPathRequired
}
// Ensure source file exists.
fi, err := os.Stat(srcPath)
if os.IsNotExist(err) {
logger.Error("srcPath does not exist")
return ErrFileNotFound
} else if err != nil {
return err
}
initialSize := fi.Size()
// Open source database.
src, err := bolt.Open(srcPath, 0444, nil)
if err != nil {
logger.Error("unable to open the src database")
return err
}
defer src.Close()
// Open destination database.
dst, err := bolt.Open(dstPath, fi.Mode(), nil)
if err != nil {
logger.Error("unable to open the dst database")
return err
}
defer dst.Close()
// Run compaction.
if err := bc.compact(dst, src); err != nil {
logger.Error("unable to compact the database")
return err
}
// Report stats on new size.
fi, err = os.Stat(dstPath)
if err != nil {
logger.Error("unable to get the stat on the compacted database")
return err
} else if fi.Size() == 0 {
return fmt.Errorf("zero db size")
}
logger.WithFields(log.Fields{
"stat": fmt.Sprintf("%d -> %d bytes (gain=%.2fx)\n", initialSize, fi.Size(), float64(initialSize)/float64(fi.Size())),
}).Info("Compact action ended!")
// mv srcPath => bckPath
if err := os.Rename(srcPath, bckPath); err != nil {
return err
}
if err := os.Rename(dstPath, srcPath); err != nil {
return err
}
// delete backup
os.Remove(bckPath)
return nil
}
func (bc *BoltCompacter) compact(dst, src *bolt.DB) error {
// commit regularly, or we'll run out of memory for large datasets if using one transaction.
var size int64
tx, err := dst.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
if err := bc.walk(src, func(keys [][]byte, k, v []byte, seq uint64) error {
// On each key/value, check if we have exceeded tx size.
sz := int64(len(k) + len(v))
if size+sz > bc.TxMaxSize && bc.TxMaxSize != 0 {
// Commit previous transaction.
if err := tx.Commit(); err != nil {
return err
}
// Start new transaction.
tx, err = dst.Begin(true)
if err != nil {
return err
}
size = 0
}
size += sz
// Create bucket on the root transaction if this is the first level.
nk := len(keys)
if nk == 0 {
bkt, err := tx.CreateBucket(k)
if err != nil {
return err
}
if err := bkt.SetSequence(seq); err != nil {
return err
}
return nil
}
// Create buckets on subsequent levels, if necessary.
b := tx.Bucket(keys[0])
if nk > 1 {
for _, k := range keys[1:] {
b = b.Bucket(k)
}
}
// If there is no value then this is a bucket call.
if v == nil {
bkt, err := b.CreateBucket(k)
if err != nil {
return err
}
if err := bkt.SetSequence(seq); err != nil {
return err
}
return nil
}
// Otherwise treat it as a key/value pair.
return b.Put(k, v)
}); err != nil {
return err
}
return tx.Commit()
}
// walkFunc is the type of the function called for keys (buckets and "normal"
// values) discovered by Walk. keys is the list of keys to descend to the bucket
// owning the discovered key/value pair k/v.
type walkFunc func(keys [][]byte, k, v []byte, seq uint64) error
// walk walks recursively the bolt database db, calling walkFn for each key it finds.
func (bc *BoltCompacter) walk(db *bolt.DB, walkFn walkFunc) error {
return db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, b *bolt.Bucket) error {
return bc.walkBucket(b, nil, name, nil, b.Sequence(), walkFn)
})
})
}
func (bc *BoltCompacter) walkBucket(b *bolt.Bucket, keypath [][]byte, k, v []byte, seq uint64, fn walkFunc) error {
// Execute callback.
if err := fn(keypath, k, v, seq); err != nil {
return err
}
// If this is not a bucket then stop.
if v != nil {
return nil
}
// Iterate over each child key/value.
keypath = append(keypath, k)
return b.ForEach(func(k, v []byte) error {
if v == nil {
bkt := b.Bucket(k)
return bc.walkBucket(bkt, keypath, k, nil, bkt.Sequence(), fn)
}
return bc.walkBucket(b, keypath, k, v, b.Sequence(), fn)
})
}