2020-10-28 14:49:30 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2020-10-29 16:12:38 +00:00
|
|
|
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2020-11-09 10:22:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2020-10-28 14:49:30 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2020-11-09 10:22:36 +00:00
|
|
|
"go.uber.org/zap"
|
2020-10-28 14:49:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DB represents local metabase of storage node.
|
|
|
|
type DB struct {
|
2020-11-02 07:37:16 +00:00
|
|
|
path string
|
|
|
|
|
2020-11-09 10:14:52 +00:00
|
|
|
*cfg
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-10-29 16:12:38 +00:00
|
|
|
matchers map[object.SearchMatchType]func(string, string, string) bool
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 10:14:52 +00:00
|
|
|
// Option is an option of DB constructor.
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
type cfg struct {
|
|
|
|
boltDB *bbolt.DB
|
2020-11-09 10:22:36 +00:00
|
|
|
|
|
|
|
log *logger.Logger
|
2020-11-09 10:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
2020-11-09 10:22:36 +00:00
|
|
|
return &cfg{
|
|
|
|
log: zap.L(),
|
|
|
|
}
|
2020-11-09 10:14:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
// NewDB creates, initializes and returns DB instance.
|
2020-11-09 10:14:52 +00:00
|
|
|
func NewDB(opts ...Option) *DB {
|
|
|
|
c := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](c)
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
return &DB{
|
2020-11-09 10:14:52 +00:00
|
|
|
path: c.boltDB.Path(),
|
|
|
|
cfg: c,
|
2020-10-29 16:12:38 +00:00
|
|
|
matchers: map[object.SearchMatchType]func(string, string, string) bool{
|
|
|
|
object.MatchStringEqual: stringEqualMatcher,
|
2020-10-28 14:49:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 16:12:38 +00:00
|
|
|
|
2020-10-29 17:16:49 +00:00
|
|
|
func (db *DB) Close() error {
|
|
|
|
return db.boltDB.Close()
|
|
|
|
}
|
|
|
|
|
2020-11-02 07:37:16 +00:00
|
|
|
// Path returns the path to meta database.
|
|
|
|
func (db *DB) Path() string {
|
|
|
|
return db.path
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:12:38 +00:00
|
|
|
func stringEqualMatcher(key, objVal, filterVal string) bool {
|
|
|
|
switch key {
|
|
|
|
default:
|
|
|
|
return objVal == filterVal
|
|
|
|
case
|
|
|
|
v2object.FilterPropertyRoot,
|
|
|
|
v2object.FilterPropertyChildfree,
|
|
|
|
v2object.FilterPropertyLeaf:
|
|
|
|
return (filterVal == v2object.BooleanPropertyValueTrue) == (objVal == v2object.BooleanPropertyValueTrue)
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 10:14:52 +00:00
|
|
|
|
|
|
|
// FromBoltDB returns option to construct DB from BoltDB instance.
|
|
|
|
func FromBoltDB(db *bbolt.DB) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.boltDB = db
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 10:22:36 +00:00
|
|
|
|
|
|
|
// WithLogger returns option to set logger of DB.
|
|
|
|
func WithLogger(l *logger.Logger) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.log = l
|
|
|
|
}
|
|
|
|
}
|