From 8a1593fdcce30b8d3717c26f85896f672d8987af Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 14 Sep 2021 19:20:31 +0300 Subject: [PATCH] [#791] writecache: Export OpenDB function The function will be useful for the tool which works with write-cache parts. Signed-off-by: Leonard Lyubich --- .../writecache/storage.go | 24 +++++++++---------- pkg/local_object_storage/writecache/util.go | 17 +++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 pkg/local_object_storage/writecache/util.go diff --git a/pkg/local_object_storage/writecache/storage.go b/pkg/local_object_storage/writecache/storage.go index e82e1fb41..80842317b 100644 --- a/pkg/local_object_storage/writecache/storage.go +++ b/pkg/local_object_storage/writecache/storage.go @@ -2,8 +2,8 @@ package writecache import ( "errors" + "fmt" "os" - "path" lru "github.com/hashicorp/golang-lru" "github.com/hashicorp/golang-lru/simplelru" @@ -28,16 +28,22 @@ const lruKeysCount = 256 * 1024 * 8 const dbName = "small.bolt" 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 } - db, err := bbolt.Open(path.Join(c.path, dbName), os.ModePerm, &bbolt.Options{ - NoFreelistSync: true, - NoSync: true, + c.db, err = OpenDB(c.path, false) + if err != nil { + 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 { - return err + return fmt.Errorf("could not create default bucket: %w", err) } c.fsTree = &fstree.FSTree{ @@ -49,12 +55,6 @@ func (c *cache) openStore() error { DirNameLen: 1, } - _ = db.Update(func(tx *bbolt.Tx) error { - _, err := tx.CreateBucketIfNotExists(defaultBucket) - return err - }) - - c.db = db c.flushed, _ = lru.New(lruKeysCount) return nil } diff --git a/pkg/local_object_storage/writecache/util.go b/pkg/local_object_storage/writecache/util.go new file mode 100644 index 000000000..62e7ca748 --- /dev/null +++ b/pkg/local_object_storage/writecache/util.go @@ -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, + }) +}