-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmongopersist.go
285 lines (250 loc) · 7.2 KB
/
mongopersist.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package test161
import (
"errors"
"fmt"
"github.com/kevinburke/go.uuid"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type MongoPersistence struct {
session *mgo.Session
dbName string
}
const (
COLLECTION_SUBMISSIONS = "submissions"
COLLECTION_TESTS = "tests"
COLLECTION_STUDENTS = "students"
COLLECTION_TARGETS = "targets"
COLLECTION_USERS = "users"
COLLECTION_USAGE = "usage"
)
func NewMongoPersistence(dial *mgo.DialInfo) (PersistenceManager, error) {
var err error
m := &MongoPersistence{}
if m.session, err = mgo.DialWithInfo(dial); err != nil {
return nil, fmt.Errorf("Mongo Create Session: %s\n", err)
}
m.dbName = dial.Database
return m, nil
}
func (m *MongoPersistence) insertDocument(s *mgo.Session, collection string, data interface{}) error {
c := s.DB(m.dbName).C(collection)
err := c.Insert(data)
return err
}
func (m *MongoPersistence) updateDocumentByID(s *mgo.Session, collection string, id, data interface{}) error {
c := s.DB(m.dbName).C(collection)
err := c.UpdateId(id, data)
return err
}
func (m *MongoPersistence) updateDocument(s *mgo.Session, collection string, selector, data interface{}) error {
c := s.DB(m.dbName).C(collection)
err := c.Update(selector, data)
return err
}
// Update if it exists, otherwise insert
func (m *MongoPersistence) upsertDocument(s *mgo.Session, collection string, selector, data interface{}) error {
c := s.DB(m.dbName).C(collection)
_, err := c.Upsert(selector, data)
return err
}
func (m *MongoPersistence) Close() {
m.session.Close()
}
func getTestUpdateMap(test *Test, what int) bson.M {
changes := bson.M{}
if what&MSG_FIELD_SCORE == MSG_FIELD_SCORE {
changes["points_earned"] = test.PointsEarned
}
if what&MSG_FIELD_STATUS == MSG_FIELD_STATUS {
changes["result"] = test.Result
}
return changes
}
func (m *MongoPersistence) Notify(t interface{}, msg, what int) (err error) {
session := m.session.Copy()
defer session.Close()
switch t.(type) {
default:
{
err = fmt.Errorf("Unexpected type in Notify(): %T", t)
}
case *Test:
{
test := t.(*Test)
switch msg {
case MSG_PERSIST_CREATE:
err = m.insertDocument(session, COLLECTION_TESTS, test)
case MSG_PERSIST_COMPLETE:
err = m.updateDocumentByID(session, COLLECTION_TESTS, test.ID, test)
case MSG_PERSIST_UPDATE:
changes := bson.M{}
if what&MSG_FIELD_SCORE == MSG_FIELD_SCORE {
changes["points_earned"] = test.PointsEarned
}
if what&MSG_FIELD_STATUS == MSG_FIELD_STATUS {
changes["result"] = test.Result
}
if len(changes) > 0 {
err = m.updateDocumentByID(session, COLLECTION_TESTS, test.ID, bson.M{"$set": changes})
}
}
}
case *Command:
{
cmd := t.(*Command)
switch msg {
case MSG_PERSIST_UPDATE:
selector := bson.M{
"_id": cmd.Test.ID,
"commands._id": cmd.ID,
}
changes := bson.M{}
if what&MSG_FIELD_OUTPUT == MSG_FIELD_OUTPUT {
changes["commands.$.output"] = cmd.Output
}
if what&MSG_FIELD_SCORE == MSG_FIELD_SCORE {
changes["commands.$.points_earned"] = cmd.PointsEarned
}
if what&MSG_FIELD_STATUS == MSG_FIELD_STATUS {
changes["commands.$.status"] = cmd.Status
}
err = m.updateDocument(session, COLLECTION_TESTS, selector, bson.M{"$set": changes})
}
}
case *Submission:
{
submission := t.(*Submission)
switch msg {
case MSG_PERSIST_CREATE:
err = m.insertDocument(session, COLLECTION_SUBMISSIONS, submission)
case MSG_PERSIST_COMPLETE:
fallthrough
case MSG_PERSIST_UPDATE:
err = m.updateDocumentByID(session, COLLECTION_SUBMISSIONS, submission.ID, submission)
}
}
case *BuildTest:
{
test := t.(*BuildTest)
switch msg {
case MSG_PERSIST_CREATE:
err = m.insertDocument(session, COLLECTION_TESTS, test)
case MSG_PERSIST_COMPLETE:
err = m.updateDocumentByID(session, COLLECTION_TESTS, test.ID, test)
case MSG_PERSIST_UPDATE:
changes := bson.M{}
if what&MSG_FIELD_STATUS == MSG_FIELD_STATUS {
changes["result"] = test.Result
}
if len(changes) > 0 {
err = m.updateDocumentByID(session, COLLECTION_TESTS, test.ID, bson.M{"$set": changes})
}
}
}
case *BuildCommand:
{
cmd := t.(*BuildCommand)
switch msg {
case MSG_PERSIST_UPDATE:
selector := bson.M{
"_id": cmd.test.ID,
"commands._id": cmd.ID,
}
changes := bson.M{}
if what&MSG_FIELD_OUTPUT == MSG_FIELD_OUTPUT {
changes["commands.$.output"] = cmd.Output
}
if what&MSG_FIELD_STATUS == MSG_FIELD_STATUS {
changes["commands.$.status"] = cmd.Status
}
err = m.updateDocument(session, COLLECTION_TESTS, selector, bson.M{"$set": changes})
}
}
case *Student:
{
student := t.(*Student)
switch msg {
case MSG_PERSIST_UPDATE:
err = m.updateDocumentByID(session, COLLECTION_STUDENTS, student.ID, student)
}
}
case *Target:
{
target := t.(*Target)
switch msg {
case MSG_TARGET_LOAD:
c := session.DB(m.dbName).C(COLLECTION_TARGETS)
targets := []*Target{}
c.Find(bson.M{
"name": target.Name,
"version": target.Version,
}).All(&targets)
if len(targets) == 0 {
// Insert
target.ID = uuid.NewV4().String()
err = m.insertDocument(session, COLLECTION_TARGETS, target)
} else if len(targets) == 1 {
target.ID = targets[0].ID
if target.FileHash != targets[0].FileHash {
// Sanity checks to make sure no one changed a target that has a submission.
// (If this happens in testing, just clear the DB manually)
changeErr := targets[0].isChangeAllowed(target)
// Figure out if there are any submissions with this id. If so, fail.
var submissions []*Submission
subColl := session.DB(m.dbName).C(COLLECTION_SUBMISSIONS)
err = subColl.Find(bson.M{"target_id": target.ID}).Limit(1).All(&submissions)
if err == nil {
if len(submissions) > 0 && changeErr != nil {
err = fmt.Errorf(
"Target details changed and previous submissions exist. Increment the version number of the new target.\n%v", changeErr)
} else {
// Just update it with the new version
err = m.updateDocumentByID(session, COLLECTION_TARGETS, target.ID, target)
}
}
}
} else {
err = errors.New("Multiple targets exist in DB for '" + target.Name + "'")
}
}
}
case *UsageStat:
{
stat := t.(*UsageStat)
switch msg {
case MSG_PERSIST_CREATE:
if len(stat.ID) == 0 {
return errors.New("ID required to upsert UsageStat")
}
selector := bson.M{
"_id": stat.ID,
}
err = m.upsertDocument(session, COLLECTION_USAGE, selector, stat)
}
}
}
return
}
func (m *MongoPersistence) CanRetrieve() bool {
return true
}
func (m *MongoPersistence) Retrieve(what int, who map[string]interface{}, filter map[string]interface{}, res interface{}) error {
session := m.session.Copy()
defer session.Close()
collection := ""
switch what {
case PERSIST_TYPE_STUDENTS:
collection = COLLECTION_STUDENTS
case PERSIST_TYPE_USERS:
collection = COLLECTION_USERS
default:
return errors.New("Persistence: Invalid data type")
}
c := session.DB(m.dbName).C(collection)
query := c.Find(bson.M(who))
if filter != nil {
query = query.Select(bson.M(filter))
}
return query.All(res)
}