Skip to content

Commit

Permalink
refactor(storage): BuntDB initialization and locking mechanism (#781)
Browse files Browse the repository at this point in the history
- Update `github.com/tidwall/buntdb` dependency from v1.3.0 to v1.3.1
- Modify `buntdb.New` initialization to use `conf.Stat.BuntDB.Path`
- Remove explicit lock field in `Storage` struct and use embedded `sync.RWMutex`
- Change lock usage in `Storage` methods to use embedded `sync.RWMutex` methods
- Add `os` package import in `buntdb.go`
- Refactor `New` function in `buntdb.go` to accept `dbPath` string instead of `config`
- Update `Init` method in `buntdb.go` to handle empty `dbPath` by setting it to a temporary directory
- Remove `os` and `config` imports from `buntdb_test.go`
- Simplify `TestBuntDBEngine` by removing configuration loading and file existence checks
- Add test steps to reset and verify the value of a key in `TestBuntDBEngine`

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Jun 16, 2024
1 parent b2f4a28 commit d131891
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/syndtr/goleveldb v1.0.0
github.com/thoas/stats v0.0.0-20190407194641-965cb2de1678
github.com/tidwall/buntdb v1.3.0
github.com/tidwall/buntdb v1.3.1
go.opencensus.io v0.24.0
go.uber.org/atomic v1.11.0
golang.org/x/crypto v0.24.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ github.com/tidwall/assert v0.1.0 h1:aWcKyRBUAdLoVebxo95N7+YZVTFF/ASTr7BN4sLP6XI=
github.com/tidwall/assert v0.1.0/go.mod h1:QLYtGyeqse53vuELQheYl9dngGCJQ+mTtlxcktb+Kj8=
github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
github.com/tidwall/buntdb v1.3.0 h1:gdhWO+/YwoB2qZMeAU9JcWWsHSYU3OvcieYgFRS0zwA=
github.com/tidwall/buntdb v1.3.0/go.mod h1:lZZrZUWzlyDJKlLQ6DKAy53LnG7m5kHyrEHvvcDmBpU=
github.com/tidwall/buntdb v1.3.1 h1:HKoDF01/aBhl9RjYtbaLnvX9/OuenwvQiC3OP1CcL4o=
github.com/tidwall/buntdb v1.3.1/go.mod h1:lZZrZUWzlyDJKlLQ6DKAy53LnG7m5kHyrEHvvcDmBpU=
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
Expand Down
4 changes: 3 additions & 1 deletion status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ func InitAppStatus(conf *config.ConfYaml) error {
conf.Stat.BoltDB.Bucket,
)
case "buntdb":
store = buntdb.New(conf)
store = buntdb.New(
conf.Stat.BuntDB.Path,
)
case "leveldb":
store = leveldb.New(conf)
case "badger":
Expand Down
14 changes: 7 additions & 7 deletions storage/boltdb/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ type Storage struct {
dbPath string
bucket string
db *storm.DB
lock sync.RWMutex
sync.RWMutex
}

func (s *Storage) Add(key string, count int64) {
s.lock.Lock()
defer s.lock.Unlock()
s.Lock()
defer s.Unlock()
s.setBoltDB(key, s.getBoltDB(key)+count)
}

func (s *Storage) Set(key string, count int64) {
s.lock.Lock()
defer s.lock.Unlock()
s.Lock()
defer s.Unlock()
s.setBoltDB(key, count)
}

func (s *Storage) Get(key string) int64 {
s.lock.RLock()
defer s.lock.RUnlock()
s.RLock()
defer s.RUnlock()
return s.getBoltDB(key)
}

Expand Down
27 changes: 15 additions & 12 deletions storage/buntdb/buntdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,52 @@ package buntdb
import (
"fmt"
"log"
"os"
"strconv"
"sync"

"github.com/appleboy/gorush/config"
"github.com/tidwall/buntdb"
)

// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
func New(config *config.ConfYaml) *Storage {
func New(dbPath string) *Storage {
return &Storage{
config: config,
dbPath: dbPath,
}
}

// Storage is interface structure
type Storage struct {
config *config.ConfYaml
dbPath string
db *buntdb.DB
lock sync.RWMutex
sync.RWMutex
}

func (s *Storage) Add(key string, count int64) {
s.lock.Lock()
defer s.lock.Unlock()
s.Lock()
defer s.Unlock()
s.setBuntDB(key, s.getBuntDB(key)+count)
}

func (s *Storage) Set(key string, count int64) {
s.lock.Lock()
defer s.lock.Unlock()
s.Lock()
defer s.Unlock()
s.setBuntDB(key, count)
}

func (s *Storage) Get(key string) int64 {
s.lock.RLock()
defer s.lock.RUnlock()
s.RLock()
defer s.RUnlock()
return s.getBuntDB(key)
}

// Init client storage.
func (s *Storage) Init() error {
var err error
s.db, err = buntdb.Open(s.config.Stat.BuntDB.Path)
if s.dbPath == "" {
s.dbPath = os.TempDir() + "buntdb.db"
}
s.db, err = buntdb.Open(s.dbPath)
return err
}

Expand Down
16 changes: 6 additions & 10 deletions storage/buntdb/buntdb_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package buntdb

import (
"os"
"sync"
"testing"

"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"

"github.com/stretchr/testify/assert"
Expand All @@ -14,17 +12,15 @@ import (
func TestBuntDBEngine(t *testing.T) {
var val int64

cfg, _ := config.LoadConf()

if _, err := os.Stat(cfg.Stat.BuntDB.Path); os.IsNotExist(err) {
err := os.RemoveAll(cfg.Stat.BuntDB.Path)
assert.Nil(t, err)
}

buntDB := New(cfg)
buntDB := New("")
err := buntDB.Init()
assert.Nil(t, err)

// reset the value of the key to 0
buntDB.Set(core.HuaweiSuccessKey, 0)
val = buntDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)

buntDB.Add(core.HuaweiSuccessKey, 10)
val = buntDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
Expand Down

0 comments on commit d131891

Please sign in to comment.