integrate boltdb engine.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2016-05-02 20:22:37 +08:00
parent 03ab8eeac7
commit 5994f48e7e
5 changed files with 136 additions and 119 deletions

View File

@@ -1,37 +1,40 @@
package boltdb
import (
"github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/gorush/config"
"github.com/asdine/storm"
)
// Stat variable for redis
const (
TotalCountKey = "gorush-total-count"
IosSuccessKey = "gorush-ios-success-count"
IosErrorKey = "gorush-ios-error-count"
AndroidSuccessKey = "gorush-android-success-count"
AndroidErrorKey = "gorush-android-error-count"
)
// Storage implements the storage interface for gorush (https://github.com/appleboy/gorush)
func New(config gorush.ConfYaml, stat gorush.StatusApp) *Storage {
func New(config config.ConfYaml) *Storage {
return &Storage{
stat: stat,
config: config,
}
}
type Storage struct {
config gorush.ConfYaml
stat gorush.StatusApp
config config.ConfYaml
}
func (s *Storage) initBoltDB() {
s.stat.TotalCount = s.getTotalCount()
s.stat.Ios.PushSuccess = s.getIosSuccess()
s.stat.Ios.PushError = s.getIosError()
s.stat.Android.PushSuccess = s.getAndroidSuccess()
s.stat.Android.PushError = s.getAndroidError()
func (s *Storage) Init() error {
return nil
}
func (s *Storage) resetBoltDB() {
s.setBoltDB(gorush.TotalCountKey, 0)
s.setBoltDB(gorush.IosSuccessKey, 0)
s.setBoltDB(gorush.IosErrorKey, 0)
s.setBoltDB(gorush.AndroidSuccessKey, 0)
s.setBoltDB(gorush.AndroidErrorKey, 0)
func (s *Storage) Reset() {
s.setBoltDB(TotalCountKey, 0)
s.setBoltDB(IosSuccessKey, 0)
s.setBoltDB(IosErrorKey, 0)
s.setBoltDB(AndroidSuccessKey, 0)
s.setBoltDB(AndroidErrorKey, 0)
}
func (s *Storage) setBoltDB(key string, count int64) {
@@ -46,62 +49,62 @@ func (s *Storage) getBoltDB(key string, count *int64) {
defer db.Close()
}
func (s *Storage) addTotalCount(count int64) {
total := s.getTotalCount() + count
s.setBoltDB(gorush.TotalCountKey, total)
func (s *Storage) AddTotalCount(count int64) {
total := s.GetTotalCount() + count
s.setBoltDB(TotalCountKey, total)
}
func (s *Storage) addIosSuccess(count int64) {
total := s.getIosSuccess() + count
s.setBoltDB(gorush.IosSuccessKey, total)
func (s *Storage) AddIosSuccess(count int64) {
total := s.GetIosSuccess() + count
s.setBoltDB(IosSuccessKey, total)
}
func (s *Storage) addIosError(count int64) {
total := s.getIosError() + count
s.setBoltDB(gorush.IosErrorKey, total)
func (s *Storage) AddIosError(count int64) {
total := s.GetIosError() + count
s.setBoltDB(IosErrorKey, total)
}
func (s *Storage) addAndroidSuccess(count int64) {
total := s.getAndroidSuccess() + count
s.setBoltDB(gorush.AndroidSuccessKey, total)
func (s *Storage) AddAndroidSuccess(count int64) {
total := s.GetAndroidSuccess() + count
s.setBoltDB(AndroidSuccessKey, total)
}
func (s *Storage) addAndroidError(count int64) {
total := s.getAndroidError() + count
s.setBoltDB(gorush.AndroidErrorKey, total)
func (s *Storage) AddAndroidError(count int64) {
total := s.GetAndroidError() + count
s.setBoltDB(AndroidErrorKey, total)
}
func (s *Storage) getTotalCount() int64 {
func (s *Storage) GetTotalCount() int64 {
var count int64
s.getBoltDB(gorush.TotalCountKey, &count)
s.getBoltDB(TotalCountKey, &count)
return count
}
func (s *Storage) getIosSuccess() int64 {
func (s *Storage) GetIosSuccess() int64 {
var count int64
s.getBoltDB(gorush.IosSuccessKey, &count)
s.getBoltDB(IosSuccessKey, &count)
return count
}
func (s *Storage) getIosError() int64 {
func (s *Storage) GetIosError() int64 {
var count int64
s.getBoltDB(gorush.IosErrorKey, &count)
s.getBoltDB(IosErrorKey, &count)
return count
}
func (s *Storage) getAndroidSuccess() int64 {
func (s *Storage) GetAndroidSuccess() int64 {
var count int64
s.getBoltDB(gorush.AndroidSuccessKey, &count)
s.getBoltDB(AndroidSuccessKey, &count)
return count
}
func (s *Storage) getAndroidError() int64 {
func (s *Storage) GetAndroidError() int64 {
var count int64
s.getBoltDB(gorush.AndroidErrorKey, &count)
s.getBoltDB(AndroidErrorKey, &count)
return count
}

View File

@@ -1,7 +1,7 @@
package boltdb
import (
"github.com/appleboy/gorush/gorush"
c "github.com/appleboy/gorush/gorush/config"
"github.com/stretchr/testify/assert"
"testing"
)
@@ -9,32 +9,32 @@ import (
func TestRedisEngine(t *testing.T) {
var val int64
config := gorush.BuildDefaultPushConf()
config := c.BuildDefaultPushConf()
boltDB := New(config, gorush.StatusApp{})
boltDB.initBoltDB()
boltDB.resetBoltDB()
boltDB := New(config)
boltDB.Init()
boltDB.Reset()
boltDB.addTotalCount(10)
val = boltDB.getTotalCount()
boltDB.AddTotalCount(10)
val = boltDB.GetTotalCount()
assert.Equal(t, int64(10), val)
boltDB.addTotalCount(10)
val = boltDB.getTotalCount()
boltDB.AddTotalCount(10)
val = boltDB.GetTotalCount()
assert.Equal(t, int64(20), val)
boltDB.addIosSuccess(20)
val = boltDB.getIosSuccess()
boltDB.AddIosSuccess(20)
val = boltDB.GetIosSuccess()
assert.Equal(t, int64(20), val)
boltDB.addIosError(30)
val = boltDB.getIosError()
boltDB.AddIosError(30)
val = boltDB.GetIosError()
assert.Equal(t, int64(30), val)
boltDB.addAndroidSuccess(40)
val = boltDB.getAndroidSuccess()
boltDB.AddAndroidSuccess(40)
val = boltDB.GetAndroidSuccess()
assert.Equal(t, int64(40), val)
boltDB.addAndroidError(50)
val = boltDB.getAndroidError()
boltDB.AddAndroidError(50)
val = boltDB.GetAndroidError()
assert.Equal(t, int64(50), val)
}

View File

@@ -25,7 +25,7 @@ func New(config config.ConfYaml) *Storage {
}
}
func getRedisInt64Result(key string, count *int64) {
func getInt64(key string, count *int64) {
val, _ := RedisClient.Get(key).Result()
*count, _ = strconv.ParseInt(val, 10, 64)
}
@@ -88,35 +88,35 @@ func (s *Storage) AddAndroidError(count int64) {
func (s *Storage) GetTotalCount() int64 {
var count int64
getRedisInt64Result(TotalCountKey, &count)
getInt64(TotalCountKey, &count)
return count
}
func (s *Storage) GetIosSuccess() int64 {
var count int64
getRedisInt64Result(IosSuccessKey, &count)
getInt64(IosSuccessKey, &count)
return count
}
func (s *Storage) GetIosError() int64 {
var count int64
getRedisInt64Result(IosErrorKey, &count)
getInt64(IosErrorKey, &count)
return count
}
func (s *Storage) GetAndroidSuccess() int64 {
var count int64
getRedisInt64Result(AndroidSuccessKey, &count)
getInt64(AndroidSuccessKey, &count)
return count
}
func (s *Storage) GetAndroidError() int64 {
var count int64
getRedisInt64Result(AndroidErrorKey, &count)
getInt64(AndroidErrorKey, &count)
return count
}