Merge pull request #132 from appleboy/leveldb

support LevelDB key/value database.
This commit is contained in:
Bo-Yi Wu 2016-09-19 22:08:13 +08:00 committed by GitHub
commit f73fff9eef
12 changed files with 302 additions and 35 deletions

1
.gitignore vendored
View File

@ -36,3 +36,4 @@ vendor
Dockerfile.tmp Dockerfile.tmp
.cover .cover
*.pid *.pid
*.db*

View File

@ -40,7 +40,7 @@ build_static:
build: clean build: clean
sh script/build.sh $(VERSION) 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/... go test -v -cover ./gorush/...
redis_test: init redis_test: init
@ -55,6 +55,9 @@ memory_test: init
buntdb_test: init buntdb_test: init
go test -v -cover ./storage/buntdb/... go test -v -cover ./storage/buntdb/...
leveldb_test: init
go test -v -cover ./storage/leveldb/...
config_test: init config_test: init
go test -v -cover ./config/... go test -v -cover ./config/...

View File

@ -65,17 +65,18 @@ type SectionLog struct {
// SectionStat is sub seciont of config. // SectionStat is sub seciont of config.
type SectionStat struct { type SectionStat struct {
Engine string `yaml:"engine"` Engine string `yaml:"engine"`
Redis SectionRedis `yaml:"redis"` Redis SectionRedis `yaml:"redis"`
BoltDB SectionBoltDB `yaml:"boltdb"` BoltDB SectionBoltDB `yaml:"boltdb"`
BuntDB SectionBuntDB `yaml:"buntdb"` BuntDB SectionBuntDB `yaml:"buntdb"`
LevelDB SectionLevelDB `yaml:"leveldb"`
} }
// SectionRedis is sub seciont of config. // SectionRedis is sub seciont of config.
type SectionRedis struct { type SectionRedis struct {
Addr string `yaml:"addr"` Addr string `yaml:"addr"`
Password string `yaml:"password"` Password string `yaml:"password"`
DB int64 `yaml:"db"` DB int `yaml:"db"`
} }
// SectionBoltDB is sub seciont of config. // SectionBoltDB is sub seciont of config.
@ -89,6 +90,11 @@ type SectionBuntDB struct {
Path string `yaml:"path"` Path string `yaml:"path"`
} }
// SectionLevelDB is sub seciont of config.
type SectionLevelDB struct {
Path string `yaml:"path"`
}
// SectionPID is sub seciont of config. // SectionPID is sub seciont of config.
type SectionPID struct { type SectionPID struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"enabled"`
@ -144,10 +150,11 @@ func BuildDefaultPushConf() ConfYaml {
conf.Stat.Redis.Password = "" conf.Stat.Redis.Password = ""
conf.Stat.Redis.DB = 0 conf.Stat.Redis.DB = 0
conf.Stat.BoltDB.Path = "gorush.db" conf.Stat.BoltDB.Path = "bolt.db"
conf.Stat.BoltDB.Bucket = "gorush" conf.Stat.BoltDB.Bucket = "gorush"
conf.Stat.BuntDB.Path = "gorush.db" conf.Stat.BuntDB.Path = "bunt.db"
conf.Stat.LevelDB.Path = "level.db"
return conf return conf
} }

View File

@ -45,7 +45,9 @@ stat:
password: "" password: ""
db: 0 db: 0
boltdb: boltdb:
path: "gorush.db" path: "bolt.db"
bucket: "gorush" bucket: "gorush"
buntdb: buntdb:
path: "gorush.db" path: "bunt.db"
leveldb:
path: "level.db"

View File

@ -96,12 +96,13 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
assert.Equal(suite.T(), "memory", suite.ConfGorushDefault.Stat.Engine) assert.Equal(suite.T(), "memory", suite.ConfGorushDefault.Stat.Engine)
assert.Equal(suite.T(), "localhost:6379", suite.ConfGorushDefault.Stat.Redis.Addr) assert.Equal(suite.T(), "localhost:6379", suite.ConfGorushDefault.Stat.Redis.Addr)
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Stat.Redis.Password) assert.Equal(suite.T(), "", suite.ConfGorushDefault.Stat.Redis.Password)
assert.Equal(suite.T(), int64(0), suite.ConfGorushDefault.Stat.Redis.DB) assert.Equal(suite.T(), 0, suite.ConfGorushDefault.Stat.Redis.DB)
assert.Equal(suite.T(), "gorush.db", suite.ConfGorushDefault.Stat.BoltDB.Path) assert.Equal(suite.T(), "bolt.db", suite.ConfGorushDefault.Stat.BoltDB.Path)
assert.Equal(suite.T(), "gorush", suite.ConfGorushDefault.Stat.BoltDB.Bucket) 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(), "bunt.db", suite.ConfGorushDefault.Stat.BuntDB.Path)
assert.Equal(suite.T(), "level.db", suite.ConfGorushDefault.Stat.LevelDB.Path)
} }
func (suite *ConfigTestSuite) TestValidateConf() { func (suite *ConfigTestSuite) TestValidateConf() {
@ -148,12 +149,13 @@ func (suite *ConfigTestSuite) TestValidateConf() {
assert.Equal(suite.T(), "memory", suite.ConfGorush.Stat.Engine) assert.Equal(suite.T(), "memory", suite.ConfGorush.Stat.Engine)
assert.Equal(suite.T(), "localhost:6379", suite.ConfGorush.Stat.Redis.Addr) assert.Equal(suite.T(), "localhost:6379", suite.ConfGorush.Stat.Redis.Addr)
assert.Equal(suite.T(), "", suite.ConfGorush.Stat.Redis.Password) assert.Equal(suite.T(), "", suite.ConfGorush.Stat.Redis.Password)
assert.Equal(suite.T(), int64(0), suite.ConfGorush.Stat.Redis.DB) assert.Equal(suite.T(), 0, suite.ConfGorush.Stat.Redis.DB)
assert.Equal(suite.T(), "gorush.db", suite.ConfGorush.Stat.BoltDB.Path) assert.Equal(suite.T(), "bolt.db", suite.ConfGorush.Stat.BoltDB.Path)
assert.Equal(suite.T(), "gorush", suite.ConfGorush.Stat.BoltDB.Bucket) 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(), "bunt.db", suite.ConfGorush.Stat.BuntDB.Path)
assert.Equal(suite.T(), "level.db", suite.ConfGorush.Stat.LevelDB.Path)
} }
func TestConfigTestSuite(t *testing.T) { func TestConfigTestSuite(t *testing.T) {

30
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: a723ec6556a185c4a20185378b0e042fd41453232da81489e06307b0ac8feba7 hash: 4ee18de0a09fd239b069a5830dc26083f603fd14e06f8cada4c0934366ee343e
updated: 2016-08-02T15:01:02.331147237+08:00 updated: 2016-09-19T16:53:10.40422002+08:00
imports: imports:
- name: github.com/asdine/storm - name: github.com/asdine/storm
version: 00b2f2df7ab7af9db746b826649395628cb5374e version: 00b2f2df7ab7af9db746b826649395628cb5374e
@ -22,6 +22,8 @@ imports:
version: 2402d76f3d41f928c7902a765dfc872356dd3aad version: 2402d76f3d41f928c7902a765dfc872356dd3aad
subpackages: subpackages:
- proto - proto
- name: github.com/golang/snappy
version: d9eb7a3d35ec988b8585d4a0068e462c27d28380
- name: github.com/google/go-gcm - name: github.com/google/go-gcm
version: 190e93b4cedb43562b5bd558eb1a1bbd38695bcd version: 190e93b4cedb43562b5bd558eb1a1bbd38695bcd
- name: github.com/jpillora/backoff - name: github.com/jpillora/backoff
@ -45,6 +47,21 @@ imports:
- assert - assert
- suite - suite
- require - require
- name: github.com/syndtr/goleveldb
version: 6ae1797c0b42b9323fc27ff7dcf568df88f2f33d
subpackages:
- leveldb
- leveldb/cache
- leveldb/comparer
- leveldb/errors
- leveldb/filter
- leveldb/iterator
- leveldb/journal
- leveldb/memdb
- leveldb/opt
- leveldb/storage
- leveldb/table
- leveldb/util
- name: github.com/thoas/stats - name: github.com/thoas/stats
version: 69e3c072eec2df2df41afe6214f62eb940e4cd80 version: 69e3c072eec2df2df41afe6214f62eb940e4cd80
- name: github.com/tidwall/btree - name: github.com/tidwall/btree
@ -99,13 +116,8 @@ imports:
version: 90f0b59102629831cc109845475a8d77043412ec version: 90f0b59102629831cc109845475a8d77043412ec
- name: gopkg.in/go-playground/validator.v8 - name: gopkg.in/go-playground/validator.v8
version: c193cecd124b5cc722d7ee5538e945bdb3348435 version: c193cecd124b5cc722d7ee5538e945bdb3348435
- name: gopkg.in/redis.v3 - name: gopkg.in/redis.v4
version: b5e368500d0a508ef8f16e9c2d4025a8a46bcc29 version: 938235994ea88a05678f8060741d5f34ed6a5ff3
subpackages:
- internal
- internal/consistenthash
- internal/hashtag
- internal/pool
- name: gopkg.in/yaml.v2 - name: gopkg.in/yaml.v2
version: a83829b6f1293c91addabc89d0571c246397bbf4 version: a83829b6f1293c91addabc89d0571c246397bbf4
testImports: testImports:

View File

@ -1,7 +1,6 @@
package: github.com/appleboy/gorush package: github.com/appleboy/gorush
import: import:
- package: gopkg.in/yaml.v2 - package: gopkg.in/yaml.v2
- package: gopkg.in/redis.v3
- package: github.com/Sirupsen/logrus - package: github.com/Sirupsen/logrus
version: v0.10.0 version: v0.10.0
- package: gopkg.in/appleboy/gin-status-api.v1 - package: gopkg.in/appleboy/gin-status-api.v1
@ -19,3 +18,5 @@ import:
- package: github.com/buger/jsonparser - package: github.com/buger/jsonparser
- package: github.com/thoas/stats - package: github.com/thoas/stats
- package: github.com/tidwall/buntdb - package: github.com/tidwall/buntdb
- package: github.com/syndtr/goleveldb
- package: gopkg.in/redis.v4

View File

@ -2,6 +2,8 @@ package gorush
import ( import (
"github.com/appleboy/gorush/storage/boltdb" "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/memory"
"github.com/appleboy/gorush/storage/redis" "github.com/appleboy/gorush/storage/redis"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -40,20 +42,24 @@ func InitAppStatus() error {
StatStorage = memory.New() StatStorage = memory.New()
case "redis": case "redis":
StatStorage = redis.New(PushConf) StatStorage = redis.New(PushConf)
err := StatStorage.Init()
if err != nil {
LogError.Error("redis error: " + err.Error())
return err
}
case "boltdb": case "boltdb":
StatStorage = boltdb.New(PushConf) StatStorage = boltdb.New(PushConf)
case "buntdb":
StatStorage = buntdb.New(PushConf)
case "leveldb":
StatStorage = leveldb.New(PushConf)
default: default:
StatStorage = memory.New() StatStorage = memory.New()
} }
err := StatStorage.Init()
if err != nil {
LogError.Error("storage error: " + err.Error())
return err
}
return nil return nil
} }

View File

@ -120,3 +120,53 @@ func TestStatForBoltDBEngine(t *testing.T) {
val = StatStorage.GetAndroidError() val = StatStorage.GetAndroidError()
assert.Equal(t, int64(500), val) assert.Equal(t, int64(500), val)
} }
func TestStatForBuntDBEngine(t *testing.T) {
var val int64
PushConf.Stat.Engine = "buntdb"
InitAppStatus()
StatStorage.Reset()
StatStorage.AddTotalCount(100)
StatStorage.AddIosSuccess(200)
StatStorage.AddIosError(300)
StatStorage.AddAndroidSuccess(400)
StatStorage.AddAndroidError(500)
val = StatStorage.GetTotalCount()
assert.Equal(t, int64(100), val)
val = StatStorage.GetIosSuccess()
assert.Equal(t, int64(200), val)
val = StatStorage.GetIosError()
assert.Equal(t, int64(300), val)
val = StatStorage.GetAndroidSuccess()
assert.Equal(t, int64(400), val)
val = StatStorage.GetAndroidError()
assert.Equal(t, int64(500), val)
}
// func TestStatForLevelDBEngine(t *testing.T) {
// var val int64
// PushConf.Stat.Engine = "leveldb"
// InitAppStatus()
// StatStorage.Reset()
// StatStorage.AddTotalCount(100)
// StatStorage.AddIosSuccess(200)
// StatStorage.AddIosError(300)
// StatStorage.AddAndroidSuccess(400)
// StatStorage.AddAndroidError(500)
// val = StatStorage.GetTotalCount()
// assert.Equal(t, int64(100), val)
// val = StatStorage.GetIosSuccess()
// assert.Equal(t, int64(200), val)
// val = StatStorage.GetIosError()
// assert.Equal(t, int64(300), val)
// val = StatStorage.GetAndroidSuccess()
// assert.Equal(t, int64(400), val)
// val = StatStorage.GetAndroidError()
// assert.Equal(t, int64(500), val)
// }

134
storage/leveldb/leveldb.go Normal file
View File

@ -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
}

View File

@ -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)
}

View File

@ -2,7 +2,7 @@ package redis
import ( import (
"github.com/appleboy/gorush/config" "github.com/appleboy/gorush/config"
"gopkg.in/redis.v3" "gopkg.in/redis.v4"
"log" "log"
"strconv" "strconv"
) )