@@ -16,79 +16,125 @@ package store
1616
1717import (
1818 "context"
19+ "slices"
1920 "sync"
2021 "time"
2122)
2223
2324// Memory stores the configuration in memory. Used for testing.
2425type Memory struct {
25- MainNode bool
2626 collections * sync.Map
2727 histograms * sync.Map
2828 scans * sync.Map
29+
30+ mu struct {
31+ sync.RWMutex
32+ err error // Error to return
33+ mainNode bool
34+ }
2935}
3036
3137var _ Store = & Memory {}
3238
3339// DeleteCollection implements store.Store.
3440func (m * Memory ) DeleteCollection (_ context.Context , name string ) error {
41+ if m .Error () != nil {
42+ return m .Error ()
43+ }
3544 m .collections .Delete (name )
3645 return nil
3746}
3847
3948// DeleteHistogram implements store.Store.
4049func (m * Memory ) DeleteHistogram (_ context.Context , name string ) error {
50+ if m .Error () != nil {
51+ return m .Error ()
52+ }
4153 m .histograms .Delete (name )
4254 return nil
4355}
4456
4557// DeleteScan implements store.Store.
4658func (m * Memory ) DeleteScan (_ context.Context , name string ) error {
59+ if m .Error () != nil {
60+ return m .Error ()
61+ }
4762 m .scans .Delete (name )
4863 return nil
4964}
5065
66+ // Error returns the injected error
67+ func (m * Memory ) Error () error {
68+ m .mu .RLock ()
69+ defer m .mu .RUnlock ()
70+ return m .mu .err
71+ }
72+
5173// GetCollection implements store.Store.
5274func (m * Memory ) GetCollection (_ context.Context , name string ) (* Collection , error ) {
53- res , _ := m .collections .Load (name )
75+ if m .Error () != nil {
76+ return nil , m .Error ()
77+ }
78+ res , ok := m .collections .Load (name )
79+ if ! ok {
80+ return nil , nil
81+ }
5482 return res .(* Collection ), nil
5583}
5684
5785// GetCollectionNames implements store.Store.
5886func (m * Memory ) GetCollectionNames (_ context.Context ) ([]string , error ) {
59- return getNames (m .collections )
87+ return m . getNames (m .collections )
6088}
6189
6290// GetHistogram implements store.Store.
6391func (m * Memory ) GetHistogram (_ context.Context , name string ) (* Histogram , error ) {
64- res , _ := m .histograms .Load (name )
65- return res .(* Histogram ), nil
92+ if m .Error () != nil {
93+ return nil , m .Error ()
94+ }
95+ res , ok := m .histograms .Load (name )
96+ if ! ok {
97+ return nil , m .Error ()
98+ }
99+ return res .(* Histogram ), m .Error ()
66100}
67101
68102// GetHistogramNames implements store.Store.
69103func (m * Memory ) GetHistogramNames (_ context.Context ) ([]string , error ) {
70- return getNames (m .histograms )
104+ return m . getNames (m .histograms )
71105}
72106
73107// GetMetrics implements store.Store.
74108func (m * Memory ) GetMetrics (ctx context.Context , name string ) ([]Metric , error ) {
109+ if m .Error () != nil {
110+ return nil , m .Error ()
111+ }
75112 coll , _ := m .GetCollection (ctx , name )
76113 return coll .Metrics , nil
77114}
78115
79116// GetScan implements store.Store.
80117func (m * Memory ) GetScan (_ context.Context , name string ) (* Scan , error ) {
81- res , _ := m .scans .Load (name )
118+ if m .Error () != nil {
119+ return nil , m .Error ()
120+ }
121+ res , ok := m .scans .Load (name )
122+ if ! ok {
123+ return nil , nil
124+ }
82125 return res .(* Scan ), nil
83126}
84127
85128// GetScanNames implements store.Store.
86129func (m * Memory ) GetScanNames (_ context.Context ) ([]string , error ) {
87- return getNames (m .scans )
130+ return m . getNames (m .scans )
88131}
89132
90133// GetScanPatterns implements store.Store.
91134func (m * Memory ) GetScanPatterns (ctx context.Context , name string ) ([]Pattern , error ) {
135+ if m .Error () != nil {
136+ return nil , m .Error ()
137+ }
92138 scan , _ := m .GetScan (ctx , name )
93139 return scan .Patterns , nil
94140}
@@ -98,37 +144,67 @@ func (m *Memory) Init(_ context.Context) error {
98144 m .collections = & sync.Map {}
99145 m .histograms = & sync.Map {}
100146 m .scans = & sync.Map {}
147+ m .InjectError (nil )
101148 return nil
102149}
103150
151+ // InjectError sets the error that will be returned on each subsequent call.
152+ func (m * Memory ) InjectError (err error ) {
153+ m .mu .Lock ()
154+ defer m .mu .Unlock ()
155+ m .mu .err = err
156+ }
157+
104158// IsMainNode implements store.Store.
105159func (m * Memory ) IsMainNode (_ context.Context , lastUpdated time.Time ) (bool , error ) {
106- return m .MainNode , nil
160+ m .mu .RLock ()
161+ defer m .mu .RUnlock ()
162+ return m .mu .mainNode , m .mu .err
107163}
108164
109165// PutCollection implements store.Store.
110166func (m * Memory ) PutCollection (_ context.Context , collection * Collection ) error {
167+ if m .Error () != nil {
168+ return m .Error ()
169+ }
111170 m .collections .Store (collection .Name , collection )
112171 return nil
113172}
114173
115174// PutHistogram implements store.Store.
116175func (m * Memory ) PutHistogram (_ context.Context , histogram * Histogram ) error {
176+ if m .Error () != nil {
177+ return m .Error ()
178+ }
117179 m .histograms .Store (histogram .Name , histogram )
118180 return nil
119181}
120182
121183// PutScan implements store.Store.
122184func (m * Memory ) PutScan (_ context.Context , scan * Scan ) error {
185+ if m .Error () != nil {
186+ return m .Error ()
187+ }
123188 m .scans .Store (scan .Name , scan )
124189 return nil
125190}
126191
127- func getNames (m * sync.Map ) ([]string , error ) {
192+ // SetMainNode sets this store as the main node.
193+ func (m * Memory ) SetMainNode (main bool ) {
194+ m .mu .Lock ()
195+ defer m .mu .Unlock ()
196+ m .mu .mainNode = main
197+ }
198+
199+ func (m * Memory ) getNames (vals * sync.Map ) ([]string , error ) {
200+ if m .Error () != nil {
201+ return nil , m .Error ()
202+ }
128203 names := make ([]string , 0 )
129- m .Range (func (key any , value any ) bool {
204+ vals .Range (func (key any , value any ) bool {
130205 names = append (names , key .(string ))
131206 return true
132207 })
208+ slices .Sort (names )
133209 return names , nil
134210}
0 commit comments