frostfs-node/pkg/local_object_storage/metabase/version.go
Evgenii Stratonikov 6f243a2a76 [] metabase: Store version
The main problem is to distinguish the case of initial initialization
and update from version 0. We can't do this at `Open`, because of
`resync_metabase` flag. Thus, the following approach was taken:
1. During `Open` check whether the metabase was initialized.
2. Check for the version in `Init` or write the new one if the metabase
   is new.
3. Update version in `Reset`.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2022-06-21 17:48:28 +03:00

41 lines
955 B
Go

package meta
import (
"encoding/binary"
"fmt"
"go.etcd.io/bbolt"
)
// version contains current metabase version.
const version = 0
var versionKey = []byte("version")
func checkVersion(tx *bbolt.Tx, initialized bool) error {
b := tx.Bucket(shardInfoBucket)
if b != nil {
data := b.Get(versionKey)
if len(data) == 8 {
stored := binary.LittleEndian.Uint64(data)
if stored != version {
return fmt.Errorf("invalid version: expected=%d, stored=%d", version, stored)
}
}
}
if !initialized { // new database, write version
return updateVersion(tx, version)
}
return nil // return error here after the first version increase
}
func updateVersion(tx *bbolt.Tx, version uint64) error {
data := make([]byte, 8)
binary.LittleEndian.PutUint64(data, version)
b, err := tx.CreateBucketIfNotExists(shardInfoBucket)
if err != nil {
return fmt.Errorf("can't create auxilliary bucket: %w", err)
}
return b.Put(versionKey, data)
}