forked from fission/fission-workflows
/
store.go
49 lines (41 loc) · 975 Bytes
/
store.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 expr
import (
"sync"
)
// TODO Keep old states (but prune if OOM)
// TODO provide garbage collector
type Store struct {
entries sync.Map // map[string]interface{}
}
func NewStore() *Store {
return &Store{
entries: sync.Map{},
}
}
func (rs *Store) Set(id string, data *Scope) {
rs.entries.Store(id, data)
}
func (rs *Store) Delete(id string) {
rs.entries.Delete(id)
}
func (rs *Store) Get(id string) (*Scope, bool) {
i, ok := rs.entries.Load(id)
if !ok {
return nil, ok
}
scope, ok := i.(*Scope)
return scope, ok
}
func (rs *Store) Update(id string, updater func(entry *Scope) *Scope) {
entry, ok := rs.Get(id)
if ok {
rs.Set(id, updater(entry))
}
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
func (rs *Store) Range(fn func(key string, value *Scope) bool) {
rs.entries.Range(func(key, value interface{}) bool {
return fn(key.(string), value.(*Scope))
})
}