From b13fd54ba3b82842f711fae75d8d435255f4d1d8 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 2 Aug 2016 15:35:28 +0800 Subject: [PATCH 1/4] Support BuntDB engine. Signed-off-by: Bo-Yi Wu --- Makefile | 3 + config/config.go | 8 ++ config/config.yml | 2 + config/config_test.go | 4 + glide.lock | 39 +++++++--- glide.yaml | 1 + storage/buntdb/buntdb.go | 134 ++++++++++++++++++++++++++++++++++ storage/buntdb/buntdb_test.go | 40 ++++++++++ 8 files changed, 221 insertions(+), 10 deletions(-) create mode 100644 storage/buntdb/buntdb.go create mode 100644 storage/buntdb/buntdb_test.go diff --git a/Makefile b/Makefile index b6f4c6e..12bfe69 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,9 @@ boltdb_test: init memory_test: init go test -v -cover ./storage/memory/... +buntdb_test: init + go test -v -cover ./storage/buntdb/... + config_test: init go test -v -cover ./config/... diff --git a/config/config.go b/config/config.go index fa1b728..d5535bb 100644 --- a/config/config.go +++ b/config/config.go @@ -67,6 +67,7 @@ type SectionStat struct { Engine string `yaml:"engine"` Redis SectionRedis `yaml:"redis"` BoltDB SectionBoltDB `yaml:"boltdb"` + BuntDB SectionBuntDB `yaml:"buntdb"` } // SectionRedis is sub seciont of config. @@ -82,6 +83,11 @@ type SectionBoltDB struct { Bucket string `yaml:"bucket"` } +// SectionBuntDB is sub seciont of config. +type SectionBuntDB struct { + Path string `yaml:"path"` +} + // BuildDefaultPushConf is default config setting. func BuildDefaultPushConf() ConfYaml { var conf ConfYaml @@ -130,6 +136,8 @@ func BuildDefaultPushConf() ConfYaml { conf.Stat.BoltDB.Path = "gorush.db" conf.Stat.BoltDB.Bucket = "gorush" + conf.Stat.BuntDB.Path = "gorush.db" + return conf } diff --git a/config/config.yml b/config/config.yml index da5e334..047c7a9 100644 --- a/config/config.yml +++ b/config/config.yml @@ -43,3 +43,5 @@ stat: boltdb: path: "gorush.db" bucket: "gorush" + buntdb: + path: "gorush.db" diff --git a/config/config_test.go b/config/config_test.go index 4e10842..073c8da 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -96,6 +96,8 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() { assert.Equal(suite.T(), "gorush.db", suite.ConfGorushDefault.Stat.BoltDB.Path) assert.Equal(suite.T(), "gorush", suite.ConfGorushDefault.Stat.BoltDB.Bucket) + + assert.Equal(suite.T(), "gorush.db", suite.ConfGorushDefault.Stat.BuntDB.Path) } func (suite *ConfigTestSuite) TestValidateConf() { @@ -142,6 +144,8 @@ func (suite *ConfigTestSuite) TestValidateConf() { assert.Equal(suite.T(), "gorush.db", suite.ConfGorush.Stat.BoltDB.Path) assert.Equal(suite.T(), "gorush", suite.ConfGorush.Stat.BoltDB.Bucket) + + assert.Equal(suite.T(), "gorush.db", suite.ConfGorush.Stat.BuntDB.Path) } func TestConfigTestSuite(t *testing.T) { diff --git a/glide.lock b/glide.lock index 5308f0d..31750c8 100644 --- a/glide.lock +++ b/glide.lock @@ -1,14 +1,6 @@ -hash: d182f8dc3162313af09e706a0ba4901218d9ccde609ee7d39c6aae5e891f20ee -updated: 2016-07-19T21:31:48.923940945+08:00 +hash: a723ec6556a185c4a20185378b0e042fd41453232da81489e06307b0ac8feba7 +updated: 2016-08-02T15:01:02.331147237+08:00 imports: -- name: github.com/appleboy/gorush - version: af78db59b4675cf932aaa71fd45443ac9283bce7 - subpackages: - - config - - gorush - - storage/boltdb - - storage/memory - - storage/redis - name: github.com/asdine/storm version: 00b2f2df7ab7af9db746b826649395628cb5374e subpackages: @@ -55,6 +47,33 @@ imports: - require - name: github.com/thoas/stats version: 69e3c072eec2df2df41afe6214f62eb940e4cd80 +- name: github.com/tidwall/btree + version: 7ebb98011b36ffcdcf7a5d8062d6e6c651a49560 +- name: github.com/tidwall/buntdb + version: 912f340175491888fdd33687d7a309921b6c1e49 +- name: github.com/tidwall/rtree + version: 1737a7bdeb7e5f8303905c5b385f1eeb0f7272ab + subpackages: + - dims/d1 + - dims/d10 + - dims/d11 + - dims/d12 + - dims/d13 + - dims/d14 + - dims/d15 + - dims/d16 + - dims/d17 + - dims/d18 + - dims/d19 + - dims/d2 + - dims/d20 + - dims/d3 + - dims/d4 + - dims/d5 + - dims/d6 + - dims/d7 + - dims/d8 + - dims/d9 - name: golang.org/x/crypto version: c2f4947f41766b144bb09066e919466da5eddeae subpackages: diff --git a/glide.yaml b/glide.yaml index 7881c69..48453dc 100644 --- a/glide.yaml +++ b/glide.yaml @@ -18,3 +18,4 @@ import: version: v1.0.1 - package: github.com/buger/jsonparser - package: github.com/thoas/stats +- package: github.com/tidwall/buntdb diff --git a/storage/buntdb/buntdb.go b/storage/buntdb/buntdb.go new file mode 100644 index 0000000..7f66066 --- /dev/null +++ b/storage/buntdb/buntdb.go @@ -0,0 +1,134 @@ +package boltdb + +import ( + "fmt" + "github.com/appleboy/gorush/config" + "github.com/tidwall/buntdb" + "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" +) + +// 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 { + return nil +} + +// Reset Client storage. +func (s *Storage) Reset() { + s.setBuntDB(TotalCountKey, 0) + s.setBuntDB(IosSuccessKey, 0) + s.setBuntDB(IosErrorKey, 0) + s.setBuntDB(AndroidSuccessKey, 0) + s.setBuntDB(AndroidErrorKey, 0) +} + +func (s *Storage) setBuntDB(key string, count int64) { + db, _ := buntdb.Open(s.config.Stat.BuntDB.Path) + + db.Update(func(tx *buntdb.Tx) error { + tx.Set(key, fmt.Sprintf("%d", count), nil) + return nil + }) + defer db.Close() +} + +func (s *Storage) getBuntDB(key string, count *int64) { + db, _ := buntdb.Open(s.config.Stat.BuntDB.Path) + + db.View(func(tx *buntdb.Tx) error { + val, _ := tx.Get(key) + *count, _ = strconv.ParseInt(val, 10, 64) + return nil + }) + defer db.Close() +} + +// AddTotalCount record push notification count. +func (s *Storage) AddTotalCount(count int64) { + total := s.GetTotalCount() + count + s.setBuntDB(TotalCountKey, total) +} + +// AddIosSuccess record counts of success iOS push notification. +func (s *Storage) AddIosSuccess(count int64) { + total := s.GetIosSuccess() + count + s.setBuntDB(IosSuccessKey, total) +} + +// AddIosError record counts of error iOS push notification. +func (s *Storage) AddIosError(count int64) { + total := s.GetIosError() + count + s.setBuntDB(IosErrorKey, total) +} + +// AddAndroidSuccess record counts of success Android push notification. +func (s *Storage) AddAndroidSuccess(count int64) { + total := s.GetAndroidSuccess() + count + s.setBuntDB(AndroidSuccessKey, total) +} + +// AddAndroidError record counts of error Android push notification. +func (s *Storage) AddAndroidError(count int64) { + total := s.GetAndroidError() + count + s.setBuntDB(AndroidErrorKey, total) +} + +// GetTotalCount show counts of all notification. +func (s *Storage) GetTotalCount() int64 { + var count int64 + s.getBuntDB(TotalCountKey, &count) + + return count +} + +// GetIosSuccess show success counts of iOS notification. +func (s *Storage) GetIosSuccess() int64 { + var count int64 + s.getBuntDB(IosSuccessKey, &count) + + return count +} + +// GetIosError show error counts of iOS notification. +func (s *Storage) GetIosError() int64 { + var count int64 + s.getBuntDB(IosErrorKey, &count) + + return count +} + +// GetAndroidSuccess show success counts of Android notification. +func (s *Storage) GetAndroidSuccess() int64 { + var count int64 + s.getBuntDB(AndroidSuccessKey, &count) + + return count +} + +// GetAndroidError show error counts of Android notification. +func (s *Storage) GetAndroidError() int64 { + var count int64 + s.getBuntDB(AndroidErrorKey, &count) + + return count +} diff --git a/storage/buntdb/buntdb_test.go b/storage/buntdb/buntdb_test.go new file mode 100644 index 0000000..d7ed1c4 --- /dev/null +++ b/storage/buntdb/buntdb_test.go @@ -0,0 +1,40 @@ +package boltdb + +import ( + c "github.com/appleboy/gorush/config" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestBuntDBEngine(t *testing.T) { + var val int64 + + config := c.BuildDefaultPushConf() + + boltDB := New(config) + boltDB.Init() + boltDB.Reset() + + boltDB.AddTotalCount(10) + val = boltDB.GetTotalCount() + assert.Equal(t, int64(10), val) + boltDB.AddTotalCount(10) + val = boltDB.GetTotalCount() + assert.Equal(t, int64(20), val) + + boltDB.AddIosSuccess(20) + val = boltDB.GetIosSuccess() + assert.Equal(t, int64(20), val) + + boltDB.AddIosError(30) + val = boltDB.GetIosError() + assert.Equal(t, int64(30), val) + + boltDB.AddAndroidSuccess(40) + val = boltDB.GetAndroidSuccess() + assert.Equal(t, int64(40), val) + + boltDB.AddAndroidError(50) + val = boltDB.GetAndroidError() + assert.Equal(t, int64(50), val) +} From 87328592dc7e557231792452f708311adb8cab9b Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 2 Aug 2016 15:42:46 +0800 Subject: [PATCH 2/4] fix variable name and add reset test. Signed-off-by: Bo-Yi Wu --- storage/buntdb/buntdb.go | 2 +- storage/buntdb/buntdb_test.go | 36 +++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/storage/buntdb/buntdb.go b/storage/buntdb/buntdb.go index 7f66066..7f423a5 100644 --- a/storage/buntdb/buntdb.go +++ b/storage/buntdb/buntdb.go @@ -1,4 +1,4 @@ -package boltdb +package buntdb import ( "fmt" diff --git a/storage/buntdb/buntdb_test.go b/storage/buntdb/buntdb_test.go index d7ed1c4..2cbef12 100644 --- a/storage/buntdb/buntdb_test.go +++ b/storage/buntdb/buntdb_test.go @@ -1,4 +1,4 @@ -package boltdb +package buntdb import ( c "github.com/appleboy/gorush/config" @@ -11,30 +11,34 @@ func TestBuntDBEngine(t *testing.T) { config := c.BuildDefaultPushConf() - boltDB := New(config) - boltDB.Init() - boltDB.Reset() + buntDB := New(config) + buntDB.Init() + buntDB.Reset() - boltDB.AddTotalCount(10) - val = boltDB.GetTotalCount() + buntDB.AddTotalCount(10) + val = buntDB.GetTotalCount() assert.Equal(t, int64(10), val) - boltDB.AddTotalCount(10) - val = boltDB.GetTotalCount() + buntDB.AddTotalCount(10) + val = buntDB.GetTotalCount() assert.Equal(t, int64(20), val) - boltDB.AddIosSuccess(20) - val = boltDB.GetIosSuccess() + buntDB.AddIosSuccess(20) + val = buntDB.GetIosSuccess() assert.Equal(t, int64(20), val) - boltDB.AddIosError(30) - val = boltDB.GetIosError() + buntDB.AddIosError(30) + val = buntDB.GetIosError() assert.Equal(t, int64(30), val) - boltDB.AddAndroidSuccess(40) - val = boltDB.GetAndroidSuccess() + buntDB.AddAndroidSuccess(40) + val = buntDB.GetAndroidSuccess() assert.Equal(t, int64(40), val) - boltDB.AddAndroidError(50) - val = boltDB.GetAndroidError() + buntDB.AddAndroidError(50) + val = buntDB.GetAndroidError() assert.Equal(t, int64(50), val) + + buntDB.Reset() + val = buntDB.GetAndroidError() + assert.Equal(t, int64(0), val) } From 601472a6db61ae3b8b3c01aba0806deaa7c5ba1d Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 2 Aug 2016 15:46:42 +0800 Subject: [PATCH 3/4] update readme. Signed-off-by: Bo-Yi Wu --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 93d9968..3f5dc54 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ A push notification micro server using [Gin](https://github.com/gin-gonic/gin) f * Support notification queue and multiple workers. * Support `/api/stat/app` show notification success and failure counts. * Support `/api/config` show your [YAML](https://en.wikipedia.org/wiki/YAML) config. -* Support store app stat to memory, [Redis](http://redis.io/) or [BoltDB](https://github.com/boltdb/bolt). +* Support store app stat to memory, [Redis](http://redis.io/), [BoltDB](https://github.com/boltdb/bolt) or [BuntDB](https://github.com/tidwall/buntdb). * Support `p12` or `pem` formtat of iOS certificate file. * Support `/sys/stats` show response time, status code count, etc. * Support for HTTP proxy to Google server (GCM). @@ -98,6 +98,8 @@ stat: boltdb: path: "gorush.db" bucket: "gorush" + buntdb: + path: "gorush.db" ``` ## Basic Usage From 9dd33575e6820d6404745da928bd254fca7cb3d5 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 2 Aug 2016 16:25:04 +0800 Subject: [PATCH 4/4] remove db file before testing. Signed-off-by: Bo-Yi Wu --- storage/buntdb/buntdb_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/storage/buntdb/buntdb_test.go b/storage/buntdb/buntdb_test.go index 2cbef12..2753f2d 100644 --- a/storage/buntdb/buntdb_test.go +++ b/storage/buntdb/buntdb_test.go @@ -3,6 +3,7 @@ package buntdb import ( c "github.com/appleboy/gorush/config" "github.com/stretchr/testify/assert" + "os" "testing" ) @@ -11,6 +12,10 @@ func TestBuntDBEngine(t *testing.T) { config := c.BuildDefaultPushConf() + if _, err := os.Stat(config.Stat.BuntDB.Path); os.IsNotExist(err) { + os.RemoveAll(config.Stat.BuntDB.Path) + } + buntDB := New(config) buntDB.Init() buntDB.Reset()