Example #1
0
File: main.go Project: ngaut/bmdb
func view(db *bmdb.DB) {
	if len(bucketName) == 0 {
		if err := db.View(func(tx *bmdb.Tx) error {
			names := tx.BucketNames()
			if names == nil {
				return errors.New("cannot reach the unnamed bucket")
			}
			return names.ForEach(func(name, _ []byte) error {
				var i int64
				return tx.Bucket(name).ForEach(func(k, v []byte) error {
					i++
					printValue(i, name, k, v)
					return nil
				})
			})
		}); err != nil {
			log.Fatalln(err)
		}
		return
	}
	if err := db.View(func(tx *bmdb.Tx) error {
		var i int64
		return tx.Bucket([]byte(bucketName)).ForEach(func(k, v []byte) error {
			i++
			printValue(i, []byte(bucketName), k, v)
			return nil
		})
	}); err != nil {
		log.Fatalln(err)
	}
}
Example #2
0
File: main.go Project: ngaut/bmdb
func update(db *bmdb.DB) {
	if err := db.Update(func(tx *bmdb.Tx) error {
		b, err := tx.CreateBucketIfNotExists([]byte(bucketName))
		if err != nil {
			return err
		}
		v, _ := binny.Marshal(27)
		return b.Put([]byte(key), v)
	}); err != nil {
		log.Fatalln(err)
	}
}