Skip to content

Commit

Permalink
support LevelDB key/value database.
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Sep 19, 2016
1 parent 2fba9b3 commit bf56f59
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 16 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ build_static:
build: clean
sh script/build.sh $(VERSION)

test: redis_test boltdb_test memory_test config_test
test: redis_test boltdb_test memory_test buntdb_test leveldb_test config_test
go test -v -cover ./gorush/...

redis_test: init
Expand All @@ -55,6 +55,9 @@ memory_test: init
buntdb_test: init
go test -v -cover ./storage/buntdb/...

leveldb_test: init
go test -v -cover ./storage/leveldb/...

config_test: init
go test -v -cover ./config/...

Expand Down
15 changes: 11 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ type SectionLog struct {

// SectionStat is sub seciont of config.
type SectionStat struct {
Engine string `yaml:"engine"`
Redis SectionRedis `yaml:"redis"`
BoltDB SectionBoltDB `yaml:"boltdb"`
BuntDB SectionBuntDB `yaml:"buntdb"`
Engine string `yaml:"engine"`
Redis SectionRedis `yaml:"redis"`
BoltDB SectionBoltDB `yaml:"boltdb"`
BuntDB SectionBuntDB `yaml:"buntdb"`
LevelDB SectionLevelDB `yaml:"leveldb"`
}

// SectionRedis is sub seciont of config.
Expand All @@ -89,6 +90,11 @@ type SectionBuntDB struct {
Path string `yaml:"path"`
}

// SectionLevelDB is sub seciont of config.
type SectionLevelDB struct {
Path string `yaml:"path"`
}

// SectionPID is sub seciont of config.
type SectionPID struct {
Enabled bool `yaml:"enabled"`
Expand Down Expand Up @@ -148,6 +154,7 @@ func BuildDefaultPushConf() ConfYaml {
conf.Stat.BoltDB.Bucket = "gorush"

conf.Stat.BuntDB.Path = "gorush.db"
conf.Stat.LevelDB.Path = "gorush.db"

return conf
}
Expand Down
2 changes: 2 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ stat:
bucket: "gorush"
buntdb:
path: "gorush.db"
leveldb:
path: "gorush.db"
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
assert.Equal(suite.T(), "gorush", suite.ConfGorushDefault.Stat.BoltDB.Bucket)

assert.Equal(suite.T(), "gorush.db", suite.ConfGorushDefault.Stat.BuntDB.Path)
assert.Equal(suite.T(), "gorush.db", suite.ConfGorushDefault.Stat.LevelDB.Path)
}

func (suite *ConfigTestSuite) TestValidateConf() {
Expand Down Expand Up @@ -154,6 +155,7 @@ func (suite *ConfigTestSuite) TestValidateConf() {
assert.Equal(suite.T(), "gorush", suite.ConfGorush.Stat.BoltDB.Bucket)

assert.Equal(suite.T(), "gorush.db", suite.ConfGorush.Stat.BuntDB.Path)
assert.Equal(suite.T(), "gorush.db", suite.ConfGorush.Stat.LevelDB.Path)
}

func TestConfigTestSuite(t *testing.T) {
Expand Down
24 changes: 21 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ import:
- package: github.com/buger/jsonparser
- package: github.com/thoas/stats
- package: github.com/tidwall/buntdb
- package: github.com/syndtr/goleveldb
22 changes: 14 additions & 8 deletions gorush/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gorush

import (
"github.com/appleboy/gorush/storage/boltdb"
"github.com/appleboy/gorush/storage/buntdb"
"github.com/appleboy/gorush/storage/leveldb"
"github.com/appleboy/gorush/storage/memory"
"github.com/appleboy/gorush/storage/redis"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -40,20 +42,24 @@ func InitAppStatus() error {
StatStorage = memory.New()
case "redis":
StatStorage = redis.New(PushConf)
err := StatStorage.Init()

if err != nil {
LogError.Error("redis error: " + err.Error())

return err
}

case "boltdb":
StatStorage = boltdb.New(PushConf)
case "buntdb":
StatStorage = buntdb.New(PushConf)
case "leveldb":
StatStorage = leveldb.New(PushConf)
default:
StatStorage = memory.New()
}

err := StatStorage.Init()

if err != nil {
LogError.Error("storage error: " + err.Error())

return err
}

return nil
}

Expand Down
134 changes: 134 additions & 0 deletions storage/leveldb/leveldb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package leveldb

import (
"fmt"
"github.com/appleboy/gorush/config"
"github.com/syndtr/goleveldb/leveldb"
"strconv"
)

// Stat variable for redis
const (
TotalCountKey = "gorush-total-count"
IosSuccessKey = "gorush-ios-success-count"
IosErrorKey = "gorush-ios-error-count"
AndroidSuccessKey = "gorush-android-success-count"
AndroidErrorKey = "gorush-android-error-count"
)

var dbPath string

func setLevelDB(key string, count int64) {
db, _ := leveldb.OpenFile(dbPath, nil)
value := fmt.Sprintf("%d", count)

_ = db.Put([]byte(key), []byte(value), nil)

defer db.Close()
}

func getLevelDB(key string, count *int64) {
db, _ := leveldb.OpenFile(dbPath, nil)

data, _ := db.Get([]byte(key), nil)
*count, _ = strconv.ParseInt(string(data), 10, 64)

defer db.Close()
}

// 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 {
dbPath = s.config.Stat.LevelDB.Path
return nil
}

// Reset Client storage.
func (s *Storage) Reset() {
setLevelDB(TotalCountKey, 0)
setLevelDB(IosSuccessKey, 0)
setLevelDB(IosErrorKey, 0)
setLevelDB(AndroidSuccessKey, 0)
setLevelDB(AndroidErrorKey, 0)
}

// AddTotalCount record push notification count.
func (s *Storage) AddTotalCount(count int64) {
total := s.GetTotalCount() + count
setLevelDB(TotalCountKey, total)
}

// AddIosSuccess record counts of success iOS push notification.
func (s *Storage) AddIosSuccess(count int64) {
total := s.GetIosSuccess() + count
setLevelDB(IosSuccessKey, total)
}

// AddIosError record counts of error iOS push notification.
func (s *Storage) AddIosError(count int64) {
total := s.GetIosError() + count
setLevelDB(IosErrorKey, total)
}

// AddAndroidSuccess record counts of success Android push notification.
func (s *Storage) AddAndroidSuccess(count int64) {
total := s.GetAndroidSuccess() + count
setLevelDB(AndroidSuccessKey, total)
}

// AddAndroidError record counts of error Android push notification.
func (s *Storage) AddAndroidError(count int64) {
total := s.GetAndroidError() + count
setLevelDB(AndroidErrorKey, total)
}

// GetTotalCount show counts of all notification.
func (s *Storage) GetTotalCount() int64 {
var count int64
getLevelDB(TotalCountKey, &count)

return count
}

// GetIosSuccess show success counts of iOS notification.
func (s *Storage) GetIosSuccess() int64 {
var count int64
getLevelDB(IosSuccessKey, &count)

return count
}

// GetIosError show error counts of iOS notification.
func (s *Storage) GetIosError() int64 {
var count int64
getLevelDB(IosErrorKey, &count)

return count
}

// GetAndroidSuccess show success counts of Android notification.
func (s *Storage) GetAndroidSuccess() int64 {
var count int64
getLevelDB(AndroidSuccessKey, &count)

return count
}

// GetAndroidError show error counts of Android notification.
func (s *Storage) GetAndroidError() int64 {
var count int64
getLevelDB(AndroidErrorKey, &count)

return count
}
49 changes: 49 additions & 0 deletions storage/leveldb/leveldb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package leveldb

import (
c "github.com/appleboy/gorush/config"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func TestLevelDBEngine(t *testing.T) {
var val int64

config := c.BuildDefaultPushConf()

if _, err := os.Stat(config.Stat.LevelDB.Path); os.IsNotExist(err) {
os.RemoveAll(config.Stat.LevelDB.Path)
}

levelDB := New(config)
levelDB.Init()
levelDB.Reset()

levelDB.AddTotalCount(10)
val = levelDB.GetTotalCount()
assert.Equal(t, int64(10), val)
levelDB.AddTotalCount(10)
val = levelDB.GetTotalCount()
assert.Equal(t, int64(20), val)

levelDB.AddIosSuccess(20)
val = levelDB.GetIosSuccess()
assert.Equal(t, int64(20), val)

levelDB.AddIosError(30)
val = levelDB.GetIosError()
assert.Equal(t, int64(30), val)

levelDB.AddAndroidSuccess(40)
val = levelDB.GetAndroidSuccess()
assert.Equal(t, int64(40), val)

levelDB.AddAndroidError(50)
val = levelDB.GetAndroidError()
assert.Equal(t, int64(50), val)

levelDB.Reset()
val = levelDB.GetAndroidError()
assert.Equal(t, int64(0), val)
}

0 comments on commit bf56f59

Please sign in to comment.