-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.go
180 lines (157 loc) · 3.02 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package store
import (
"errors"
)
var (
// ErrAlreadyExist error returns when Add attempts to add already existing item
ErrAlreadyExist = errors.New("already exists")
// ErrNotFound error returns when Get attempts to get a non existing item
ErrNotFound = errors.New("not found")
)
type storeOp uint8
const (
addItem storeOp = iota + 1
removeItem
getItem
listItems
)
type Storable interface {
Key() string
}
var _ Storable = &item{}
type item struct {
key string
}
func (i *item) Key() string {
return i.key
}
type Manager interface {
Add(Storable) error
Remove(string) error
List() []Storable
Get(string) Storable
Stop()
}
var _ Manager = &itemStore{}
type mgrReply struct {
item []Storable
err error
}
type storeCh struct {
op storeOp
item []Storable
replyCh chan *mgrReply
err chan error
}
type itemStore struct {
stopCh chan struct{}
opCh chan *storeCh
}
func (s *itemStore) Add(i Storable) error {
err := make(chan error)
s.opCh <- &storeCh{
op: addItem,
item: []Storable{i},
err: err,
}
// Return the result of the operation
return <-err
}
func (s *itemStore) Remove(key string) error {
err := make(chan error)
s.opCh <- &storeCh{
op: removeItem,
item: []Storable{&item{key: key}},
err: err,
}
// Just wait for the operation to complete, no result is needed
return <-err
}
func (s *itemStore) Get(key string) Storable {
repl := make(chan *mgrReply)
s.opCh <- &storeCh{
op: getItem,
item: []Storable{
&item{
key: key,
},
},
replyCh: repl,
}
r := <-repl
if len(r.item) == 0 {
return nil
}
return r.item[0]
}
func (s *itemStore) List() []Storable {
repl := make(chan *mgrReply)
s.opCh <- &storeCh{
op: listItems,
replyCh: repl,
}
r := <-repl
return r.item
}
func (s *itemStore) Stop() {
close(s.stopCh)
}
func (s *itemStore) manager() {
items := make(map[string]Storable)
for {
select {
case <-s.stopCh:
return
case msg := <-s.opCh:
switch msg.op {
case addItem:
if _, ok := items[msg.item[0].Key()]; ok {
msg.err <- ErrAlreadyExist
continue
}
items[msg.item[0].Key()] = msg.item[0]
msg.err <- nil
case removeItem:
if _, ok := items[msg.item[0].Key()]; !ok {
msg.err <- ErrNotFound
continue
}
delete(items, msg.item[0].Key())
msg.err <- nil
case getItem:
it, ok := items[msg.item[0].Key()]
if !ok {
msg.replyCh <- &mgrReply{
item: nil,
err: ErrNotFound,
}
continue
}
msg.replyCh <- &mgrReply{
item: []Storable{it},
err: nil,
}
case listItems:
l := make([]Storable, 0)
for _, item := range items {
l = append(l, item)
}
msg.replyCh <- &mgrReply{
item: l,
err: nil,
}
}
}
}
}
// NewStore returns a new instance of a store, any object which is compatible
// with the interface Storable, can be stored in the store.
func NewStore() Manager {
s := &itemStore{
stopCh: make(chan struct{}),
opCh: make(chan *storeCh),
}
// Starting store manager
go s.manager()
return s
}