Change storage to open-close bolt.db to hopefully avoid /tmp files

master
breel 2020-08-26 17:47:44 -06:00
parent 4aea3dab40
commit 08c1f20ca8
2 changed files with 43 additions and 16 deletions

View File

@ -16,19 +16,25 @@ import (
)
type BoltDB struct {
db *bolt.DB
path string
}
func NewBoltDB(path string) *BoltDB {
db, err := bolt.Open(path, os.ModePerm, &bolt.Options{
Timeout: time.Second * 3,
})
bdb := &BoltDB{
path: path,
}
db, err := bdb.db(context.TODO())
if err != nil {
panic(err)
}
return &BoltDB{
db: db,
}
defer db.Close()
return bdb
}
func (bdb *BoltDB) db(ctx context.Context) (*bolt.DB, error) {
return bolt.Open(bdb.path, os.ModePerm, &bolt.Options{
Timeout: time.Second * 3,
})
}
func (bdb *BoltDB) Count(ctx context.Context, namespace string, filter interface{}) (int, error) {
@ -40,7 +46,7 @@ func (bdb *BoltDB) Count(ctx context.Context, namespace string, filter interface
return n, err
}
func (bdb *BoltDB) Find(_ context.Context, namespace string, filter interface{}) (chan bson.Raw, error) {
func (bdb *BoltDB) Find(ctx context.Context, namespace string, filter interface{}) (chan bson.Raw, error) {
b, err := bson.Marshal(filter)
if err != nil {
return nil, err
@ -50,7 +56,12 @@ func (bdb *BoltDB) Find(_ context.Context, namespace string, filter interface{})
return nil, err
}
results := make([]bson.Raw, 0)
err = bdb.db.View(func(tx *bolt.Tx) error {
db, err := bdb.db(ctx)
if err != nil {
return nil, err
}
defer db.Close()
err = db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(namespace))
if bucket == nil {
return nil
@ -62,7 +73,7 @@ func (bdb *BoltDB) Find(_ context.Context, namespace string, filter interface{})
return err
}
if matches(n, m) {
results = append(results, bson.Raw(v))
results = append(results, append(bson.Raw{}, bson.Raw(v)...))
}
}
return nil
@ -86,7 +97,12 @@ func (bdb *BoltDB) Update(ctx context.Context, namespace string, filter, operato
if err := bson.Unmarshal(b, &m); err != nil {
return err
}
return bdb.db.Update(func(tx *bolt.Tx) error {
db, err := bdb.db(ctx)
if err != nil {
return err
}
defer db.Close()
return db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(namespace))
if bucket == nil {
return nil
@ -129,7 +145,12 @@ func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}
} else if _, ok := m[entity.ID].(string); !ok {
return errors.New("primary key must be a string")
}
return bdb.db.Update(func(tx *bolt.Tx) error {
db, err := bdb.db(ctx)
if err != nil {
return err
}
defer db.Close()
return db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(namespace))
if err != nil {
return err
@ -143,7 +164,7 @@ func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}
})
}
func (bdb *BoltDB) Delete(_ context.Context, namespace string, filter interface{}) error {
func (bdb *BoltDB) Delete(ctx context.Context, namespace string, filter interface{}) error {
b, err := bson.Marshal(filter)
if err != nil {
return err
@ -152,7 +173,12 @@ func (bdb *BoltDB) Delete(_ context.Context, namespace string, filter interface{
if err := bson.Unmarshal(b, &m); err != nil {
return err
}
return bdb.db.Update(func(tx *bolt.Tx) error {
db, err := bdb.db(ctx)
if err != nil {
return err
}
defer db.Close()
return db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(namespace))
if bucket == nil {
return nil

View File

@ -369,13 +369,14 @@ func tempBoltDB(t *testing.T) (*BoltDB, func()) {
bdb := NewBoltDB(f.Name())
fillBoltDB(t, bdb)
return bdb, func() {
bdb.db.Close()
os.Remove(f.Name())
}
}
func fillBoltDB(t *testing.T, bdb *BoltDB) {
if err := bdb.db.Update(func(tx *bolt.Tx) error {
db, _ := bdb.db(context.TODO())
defer db.Close()
if err := db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(testNS))
if err != nil {
return err