Skip to content

Commit

Permalink
refactor(storage): move interface to core package
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy committed Nov 9, 2022
1 parent 111a7cc commit 4be2c80
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 117 deletions.
33 changes: 33 additions & 0 deletions core/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package core

const (
// TotalCountKey is key name for total count of storage
TotalCountKey = "gorush-total-count"

// IosSuccessKey is key name or ios success count of storage
IosSuccessKey = "gorush-ios-success-count"

// IosErrorKey is key name or ios success error of storage
IosErrorKey = "gorush-ios-error-count"

// AndroidSuccessKey is key name for android success count of storage
AndroidSuccessKey = "gorush-android-success-count"

// AndroidErrorKey is key name for android error count of storage
AndroidErrorKey = "gorush-android-error-count"

// HuaweiSuccessKey is key name for huawei success count of storage
HuaweiSuccessKey = "gorush-huawei-success-count"

// HuaweiErrorKey is key name for huawei error count of storage
HuaweiErrorKey = "gorush-huawei-error-count"
)

// Storage interface
type Storage interface {
Init() error
Add(key string, count int64)
Set(key string, count int64)
Get(key string) int64
Close() error
}
4 changes: 2 additions & 2 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"

"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/storage"
"github.com/appleboy/gorush/storage/badger"
"github.com/appleboy/gorush/storage/boltdb"
"github.com/appleboy/gorush/storage/buntdb"
Expand Down Expand Up @@ -57,7 +57,7 @@ type HuaweiStatus struct {
func InitAppStatus(conf *config.ConfYaml) error {
logx.LogAccess.Info("Init App Status Engine as ", conf.Stat.Engine)

var store storage.Storage
var store core.Storage
switch conf.Stat.Engine {
case "memory":
store = memory.New()
Expand Down
50 changes: 26 additions & 24 deletions status/storage.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package status

import "github.com/appleboy/gorush/storage"
import (
"github.com/appleboy/gorush/core"
)

type StateStorage struct {
store storage.Storage
store core.Storage
}

func NewStateStorage(store storage.Storage) *StateStorage {
func NewStateStorage(store core.Storage) *StateStorage {
return &StateStorage{
store: store,
}
Expand All @@ -22,81 +24,81 @@ func (s *StateStorage) Close() error {

// Reset Client storage.
func (s *StateStorage) Reset() {
s.store.Set(storage.TotalCountKey, 0)
s.store.Set(storage.IosSuccessKey, 0)
s.store.Set(storage.IosErrorKey, 0)
s.store.Set(storage.AndroidSuccessKey, 0)
s.store.Set(storage.AndroidErrorKey, 0)
s.store.Set(storage.HuaweiSuccessKey, 0)
s.store.Set(storage.HuaweiErrorKey, 0)
s.store.Set(core.TotalCountKey, 0)
s.store.Set(core.IosSuccessKey, 0)
s.store.Set(core.IosErrorKey, 0)
s.store.Set(core.AndroidSuccessKey, 0)
s.store.Set(core.AndroidErrorKey, 0)
s.store.Set(core.HuaweiSuccessKey, 0)
s.store.Set(core.HuaweiErrorKey, 0)
}

// AddTotalCount record push notification count.
func (s *StateStorage) AddTotalCount(count int64) {
s.store.Add(storage.TotalCountKey, count)
s.store.Add(core.TotalCountKey, count)
}

// AddIosSuccess record counts of success iOS push notification.
func (s *StateStorage) AddIosSuccess(count int64) {
s.store.Add(storage.IosSuccessKey, count)
s.store.Add(core.IosSuccessKey, count)
}

// AddIosError record counts of error iOS push notification.
func (s *StateStorage) AddIosError(count int64) {
s.store.Add(storage.IosErrorKey, count)
s.store.Add(core.IosErrorKey, count)
}

// AddAndroidSuccess record counts of success Android push notification.
func (s *StateStorage) AddAndroidSuccess(count int64) {
s.store.Add(storage.AndroidSuccessKey, count)
s.store.Add(core.AndroidSuccessKey, count)
}

// AddAndroidError record counts of error Android push notification.
func (s *StateStorage) AddAndroidError(count int64) {
s.store.Add(storage.AndroidErrorKey, count)
s.store.Add(core.AndroidErrorKey, count)
}

// AddHuaweiSuccess record counts of success Huawei push notification.
func (s *StateStorage) AddHuaweiSuccess(count int64) {
s.store.Add(storage.HuaweiSuccessKey, count)
s.store.Add(core.HuaweiSuccessKey, count)
}

// AddHuaweiError record counts of error Huawei push notification.
func (s *StateStorage) AddHuaweiError(count int64) {
s.store.Add(storage.HuaweiErrorKey, count)
s.store.Add(core.HuaweiErrorKey, count)
}

// GetTotalCount show counts of all notification.
func (s *StateStorage) GetTotalCount() int64 {
return s.store.Get(storage.TotalCountKey)
return s.store.Get(core.TotalCountKey)
}

// GetIosSuccess show success counts of iOS notification.
func (s *StateStorage) GetIosSuccess() int64 {
return s.store.Get(storage.IosSuccessKey)
return s.store.Get(core.IosSuccessKey)
}

// GetIosError show error counts of iOS notification.
func (s *StateStorage) GetIosError() int64 {
return s.store.Get(storage.IosErrorKey)
return s.store.Get(core.IosErrorKey)
}

// GetAndroidSuccess show success counts of Android notification.
func (s *StateStorage) GetAndroidSuccess() int64 {
return s.store.Get(storage.AndroidSuccessKey)
return s.store.Get(core.AndroidSuccessKey)
}

// GetAndroidError show error counts of Android notification.
func (s *StateStorage) GetAndroidError() int64 {
return s.store.Get(storage.AndroidErrorKey)
return s.store.Get(core.AndroidErrorKey)
}

// GetHuaweiSuccess show success counts of Huawei notification.
func (s *StateStorage) GetHuaweiSuccess() int64 {
return s.store.Get(storage.HuaweiSuccessKey)
return s.store.Get(core.HuaweiSuccessKey)
}

// GetHuaweiError show error counts of Huawei notification.
func (s *StateStorage) GetHuaweiError() int64 {
return s.store.Get(storage.HuaweiErrorKey)
return s.store.Get(core.HuaweiErrorKey)
}
20 changes: 10 additions & 10 deletions storage/badger/badger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"sync"
"testing"

"github.com/appleboy/gorush/storage"

"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"

"github.com/stretchr/testify/assert"
)

Expand All @@ -19,28 +19,28 @@ func TestBadgerEngine(t *testing.T) {
err := badger.Init()
assert.Nil(t, err)

badger.Add(storage.HuaweiSuccessKey, 10)
val = badger.Get(storage.HuaweiSuccessKey)
badger.Add(core.HuaweiSuccessKey, 10)
val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
badger.Add(storage.HuaweiSuccessKey, 10)
val = badger.Get(storage.HuaweiSuccessKey)
badger.Add(core.HuaweiSuccessKey, 10)
val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)

badger.Set(storage.HuaweiSuccessKey, 0)
val = badger.Get(storage.HuaweiSuccessKey)
badger.Set(core.HuaweiSuccessKey, 0)
val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)

// test concurrency issues
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
badger.Add(storage.HuaweiSuccessKey, 1)
badger.Add(core.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = badger.Get(storage.HuaweiSuccessKey)
val = badger.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)

assert.NoError(t, badger.Close())
Expand Down
20 changes: 10 additions & 10 deletions storage/boltdb/boltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"sync"
"testing"

"github.com/appleboy/gorush/storage"

"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"

"github.com/stretchr/testify/assert"
)

Expand All @@ -19,28 +19,28 @@ func TestBoltDBEngine(t *testing.T) {
err := boltDB.Init()
assert.Nil(t, err)

boltDB.Add(storage.HuaweiSuccessKey, 10)
val = boltDB.Get(storage.HuaweiSuccessKey)
boltDB.Add(core.HuaweiSuccessKey, 10)
val = boltDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
boltDB.Add(storage.HuaweiSuccessKey, 10)
val = boltDB.Get(storage.HuaweiSuccessKey)
boltDB.Add(core.HuaweiSuccessKey, 10)
val = boltDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)

boltDB.Set(storage.HuaweiSuccessKey, 0)
val = boltDB.Get(storage.HuaweiSuccessKey)
boltDB.Set(core.HuaweiSuccessKey, 0)
val = boltDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)

// test concurrency issues
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
boltDB.Add(storage.HuaweiSuccessKey, 1)
boltDB.Add(core.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = boltDB.Get(storage.HuaweiSuccessKey)
val = boltDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)

assert.NoError(t, boltDB.Close())
Expand Down
20 changes: 10 additions & 10 deletions storage/buntdb/buntdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"sync"
"testing"

"github.com/appleboy/gorush/storage"

"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"

"github.com/stretchr/testify/assert"
)

Expand All @@ -25,28 +25,28 @@ func TestBuntDBEngine(t *testing.T) {
err := buntDB.Init()
assert.Nil(t, err)

buntDB.Add(storage.HuaweiSuccessKey, 10)
val = buntDB.Get(storage.HuaweiSuccessKey)
buntDB.Add(core.HuaweiSuccessKey, 10)
val = buntDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
buntDB.Add(storage.HuaweiSuccessKey, 10)
val = buntDB.Get(storage.HuaweiSuccessKey)
buntDB.Add(core.HuaweiSuccessKey, 10)
val = buntDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)

buntDB.Set(storage.HuaweiSuccessKey, 0)
val = buntDB.Get(storage.HuaweiSuccessKey)
buntDB.Set(core.HuaweiSuccessKey, 0)
val = buntDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)

