-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_store.go
188 lines (154 loc) · 4.03 KB
/
data_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
181
182
183
184
185
186
187
188
package letsrest
import (
"errors"
"github.com/rs/xid"
"strings"
"sync"
)
type DataStore interface {
CanSetResponse
GetRequest(id string) (*Request, error)
CreateRequest(user *User, name string) (*Request, error)
EditRequest(id, name string) (*Request, error)
ExecRequest(id string, data *RequestData) (*Request, error)
CopyRequest(user *User, id string) (*Request, error)
List(user *User) (requests []*Request, err error)
Delete(id string) error
PutUser(*User) error
GetUser(id string) (*User, error)
}
func NewDataStore(config *Config, wp WorkerPool) DataStore {
return NewMongoDataStore(config, wp)
}
func NewMapDataStore(wp WorkerPool) *MapDataStore {
store := &MapDataStore{
wp: wp,
requestsByUser: make(map[string][]*Request),
requests: make(map[string]*Request),
users: make(map[string]*User),
}
return store
}
type MapDataStore struct {
sync.RWMutex // protecting maps
requestsByUser map[string][]*Request
requests map[string]*Request
users map[string]*User
wp WorkerPool
}
func (s *MapDataStore) PutUser(user *User) error {
s.Lock()
defer s.Unlock()
s.users[user.ID] = user
return nil
}
func (s *MapDataStore) GetUser(id string) (*User, error) {
s.RLock()
defer s.RUnlock()
if user, ok := s.users[id]; ok {
return user, nil
}
return nil, nil
}
func (s *MapDataStore) GetRequest(id string) (*Request, error) {
s.RLock()
defer s.RUnlock()
request := s.requests[id]
return request, nil
}
func (s *MapDataStore) CreateRequest(user *User, name string) (*Request, error) {
request := &Request{
ID: s.generateId(),
Name: name,
Status: &ExecStatus{Status: "idle"},
UserID: user.ID,
}
s.Lock()
defer s.Unlock()
s.requests[request.ID] = request
s.requestsByUser[user.ID] = append(s.requestsByUser[user.ID], request)
return request, nil
}
func (s *MapDataStore) generateId() string {
return strings.Replace(xid.New().String(), "-", "", -1)
}
func (s *MapDataStore) ExecRequest(id string, data *RequestData) (*Request, error) {
s.Lock()
defer s.Unlock()
if request, ok := s.requests[id]; ok {
request.RequestData = data
request.Status.Status = "in_progress"
request.Status.Error = ""
s.wp.AddRequest(request, s)
return request, nil
}
return nil, errors.New("request not found")
}
func (s *MapDataStore) EditRequest(id, name string) (*Request, error) {
s.Lock()
defer s.Unlock()
if request, ok := s.requests[id]; ok {
request.Name = name
return request, nil
}
return nil, errors.New("request not found")
}
func (s *MapDataStore) CopyRequest(user *User, id string) (*Request, error) {
s.Lock()
defer s.Unlock()
if request, ok := s.requests[id]; ok {
newRequest := &Request{
ID: s.generateId(),
Name: request.Name,
Status: &ExecStatus{Status: "idle"},
UserID: user.ID,
}
if request.RequestData != nil {
newRequest.RequestData = &*request.RequestData
}
s.requests[newRequest.ID] = newRequest
s.requestsByUser[user.ID] = append(s.requestsByUser[user.ID], newRequest)
return newRequest, nil
}
return nil, errors.New("request not found")
}
func (s *MapDataStore) List(user *User) (requests []*Request, err error) {
if len(s.requestsByUser[user.ID]) == 0 {
return make([]*Request, 0), nil
}
s.RLock()
defer s.RUnlock()
return s.requestsByUser[user.ID][:], nil
}
func (s *MapDataStore) Delete(id string) error {
s.Lock()
defer s.Unlock()
request, ok := s.requests[id]
if !ok {
return nil
}
delete(s.requests, id)
userRequests := s.requestsByUser[request.UserID]
for i := range userRequests {
if userRequests[i].ID == id {
s.requestsByUser[request.UserID] = append(userRequests[:i], userRequests[i+1:]...)
}
}
return nil
}
func (s *MapDataStore) SetResponse(id string, response *Response, err error) error {
s.Lock()
defer s.Unlock()
request, ok := s.requests[id]
if !ok {
return errors.New("request.not.found")
}
if err != nil {
request.Status.Status = "error"
request.Status.Error = err.Error()
} else {
request.Status.Status = "done"
}
request.Response = response
return nil
}