Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(storage): leveldb initialization and dependency handling #782

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading