[#791] writecache: Export OpenDB function

The function will be useful for the tool which works with write-cache parts.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-09-14 19:20:31 +03:00 committed by Leonard Lyubich
parent ca4c9d4673
commit 8a1593fdcc
2 changed files with 29 additions and 12 deletions

View file

@ -2,8 +2,8 @@ package writecache
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"path"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
"github.com/hashicorp/golang-lru/simplelru" "github.com/hashicorp/golang-lru/simplelru"
@ -28,16 +28,22 @@ const lruKeysCount = 256 * 1024 * 8
const dbName = "small.bolt" const dbName = "small.bolt"
func (c *cache) openStore() error { func (c *cache) openStore() error {
if err := util.MkdirAllX(c.path, os.ModePerm); err != nil { err := util.MkdirAllX(c.path, os.ModePerm)
if err != nil {
return err return err
} }
db, err := bbolt.Open(path.Join(c.path, dbName), os.ModePerm, &bbolt.Options{ c.db, err = OpenDB(c.path, false)
NoFreelistSync: true, if err != nil {
NoSync: true, return fmt.Errorf("could not open database: %w", err)
}
err = c.db.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(defaultBucket)
return err
}) })
if err != nil { if err != nil {
return err return fmt.Errorf("could not create default bucket: %w", err)
} }
c.fsTree = &fstree.FSTree{ c.fsTree = &fstree.FSTree{
@ -49,12 +55,6 @@ func (c *cache) openStore() error {
DirNameLen: 1, DirNameLen: 1,
} }
_ = db.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(defaultBucket)
return err
})
c.db = db
c.flushed, _ = lru.New(lruKeysCount) c.flushed, _ = lru.New(lruKeysCount)
return nil return nil
} }

View file

@ -0,0 +1,17 @@
package writecache
import (
"os"
"path"
"go.etcd.io/bbolt"
)
// OpenDB opens BoltDB instance for write-cache. Opens in read-only mode if ro is true.
func OpenDB(p string, ro bool) (*bbolt.DB, error) {
return bbolt.Open(path.Join(p, dbName), os.ModePerm, &bbolt.Options{
NoFreelistSync: true,
NoSync: true,
ReadOnly: ro,
})
}