forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrar.go
49 lines (41 loc) · 1.12 KB
/
registrar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package storage
import "sync"
// StoreActioner exposes and interface for various actions that can be performed on a store.
type StoreActioner interface {
// Rebuild the entire store, this should be considered to be an expensive action.
Rebuild() error
}
type StoreActionerRegistrar interface {
List() []string
Register(name string, store StoreActioner)
Get(name string) (StoreActioner, bool)
}
func NewStorageResitrar() StoreActionerRegistrar {
return &storeActionerRegistrar{
stores: make(map[string]StoreActioner),
}
}
type storeActionerRegistrar struct {
mu sync.RWMutex
stores map[string]StoreActioner
}
func (sr *storeActionerRegistrar) List() []string {
sr.mu.RLock()
defer sr.mu.RUnlock()
list := make([]string, 0, len(sr.stores))
for name := range sr.stores {
list = append(list, name)
}
return list
}
func (sr *storeActionerRegistrar) Register(name string, store StoreActioner) {
sr.mu.Lock()
defer sr.mu.Unlock()
sr.stores[name] = store
}
func (sr *storeActionerRegistrar) Get(name string) (store StoreActioner, ok bool) {
sr.mu.RLock()
defer sr.mu.RUnlock()
store, ok = sr.stores[name]
return
}