Separate storage logic from actual business logic (#649)
This commit is contained in:
@@ -2,10 +2,9 @@ package boltdb
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/appleboy/gorush/storage"
|
||||
|
||||
"github.com/asdine/storm/v3"
|
||||
)
|
||||
|
||||
@@ -20,6 +19,25 @@ func New(config *config.ConfYaml) *Storage {
|
||||
type Storage struct {
|
||||
config *config.ConfYaml
|
||||
db *storm.DB
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *Storage) Add(key string, count int64) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
s.setBoltDB(key, s.getBoltDB(key)+count)
|
||||
}
|
||||
|
||||
func (s *Storage) Set(key string, count int64) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
s.setBoltDB(key, count)
|
||||
}
|
||||
|
||||
func (s *Storage) Get(key string) int64 {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
return s.getBoltDB(key)
|
||||
}
|
||||
|
||||
// Init client storage.
|
||||
@@ -38,17 +56,6 @@ func (s *Storage) Close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
// Reset Client storage.
|
||||
func (s *Storage) Reset() {
|
||||
s.setBoltDB(storage.TotalCountKey, 0)
|
||||
s.setBoltDB(storage.IosSuccessKey, 0)
|
||||
s.setBoltDB(storage.IosErrorKey, 0)
|
||||
s.setBoltDB(storage.AndroidSuccessKey, 0)
|
||||
s.setBoltDB(storage.AndroidErrorKey, 0)
|
||||
s.setBoltDB(storage.HuaweiSuccessKey, 0)
|
||||
s.setBoltDB(storage.HuaweiErrorKey, 0)
|
||||
}
|
||||
|
||||
func (s *Storage) setBoltDB(key string, count int64) {
|
||||
err := s.db.Set(s.config.Stat.BoltDB.Bucket, key, count)
|
||||
if err != nil {
|
||||
@@ -56,107 +63,11 @@ func (s *Storage) setBoltDB(key string, count int64) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Storage) getBoltDB(key string, count *int64) {
|
||||
err := s.db.Get(s.config.Stat.BoltDB.Bucket, key, count)
|
||||
func (s *Storage) getBoltDB(key string) int64 {
|
||||
var count int64
|
||||
err := s.db.Get(s.config.Stat.BoltDB.Bucket, key, &count)
|
||||
if err != nil {
|
||||
log.Println("BoltDB get error:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// AddTotalCount record push notification count.
|
||||
func (s *Storage) AddTotalCount(count int64) {
|
||||
total := s.GetTotalCount() + count
|
||||
s.setBoltDB(storage.TotalCountKey, total)
|
||||
}
|
||||
|
||||
// AddIosSuccess record counts of success iOS push notification.
|
||||
func (s *Storage) AddIosSuccess(count int64) {
|
||||
total := s.GetIosSuccess() + count
|
||||
s.setBoltDB(storage.IosSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddIosError record counts of error iOS push notification.
|
||||
func (s *Storage) AddIosError(count int64) {
|
||||
total := s.GetIosError() + count
|
||||
s.setBoltDB(storage.IosErrorKey, total)
|
||||
}
|
||||
|
||||
// AddAndroidSuccess record counts of success Android push notification.
|
||||
func (s *Storage) AddAndroidSuccess(count int64) {
|
||||
total := s.GetAndroidSuccess() + count
|
||||
s.setBoltDB(storage.AndroidSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddAndroidError record counts of error Android push notification.
|
||||
func (s *Storage) AddAndroidError(count int64) {
|
||||
total := s.GetAndroidError() + count
|
||||
s.setBoltDB(storage.AndroidErrorKey, total)
|
||||
}
|
||||
|
||||
// AddHuaweiSuccess record counts of success Huawei push notification.
|
||||
func (s *Storage) AddHuaweiSuccess(count int64) {
|
||||
total := s.GetHuaweiSuccess() + count
|
||||
s.setBoltDB(storage.HuaweiSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddHuaweiError record counts of error Huawei push notification.
|
||||
func (s *Storage) AddHuaweiError(count int64) {
|
||||
total := s.GetHuaweiError() + count
|
||||
s.setBoltDB(storage.HuaweiErrorKey, total)
|
||||
}
|
||||
|
||||
// GetTotalCount show counts of all notification.
|
||||
func (s *Storage) GetTotalCount() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(storage.TotalCountKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosSuccess show success counts of iOS notification.
|
||||
func (s *Storage) GetIosSuccess() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(storage.IosSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosError show error counts of iOS notification.
|
||||
func (s *Storage) GetIosError() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(storage.IosErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidSuccess show success counts of Android notification.
|
||||
func (s *Storage) GetAndroidSuccess() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(storage.AndroidSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidError show error counts of Android notification.
|
||||
func (s *Storage) GetAndroidError() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(storage.AndroidErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetHuaweiSuccess show success counts of Huawei notification.
|
||||
func (s *Storage) GetHuaweiSuccess() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(storage.HuaweiSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetHuaweiError show error counts of Huawei notification.
|
||||
func (s *Storage) GetHuaweiError() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(storage.HuaweiErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package boltdb
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/appleboy/gorush/storage"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -15,35 +18,30 @@ func TestBoltDBEngine(t *testing.T) {
|
||||
boltDB := New(cfg)
|
||||
err := boltDB.Init()
|
||||
assert.Nil(t, err)
|
||||
boltDB.Reset()
|
||||
|
||||
boltDB.AddTotalCount(10)
|
||||
val = boltDB.GetTotalCount()
|
||||
boltDB.Add(storage.HuaweiSuccessKey, 10)
|
||||
val = boltDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(10), val)
|
||||
boltDB.AddTotalCount(10)
|
||||
val = boltDB.GetTotalCount()
|
||||
boltDB.Add(storage.HuaweiSuccessKey, 10)
|
||||
val = boltDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(20), val)
|
||||
|
||||
boltDB.AddIosSuccess(20)
|
||||
val = boltDB.GetIosSuccess()
|
||||
assert.Equal(t, int64(20), val)
|
||||
|
||||
boltDB.AddIosError(30)
|
||||
val = boltDB.GetIosError()
|
||||
assert.Equal(t, int64(30), val)
|
||||
|
||||
boltDB.AddAndroidSuccess(40)
|
||||
val = boltDB.GetAndroidSuccess()
|
||||
assert.Equal(t, int64(40), val)
|
||||
|
||||
boltDB.AddAndroidError(50)
|
||||
val = boltDB.GetAndroidError()
|
||||
assert.Equal(t, int64(50), val)
|
||||
|
||||
// test reset db
|
||||
boltDB.Reset()
|
||||
val = boltDB.GetAndroidError()
|
||||
boltDB.Set(storage.HuaweiSuccessKey, 0)
|
||||
val = boltDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(0), val)
|
||||
|
||||
// test concurrency issues
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
boltDB.Add(storage.HuaweiSuccessKey, 1)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
val = boltDB.Get(storage.HuaweiSuccessKey)
|
||||
assert.Equal(t, int64(10), val)
|
||||
|
||||
assert.NoError(t, boltDB.Close())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user