Example #1
0
// addFileContract adds a file contract to the database. An error is returned
// if the file contract is already in the database.
func addFileContract(tx *bolt.Tx, id types.FileContractID, fc types.FileContract) {
	// Add the file contract to the database.
	fcBucket := tx.Bucket(FileContracts)
	// Sanity check - should not be adding a zero-payout file contract.
	if build.DEBUG && fc.Payout.IsZero() {
		panic("adding zero-payout file contract")
	}
	// Sanity check - should not be adding a file contract already in the db.
	if build.DEBUG && fcBucket.Get(id[:]) != nil {
		panic("repeat file contract")
	}
	err := fcBucket.Put(id[:], encoding.Marshal(fc))
	if build.DEBUG && err != nil {
		panic(err)
	}

	// Add an entry for when the file contract expires.
	expirationBucketID := append(prefixFCEX, encoding.Marshal(fc.WindowEnd)...)
	expirationBucket, err := tx.CreateBucketIfNotExists(expirationBucketID)
	if build.DEBUG && err != nil {
		panic(err)
	}
	err = expirationBucket.Put(id[:], []byte{})
	if build.DEBUG && err != nil {
		panic(err)
	}
}
Example #2
0
File: db.go Project: kalafut/gmw
func createBucket(name []byte, tx *bolt.Tx) error {
	_, err := tx.CreateBucketIfNotExists(name)
	if err != nil {
		return fmt.Errorf("create bucket: %s", err)
	}
	return nil
}
Example #3
0
func Open(dbPath string, conf *Config, create, repair bool) (*BoltDB, error) {
	os.MkdirAll(dbPath, 0755)
	name := path.Join(dbPath, "rpdb_bolt.db")
	db := &BoltDB{
		path: dbPath,
		cfg:  conf,
	}
	var err error

	db.db, err = bolt.Open(name, 0600, nil)
	if err != nil {
		return nil, err
	}

	var tx *bolt.Tx
	tx, err = db.db.Begin(true)
	if err != nil {
		return nil, err
	}

	_, err = tx.CreateBucketIfNotExists(bucketName)
	if err != nil {
		tx.Rollback()
		return nil, err
	}

	if err = tx.Commit(); err != nil {
		return nil, err
	}

	return db, nil
}
Example #4
0
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))
}
Example #5
0
func (bs *boltStorage) bucket(tx *bolt.Tx) (b *bolt.Bucket, err error) {
	b = tx.Bucket(dbBucket)
	if b == nil {
		b, err = tx.CreateBucketIfNotExists(dbBucket)
	}
	return b, err
}
Example #6
0
// fullSync is an internal versino of the FullSync method which allows callers
// to sync the contents of an OpenChannel while re-using an existing database
// transaction.
func (c *OpenChannel) fullSync(tx *bolt.Tx) error {
	// TODO(roasbeef): add helper funcs to create scoped update
	// First fetch the top level bucket which stores all data related to
	// current, active channels.
	chanBucket, err := tx.CreateBucketIfNotExists(openChannelBucket)
	if err != nil {
		return err
	}

	// Within this top level bucket, fetch the bucket dedicated to storing
	// open channel data specific to the remote node.
	nodePub := c.IdentityPub.SerializeCompressed()
	nodeChanBucket, err := chanBucket.CreateBucketIfNotExists(nodePub)
	if err != nil {
		return err
	}

	// Add this channel ID to the node's active channel index if
	// it doesn't already exist.
	chanIDBucket, err := nodeChanBucket.CreateBucketIfNotExists(chanIDBucket)
	if err != nil {
		return err
	}
	var b bytes.Buffer
	if err := writeOutpoint(&b, c.ChanID); err != nil {
		return err
	}
	if chanIDBucket.Get(b.Bytes()) == nil {
		if err := chanIDBucket.Put(b.Bytes(), nil); err != nil {
			return err
		}
	}

	return putOpenChannel(chanBucket, nodeChanBucket, c)
}
Example #7
0
File: db.go Project: Aurlin/ledisdb
func (s Store) Open(dbPath string, cfg *config.Config) (driver.IDB, error) {
	os.MkdirAll(dbPath, 0755)
	name := path.Join(dbPath, "ledis_bolt.db")
	db := new(DB)
	var err error

	db.path = name
	db.cfg = cfg

	db.db, err = bolt.Open(name, 0600, nil)
	if err != nil {
		return nil, err
	}

	var tx *bolt.Tx
	tx, err = db.db.Begin(true)
	if err != nil {
		return nil, err
	}

	_, err = tx.CreateBucketIfNotExists(bucketName)
	if err != nil {
		tx.Rollback()
		return nil, err
	}

	if err = tx.Commit(); err != nil {
		return nil, err
	}

	return db, nil
}
Example #8
0
func (db *DB) Open(dbPath string) error {
	var (
		err    error
		boltDB *bolt.DB
	)
	boltDB, err = bolt.Open(dbPath, 0600, nil)
	if err != nil {
		return err
	}
	var tx *bolt.Tx
	tx, err = boltDB.Begin(true)
	if err != nil {
		return err
	}

	bucketName := []byte(base64.StdEncoding.EncodeToString([]byte(dbPath)))
	_, err = tx.CreateBucketIfNotExists(bucketName)
	if err != nil {
		tx.Rollback()
		return err
	}

	if err = tx.Commit(); err != nil {
		return err
	}
	db.boltDB = boltDB
	db.dbPath = dbPath
	db.bucketName = bucketName
	return nil
}
Example #9
0
func (q *Queue) DepersistBoltTx(tx *bolt.Tx) error {
	bucket, err := tx.CreateBucketIfNotExists(QUEUE_BUCKET_NAME)
	if err != nil {
		return fmt.Errorf("create bucket: %s", err)
	}
	return persist.DepersistOneBoltTx(bucket, q.Name)
}
Example #10
0
func Open(cfg *Config) (*DB, error) {
	os.MkdirAll(cfg.Path, os.ModePerm)
	name := path.Join(cfg.Path, "ledis_bolt.db")
	db := new(DB)
	var err error
	db.db, err = bolt.Open(name, 0600, nil)
	if err != nil {
		return nil, err
	}

	db.db.NoSync = cfg.NoSync

	var tx *bolt.Tx
	tx, err = db.db.Begin(true)
	if err != nil {
		return nil, err
	}

	_, err = tx.CreateBucketIfNotExists(bucketName)
	if err != nil {
		tx.Rollback()
		return nil, err
	}

	if err = tx.Commit(); err != nil {
		return nil, err
	}

	return db, nil
}
Example #11
0
func persistIndexMessage(tx *bolt.Tx, im *amqp.IndexMessage) error {
	content_bucket, err := tx.CreateBucketIfNotExists(MESSAGE_INDEX_BUCKET)
	b, err := proto.Marshal(im)
	if err != nil {
		return err
	}
	return content_bucket.Put(binaryId(im.Id), b)
}
Example #12
0
func persistMessage(tx *bolt.Tx, msg *amqp.Message) error {
	content_bucket, err := tx.CreateBucketIfNotExists(MESSAGE_CONTENT_BUCKET)
	b, err := proto.Marshal(msg)
	if err != nil {
		return err
	}
	return content_bucket.Put(binaryId(msg.Id), b)
}
Example #13
0
func initBuckets(t *bolt.Tx) error {
	for _, name := range [][]byte{BucketHooks, BucketStats, BucketComponents} {
		if _, err := t.CreateBucketIfNotExists(name); err != nil {
			return err
		}
	}
	return nil
}
Example #14
0
func (binding *Binding) DepersistBoltTx(tx *bolt.Tx) error {
	bucket, err := tx.CreateBucketIfNotExists(BINDINGS_BUCKET_NAME)
	if err != nil { // pragma: nocover
		// If we're hitting this it means the disk is full, the db is readonly,
		// or something else has gone irrecoverably wrong
		panic(fmt.Sprintf("create bucket: %s", err))
	}
	return persist.DepersistOneBoltTx(bucket, string(binding.Id))
}
Example #15
0
func depersistQueueMessage(tx *bolt.Tx, queueName string, id int64) error {
	bucketName := fmt.Sprintf("queue_%s", queueName)
	content_bucket, err := tx.CreateBucketIfNotExists([]byte(bucketName))
	if err != nil {
		return err
	}
	var key = binaryId(id)
	return content_bucket.Delete(key)
}
Example #16
0
func (m *Meme) writeBindata(tx *bolt.Tx, id uint64) error {
	bucket, err := tx.CreateBucketIfNotExists(binBucketName)
	if err != nil {
		return err
	}
	if err := bucket.Put(itob(id), m.bindata); err != nil {
		return err
	}
	return nil
}
Example #17
0
func boltInit(tx *bolt.Tx) error {
	gollBucket, e := tx.CreateBucketIfNotExists(BucketGollName)
	if e != nil {
		return e
	}
	gollBucket.CreateBucketIfNotExists(BucketWhatName)
	gollBucket.CreateBucketIfNotExists(BucketMetaName)
	gollBucket.CreateBucketIfNotExists(BucketSettingName)
	return nil
}
Example #18
0
File: store.go Project: hwhw/mesh
// put an item to the corresponding store
func (b *DB) Put(tx *bolt.Tx, item Item) error {
	bucket, err := tx.CreateBucketIfNotExists(item.StoreID())
	if err == nil {
		bytes, err := item.Bytes()
		if err == nil {
			bucket.Put(item.Key(), bytes)
		}
	}
	return err
}
Example #19
0
// Destroy deletes a meme from the DB
func (m *Meme) Destroy(tx *bolt.Tx) error {
	bucket, err := tx.CreateBucketIfNotExists(bucketName)
	if err != nil {
		return err
	}

	if err := bucket.Delete(itob(m.ID)); err != nil {
		return err
	}
	return nil
}
Example #20
0
//Put sets one key/value pair.
func Put(tx *bolt.Tx, bucket string, key []byte, value interface{}) error {
	val, err := Tob(value)
	if err != nil {
		return err
	}
	b, errr := tx.CreateBucketIfNotExists([]byte(bucket))
	if errr != nil {
		return fmt.Errorf("create bucket: %s", err)
	}
	return b.Put(key, val)
}
Example #21
0
func putClosedChannelSummary(tx *bolt.Tx, chanID []byte) error {
	// For now, a summary of a closed channel simply involves recording the
	// outpoint of the funding transaction.
	closedChanBucket, err := tx.CreateBucketIfNotExists(closedChannelBucket)
	if err != nil {
		return err
	}

	// TODO(roasbeef): add other info
	//  * should likely have each in own bucket per node
	return closedChanBucket.Put(chanID, nil)
}
Example #22
0
func persistQueueMessage(tx *bolt.Tx, queueName string, qm *amqp.QueueMessage) error {
	bucketName := fmt.Sprintf("queue_%s", queueName)
	content_bucket, err := tx.CreateBucketIfNotExists([]byte(bucketName))
	if err != nil {
		return err
	}
	protoBytes, err := proto.Marshal(qm)
	if err != nil {
		return err
	}
	return content_bucket.Put(binaryId(qm.Id), protoBytes)
}
Example #23
0
func addIDToTag(tx *bolt.Tx, tag, id string) error {
	buck, err := tx.CreateBucketIfNotExists(tagBucket)
	if err != nil {
		return err
	}
	buck, err = buck.CreateBucketIfNotExists([]byte(tag))
	if err != nil {
		return err
	}
	buck.Put([]byte(id), []byte{1})
	return nil
}
Example #24
0
// ClearDates clears the date schedule for the group
func (g *Group) ClearDates(tx *bolt.Tx) error {
	b, err := tx.CreateBucketIfNotExists([]byte(g.ID))
	if err != nil {
		return err
	}

	err = b.DeleteBucket(datesBucket)
	if err != nil && err != bolt.ErrBucketNotFound {
		return err
	}

	return nil
}
func saveMetaData(tx *bolt.Tx, schema []Column) error {
	b, err := tx.CreateBucketIfNotExists([]byte(SCHEMABUCKET))

	if err != nil {
		return err
	}

	for i := 0; i < len(schema); i++ {
		b.Put([]byte(schema[i].name), []byte(schema[i].dataType))
	}

	return nil

}
Example #26
0
func createBucketIfNotExists(tx *bolt.Tx, keys ...[]byte) (*bolt.Bucket, error) {
	bkt, err := tx.CreateBucketIfNotExists(keys[0])
	if err != nil {
		return nil, err
	}

	for _, key := range keys[1:] {
		bkt, err = bkt.CreateBucketIfNotExists(key)
		if err != nil {
			return nil, err
		}
	}

	return bkt, nil
}
Example #27
0
// Save stores the Date in the database
func (d *Date) Save(tx *bolt.Tx) error {
	b, err := tx.CreateBucketIfNotExists([]byte(d.Group))
	if err != nil {
		return err
	}
	b, err = b.CreateBucketIfNotExists(datesBucket)
	if err != nil {
		return err
	}
	data, err := encodeDate(d)
	if err != nil {
		return err
	}
	return b.Put(d.Key(), data)
}
Example #28
0
func ensureMsgBucket(tx *bolt.Tx, connection string, bucket string) (b *bolt.Bucket, err error) {
	// make sure our connection bucket exists
	b, err = tx.CreateBucketIfNotExists([]byte(connection))
	if err != nil {
		return b, err
	}

	// and make sure our sub bucket exists
	b, err = b.CreateBucketIfNotExists([]byte(bucket))
	if err != nil {
		return b, err
	}

	return b, err
}
Example #29
0
// updateDbMetadata will set the contents of the metadata bucket to be
// what is stored inside the metadata argument
func (db *BoltDatabase) updateMetadata(tx *bolt.Tx) error {
	bucket, err := tx.CreateBucketIfNotExists([]byte("Metadata"))
	if err != nil {
		return err
	}
	err = bucket.Put([]byte("Header"), []byte(db.meta.Header))
	if err != nil {
		return err
	}
	err = bucket.Put([]byte("Version"), []byte(db.meta.Version))
	if err != nil {
		return err
	}
	return nil
}
Example #30
0
func (b *BoltQ) decreaseTotalItem(tx *bolt.Tx) (err error) {
	stats_bucket, err := tx.CreateBucketIfNotExists([]byte(BUCKET_STATS))
	tmp_count := stats_bucket.Get([]byte(KEY_TOTALITEM))
	if tmp_count != nil {
		tmp, err := strconv.ParseInt(string(tmp_count), 10, 64)
		if err != nil {
			// log.Printf("ERROR: %v\n", err)
			return err
		}
		b.total_item = tmp
	}
	// remove above codes won't boost performance ...

	b.total_item -= 1
	err = stats_bucket.Put([]byte(KEY_TOTALITEM), []byte(fmt.Sprintf("%d", b.total_item)))
	return
}