Skip to content

Commit

Permalink
refactor(storage): Remove decommissioned path for memory store (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwiN authored Aug 12, 2022
1 parent 262d436 commit f01b66f
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 79 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/TwiN/g8 v1.3.0
github.com/TwiN/gocache/v2 v2.0.0
github.com/TwiN/gocache/v2 v2.1.0
github.com/TwiN/health v1.4.0
github.com/coreos/go-oidc/v3 v3.1.0
github.com/go-ping/ping v0.0.0-20210911151512-381826476871
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/TwiN/g8 v1.3.0 h1:mNv3R35GhDn1gEV0BKMl1oupZ1tDtOWPTHUKu+W/k3U=
github.com/TwiN/g8 v1.3.0/go.mod h1:SiIdItS0agSUloFqdQQt/RObB2jGSq+nnE9WfFv3RIo=
github.com/TwiN/gocache/v2 v2.0.0 h1:CPbDNKdSJpmBkh7aWcO7D3KK1yWaMlwX+3dsBPE8/so=
github.com/TwiN/gocache/v2 v2.0.0/go.mod h1:j4MABVaia2Tp53ERWc/3l4YxkswtPjB2hQzmL/kD/VQ=
github.com/TwiN/gocache/v2 v2.1.0 h1:AJnSX7Sgz22fsO7rdXYQzMQ4zWpMjBKqk70ADeqtLDU=
github.com/TwiN/gocache/v2 v2.1.0/go.mod h1:AKHAFZSwLLmoLm1a2siDOWmZ2RjIKqentRGfOFWkllY=
github.com/TwiN/health v1.4.0 h1:Ts7lb4ihYDpVEbFSGAhSEZTSwuDOADnwJLFngFl4xzw=
github.com/TwiN/health v1.4.0/go.mod h1:CSUh+ryfD2POS2vKtc/yO4IxgR58lKvQ0/8qnoPqPqs=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
11 changes: 1 addition & 10 deletions storage/store/memory/memory.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package memory

import (
"encoding/gob"
"sort"
"sync"
"time"
Expand All @@ -13,14 +12,6 @@ import (
"github.com/TwiN/gocache/v2"
)

func init() {
gob.Register(&core.EndpointStatus{})
gob.Register(&core.HourlyUptimeStatistics{})
gob.Register(&core.Uptime{})
gob.Register(&core.Result{})
gob.Register(&core.Event{})
}

// Store that leverages gocache
type Store struct {
sync.RWMutex
Expand All @@ -32,7 +23,7 @@ type Store struct {
//
// This store holds everything in memory, and if the file parameter is not blank,
// supports eventual persistence.
func NewStore(file string) (*Store, error) {
func NewStore() (*Store, error) {
store := &Store{
cache: gocache.NewCache().WithMaxSize(gocache.NoMaxSize),
}
Expand Down
26 changes: 9 additions & 17 deletions storage/store/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var (
// Note that are much more extensive tests in /storage/store/store_test.go.
// This test is simply an extra sanity check
func TestStore_SanityCheck(t *testing.T) {
store, _ := NewStore("")
store, _ := NewStore()
defer store.Close()
store.Insert(&testEndpoint, &testSuccessfulResult)
endpointStatuses, _ := store.GetAllEndpointStatuses(paging.NewEndpointStatusParams())
Expand Down Expand Up @@ -122,22 +122,14 @@ func TestStore_SanityCheck(t *testing.T) {
}

func TestStore_Save(t *testing.T) {
files := []string{
"",
t.TempDir() + "/test.db",
store, err := NewStore()
if err != nil {
t.Fatal("expected no error, got", err.Error())
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
store, err := NewStore(file)
if err != nil {
t.Fatal("expected no error, got", err.Error())
}
err = store.Save()
if err != nil {
t.Fatal("expected no error, got", err.Error())
}
store.Clear()
store.Close()
})
err = store.Save()
if err != nil {
t.Fatal("expected no error, got", err.Error())
}
store.Clear()
store.Close()
}
10 changes: 1 addition & 9 deletions storage/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,7 @@ func Initialize(cfg *storage.Config) error {
case storage.TypeMemory:
fallthrough
default:
if len(cfg.Path) > 0 {
store, err = memory.NewStore(cfg.Path)
if err != nil {
return err
}
go autoSave(ctx, store, 7*time.Minute)
} else {
store, _ = memory.NewStore("")
}
store, _ = memory.NewStore()
}
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions storage/store/store_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func BenchmarkStore_GetAllEndpointStatuses(b *testing.B) {
memoryStore, err := memory.NewStore("")
memoryStore, err := memory.NewStore()
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func BenchmarkStore_GetAllEndpointStatuses(b *testing.B) {
}

func BenchmarkStore_Insert(b *testing.B) {
memoryStore, err := memory.NewStore("")
memoryStore, err := memory.NewStore()
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func BenchmarkStore_Insert(b *testing.B) {
}

func BenchmarkStore_GetEndpointStatusByKey(b *testing.B) {
memoryStore, err := memory.NewStore("")
memoryStore, err := memory.NewStore()
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
Expand Down
10 changes: 5 additions & 5 deletions storage/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type Scenario struct {
}

func initStoresAndBaseScenarios(t *testing.T, testName string) []*Scenario {
memoryStore, err := memory.NewStore("")
memoryStore, err := memory.NewStore()
if err != nil {
t.Fatal("failed to create store:", err.Error())
}
Expand Down Expand Up @@ -528,17 +528,17 @@ func TestStore_DeleteAllEndpointStatusesNotInKeys(t *testing.T) {
scenario.Store.Insert(&firstEndpoint, result)
scenario.Store.Insert(&secondEndpoint, result)
if ss, _ := scenario.Store.GetEndpointStatusByKey(firstEndpoint.Key(), paging.NewEndpointStatusParams()); ss == nil {
t.Fatal("firstEndpoint should exist")
t.Fatal("firstEndpoint should exist, got", ss)
}
if ss, _ := scenario.Store.GetEndpointStatusByKey(secondEndpoint.Key(), paging.NewEndpointStatusParams()); ss == nil {
t.Fatal("secondEndpoint should exist")
t.Fatal("secondEndpoint should exist, got", ss)
}
scenario.Store.DeleteAllEndpointStatusesNotInKeys([]string{firstEndpoint.Key()})
if ss, _ := scenario.Store.GetEndpointStatusByKey(firstEndpoint.Key(), paging.NewEndpointStatusParams()); ss == nil {
t.Error("secondEndpoint should've been deleted")
t.Error("secondEndpoint should still exist, got", ss)
}
if ss, _ := scenario.Store.GetEndpointStatusByKey(secondEndpoint.Key(), paging.NewEndpointStatusParams()); ss != nil {
t.Error("firstEndpoint should still exist")
t.Error("firstEndpoint should have been deleted, got", ss)
}
// Delete everything
scenario.Store.DeleteAllEndpointStatusesNotInKeys([]string{})
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/TwiN/gocache/v2/LICENSE.md

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

53 changes: 27 additions & 26 deletions vendor/github.com/TwiN/gocache/v2/README.md

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

12 changes: 9 additions & 3 deletions vendor/github.com/TwiN/gocache/v2/gocache.go

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

4 changes: 2 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# github.com/TwiN/g8 v1.3.0
## explicit; go 1.17
github.com/TwiN/g8
# github.com/TwiN/gocache/v2 v2.0.0
## explicit; go 1.17
# github.com/TwiN/gocache/v2 v2.1.0
## explicit; go 1.18
github.com/TwiN/gocache/v2
# github.com/TwiN/health v1.4.0
## explicit; go 1.18
Expand Down

0 comments on commit f01b66f

Please sign in to comment.