Initial commit: bolt, map, leveldb support and test

This commit is contained in:
Bel LaPointe
2019-03-13 14:06:46 -06:00
commit 8a1cf7104c
7 changed files with 220 additions and 0 deletions

44
bolt.go Normal file
View File

@@ -0,0 +1,44 @@
package storage
import "github.com/boltdb/bolt"
type Bolt struct {
db *bolt.DB
}
func NewBolt(path string) (*Bolt, error) {
db, err := bolt.Open(path, 0600, nil)
return &Bolt{
db: db,
}, err
}
func (b *Bolt) Get(key string) ([]byte, error) {
var result []byte
err := b.db.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(DefaultNamespace))
if bkt == nil {
return ErrNotFound
}
result = bkt.Get([]byte(key))
if result == nil {
return ErrNotFound
}
return nil
})
return result, err
}
func (b *Bolt) Set(key string, value []byte) error {
return b.db.Update(func(tx *bolt.Tx) error {
bkt, err := tx.CreateBucketIfNotExists([]byte(DefaultNamespace))
if err != nil {
return err
}
return bkt.Put([]byte(key), value)
})
}
func (b *Bolt) Close() error {
return b.db.Close()
}