134
storage/buntdb/buntdb.go
Normal file
134
storage/buntdb/buntdb.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package boltdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/tidwall/buntdb"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 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"
|
||||
)
|
||||
|
||||
// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
|
||||
func New(config config.ConfYaml) *Storage {
|
||||
return &Storage{
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
// Storage is interface structure
|
||||
type Storage struct {
|
||||
config config.ConfYaml
|
||||
}
|
||||
|
||||
// Init client storage.
|
||||
func (s *Storage) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset Client storage.
|
||||
func (s *Storage) Reset() {
|
||||
s.setBuntDB(TotalCountKey, 0)
|
||||
s.setBuntDB(IosSuccessKey, 0)
|
||||
s.setBuntDB(IosErrorKey, 0)
|
||||
s.setBuntDB(AndroidSuccessKey, 0)
|
||||
s.setBuntDB(AndroidErrorKey, 0)
|
||||
}
|
||||
|
||||
func (s *Storage) setBuntDB(key string, count int64) {
|
||||
db, _ := buntdb.Open(s.config.Stat.BuntDB.Path)
|
||||
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set(key, fmt.Sprintf("%d", count), nil)
|
||||
return nil
|
||||
})
|
||||
defer db.Close()
|
||||
}
|
||||
|
||||
func (s *Storage) getBuntDB(key string, count *int64) {
|
||||
db, _ := buntdb.Open(s.config.Stat.BuntDB.Path)
|
||||
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
val, _ := tx.Get(key)
|
||||
*count, _ = strconv.ParseInt(val, 10, 64)
|
||||
return nil
|
||||
})
|
||||
defer db.Close()
|
||||
}
|
||||
|
||||
// AddTotalCount record push notification count.
|
||||
func (s *Storage) AddTotalCount(count int64) {
|
||||
total := s.GetTotalCount() + count
|
||||
s.setBuntDB(TotalCountKey, total)
|
||||
}
|
||||
|
||||
// AddIosSuccess record counts of success iOS push notification.
|
||||
func (s *Storage) AddIosSuccess(count int64) {
|
||||
total := s.GetIosSuccess() + count
|
||||
s.setBuntDB(IosSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddIosError record counts of error iOS push notification.
|
||||
func (s *Storage) AddIosError(count int64) {
|
||||
total := s.GetIosError() + count
|
||||
s.setBuntDB(IosErrorKey, total)
|
||||
}
|
||||
|
||||
// AddAndroidSuccess record counts of success Android push notification.
|
||||
func (s *Storage) AddAndroidSuccess(count int64) {
|
||||
total := s.GetAndroidSuccess() + count
|
||||
s.setBuntDB(AndroidSuccessKey, total)
|
||||
}
|
||||
|
||||
// AddAndroidError record counts of error Android push notification.
|
||||
func (s *Storage) AddAndroidError(count int64) {
|
||||
total := s.GetAndroidError() + count
|
||||
s.setBuntDB(AndroidErrorKey, total)
|
||||
}
|
||||
|
||||
// GetTotalCount show counts of all notification.
|
||||
func (s *Storage) GetTotalCount() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(TotalCountKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosSuccess show success counts of iOS notification.
|
||||
func (s *Storage) GetIosSuccess() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(IosSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetIosError show error counts of iOS notification.
|
||||
func (s *Storage) GetIosError() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(IosErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidSuccess show success counts of Android notification.
|
||||
func (s *Storage) GetAndroidSuccess() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(AndroidSuccessKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// GetAndroidError show error counts of Android notification.
|
||||
func (s *Storage) GetAndroidError() int64 {
|
||||
var count int64
|
||||
s.getBuntDB(AndroidErrorKey, &count)
|
||||
|
||||
return count
|
||||
}
|
||||
40
storage/buntdb/buntdb_test.go
Normal file
40
storage/buntdb/buntdb_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package boltdb
|
||||
|
||||
import (
|
||||
c "github.com/appleboy/gorush/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuntDBEngine(t *testing.T) {
|
||||
var val int64
|
||||
|
||||
config := c.BuildDefaultPushConf()
|
||||
|
||||
boltDB := New(config)
|
||||
boltDB.Init()
|
||||
boltDB.Reset()
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user