wtxmgr/db: define new helpers to set/get store version
This commit is contained in:
parent
1e582298b2
commit
3bdfb6cc23
1 changed files with 36 additions and 31 deletions
67
wtxmgr/db.go
67
wtxmgr/db.go
|
@ -1264,12 +1264,10 @@ func deleteRawUnminedInput(ns walletdb.ReadWriteBucket, k []byte) error {
|
||||||
|
|
||||||
// openStore opens an existing transaction store from the passed namespace.
|
// openStore opens an existing transaction store from the passed namespace.
|
||||||
func openStore(ns walletdb.ReadBucket) error {
|
func openStore(ns walletdb.ReadBucket) error {
|
||||||
v := ns.Get(rootVersion)
|
version, err := fetchVersion(ns)
|
||||||
if len(v) != 4 {
|
if err != nil {
|
||||||
str := "no transaction store exists in namespace"
|
return err
|
||||||
return storeError(ErrNoExists, str, nil)
|
|
||||||
}
|
}
|
||||||
version := byteOrder.Uint32(v)
|
|
||||||
|
|
||||||
if version < LatestVersion {
|
if version < LatestVersion {
|
||||||
str := fmt.Sprintf("a database upgrade is required to upgrade "+
|
str := fmt.Sprintf("a database upgrade is required to upgrade "+
|
||||||
|
@ -1279,24 +1277,11 @@ func openStore(ns walletdb.ReadBucket) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if version > LatestVersion {
|
if version > LatestVersion {
|
||||||
str := fmt.Sprintf("version recorded version %d is newer that latest "+
|
str := fmt.Sprintf("version recorded version %d is newer that "+
|
||||||
"understood version %d", version, LatestVersion)
|
"latest understood version %d", version, latestVersion)
|
||||||
return storeError(ErrUnknownVersion, str, nil)
|
return storeError(ErrUnknownVersion, str, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upgrade the tx store as needed, one version at a time, until
|
|
||||||
// LatestVersion is reached. Versions are not skipped when performing
|
|
||||||
// database upgrades, and each upgrade is done in its own transaction.
|
|
||||||
//
|
|
||||||
// No upgrades yet.
|
|
||||||
//if version < LatestVersion {
|
|
||||||
// err := scopedUpdate(namespace, func(ns walletdb.Bucket) error {
|
|
||||||
// })
|
|
||||||
// if err != nil {
|
|
||||||
// // Handle err
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1311,26 +1296,22 @@ func createStore(ns walletdb.ReadWriteBucket) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the latest store version.
|
// Write the latest store version.
|
||||||
v := make([]byte, 4)
|
if err := putVersion(ns, getLatestVersion()); err != nil {
|
||||||
byteOrder.PutUint32(v, LatestVersion)
|
return err
|
||||||
err := ns.Put(rootVersion, v)
|
|
||||||
if err != nil {
|
|
||||||
str := "failed to store latest database version"
|
|
||||||
return storeError(ErrDatabase, str, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the creation date of the store.
|
// Save the creation date of the store.
|
||||||
v = make([]byte, 8)
|
var v [8]byte
|
||||||
byteOrder.PutUint64(v, uint64(time.Now().Unix()))
|
byteOrder.PutUint64(v[:], uint64(time.Now().Unix()))
|
||||||
err = ns.Put(rootCreateDate, v)
|
err := ns.Put(rootCreateDate, v[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
str := "failed to store database creation time"
|
str := "failed to store database creation time"
|
||||||
return storeError(ErrDatabase, str, err)
|
return storeError(ErrDatabase, str, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write a zero balance.
|
// Write a zero balance.
|
||||||
v = make([]byte, 8)
|
byteOrder.PutUint64(v[:], 0)
|
||||||
err = ns.Put(rootMinedBalance, v)
|
err = ns.Put(rootMinedBalance, v[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
str := "failed to write zero balance"
|
str := "failed to write zero balance"
|
||||||
return storeError(ErrDatabase, str, err)
|
return storeError(ErrDatabase, str, err)
|
||||||
|
@ -1387,6 +1368,30 @@ func createStore(ns walletdb.ReadWriteBucket) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// putVersion modifies the version of the store to reflect the given version
|
||||||
|
// number.
|
||||||
|
func putVersion(ns walletdb.ReadWriteBucket, version uint32) error {
|
||||||
|
var v [4]byte
|
||||||
|
byteOrder.PutUint32(v[:], version)
|
||||||
|
if err := ns.Put(rootVersion, v[:]); err != nil {
|
||||||
|
str := "failed to store database version"
|
||||||
|
return storeError(ErrDatabase, str, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetchVersion fetches the current version of the store.
|
||||||
|
func fetchVersion(ns walletdb.ReadBucket) (uint32, error) {
|
||||||
|
v := ns.Get(rootVersion)
|
||||||
|
if len(v) != 4 {
|
||||||
|
str := "no transaction store exists in namespace"
|
||||||
|
return 0, storeError(ErrNoExists, str, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
return byteOrder.Uint32(v), nil
|
||||||
|
}
|
||||||
|
|
||||||
func scopedUpdate(db walletdb.DB, namespaceKey []byte, f func(walletdb.ReadWriteBucket) error) error {
|
func scopedUpdate(db walletdb.DB, namespaceKey []byte, f func(walletdb.ReadWriteBucket) error) error {
|
||||||
tx, err := db.BeginReadWriteTx()
|
tx, err := db.BeginReadWriteTx()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue