本文整理匯總了Golang中github.com/decred/dcrwallet/walletdb.Namespace.Update方法的典型用法代碼示例。如果您正苦於以下問題:Golang Namespace.Update方法的具體用法?Golang Namespace.Update怎麽用?Golang Namespace.Update使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/decred/dcrwallet/walletdb.Namespace
的用法示例。
在下文中一共展示了Namespace.Update方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Create
// Create creates a new entry in the database with the given ID
// and returns the Pool representing it.
func Create(namespace walletdb.Namespace, m *waddrmgr.Manager, poolID []byte) (*Pool, error) {
err := namespace.Update(
func(tx walletdb.Tx) error {
return putPool(tx, poolID)
})
if err != nil {
str := fmt.Sprintf("unable to add voting pool %v to db", poolID)
return nil, newError(ErrPoolAlreadyExists, str, err)
}
return newPool(namespace, m, poolID), nil
}
示例2: initializeEmpty
// initialize creates the DB if it doesn't exist, and otherwise
// loads the database.
func initializeEmpty(namespace walletdb.Namespace) error {
// Initialize the buckets and main db fields as needed.
var version uint32
var createDate uint64
err := namespace.Update(func(tx walletdb.Tx) error {
rootBucket := tx.RootBucket()
mainBucket, err := rootBucket.CreateBucketIfNotExists(
mainBucketName)
if err != nil {
str := "failed to create main bucket"
return stakeStoreError(ErrDatabase, str, err)
}
_, err = rootBucket.CreateBucketIfNotExists(sstxRecordsBucketName)
if err != nil {
str := "failed to create sstx records bucket"
return stakeStoreError(ErrDatabase, str, err)
}
_, err = rootBucket.CreateBucketIfNotExists(ssgenRecordsBucketName)
if err != nil {
str := "failed to create ssgen records bucket"
return stakeStoreError(ErrDatabase, str, err)
}
_, err = rootBucket.CreateBucketIfNotExists(ssrtxRecordsBucketName)
if err != nil {
str := "failed to create ssrtx records bucket"
return stakeStoreError(ErrDatabase, str, err)
}
_, err = rootBucket.CreateBucketIfNotExists(metaBucketName)
if err != nil {
str := "failed to create meta bucket"
return stakeStoreError(ErrDatabase, str, err)
}
// Save the most recent tx store version if it isn't already
// there, otherwise keep track of it for potential upgrades.
verBytes := mainBucket.Get(stakeStoreVersionName)
if verBytes == nil {
version = LatestStakeMgrVersion
var buf [4]byte
byteOrder.PutUint32(buf[:], version)
err := mainBucket.Put(stakeStoreVersionName, buf[:])
if err != nil {
str := "failed to store latest database version"
return stakeStoreError(ErrDatabase, str, err)
}
} else {
version = byteOrder.Uint32(verBytes)
}
createBytes := mainBucket.Get(stakeStoreCreateDateName)
if createBytes == nil {
createDate = uint64(time.Now().Unix())
var buf [8]byte
byteOrder.PutUint64(buf[:], createDate)
err := mainBucket.Put(stakeStoreCreateDateName, buf[:])
if err != nil {
str := "failed to store database creation time"
return stakeStoreError(ErrDatabase, str, err)
}
} else {
createDate = byteOrder.Uint64(createBytes)
}
return nil
})
if err != nil {
str := "failed to load database"
return stakeStoreError(ErrDatabase, str, err)
}
return nil
}