本文整理汇总了Golang中github.com/btcsuite/btcwallet/walletdb.Namespace类的典型用法代码示例。如果您正苦于以下问题:Golang Namespace类的具体用法?Golang Namespace怎么用?Golang Namespace使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Namespace类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: scopedUpdate
func scopedUpdate(ns walletdb.Namespace, f func(walletdb.Bucket) error) error {
tx, err := ns.Begin(true)
if err != nil {
str := "cannot begin update"
return storeError(ErrDatabase, str, err)
}
err = f(tx.RootBucket())
if err != nil {
rbErr := tx.Rollback()
if rbErr != nil {
const desc = "rollback failed"
serr, ok := err.(Error)
if !ok {
// This really shouldn't happen.
return storeError(ErrDatabase, desc, rbErr)
}
serr.Desc = desc + ": " + serr.Desc
return serr
}
return err
}
err = tx.Commit()
if err != nil {
str := "commit failed"
return storeError(ErrDatabase, str, err)
}
return nil
}
示例2: 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
}
示例3: scopedView
func scopedView(ns walletdb.Namespace, f func(walletdb.Bucket) error) error {
tx, err := ns.Begin(false)
if err != nil {
str := "cannot begin view"
return storeError(ErrDatabase, str, err)
}
err = f(tx.RootBucket())
rbErr := tx.Rollback()
if err != nil {
return err
}
if rbErr != nil {
str := "cannot close view"
return storeError(ErrDatabase, str, rbErr)
}
return nil
}
示例4: Load
// Load fetches the entry in the database with the given ID and returns the Pool
// representing it.
func Load(namespace walletdb.Namespace, m *waddrmgr.Manager, poolID []byte) (*Pool, error) {
err := namespace.View(
func(tx walletdb.Tx) error {
if exists := existsPool(tx, poolID); !exists {
str := fmt.Sprintf("unable to find voting pool %v in db", poolID)
return newError(ErrPoolNotExists, str, nil)
}
return nil
})
if err != nil {
return nil, err
}
p := newPool(namespace, m, poolID)
if err = p.LoadAllSeries(); err != nil {
return nil, err
}
return p, nil
}
示例5: testManualTxInterface
// testManualTxInterface ensures that manual transactions work as expected.
func testManualTxInterface(tc *testContext, namespace walletdb.Namespace) bool {
// populateValues tests that populating values works as expected.
//
// When the writable flag is false, a read-only tranasction is created,
// standard bucket tests for read-only transactions are performed, and
// the Commit function is checked to ensure it fails as expected.
//
// Otherwise, a read-write transaction is created, the values are
// written, standard bucket tests for read-write transactions are
// performed, and then the transaction is either commited or rolled
// back depending on the flag.
populateValues := func(writable, rollback bool, putValues map[string]string) bool {
tx, err := namespace.Begin(writable)
if err != nil {
tc.t.Errorf("Begin: unexpected error %v", err)
return false
}
rootBucket := tx.RootBucket()
if rootBucket == nil {
tc.t.Errorf("RootBucket: unexpected nil root bucket")
_ = tx.Rollback()
return false
}
tc.isWritable = writable
if !testBucketInterface(tc, rootBucket) {
_ = tx.Rollback()
return false
}
if !writable {
// The transaction is not writable, so it should fail
// the commit.
if err := tx.Commit(); err != walletdb.ErrTxNotWritable {
tc.t.Errorf("Commit: unexpected error %v, "+
"want %v", err, walletdb.ErrTxNotWritable)
_ = tx.Rollback()
return false
}
// Rollback the transaction.
if err := tx.Rollback(); err != nil {
tc.t.Errorf("Commit: unexpected error %v", err)
return false
}
} else {
if !testPutValues(tc, rootBucket, putValues) {
return false
}
if rollback {
// Rollback the transaction.
if err := tx.Rollback(); err != nil {
tc.t.Errorf("Rollback: unexpected "+
"error %v", err)
return false
}
} else {
// The commit should succeed.
if err := tx.Commit(); err != nil {
tc.t.Errorf("Commit: unexpected error "+
"%v", err)
return false
}
}
}
return true
}
// checkValues starts a read-only transaction and checks that all of
// the key/value pairs specified in the expectedValues parameter match
// what's in the database.
checkValues := func(expectedValues map[string]string) bool {
// Begin another read-only transaction to ensure...
tx, err := namespace.Begin(false)
if err != nil {
tc.t.Errorf("Begin: unexpected error %v", err)
return false
}
rootBucket := tx.RootBucket()
if rootBucket == nil {
tc.t.Errorf("RootBucket: unexpected nil root bucket")
_ = tx.Rollback()
return false
}
if !testGetValues(tc, rootBucket, expectedValues) {
_ = tx.Rollback()
return false
}
// Rollback the read-only transaction.
if err := tx.Rollback(); err != nil {
tc.t.Errorf("Commit: unexpected error %v", err)
return false
}
//.........这里部分代码省略.........