2020-11-26 08:17:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
2020-11-26 14:26:21 +00:00
|
|
|
import (
|
|
|
|
"os"
|
2021-01-18 13:03:00 +00:00
|
|
|
"time"
|
2020-11-26 14:26:21 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2020-11-26 08:17:53 +00:00
|
|
|
// Blobovnicza represents the implementation of NeoFS Blobovnicza.
|
|
|
|
type Blobovnicza struct {
|
|
|
|
*cfg
|
2020-11-26 14:26:21 +00:00
|
|
|
|
|
|
|
filled *atomic.Uint64
|
|
|
|
|
|
|
|
boltDB *bbolt.DB
|
2020-11-26 08:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option is an option of Blobovnicza's constructor.
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
type cfg struct {
|
2020-11-26 14:26:21 +00:00
|
|
|
boltDBCfg
|
|
|
|
|
|
|
|
fullSizeLimit uint64
|
|
|
|
|
|
|
|
objSizeLimit uint64
|
|
|
|
|
|
|
|
log *logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type boltDBCfg struct {
|
|
|
|
perm os.FileMode
|
|
|
|
|
|
|
|
path string
|
|
|
|
|
|
|
|
boltOptions *bbolt.Options
|
2020-11-26 08:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
2020-11-26 14:26:21 +00:00
|
|
|
return &cfg{
|
|
|
|
boltDBCfg: boltDBCfg{
|
|
|
|
perm: os.ModePerm, // 0777
|
2021-01-18 13:03:00 +00:00
|
|
|
boltOptions: &bbolt.Options{
|
|
|
|
Timeout: 100 * time.Millisecond,
|
|
|
|
},
|
2020-11-26 14:26:21 +00:00
|
|
|
},
|
|
|
|
fullSizeLimit: 1 << 30, // 1GB
|
|
|
|
objSizeLimit: 1 << 20, // 1MB
|
|
|
|
log: zap.L(),
|
|
|
|
}
|
2020-11-26 08:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates and returns new Blobovnicza instance.
|
|
|
|
func New(opts ...Option) *Blobovnicza {
|
|
|
|
c := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Blobovnicza{
|
2020-11-26 14:26:21 +00:00
|
|
|
cfg: c,
|
|
|
|
filled: atomic.NewUint64(0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPath returns option to set system path to Blobovnicza.
|
|
|
|
func WithPath(path string) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.path = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPermissions returns option to specify permission bits
|
|
|
|
// of Blobovnicza's system path.
|
|
|
|
func WithPermissions(perm os.FileMode) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.perm = perm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSizeLimit returns option to specify maximum size
|
|
|
|
// of the objects stored in Blobovnicza.
|
|
|
|
func WithObjectSizeLimit(lim uint64) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.objSizeLimit = lim
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFullSizeLimit returns option to set maximum sum size
|
|
|
|
// of all stored objects.
|
|
|
|
func WithFullSizeLimit(lim uint64) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.fullSizeLimit = lim
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger returns option to specify Blobovnicza's logger.
|
|
|
|
func WithLogger(l *logger.Logger) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.log = l.With(zap.String("component", "Blobovnicza"))
|
|
|
|
}
|
|
|
|
}
|