2014-12-09 04:29:25 +00:00
|
|
|
package util
|
|
|
|
|
2015-01-08 07:54:50 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
2014-12-09 04:29:25 +00:00
|
|
|
|
|
|
|
// A mostly for read map, which can thread-safely
|
|
|
|
// initialize the map entries.
|
|
|
|
type ConcurrentReadMap struct {
|
2016-05-30 19:30:26 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
items map[string]interface{}
|
2014-12-09 04:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConcurrentReadMap() *ConcurrentReadMap {
|
2016-05-30 19:30:26 +00:00
|
|
|
return &ConcurrentReadMap{items: make(map[string]interface{})}
|
2014-12-09 04:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ConcurrentReadMap) initMapEntry(key string, newEntry func() interface{}) (value interface{}) {
|
2016-05-30 19:30:26 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
if value, ok := m.items[key]; ok {
|
2014-12-09 04:29:25 +00:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
value = newEntry()
|
2016-05-30 19:30:26 +00:00
|
|
|
m.items[key] = value
|
2014-12-09 04:29:25 +00:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ConcurrentReadMap) Get(key string, newEntry func() interface{}) interface{} {
|
2016-05-30 19:30:26 +00:00
|
|
|
m.RLock()
|
|
|
|
if value, ok := m.items[key]; ok {
|
|
|
|
m.RUnlock()
|
2014-12-09 04:29:25 +00:00
|
|
|
return value
|
|
|
|
}
|
2016-05-30 19:30:26 +00:00
|
|
|
m.RUnlock()
|
2015-03-10 07:20:31 +00:00
|
|
|
return m.initMapEntry(key, newEntry)
|
2014-12-09 04:29:25 +00:00
|
|
|
}
|
2016-05-30 19:30:26 +00:00
|
|
|
|
|
|
|
func (m *ConcurrentReadMap) Find(key string) (interface{}, bool) {
|
|
|
|
m.RLock()
|
|
|
|
value, ok := m.items[key]
|
|
|
|
m.RUnlock()
|
|
|
|
return value, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ConcurrentReadMap) Items() (itemsCopy []interface{}) {
|
|
|
|
m.RLock()
|
|
|
|
for _, i := range m.items {
|
|
|
|
itemsCopy = append(itemsCopy, i)
|
|
|
|
}
|
2016-06-02 18:06:05 +00:00
|
|
|
m.RUnlock()
|
2016-05-30 19:30:26 +00:00
|
|
|
return itemsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ConcurrentReadMap) Delete(key string) {
|
|
|
|
m.Lock()
|
|
|
|
delete(m.items, key)
|
|
|
|
m.Unlock()
|
|
|
|
}
|