本文整理汇总了Golang中github.com/decred/dcrwallet/walletdb.ReadWriteBucket.CreateBucketIfNotExists方法的典型用法代码示例。如果您正苦于以下问题:Golang ReadWriteBucket.CreateBucketIfNotExists方法的具体用法?Golang ReadWriteBucket.CreateBucketIfNotExists怎么用?Golang ReadWriteBucket.CreateBucketIfNotExists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/decred/dcrwallet/walletdb.ReadWriteBucket
的用法示例。
在下文中一共展示了ReadWriteBucket.CreateBucketIfNotExists方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: testBucketInterface
// testBucketInterface ensures the bucket interface is working properly by
// exercising all of its functions.
func testBucketInterface(tc *testContext, bucket walletdb.ReadWriteBucket) bool {
if bucket.Writable() != tc.isWritable {
tc.t.Errorf("Bucket writable state does not match.")
return false
}
if tc.isWritable {
// keyValues holds the keys and values to use when putting
// values into the bucket.
var keyValues = map[string]string{
"bucketkey1": "foo1",
"bucketkey2": "foo2",
"bucketkey3": "foo3",
}
if !testPutValues(tc, bucket, keyValues) {
return false
}
if !testGetValues(tc, bucket, keyValues) {
return false
}
// Iterate all of the keys using ForEach while making sure the
// stored values are the expected values.
keysFound := make(map[string]struct{}, len(keyValues))
err := bucket.ForEach(func(k, v []byte) error {
kString := string(k)
wantV, ok := keyValues[kString]
if !ok {
return fmt.Errorf("ForEach: key '%s' should "+
"exist", kString)
}
if !reflect.DeepEqual(v, []byte(wantV)) {
return fmt.Errorf("ForEach: value for key '%s' "+
"does not match - got %s, want %s",
kString, v, wantV)
}
keysFound[kString] = struct{}{}
return nil
})
if err != nil {
tc.t.Errorf("%v", err)
return false
}
// Ensure all keys were iterated.
for k := range keyValues {
if _, ok := keysFound[k]; !ok {
tc.t.Errorf("ForEach: key '%s' was not iterated "+
"when it should have been", k)
return false
}
}
// Delete the keys and ensure they were deleted.
if !testDeleteValues(tc, bucket, keyValues) {
return false
}
if !testGetValues(tc, bucket, rollbackValues(keyValues)) {
return false
}
// Ensure creating a new bucket works as expected.
testBucketName := []byte("testbucket")
testBucket, err := bucket.CreateBucket(testBucketName)
if err != nil {
tc.t.Errorf("CreateBucket: unexpected error: %v", err)
return false
}
if !testNestedBucket(tc, testBucket) {
return false
}
// Ensure creating a bucket that already exists fails with the
// expected error.
wantErr := walletdb.ErrBucketExists
if _, err := bucket.CreateBucket(testBucketName); err != wantErr {
tc.t.Errorf("CreateBucket: unexpected error - got %v, "+
"want %v", err, wantErr)
return false
}
// Ensure CreateBucketIfNotExists returns an existing bucket.
testBucket, err = bucket.CreateBucketIfNotExists(testBucketName)
if err != nil {
tc.t.Errorf("CreateBucketIfNotExists: unexpected "+
"error: %v", err)
return false
}
if !testNestedBucket(tc, testBucket) {
return false
}
// Ensure retrieving and existing bucket works as expected.
testBucket = bucket.ReadWriteBucket(testBucketName)
if !testNestedBucket(tc, testBucket) {
//.........这里部分代码省略.........
示例2: initializeEmpty
// initialize creates the DB if it doesn't exist, and otherwise
// loads the database.
func initializeEmpty(ns walletdb.ReadWriteBucket) error {
// Initialize the buckets and main db fields as needed.
mainBucket, err := ns.CreateBucketIfNotExists(mainBucketName)
if err != nil {
str := "failed to create main bucket"
return stakeStoreError(ErrDatabase, str, err)
}
_, err = ns.CreateBucketIfNotExists(sstxRecordsBucketName)
if err != nil {
str := "failed to create sstx records bucket"
return stakeStoreError(ErrDatabase, str, err)
}
_, err = ns.CreateBucketIfNotExists(ssgenRecordsBucketName)
if err != nil {
str := "failed to create ssgen records bucket"
return stakeStoreError(ErrDatabase, str, err)
}
_, err = ns.CreateBucketIfNotExists(ssrtxRecordsBucketName)
if err != nil {
str := "failed to create ssrtx records bucket"
return stakeStoreError(ErrDatabase, str, err)
}
_, err = ns.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.
var version uint32
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)
}
var createDate uint64
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)
}
if err != nil {
str := "failed to load database"
return stakeStoreError(ErrDatabase, str, err)
}
return nil
}