2022-07-05 04:55:46 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
2022-07-05 04:55:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetMode sets the metabase mode of operation.
|
|
|
|
// If the mode assumes no operation metabase, the database is closed.
|
|
|
|
func (db *DB) SetMode(m mode.Mode) error {
|
|
|
|
db.modeMtx.Lock()
|
|
|
|
defer db.modeMtx.Unlock()
|
|
|
|
|
|
|
|
if db.mode == m {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !db.mode.NoMetabase() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
return fmt.Errorf("can't set metabase mode (old=%s, new=%s): %w", db.mode, m, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2022-07-18 11:44:29 +00:00
|
|
|
switch {
|
|
|
|
case m.NoMetabase():
|
2022-07-05 04:55:46 +00:00
|
|
|
db.boltDB = nil
|
2022-07-18 11:44:29 +00:00
|
|
|
case m.ReadOnly():
|
2022-07-05 04:55:46 +00:00
|
|
|
err = db.Open(true)
|
2022-07-18 11:44:29 +00:00
|
|
|
default:
|
2022-07-05 04:55:46 +00:00
|
|
|
err = db.Open(false)
|
|
|
|
}
|
|
|
|
if err == nil && !m.NoMetabase() && !m.ReadOnly() {
|
|
|
|
err = db.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't set metabase mode (old=%s, new=%s): %w", db.mode, m, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.mode = m
|
|
|
|
return nil
|
|
|
|
}
|