forked from appleboy/gorush
-
Notifications
You must be signed in to change notification settings - Fork 1
/
badger.go
197 lines (160 loc) · 4.51 KB
/
badger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package badger
import (
"fmt"
"log"
"os"
"strconv"
"github.com/miczone/gorush/config"
"github.com/miczone/gorush/storage"
"github.com/dgraph-io/badger/v3"
)
// New func implements the storage interface for gorush (https://github.com/miczone/gorush)
func New(config config.ConfYaml) *Storage {
return &Storage{
config: config,
}
}
// Storage is interface structure
type Storage struct {
config config.ConfYaml
opts badger.Options
name string
db *badger.DB
}
// Init client storage.
func (s *Storage) Init() error {
var err error
s.name = "badger"
dbPath := s.config.Stat.BadgerDB.Path
if dbPath == "" {
dbPath = os.TempDir() + "badger"
}
s.opts = badger.DefaultOptions(dbPath)
s.db, err = badger.Open(s.opts)
return err
}
// Close the storage connection
func (s *Storage) Close() error {
if s.db == nil {
return nil
}
return s.db.Close()
}
// Reset Client storage.
func (s *Storage) Reset() {
s.setBadger(storage.TotalCountKey, 0)
s.setBadger(storage.IosSuccessKey, 0)
s.setBadger(storage.IosErrorKey, 0)
s.setBadger(storage.AndroidSuccessKey, 0)
s.setBadger(storage.AndroidErrorKey, 0)
s.setBadger(storage.HuaweiSuccessKey, 0)
s.setBadger(storage.HuaweiErrorKey, 0)
}
func (s *Storage) setBadger(key string, count int64) {
err := s.db.Update(func(txn *badger.Txn) error {
value := strconv.FormatInt(count, 10)
return txn.Set([]byte(key), []byte(value))
})
if err != nil {
log.Println(s.name, "update error:", err.Error())
}
}
func (s *Storage) getBadger(key string, count *int64) {
err := s.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(key))
if err != nil {
return err
}
dst := []byte{}
val, err := item.ValueCopy(dst)
if err != nil {
return err
}
i, err := strconv.ParseInt(fmt.Sprintf("%s", val), 10, 64)
if err != nil {
return err
}
*count = i
return nil
})
if err != nil {
log.Println(s.name, "get error:", err.Error())
}
}
// AddTotalCount record push notification count.
func (s *Storage) AddTotalCount(count int64) {
total := s.GetTotalCount() + count
s.setBadger(storage.TotalCountKey, total)
}
// AddIosSuccess record counts of success iOS push notification.
func (s *Storage) AddIosSuccess(count int64) {
total := s.GetIosSuccess() + count
s.setBadger(storage.IosSuccessKey, total)
}
// AddIosError record counts of error iOS push notification.
func (s *Storage) AddIosError(count int64) {
total := s.GetIosError() + count
s.setBadger(storage.IosErrorKey, total)
}
// AddAndroidSuccess record counts of success Android push notification.
func (s *Storage) AddAndroidSuccess(count int64) {
total := s.GetAndroidSuccess() + count
s.setBadger(storage.AndroidSuccessKey, total)
}
// AddAndroidError record counts of error Android push notification.
func (s *Storage) AddAndroidError(count int64) {
total := s.GetAndroidError() + count
s.setBadger(storage.AndroidErrorKey, total)
}
// AddHuaweiSuccess record counts of success Huawei push notification.
func (s *Storage) AddHuaweiSuccess(count int64) {
total := s.GetHuaweiSuccess() + count
s.setBadger(storage.HuaweiSuccessKey, total)
}
// AddHuaweiError record counts of error Huawei push notification.
func (s *Storage) AddHuaweiError(count int64) {
total := s.GetHuaweiError() + count
s.setBadger(storage.HuaweiErrorKey, total)
}
// GetTotalCount show counts of all notification.
func (s *Storage) GetTotalCount() int64 {
var count int64
s.getBadger(storage.TotalCountKey, &count)
return count
}
// GetIosSuccess show success counts of iOS notification.
func (s *Storage) GetIosSuccess() int64 {
var count int64
s.getBadger(storage.IosSuccessKey, &count)
return count
}
// GetIosError show error counts of iOS notification.
func (s *Storage) GetIosError() int64 {
var count int64
s.getBadger(storage.IosErrorKey, &count)
return count
}
// GetAndroidSuccess show success counts of Android notification.
func (s *Storage) GetAndroidSuccess() int64 {
var count int64
s.getBadger(storage.AndroidSuccessKey, &count)
return count
}
// GetAndroidError show error counts of Android notification.
func (s *Storage) GetAndroidError() int64 {
var count int64
s.getBadger(storage.AndroidErrorKey, &count)
return count
}
// GetHuaweiSuccess show success counts of Huawei notification.
func (s *Storage) GetHuaweiSuccess() int64 {
var count int64
s.getBadger(storage.HuaweiSuccessKey, &count)
return count
}
// GetHuaweiError show error counts of Huawei notification.
func (s *Storage) GetHuaweiError() int64 {
var count int64
s.getBadger(storage.HuaweiErrorKey, &count)
return count
}