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)
|
2021-08-02 06:07:30 +00:00
|
|
|
func New(config *config.ConfYaml) *Storage {
|
2016-08-02 07:35:28 +00:00
|
|
|
return &Storage{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storage is interface structure
|
|
|
|
type Storage struct {
|
2021-08-02 06:07:30 +00:00
|
|
|
config *config.ConfYaml
|
2020-04-23 07:39:24 +00:00
|
|
|
db *buntdb.DB
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init client storage.
|
|
|
|
func (s *Storage) Init() error {
|
2020-04-23 07:39:24 +00:00
|
|
|
var err error
|
|
|
|
s.db, err = buntdb.Open(s.config.Stat.BuntDB.Path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the storage connection
|
|
|
|
func (s *Storage) Close() error {
|
|
|
|
if s.db == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.db.Close()
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2020-09-04 03:01:21 +00:00
|
|
|
s.setBuntDB(storage.HuaweiSuccessKey, 0)
|
|
|
|
s.setBuntDB(storage.HuaweiErrorKey, 0)
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) setBuntDB(key string, count int64) {
|
2020-04-23 07:39:24 +00:00
|
|
|
err := s.db.Update(func(tx *buntdb.Tx) error {
|
2017-08-24 03:25:05 +00:00
|
|
|
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())
|
|
|
|
}
|
2016-08-02 07:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) getBuntDB(key string, count *int64) {
|
2020-04-23 07:39:24 +00:00
|
|
|
err := s.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())
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-09-04 03:01:21 +00:00
|
|
|
// AddHuaweiSuccess record counts of success Huawei push notification.
|
|
|
|
func (s *Storage) AddHuaweiSuccess(count int64) {
|
|
|
|
total := s.GetHuaweiSuccess() + count
|
|
|
|
s.setBuntDB(storage.HuaweiSuccessKey, total)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddHuaweiError record counts of error Huawei push notification.
|
|
|
|
func (s *Storage) AddHuaweiError(count int64) {
|
|
|
|
total := s.GetHuaweiError() + count
|
|
|
|
s.setBuntDB(storage.HuaweiErrorKey, 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
|
|
|
|
}
|
2020-09-04 03:01:21 +00:00
|
|
|
|
|
|
|
// GetHuaweiSuccess show success counts of Huawei notification.
|
|
|
|
func (s *Storage) GetHuaweiSuccess() int64 {
|
|
|
|
var count int64
|
|
|
|
s.getBuntDB(storage.HuaweiSuccessKey, &count)
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHuaweiError show error counts of Huawei notification.
|
|
|
|
func (s *Storage) GetHuaweiError() int64 {
|
|
|
|
var count int64
|
|
|
|
s.getBuntDB(storage.HuaweiErrorKey, &count)
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|