2016-08-02 07:42:46 +00:00
|
|
|
package buntdb
|
2016-08-02 07:35:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-24 03:25:05 +00:00
|
|
|
"log"
|
2017-01-19 09:08:12 +00:00
|
|
|
"strconv"
|
|
|
|
|
2016-08-02 07:35:28 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2017-06-24 16:48:48 +00:00
|
|
|
"github.com/appleboy/gorush/storage"
|
2016-08-02 07:35:28 +00:00
|
|
|
|
2017-06-24 16:48:48 +00:00
|
|
|
"github.com/tidwall/buntdb"
|
2016-08-02 07:35:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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() {
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBuntDB(storage.TotalCountKey, 0)
|
|
|
|
s.setBuntDB(storage.IosSuccessKey, 0)
|
|
|
|
s.setBuntDB(storage.IosErrorKey, 0)
|
|
|
|
s.setBuntDB(storage.AndroidSuccessKey, 0)
|
|
|
|
s.setBuntDB(storage.AndroidErrorKey, 0)
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) setBuntDB(key string, count int64) {
|
|
|
|
db, _ := buntdb.Open(s.config.Stat.BuntDB.Path)
|
|
|
|
|
2017-08-24 03:25:05 +00:00
|
|
|
err := db.Update(func(tx *buntdb.Tx) error {
|
|
|
|
if _, _, err := tx.Set(key, fmt.Sprintf("%d", count), nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-02 07:35:28 +00:00
|
|
|
return nil
|
|
|
|
})
|
2017-08-24 03:25:05 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("BuntDB update error:", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := db.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("BuntDB error:", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) getBuntDB(key string, count *int64) {
|
|
|
|
db, _ := buntdb.Open(s.config.Stat.BuntDB.Path)
|
|
|
|
|
2017-08-24 03:25:05 +00:00
|
|
|
err := db.View(func(tx *buntdb.Tx) error {
|
2016-08-02 07:35:28 +00:00
|
|
|
val, _ := tx.Get(key)
|
|
|
|
*count, _ = strconv.ParseInt(val, 10, 64)
|
|
|
|
return nil
|
|
|
|
})
|
2017-08-24 03:25:05 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println("BuntDB get error:", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := db.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("BuntDB error:", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddTotalCount record push notification count.
|
|
|
|
func (s *Storage) AddTotalCount(count int64) {
|
|
|
|
total := s.GetTotalCount() + count
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBuntDB(storage.TotalCountKey, total)
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddIosSuccess record counts of success iOS push notification.
|
|
|
|
func (s *Storage) AddIosSuccess(count int64) {
|
|
|
|
total := s.GetIosSuccess() + count
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBuntDB(storage.IosSuccessKey, total)
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddIosError record counts of error iOS push notification.
|
|
|
|
func (s *Storage) AddIosError(count int64) {
|
|
|
|
total := s.GetIosError() + count
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBuntDB(storage.IosErrorKey, total)
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddAndroidSuccess record counts of success Android push notification.
|
|
|
|
func (s *Storage) AddAndroidSuccess(count int64) {
|
|
|
|
total := s.GetAndroidSuccess() + count
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBuntDB(storage.AndroidSuccessKey, total)
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddAndroidError record counts of error Android push notification.
|
|
|
|
func (s *Storage) AddAndroidError(count int64) {
|
|
|
|
total := s.GetAndroidError() + count
|
2017-06-24 16:48:48 +00:00
|
|
|
s.setBuntDB(storage.AndroidErrorKey, total)
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTotalCount show counts of all notification.
|
|
|
|
func (s *Storage) GetTotalCount() int64 {
|
|
|
|
var count int64
|
2017-06-24 16:48:48 +00:00
|
|
|
s.getBuntDB(storage.TotalCountKey, &count)
|
2016-08-02 07:35:28 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIosSuccess show success counts of iOS notification.
|
|
|
|
func (s *Storage) GetIosSuccess() int64 {
|
|
|
|
var count int64
|
2017-06-24 16:48:48 +00:00
|
|
|
s.getBuntDB(storage.IosSuccessKey, &count)
|
2016-08-02 07:35:28 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIosError show error counts of iOS notification.
|
|
|
|
func (s *Storage) GetIosError() int64 {
|
|
|
|
var count int64
|
2017-06-24 16:48:48 +00:00
|
|
|
s.getBuntDB(storage.IosErrorKey, &count)
|
2016-08-02 07:35:28 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAndroidSuccess show success counts of Android notification.
|
|
|
|
func (s *Storage) GetAndroidSuccess() int64 {
|
|
|
|
var count int64
|
2017-06-24 16:48:48 +00:00
|
|
|
s.getBuntDB(storage.AndroidSuccessKey, &count)
|
2016-08-02 07:35:28 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAndroidError show error counts of Android notification.
|
|
|
|
func (s *Storage) GetAndroidError() int64 {
|
|
|
|
var count int64
|
2017-06-24 16:48:48 +00:00
|
|
|
s.getBuntDB(storage.AndroidErrorKey, &count)
|
2016-08-02 07:35:28 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|