// test concurrency issues
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
buntDB.Add(storage.HuaweiSuccessKey, 1)
buntDB.Add(core.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = buntDB.Get(storage.HuaweiSuccessKey)
val = buntDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)

assert.NoError(t, buntDB.Close())
Expand Down
20 changes: 10 additions & 10 deletions storage/leveldb/leveldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"sync"
"testing"

"github.com/appleboy/gorush/storage"

"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"

"github.com/stretchr/testify/assert"
)

Expand All @@ -25,28 +25,28 @@ func TestLevelDBEngine(t *testing.T) {
err := levelDB.Init()
assert.Nil(t, err)

levelDB.Add(storage.HuaweiSuccessKey, 10)
val = levelDB.Get(storage.HuaweiSuccessKey)
levelDB.Add(core.HuaweiSuccessKey, 10)
val = levelDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)
levelDB.Add(storage.HuaweiSuccessKey, 10)
val = levelDB.Get(storage.HuaweiSuccessKey)
levelDB.Add(core.HuaweiSuccessKey, 10)
val = levelDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(20), val)

levelDB.Set(storage.HuaweiSuccessKey, 0)
val = levelDB.Get(storage.HuaweiSuccessKey)
levelDB.Set(core.HuaweiSuccessKey, 0)
val = levelDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(0), val)

// test concurrency issues
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
levelDB.Add(storage.HuaweiSuccessKey, 1)
levelDB.Add(core.HuaweiSuccessKey, 1)
wg.Done()
}()
}
wg.Wait()
val = levelDB.Get(storage.HuaweiSuccessKey)
val = levelDB.Get(core.HuaweiSuccessKey)
assert.Equal(t, int64(10), val)

assert.NoError(t, levelDB.Close())
Expand Down
Loading

0 comments on commit 4be2c80

Please sign in to comment.