From 60e4b5ddffc5dba863d877e612be018ad0379e71 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 9 Nov 2020 13:14:52 +0300 Subject: [PATCH] [#158] metabase: Construct DB using options Signed-off-by: Leonard Lyubich --- cmd/neofs-node/config.go | 4 ++- pkg/local_object_storage/metabase/db.go | 32 +++++++++++++++++--- pkg/local_object_storage/metabase/db_test.go | 4 +-- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 79c560d80..fc9554661 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -368,7 +368,9 @@ func initLocalStorage(c *cfg) { ) fatalOnErr(err) - c.cfgObject.metastorage = meta.NewDB(boltDB) + c.cfgObject.metastorage = meta.NewDB( + meta.FromBoltDB(boltDB), + ) } func initBucket(prefix string, c *cfg) (bucket bucket.Bucket, err error) { diff --git a/pkg/local_object_storage/metabase/db.go b/pkg/local_object_storage/metabase/db.go index 90e624c43..56780a595 100644 --- a/pkg/local_object_storage/metabase/db.go +++ b/pkg/local_object_storage/metabase/db.go @@ -10,16 +10,33 @@ import ( type DB struct { path string - boltDB *bbolt.DB + *cfg matchers map[object.SearchMatchType]func(string, string, string) bool } +// Option is an option of DB constructor. +type Option func(*cfg) + +type cfg struct { + boltDB *bbolt.DB +} + +func defaultCfg() *cfg { + return &cfg{} +} + // NewDB creates, initializes and returns DB instance. -func NewDB(boltDB *bbolt.DB) *DB { +func NewDB(opts ...Option) *DB { + c := defaultCfg() + + for i := range opts { + opts[i](c) + } + return &DB{ - path: boltDB.Path(), - boltDB: boltDB, + path: c.boltDB.Path(), + cfg: c, matchers: map[object.SearchMatchType]func(string, string, string) bool{ object.MatchStringEqual: stringEqualMatcher, }, @@ -46,3 +63,10 @@ func stringEqualMatcher(key, objVal, filterVal string) bool { return (filterVal == v2object.BooleanPropertyValueTrue) == (objVal == v2object.BooleanPropertyValueTrue) } } + +// FromBoltDB returns option to construct DB from BoltDB instance. +func FromBoltDB(db *bbolt.DB) Option { + return func(c *cfg) { + c.boltDB = db + } +} diff --git a/pkg/local_object_storage/metabase/db_test.go b/pkg/local_object_storage/metabase/db_test.go index a4206263b..f34a8c20d 100644 --- a/pkg/local_object_storage/metabase/db_test.go +++ b/pkg/local_object_storage/metabase/db_test.go @@ -220,7 +220,7 @@ func TestDB_Path(t *testing.T) { bdb, err := bbolt.Open(path, 0600, nil) require.NoError(t, err) - db := NewDB(bdb) + db := NewDB(FromBoltDB(bdb)) defer releaseDB(db) @@ -233,7 +233,7 @@ func newDB(t testing.TB) *DB { bdb, err := bbolt.Open(path, 0600, nil) require.NoError(t, err) - return NewDB(bdb) + return NewDB(FromBoltDB(bdb)) } func releaseDB(db *DB) {