2018-04-16 09:26:15 +00:00
|
|
|
package badger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2022-02-03 09:03:37 +00:00
|
|
|
"sync"
|
2018-04-16 09:26:15 +00:00
|
|
|
|
|
|
|
"github.com/appleboy/gorush/config"
|
2021-07-11 05:49:56 +00:00
|
|
|
"github.com/dgraph-io/badger/v3"
|
2018-04-16 09:26:15 +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 {
|
2018-04-16 09:26:15 +00:00
|
|
|
return &Storage{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storage is interface structure
|
|
|
|
type Storage struct {
|
2021-08-02 06:07:30 +00:00
|
|
|
config *config.ConfYaml
|
2018-04-16 09:26:15 +00:00
|
|
|
opts badger.Options
|
|
|
|
name string
|
2020-04-23 07:39:24 +00:00
|
|
|
db *badger.DB
|
2022-02-03 09:03:37 +00:00
|
|
|
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) Add(key string, count int64) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
s.setBadger(key, s.getBadger(key)+count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) Set(key string, count int64) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
s.setBadger(key, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) Get(key string) int64 {
|
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
|
|
|
return s.getBadger(key)
|
2018-04-16 09:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init client storage.
|
|
|
|
func (s *Storage) Init() error {
|
2020-04-23 07:39:24 +00:00
|
|
|
var err error
|
2019-04-13 14:39:33 +00:00
|
|
|
s.name = "badger"
|
2020-04-23 02:34:35 +00:00
|
|
|
dbPath := s.config.Stat.BadgerDB.Path
|
|
|
|
if dbPath == "" {
|
|
|
|
dbPath = os.TempDir() + "badger"
|
|
|
|
}
|
|
|
|
s.opts = badger.DefaultOptions(dbPath)
|
|
|
|
|
2020-04-23 07:39:24 +00:00
|
|
|
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()
|
2018-04-16 09:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) setBadger(key string, count int64) {
|
2020-04-23 07:39:24 +00:00
|
|
|
err := s.db.Update(func(txn *badger.Txn) error {
|
2021-03-27 05:51:51 +00:00
|
|
|
value := strconv.FormatInt(count, 10)
|
2018-04-16 09:26:15 +00:00
|
|
|
return txn.Set([]byte(key), []byte(value))
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(s.name, "update error:", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
func (s *Storage) getBadger(key string) int64 {
|
|
|
|
var count int64
|
2020-04-23 07:39:24 +00:00
|
|
|
err := s.db.View(func(txn *badger.Txn) error {
|
2018-04-16 09:26:15 +00:00
|
|
|
item, err := txn.Get([]byte(key))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-03 09:03:37 +00:00
|
|
|
var dst []byte
|
2019-04-13 14:39:33 +00:00
|
|
|
val, err := item.ValueCopy(dst)
|
2018-04-16 09:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
count, err = strconv.ParseInt(string(val), 10, 64)
|
2018-04-16 09:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(s.name, "get error:", err.Error())
|
|
|
|
}
|
2020-09-04 03:01:21 +00:00
|
|
|
return count
|
|
|
|
}
|