Fixed #70 Support boltDB interface.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
1ccd81bbd0
commit
17c4ca4b2e
|
@ -0,0 +1,107 @@
|
|||
package boltdb
|
||||
|
||||
import (
|
||||
"github.com/appleboy/gorush/gorush"
|
||||
"github.com/asdine/storm"
|
||||
)
|
||||
|
||||
// Storage implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
||||
func New(config gorush.ConfYaml, stat gorush.StatusApp) *Storage {
|
||||
return &Storage{
|
||||
stat: stat,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
type Storage struct {
|
||||
config gorush.ConfYaml
|
||||
stat gorush.StatusApp
|
||||
}
|
||||
|
||||
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) 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) setBoltDB(key string, count int64) {
|
||||
db, _ := storm.Open(s.config.Stat.BoltDB.Path)
|
||||
db.Set(s.config.Stat.BoltDB.Bucket, key, count)
|
||||
defer db.Close()
|
||||
}
|
||||
|
||||
func (s *Storage) getBoltDB(key string, count *int64) {
|
||||
db, _ := storm.Open(s.config.Stat.BoltDB.Path)
|
||||
db.Get(s.config.Stat.BoltDB.Bucket, key, count)
|
||||
defer db.Close()
|
||||
}
|
||||
|
||||
func (s *Storage) addTotalCount(count int64) {
|
||||
total := s.getTotalCount() + count
|
||||
s.setBoltDB(gorush.TotalCountKey, total)
|
||||
}
|
||||
|
||||
func (s *Storage) addIosSuccess(count int64) {
|
||||
total := s.getIosSuccess() + count
|
||||
s.setBoltDB(gorush.IosSuccessKey, total)
|
||||
}
|
||||
|
||||
func (s *Storage) addIosError(count int64) {
|
||||
total := s.getIosError() + count
|
||||
s.setBoltDB(gorush.IosErrorKey, total)
|
||||
}
|
||||
|
||||
func (s *Storage) addAndroidSuccess(count int64) {
|
||||
total := s.getAndroidSuccess() + count
|
||||
s.setBoltDB(gorush.AndroidSuccessKey, total)
|
||||
}
|
||||
|
||||
func (s *Storage) addAndroidError(count int64) {
|
||||
total := s.getAndroidError() + count
|
||||
s.setBoltDB(gorush.AndroidErrorKey, total)
|
||||
}
|
||||
|
||||
func (s *Storage) getTotalCount() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(gorush.TotalCountKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func (s *Storage) getIosSuccess() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(gorush.IosSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func (s *Storage) getIosError() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(gorush.IosErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func (s *Storage) getAndroidSuccess() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(gorush.AndroidSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func (s *Storage) getAndroidError() int64 {
|
||||
var count int64
|
||||
s.getBoltDB(gorush.AndroidErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package boltdb
|
||||
|
||||
import (
|
||||
"github.com/appleboy/gorush/gorush"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRedisEngine(t *testing.T) {
|
||||
var val int64
|
||||
|
||||
config := gorush.BuildDefaultPushConf()
|
||||
|
||||
boltDB := New(config, gorush.StatusApp{})
|
||||
boltDB.initBoltDB()
|
||||
boltDB.resetBoltDB()
|
||||
|
||||
boltDB.addTotalCount(10)
|
||||
val = boltDB.getTotalCount()
|
||||
assert.Equal(t, int64(10), val)
|
||||
boltDB.addTotalCount(10)
|
||||
val = boltDB.getTotalCount()
|
||||
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)
|
||||
}
|
Loading…
Reference in New Issue