本文整理汇总了Golang中github.com/boltdb/bolt.Tx.Bucket方法的典型用法代码示例。如果您正苦于以下问题:Golang Tx.Bucket方法的具体用法?Golang Tx.Bucket怎么用?Golang Tx.Bucket使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/boltdb/bolt.Tx
的用法示例。
在下文中一共展示了Tx.Bucket方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: applyFileContractMaintenance
// applyFileContractMaintenance looks for all of the file contracts that have
// expired without an appropriate storage proof, and calls 'applyMissedProof'
// for the file contract.
func applyFileContractMaintenance(tx *bolt.Tx, pb *processedBlock) {
// Get the bucket pointing to all of the expiring file contracts.
fceBucketID := append(prefixFCEX, encoding.Marshal(pb.Height)...)
fceBucket := tx.Bucket(fceBucketID)
// Finish if there are no expiring file contracts.
if fceBucket == nil {
return
}
var dscods []modules.DelayedSiacoinOutputDiff
var fcds []modules.FileContractDiff
err := fceBucket.ForEach(func(keyBytes, valBytes []byte) error {
var id types.FileContractID
copy(id[:], keyBytes)
amspDSCODS, fcd := applyMissedStorageProof(tx, pb, id)
fcds = append(fcds, fcd)
dscods = append(dscods, amspDSCODS...)
return nil
})
if build.DEBUG && err != nil {
panic(err)
}
for _, dscod := range dscods {
pb.DelayedSiacoinOutputDiffs = append(pb.DelayedSiacoinOutputDiffs, dscod)
commitDelayedSiacoinOutputDiff(tx, dscod, modules.DiffApply)
}
for _, fcd := range fcds {
pb.FileContractDiffs = append(pb.FileContractDiffs, fcd)
commitFileContractDiff(tx, fcd, modules.DiffApply)
}
err = tx.DeleteBucket(fceBucketID)
if build.DEBUG && err != nil {
panic(err)
}
}
示例2: simulateGetHandler
// Retrieves a key from the database and verifies that it is what is expected.
func simulateGetHandler(tx *bolt.Tx, qdb *QuickDB) {
// Randomly retrieve an existing exist.
keys := qdb.Rand()
if len(keys) == 0 {
return
}
// Retrieve root bucket.
b := tx.Bucket(keys[0])
if b == nil {
panic(fmt.Sprintf("bucket[0] expected: %08x\n", trunc(keys[0], 4)))
}
// Drill into nested buckets.
for _, key := range keys[1 : len(keys)-1] {
b = b.Bucket(key)
if b == nil {
panic(fmt.Sprintf("bucket[n] expected: %v -> %v\n", keys, key))
}
}
// Verify key/value on the final bucket.
expected := qdb.Get(keys)
actual := b.Get(keys[len(keys)-1])
if !bytes.Equal(actual, expected) {
fmt.Println("=== EXPECTED ===")
fmt.Println(expected)
fmt.Println("=== ACTUAL ===")
fmt.Println(actual)
fmt.Println("=== END ===")
panic("value mismatch")
}
}
示例3: Del
//Del deletes one key-value pair.
func Del(tx *bolt.Tx, bucket string, key []byte) error {
b := tx.Bucket([]byte(bucket))
if b == nil {
return errors.New("bucket not found " + bucket)
}
return b.Delete(key)
}
示例4: EntrySave
func EntrySave(tx *bolt.Tx, entry DbEntry, key string) error {
godbc.Require(tx != nil)
godbc.Require(len(key) > 0)
// Access bucket
b := tx.Bucket([]byte(entry.BucketName()))
if b == nil {
err := errors.New("Unable to access db")
logger.Err(err)
return err
}
// Save device entry to db
buffer, err := entry.Marshal()
if err != nil {
logger.Err(err)
return err
}
// Save data using the id as the key
err = b.Put([]byte(key), buffer)
if err != nil {
logger.Err(err)
return err
}
return nil
}
示例5: saveRepository
// saveRepository saves a repository in the store.
func (s *Store) saveRepository(tx *bolt.Tx, r *internal.Repository) error {
buf, err := proto.Marshal(r)
if err != nil {
return err
}
return tx.Bucket([]byte("repositories")).Put([]byte(r.GetID()), buf)
}
示例6: addBlockMap
// addBlockMap adds a processed block to the block map.
func addBlockMap(tx *bolt.Tx, pb *processedBlock) {
id := pb.Block.ID()
err := tx.Bucket(BlockMap).Put(id[:], encoding.Marshal(*pb))
if build.DEBUG && err != nil {
panic(err)
}
}
示例7: removeFileContract
// removeFileContract removes a file contract from the database.
func removeFileContract(tx *bolt.Tx, id types.FileContractID) {
// Delete the file contract entry.
fcBucket := tx.Bucket(FileContracts)
fcBytes := fcBucket.Get(id[:])
// Sanity check - should not be removing a file contract not in the db.
if build.DEBUG && fcBytes == nil {
panic("nil file contract")
}
err := fcBucket.Delete(id[:])
if build.DEBUG && err != nil {
panic(err)
}
// Delete the entry for the file contract's expiration. The portion of
// 'fcBytes' used to determine the expiration bucket id is the
// byte-representation of the file contract window end, which always
// appears at bytes 48-56.
expirationBucketID := append(prefixFCEX, fcBytes[48:56]...)
expirationBucket := tx.Bucket(expirationBucketID)
expirationBytes := expirationBucket.Get(id[:])
if expirationBytes == nil {
panic(errNilItem)
}
err = expirationBucket.Delete(id[:])
if build.DEBUG && err != nil {
panic(err)
}
}
示例8: validSiacoins
// validSiacoins checks that the siacoin inputs and outputs are valid in the
// context of the current consensus set.
func validSiacoins(tx *bolt.Tx, t types.Transaction) error {
scoBucket := tx.Bucket(SiacoinOutputs)
var inputSum types.Currency
for _, sci := range t.SiacoinInputs {
// Check that the input spends an existing output.
scoBytes := scoBucket.Get(sci.ParentID[:])
if scoBytes == nil {
return errMissingSiacoinOutput
}
// Check that the unlock conditions match the required unlock hash.
var sco types.SiacoinOutput
err := encoding.Unmarshal(scoBytes, &sco)
if build.DEBUG && err != nil {
panic(err)
}
if sci.UnlockConditions.UnlockHash() != sco.UnlockHash {
return errWrongUnlockConditions
}
inputSum = inputSum.Add(sco.Value)
}
if inputSum.Cmp(t.SiacoinOutputSum()) != 0 {
return errSiacoinInputOutputMismatch
}
return nil
}
示例9: addTerm
func (db *DB) addTerm(tx *bolt.Tx, term rdf.Term) (id uint32, err error) {
bt := db.encode(term)
if id, err = db.getIDb(tx, bt); err == nil {
// Term is allready in database
return id, nil
} else if err != ErrNotFound {
// Some other IO error occured
return 0, err
}
// get a new ID
bkt := tx.Bucket(bucketTerms)
n, err := bkt.NextSequence()
if err != nil {
return 0, err
}
if n > MaxTerms {
return 0, ErrDBFull
}
id = uint32(n)
idb := u32tob(uint32(n))
// store term and index it
err = bkt.Put(idb, bt)
if err != nil {
return 0, err
}
bkt = tx.Bucket(bucketIdxTerms)
err = bkt.Put(bt, idb)
return id, err
}
示例10: bucketOrPanic
func bucketOrPanic(tx *bolt.Tx, name []byte) *bolt.Bucket {
b := tx.Bucket(name)
if b == nil {
panic("Missing bucket")
}
return b
}
示例11: addSentence
// Add a single sentence to the database
func (ns *Nonsentence) addSentence(tx *bolt.Tx, sentence string) error {
wordsBucket := tx.Bucket([]byte("words"))
startsBucket := tx.Bucket([]byte("starts"))
if (wordsBucket == nil) || (startsBucket == nil) {
return fmt.Errorf("Buckets not found")
}
// Split sentence on whitespace
var words = strings.Fields(sentence)
if len(words) < 3 {
// log.Printf("Ignoring small sentence: %v", sentence)
return nil
}
// Store words in wordsBucket
for i := 2; i < len(words); i++ {
if err := storeWords(wordsBucket, words[i-2], words[i-1], words[i]); err != nil {
return err
}
}
if err := storeWords(wordsBucket, words[len(words)-2], words[len(words)-1], ""); err != nil {
return err
}
// Store starts in startsBucket
key := []byte(words[0] + " " + words[1])
if err := startsBucket.Put(key, []byte{}); err != nil {
return err
}
return nil
}
示例12: setSiafundPool
// setSiafundPool updates the saved siafund pool on disk
func setSiafundPool(tx *bolt.Tx, c types.Currency) {
bucket := tx.Bucket(SiafundPool)
err := bucket.Put(SiafundPool, encoding.Marshal(c))
if build.DEBUG && err != nil {
panic(err)
}
}
示例13: addSiacoinOutput
func addSiacoinOutput(tx *bolt.Tx, id types.SiacoinOutputID, sco types.SiacoinOutput) error {
siacoinOutputs := tx.Bucket(SiacoinOutputs)
if build.DEBUG && siacoinOutputs.Get(id[:]) != nil {
panic(errRepeatInsert)
}
return siacoinOutputs.Put(id[:], encoding.Marshal(sco))
}
示例14: forEach
// forEach iterates through a bucket, calling the supplied closure on each
// element.
func forEach(tx *bolt.Tx, bucket []byte, fn func(k, v []byte) error) error {
b := tx.Bucket(bucket)
if build.DEBUG && b == nil {
panic(errNilBucket)
}
return b.ForEach(fn)
}
示例15: UpdateValueKeyBy
func (qs *QuadStore) UpdateValueKeyBy(name string, amount int64, tx *bolt.Tx) error {
value := ValueData{name, amount}
b := tx.Bucket(nodeBucket)
b.FillPercent = localFillPercent
key := qs.createValueKeyFor(name)
data := b.Get(key)
if data != nil {
// Node exists in the database -- unmarshal and update.
err := json.Unmarshal(data, &value)
if err != nil {
glog.Errorf("Error: couldn't reconstruct value: %v", err)
return err
}
value.Size += amount
}
// Are we deleting something?
if value.Size <= 0 {
value.Size = 0
}
// Repackage and rewrite.
bytes, err := json.Marshal(&value)
if err != nil {
glog.Errorf("Couldn't write to buffer for value %s: %s", name, err)
return err
}
err = b.Put(key, bytes)
return err
}