Skip to content

Commit

Permalink
refactor(storage): leveldb initialization and dependency handling (#782)
Browse files Browse the repository at this point in the history
- Refactor `leveldb.New` initialization to accept `dbPath` instead of `config`
- Add `os` package import in `leveldb.go`
- Remove `config` dependency from `leveldb` package
- Replace `config` field with `dbPath` in `Storage` struct
- Simplify locking mechanism by directly using `sync.RWMutex` methods
- Add default `dbPath` handling in `Init` method if `dbPath` is empty
- Update `leveldb_test.go` to use new `New` function signature
- Add test assertions to reset and verify key values in `leveldb_test.go`

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy authored Jun 16, 2024
1 parent d131891 commit 005aa23
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
4 changes: 3 additions & 1 deletion status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func InitAppStatus(conf *config.ConfYaml) error {
conf.Stat.BuntDB.Path,
)
case "leveldb":
store = leveldb.New(conf)
store = leveldb.New(
conf.Stat.LevelDB.Path,
)
case "badger":
store = badger.New(
conf.Stat.BadgerDB.Path,
Expand Down
27 changes: 15 additions & 12 deletions storage/leveldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package leveldb

import (
"fmt"
"os"
"strconv"
"sync"

"github.com/appleboy/gorush/config"
"github.com/syndtr/goleveldb/leveldb"
)

Expand All @@ -21,41 +21,44 @@ func (s *Storage) getLevelDB(key string) int64 {
}

// 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 *leveldb.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.setLevelDB(key, s.getLevelDB(key)+count)
}

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

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

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

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

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 TestLevelDBEngine(t *testing.T) {
var val int64

cfg, _ := config.LoadConf()

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

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

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

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

0 comments on commit 005aa23

Please sign in to comment.