wtxmgr/db: define new helpers to set/get store version

This commit is contained in:
Wilmer Paulino 2018-11-02 18:42:58 -07:00
parent 1e582298b2
commit 3bdfb6cc23
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F

View file

@ -1264,12 +1264,10 @@ func deleteRawUnminedInput(ns walletdb.ReadWriteBucket, k []byte) error {
// openStore opens an existing transaction store from the passed namespace.
func openStore(ns walletdb.ReadBucket) error {
v := ns.Get(rootVersion)
if len(v) != 4 {
str := "no transaction store exists in namespace"
return storeError(ErrNoExists, str, nil)
version, err := fetchVersion(ns)
if err != nil {
return err
}
version := byteOrder.Uint32(v)
if version < LatestVersion {
str := fmt.Sprintf("a database upgrade is required to upgrade "+
@ -1279,24 +1277,11 @@ func openStore(ns walletdb.ReadBucket) error {
}
if version > LatestVersion {
str := fmt.Sprintf("version recorded version %d is newer that latest "+
"understood version %d", version, LatestVersion)
str := fmt.Sprintf("version recorded version %d is newer that "+
"latest understood version %d", version, latestVersion)
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
}
@ -1311,26 +1296,22 @@ func createStore(ns walletdb.ReadWriteBucket) error {
}
// Write the latest store version.
v := make([]byte, 4)
byteOrder.PutUint32(v, LatestVersion)
err := ns.Put(rootVersion, v)
if err != nil {
str := "failed to store latest database version"
return storeError(ErrDatabase, str, err)
if err := putVersion(ns, getLatestVersion()); err != nil {
return err
}
// Save the creation date of the store.
v = make([]byte, 8)
byteOrder.PutUint64(v, uint64(time.Now().Unix()))
err = ns.Put(rootCreateDate, v)
var v [8]byte
byteOrder.PutUint64(v[:], uint64(time.Now().Unix()))
err := ns.Put(rootCreateDate, v[:])
if err != nil {
str := "failed to store database creation time"
return storeError(ErrDatabase, str, err)
}
// Write a zero balance.
v = make([]byte, 8)
err = ns.Put(rootMinedBalance, v)
byteOrder.PutUint64(v[:], 0)
err = ns.Put(rootMinedBalance, v[:])
if err != nil {
str := "failed to write zero balance"
return storeError(ErrDatabase, str, err)
@ -1387,6 +1368,30 @@ func createStore(ns walletdb.ReadWriteBucket) error {
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 {
tx, err := db.BeginReadWriteTx()
if err != nil {