forked from 42wim/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
60 lines (49 loc) · 1.16 KB
/
registry.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
50
51
52
53
54
55
56
57
58
59
60
package cfgfile
import "sync"
// Registry holds a list of Runners mapped by their unique hashes
type Registry struct {
sync.RWMutex
List map[uint64]Runner
}
// NewRegistry returns a new empty Registry
func NewRegistry() *Registry {
return &Registry{
List: map[uint64]Runner{},
}
}
// Add the given Runner to the list, indexed by a hash
func (r *Registry) Add(hash uint64, m Runner) {
r.Lock()
defer r.Unlock()
r.List[hash] = m
}
// Remove the Runner with the given hash from the list
func (r *Registry) Remove(hash uint64) {
r.Lock()
defer r.Unlock()
delete(r.List, hash)
}
// Has returns true if there is a Runner with the given hash
func (r *Registry) Has(hash uint64) bool {
r.RLock()
defer r.RUnlock()
_, ok := r.List[hash]
return ok
}
// Get returns the Runner with the given hash, or nil if it doesn't exist
func (r *Registry) Get(hash uint64) Runner {
r.RLock()
defer r.RUnlock()
return r.List[hash]
}
// CopyList returns a static copy of the Runners map
func (r *Registry) CopyList() map[uint64]Runner {
r.RLock()
defer r.RUnlock()
// Create a copy of the list
list := map[uint64]Runner{}
for k, v := range r.List {
list[k] = v
}
return list
}