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