2022-03-01 08:59:05 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-09-08 11:54:21 +00:00
|
|
|
shardInfoBucket = []byte{shardInfoPrefix}
|
2022-03-01 08:59:05 +00:00
|
|
|
shardIDKey = []byte("id")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadShardID reads shard id from db.
|
|
|
|
// If id is missing, returns nil, nil.
|
|
|
|
func (db *DB) ReadShardID() ([]byte, error) {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return nil, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2022-03-01 08:59:05 +00:00
|
|
|
var id []byte
|
|
|
|
err := db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(shardInfoBucket)
|
|
|
|
if b != nil {
|
|
|
|
id = slice.Copy(b.Get(shardIDKey))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteShardID writes shard it to db.
|
|
|
|
func (db *DB) WriteShardID(id []byte) error {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
2022-11-15 14:53:23 +00:00
|
|
|
} else if db.mode.ReadOnly() {
|
|
|
|
return ErrReadOnlyMode
|
2022-11-15 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 08:59:05 +00:00
|
|
|
return db.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
b, err := tx.CreateBucketIfNotExists(shardInfoBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.Put(shardIDKey, id)
|
|
|
|
})
|
|
|
|
}
|