-
Notifications
You must be signed in to change notification settings - Fork 38
/
control.go
94 lines (76 loc) · 2.08 KB
/
control.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
package blobovnicza
import (
"fmt"
"os"
"path/filepath"
"github.com/nspcc-dev/neofs-node/pkg/util"
"go.etcd.io/bbolt"
"go.uber.org/zap"
)
// Open opens an internal database at the configured path with the configured permissions.
//
// If the database file does not exist, it will be created automatically.
func (b *Blobovnicza) Open() error {
b.log.Debug("creating directory for BoltDB",
zap.String("path", b.path),
zap.Bool("ro", b.boltOptions.ReadOnly),
)
var err error
if !b.boltOptions.ReadOnly {
err = util.MkdirAllX(filepath.Dir(b.path), b.perm)
if err != nil {
return err
}
}
b.log.Debug("opening BoltDB",
zap.String("path", b.path),
zap.Stringer("permissions", b.perm),
)
b.boltDB, err = bbolt.Open(b.path, b.perm, b.boltOptions)
return err
}
// Init initializes internal database structure.
//
// If Blobovnicza is already initialized, no action is taken.
//
// Should not be called in read-only configuration.
func (b *Blobovnicza) Init() error {
b.log.Debug("initializing...",
zap.Uint64("object size limit", b.objSizeLimit),
zap.Uint64("storage size limit", b.fullSizeLimit),
)
if size := b.filled.Load(); size != 0 {
b.log.Debug("already initialized", zap.Uint64("size", size))
return nil
}
err := b.boltDB.Update(func(tx *bbolt.Tx) error {
return b.iterateBucketKeys(func(lower, upper uint64, key []byte) (bool, error) {
// create size range bucket
rangeStr := stringifyBounds(lower, upper)
b.log.Debug("creating bucket for size range",
zap.String("range", rangeStr))
_, err := tx.CreateBucketIfNotExists(key)
if err != nil {
return false, fmt.Errorf("(%T) could not create bucket for bounds %s: %w",
b, rangeStr, err)
}
return false, nil
})
})
if err != nil {
return err
}
info, err := os.Stat(b.path)
if err != nil {
return fmt.Errorf("can't determine DB size: %w", err)
}
b.filled.Store(uint64(info.Size()))
return err
}
// Close releases all internal database resources.
func (b *Blobovnicza) Close() error {
b.log.Debug("closing BoltDB",
zap.String("path", b.path),
)
return b.boltDB.Close()
}