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"
|
2022-02-03 09:03:37 +00:00
|
|
|
"sync"
|
2017-01-19 09:08:12 +00:00
|
|
|
|
2016-08-02 07:35:28 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
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
|
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.setBuntDB(key, s.getBuntDB(key)+count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) Set(key string, count int64) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
s.setBuntDB(key, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) Get(key string) int64 {
|
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
|
|
|
return s.getBuntDB(key)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-03 09:03:37 +00:00
|
|
|
func (s *Storage) getBuntDB(key string) int64 {
|
|
|
|
var 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)
|
2022-02-03 09:03:37 +00:00
|
|
|
count, _ = strconv.ParseInt(val, 10, 64)
|
2016-08-02 07:35:28 +00:00
|
|
|
return nil
|
|
|
|
})
|
2017-08-24 03:25:05 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("BuntDB get error:", err.Error())
|
|
|
|
}
|
2020-09-04 03:01:21 +00:00
|
|
|
|
|
|
|
return count
|
|
|
|
}
|