Implement namespace optional arg

This commit is contained in:
Bel LaPointe
2019-03-20 09:54:26 -06:00
parent b79cb97ba6
commit b7a231feaf
10 changed files with 101 additions and 45 deletions

10
bolt.go
View File

@@ -13,10 +13,11 @@ func NewBolt(path string) (*Bolt, error) {
}, err
}
func (b *Bolt) Get(key string) ([]byte, error) {
func (b *Bolt) Get(key string, ns ...string) ([]byte, error) {
namespace := resolveNamespace(ns)
var result []byte
err := b.db.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(DefaultNamespace))
bkt := tx.Bucket([]byte(namespace))
if bkt == nil {
return ErrNotFound
}
@@ -29,9 +30,10 @@ func (b *Bolt) Get(key string) ([]byte, error) {
return result, err
}
func (b *Bolt) Set(key string, value []byte) error {
func (b *Bolt) Set(key string, value []byte, ns ...string) error {
namespace := resolveNamespace(ns)
return b.db.Update(func(tx *bolt.Tx) error {
bkt, err := tx.CreateBucketIfNotExists([]byte(DefaultNamespace))
bkt, err := tx.CreateBucketIfNotExists([]byte(namespace))
if err != nil {
return err
}