forked from TrueCloudLab/frostfs-node
[#158] metabase: Construct DB using options
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
0cd05fdca5
commit
60e4b5ddff
3 changed files with 33 additions and 7 deletions
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue