forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.go
50 lines (40 loc) · 892 Bytes
/
meta.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
package meta
import (
"sync"
"github.com/elastic/beats/libbeat/common"
)
// Map stores a map of id -> MapStrPointer
type Map struct {
mutex sync.RWMutex
meta map[uint64]common.MapStrPointer
}
// NewMap instantiates and returns a new meta.Map
func NewMap() *Map {
return &Map{
meta: make(map[uint64]common.MapStrPointer),
}
}
// Store inserts or updates given meta under the given id. Then it returns a MapStrPointer to it
func (m *Map) Store(id uint64, meta common.MapStr) common.MapStrPointer {
m.mutex.Lock()
defer m.mutex.Unlock()
if meta == nil {
return common.MapStrPointer{}
}
p, ok := m.meta[id]
if !ok {
// create
p = common.NewMapStrPointer(meta)
m.meta[id] = p
} else {
// update
p.Set(meta)
}
return p
}
// Remove meta stored under the given id
func (m *Map) Remove(id uint64) {
m.mutex.Lock()
defer m.mutex.Unlock()
delete(m.meta, id)
}