-
Notifications
You must be signed in to change notification settings - Fork 38
/
blobovnicza.go
114 lines (90 loc) · 2.13 KB
/
blobovnicza.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
package blobovnicza
import (
"io/fs"
"os"
"time"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"go.etcd.io/bbolt"
"go.uber.org/atomic"
"go.uber.org/zap"
)
// Blobovnicza represents the implementation of NeoFS Blobovnicza.
type Blobovnicza struct {
cfg
filled atomic.Uint64
boltDB *bbolt.DB
}
// Option is an option of Blobovnicza's constructor.
type Option func(*cfg)
type cfg struct {
boltDBCfg
fullSizeLimit uint64
objSizeLimit uint64
log *logger.Logger
}
type boltDBCfg struct {
perm fs.FileMode
path string
boltOptions *bbolt.Options
}
func defaultCfg(с *cfg) {
*с = cfg{
boltDBCfg: boltDBCfg{
perm: os.ModePerm, // 0777
boltOptions: &bbolt.Options{
Timeout: 100 * time.Millisecond,
},
},
fullSizeLimit: 1 << 30, // 1GB
objSizeLimit: 1 << 20, // 1MB
log: &logger.Logger{Logger: zap.L()},
}
}
// New creates and returns a new Blobovnicza instance.
func New(opts ...Option) *Blobovnicza {
var b Blobovnicza
defaultCfg(&b.cfg)
for i := range opts {
opts[i](&b.cfg)
}
return &b
}
// WithPath returns option to set system path to Blobovnicza.
func WithPath(path string) Option {
return func(c *cfg) {
c.path = path
}
}
// WithPermissions returns an option to specify permission bits
// of Blobovnicza's system path.
func WithPermissions(perm fs.FileMode) Option {
return func(c *cfg) {
c.perm = perm
}
}
// WithObjectSizeLimit returns an option to specify the maximum size
// of the objects stored in Blobovnicza.
func WithObjectSizeLimit(lim uint64) Option {
return func(c *cfg) {
c.objSizeLimit = lim
}
}
// WithFullSizeLimit returns an option to set the maximum sum size
// of all stored objects.
func WithFullSizeLimit(lim uint64) Option {
return func(c *cfg) {
c.fullSizeLimit = lim
}
}
// WithLogger returns an option to specify Blobovnicza's logger.
func WithLogger(l *logger.Logger) Option {
return func(c *cfg) {
c.log = &logger.Logger{Logger: l.With(zap.String("component", "Blobovnicza"))}
}
}
// WithReadOnly returns an option to open Blobovnicza in read-only mode.
func WithReadOnly(ro bool) Option {
return func(c *cfg) {
c.boltOptions.ReadOnly = ro
}
}