-
Notifications
You must be signed in to change notification settings - Fork 171
/
bolt.go
284 lines (239 loc) · 6.93 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
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
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package versions
import (
"context"
"encoding/binary"
"os"
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/dao/boltdb"
"github.com/pydio/cells/v4/common/log"
"github.com/pydio/cells/v4/common/proto/tree"
servicecontext "github.com/pydio/cells/v4/common/service/context"
"github.com/pydio/cells/v4/common/service/errors"
)
var (
bucketName = []byte("versions")
)
type BoltStore struct {
boltdb.DAO
// For Testing purpose : delete file after closing
DeleteOnClose bool
// Path to the DB file
DbPath string
}
func NewBoltStore(dao boltdb.DAO, fileName string, deleteOnClose ...bool) (*BoltStore, error) {
bs := &BoltStore{
DAO: dao,
DbPath: fileName,
}
if len(deleteOnClose) > 0 && deleteOnClose[0] {
bs.DeleteOnClose = true
}
e2 := bs.DB().Update(func(tx *bolt.Tx) error {
_, e := tx.CreateBucketIfNotExists(bucketName)
return e
})
return bs, e2
}
func (b *BoltStore) Close() error {
err := b.DB().Close()
if b.DeleteOnClose {
os.Remove(b.DbPath)
}
return err
}
// GetLastVersion retrieves the last version registered for this node.
func (b *BoltStore) GetLastVersion(nodeUuid string) (log *tree.ChangeLog, err error) {
err = b.DB().View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(bucketName)
if bucket == nil {
return errors.NotFound(common.ServiceVersions, "Bucket not found, this is not normal")
}
nodeBucket := bucket.Bucket([]byte(nodeUuid))
if nodeBucket == nil {
// Ignore not found
return nil
}
c := nodeBucket.Cursor()
_, v := c.Last()
theLog := &tree.ChangeLog{}
e := proto.Unmarshal(v, theLog)
if e != nil {
return e
}
log = theLog
return nil
})
return log, err
}
// GetVersions returns all versions from the node bucket, in reverse order (last inserted first).
func (b *BoltStore) GetVersions(nodeUuid string) (chan *tree.ChangeLog, error) {
logChan := make(chan *tree.ChangeLog)
go func() {
defer func() {
close(logChan)
}()
e := b.DB().View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(bucketName)
if bucket == nil {
return errors.NotFound(common.ServiceVersions, "Bucket not found, this is not normal")
}
nodeBucket := bucket.Bucket([]byte(nodeUuid))
if nodeBucket == nil {
return errors.NotFound(common.ServiceVersions, "No bucket found for this node")
}
c := nodeBucket.Cursor()
for k, v := c.Last(); k != nil; k, v = c.Prev() {
aLog := &tree.ChangeLog{}
e := proto.Unmarshal(v, aLog)
if e != nil {
return e
}
log.Logger(context.Background()).Debug("Versions:Bolt", aLog.Zap())
logChan <- aLog
}
return nil
})
if e != nil {
log.Logger(servicecontext.WithServiceName(context.Background(), common.ServiceGrpcNamespace_+common.ServiceVersions)).Warn("ListVersions", zap.Error(e))
}
}()
return logChan, nil
}
// StoreVersion stores a version in the node bucket.
func (b *BoltStore) StoreVersion(nodeUuid string, log *tree.ChangeLog) error {
return b.DB().Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(bucketName)
if bucket == nil {
return errors.NotFound(common.ServiceVersions, "bucket not found")
}
nodeBucket, err := bucket.CreateBucketIfNotExists([]byte(nodeUuid))
if err != nil {
return err
}
newValue, e := proto.Marshal(log)
if e != nil {
return e
}
objectKey, _ := nodeBucket.NextSequence()
k := make([]byte, 8)
binary.BigEndian.PutUint64(k, objectKey)
return nodeBucket.Put(k, newValue)
})
}
// GetVersion retrieves a specific version from the node bucket.
func (b *BoltStore) GetVersion(nodeUuid string, versionId string) (*tree.ChangeLog, error) {
version := &tree.ChangeLog{}
b.DB().View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(bucketName)
if bucket == nil {
return errors.NotFound(common.ServiceVersions, "bucket not found")
}
nodeBucket := bucket.Bucket([]byte(nodeUuid))
if nodeBucket == nil {
return nil
}
c := nodeBucket.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
vers := &tree.ChangeLog{}
if e := proto.Unmarshal(v, vers); e == nil && vers.Uuid == versionId {
version = vers
break
}
}
return nil
})
return version, nil
}
// DeleteVersionsForNode deletes whole node bucket at once.
func (b *BoltStore) DeleteVersionsForNode(nodeUuid string, versions ...*tree.ChangeLog) error {
return b.DB().Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(bucketName)
if bucket == nil {
return errors.NotFound(common.ServiceVersions, "bucket not found")
}
nodeBucket := bucket.Bucket([]byte(nodeUuid))
if nodeBucket != nil {
if len(versions) > 0 { // delete some specific versions
c := nodeBucket.Cursor()
var keys [][]byte
for k, v := c.First(); k != nil; k, v = c.Next() {
vers := &tree.ChangeLog{}
if e := proto.Unmarshal(v, vers); e == nil {
for _, version := range versions {
if vers.Uuid == version.Uuid {
keys = append(keys, k)
}
}
}
}
for _, key := range keys {
nodeBucket.Delete(key)
}
} else { // delete whole bucket
return bucket.DeleteBucket([]byte(nodeUuid))
}
}
return nil
})
}
// DeleteVersionsForNodes delete versions in a batch
func (b *BoltStore) DeleteVersionsForNodes(nodeUuid []string) error {
er := b.DB().Batch(func(tx *bolt.Tx) error {
bucket := tx.Bucket(bucketName)
for _, uuid := range nodeUuid {
bucket.DeleteBucket([]byte(uuid))
}
return nil
})
return er
}
// ListAllVersionedNodesUuids lists all nodes uuids
func (b *BoltStore) ListAllVersionedNodesUuids() (chan string, chan bool, chan error) {
idsChan := make(chan string)
done := make(chan bool, 1)
errChan := make(chan error)
go func() {
e := b.DB().View(func(tx *bolt.Tx) error {
defer func() {
done <- true
close(done)
}()
bucket := tx.Bucket(bucketName)
if bucket == nil {
return errors.NotFound(common.ServiceVersions, "version bucket not found")
}
c := bucket.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
key := string(k)
idsChan <- key
}
return nil
})
if e != nil {
errChan <- e
}
}()
return idsChan, done, errChan
}