本文整理汇总了Golang中github.com/boltdb/bolt.Tx类的典型用法代码示例。如果您正苦于以下问题:Golang Tx类的具体用法?Golang Tx怎么用?Golang Tx使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Tx类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例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: 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)
}
}
示例7: persistProvider
func (m *Manager) persistProvider(tx *bolt.Tx, id string) error {
// Note: This method does *not* include re-serializing per-volume state,
// because we assume that hasn't changed unless the change request
// for the volume came through us and was handled elsewhere already.
provider, ok := m.providers[id]
if !ok {
return tx.Bucket([]byte("providers")).DeleteBucket([]byte(id))
}
providersBucket, err := m.getProviderBucket(tx, id)
if err != nil {
return fmt.Errorf("could not persist provider info to boltdb: %s", err)
}
pspec := &volume.ProviderSpec{}
pspec.Kind = provider.Kind()
b, err := provider.MarshalGlobalState()
if err != nil {
return fmt.Errorf("failed to serialize provider info: %s", err)
}
pspec.Config = b
b, err = json.Marshal(pspec)
if err != nil {
return fmt.Errorf("failed to serialize provider info: %s", err)
}
err = providersBucket.Put([]byte("global"), b)
if err != nil {
return fmt.Errorf("could not persist provider info to boltdb: %s", err)
}
return nil
}
示例8: getReview
func getReview(tx *bolt.Tx, id int) (review.R, error) {
var (
revisions []review.Revision
review review.R
)
metaData, err := getMetaData(tx)
if err != nil {
return review, err
}
root := tx.Bucket(rootKey)
if root == nil {
return review, ErrNoDB
}
reviews := root.Bucket(reviewsKey)
if reviews == nil {
return review, ErrNoDB
}
reviewBkt, err := getReviewBucket(tx, id)
if err != nil {
return review, err
}
review.Summary = metaData.Summaries[strconv.Itoa(id)]
rev := reviewBkt.Get(revisionsKey)
dec := gob.NewDecoder(bytes.NewReader(rev))
if err := dec.Decode(&revisions); err != nil {
return review, err
}
review.Revisions = revisions
return review, nil
}
示例9: SetCounterparty
func SetCounterparty(tx *bolt.Tx, cpt *core.Counterparty) error {
b, err := json.Marshal(cpt)
if err != nil {
return err
}
err = tx.Bucket([]byte("Counterparties")).Put([]byte(cpt.Pubkey), b)
if err != nil {
return err
}
// Relations
b, err = json.Marshal(cpt.Judge)
if err != nil {
return err
}
err = tx.Bucket([]byte("Judges")).Put(cpt.Judge.Pubkey, b)
if err != nil {
return err
}
return nil
}
示例10: removeTriple
// removeTriple removes a triple from the indices. If the triple
// contains any terms unique to that triple, they will also be removed.
func (db *DB) removeTriple(tx *bolt.Tx, s, p, o uint32) error {
// TODO think about what to do if present in one index but
// not in another: maybe panic? Cause It's a bug that should be fixed.
indices := []struct {
k1 uint32
k2 uint32
v uint32
bk []byte
}{
{s, p, o, bucketSPO},
{o, s, p, bucketOSP},
{p, o, s, bucketPOS},
}
key := make([]byte, 8)
for _, i := range indices {
bkt := tx.Bucket(i.bk)
copy(key, u32tob(i.k1))
copy(key[4:], u32tob(i.k2))
bo := bkt.Get(key)
if bo == nil {
// TODO should never happen, return bug error?
return ErrNotFound
}
bitmap := roaring.NewBitmap()
_, err := bitmap.ReadFrom(bytes.NewReader(bo))
if err != nil {
return err
}
hasTriple := bitmap.CheckedRemove(i.v)
if !hasTriple {
// TODO should never happen, return bug error?
return ErrNotFound
}
// Remove from index if bitmap is empty
if bitmap.GetCardinality() == 0 {
err = bkt.Delete(key)
if err != nil {
return err
}
} else {
var b bytes.Buffer
_, err = bitmap.WriteTo(&b)
if err != nil {
return err
}
err = bkt.Put(key, b.Bytes())
if err != nil {
return err
}
}
}
//atomic.AddInt64(&db.numTr, -1)
return db.removeOrphanedTerms(tx, s, p, o)
}
示例11: nav
//line nav.ego:1
func nav(w io.Writer, tx *bolt.Tx) error {
//line nav.ego:2
if _, err := fmt.Fprintf(w, "\n\n"); err != nil {
return err
}
//line nav.ego:4
if _, err := fmt.Fprintf(w, "\n"); err != nil {
return err
}
//line nav.ego:5
if _, err := fmt.Fprintf(w, "\n\n"); err != nil {
return err
}
//line nav.ego:6
if _, err := fmt.Fprintf(w, "<h1>"); err != nil {
return err
}
//line nav.ego:6
if _, err := fmt.Fprintf(w, "%v", filepath.Base(tx.DB().Path())); err != nil {
return err
}
//line nav.ego:6
if _, err := fmt.Fprintf(w, "</h1>\n"); err != nil {
return err
}
return nil
}
示例12: 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
}
示例13: depersistMessage
func depersistMessage(tx *bolt.Tx, id int64) error {
content_bucket, err := tx.CreateBucketIfNotExists(MESSAGE_CONTENT_BUCKET)
if err != nil {
return err
}
return content_bucket.Delete(binaryId(id))
}
示例14: 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
}
示例15: 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)
}
}