Initial commit: bolt, map, leveldb support and test
This commit is contained in:
44
bolt.go
Normal file
44
bolt.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user