-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
103 lines (89 loc) · 1.89 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package fwk
import (
"reflect"
)
type achan chan interface{}
type datastore struct {
SvcBase
store map[string]achan
quit chan struct{}
}
func (ds *datastore) Configure(ctx Context) error {
return nil
}
func (ds *datastore) Get(k string) (interface{}, error) {
ch, ok := ds.store[k]
if !ok {
return nil, Errorf("Store.Get: no such key [%v]", k)
}
select {
case v, ok := <-ch:
if !ok {
return nil, Errorf("%s: closed channel for key [%s]", ds.Name(), k)
}
ch <- v
return v, nil
case <-ds.quit:
return nil, Errorf("%s: timeout to get [%s]", ds.Name(), k)
}
}
func (ds *datastore) Put(k string, v interface{}) error {
select {
case ds.store[k] <- v:
return nil
case <-ds.quit:
return Errorf("%s: timeout to put [%s]", ds.Name(), k)
}
}
func (ds *datastore) Has(k string) bool {
_, ok := ds.store[k]
return ok
}
func (ds *datastore) StartSvc(ctx Context) error {
ds.store = make(map[string]achan)
return nil
}
func (ds *datastore) StopSvc(ctx Context) error {
ds.store = make(map[string]achan)
return nil
}
// reset deletes the payload and resets the associated channel
func (ds *datastore) reset(keys []string) error {
var err error
for _, k := range keys {
ch, ok := ds.store[k]
if ok {
select {
case vv := <-ch:
if vv, ok := vv.(Deleter); ok {
err = vv.Delete()
if err != nil {
return err
}
}
default:
}
}
ds.store[k] = make(achan, 1)
}
ds.quit = make(chan struct{})
return err
}
// close notifies components hanging on store.Get or .Put that event has been aborted
func (ds *datastore) close() {
close(ds.quit)
}
func init() {
Register(reflect.TypeOf(datastore{}),
func(typ, name string, mgr App) (Component, error) {
return &datastore{
SvcBase: NewSvc(typ, name, mgr),
store: make(map[string]achan),
quit: make(chan struct{}),
}, nil
},
)
}
// interface tests
var _ Store = (*datastore)(nil)
// EOF