forked from facebookarchive/flashback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops_executor.go
130 lines (105 loc) · 3.07 KB
/
ops_executor.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
package flashback
import (
"errors"
"gopkg.in/mgo.v2"
)
var (
NotSupported = errors.New("op type not supported")
)
type execute func(content Document, collection *mgo.Collection) error
type OpsExecutor struct {
session *mgo.Session
statsCollector IStatsCollector
// keep track of the results retrieved by find(). For verification purpose
// only.
lastResult interface{}
subExecutes map[OpType]execute
}
func OpsExecutorWithStats(session *mgo.Session,
statsCollector IStatsCollector) *OpsExecutor {
e := &OpsExecutor{
session: session,
statsCollector: statsCollector,
}
e.subExecutes = map[OpType]execute{
Query: e.execQuery,
Insert: e.execInsert,
Update: e.execUpdate,
Remove: e.execRemove,
Count: e.execCount,
FindAndModify: e.execFindAndModify,
}
return e
}
func NewOpsExecutor(session *mgo.Session) *OpsExecutor {
return OpsExecutorWithStats(session, NewNullStatsCollector())
}
func (e *OpsExecutor) execQuery(
content Document, coll *mgo.Collection) error {
query := coll.Find(content["query"])
result := []Document{}
if content["ntoreturn"] != nil {
ntoreturn := int(content["ntoreturn"].(float64))
query.Limit(ntoreturn)
}
if content["ntoskip"] != nil {
ntoskip := int(content["ntoskip"].(float64))
query.Skip(ntoskip)
}
err := query.All(&result)
e.lastResult = &result
return err
}
func (e *OpsExecutor) execInsert(content Document, coll *mgo.Collection) error {
return coll.Insert(content["o"])
}
func (e *OpsExecutor) execUpdate(content Document, coll *mgo.Collection) error {
return coll.Update(content["query"], content["updateobj"])
}
func (e *OpsExecutor) execRemove(content Document, coll *mgo.Collection) error {
return coll.Remove(content["query"])
}
func (e *OpsExecutor) execCount(content Document, coll *mgo.Collection) error {
_, err := coll.Count()
return err
}
func (e *OpsExecutor) execFindAndModify(content Document, coll *mgo.Collection) error {
result := Document{}
change := mgo.Change{Update: content["update"].(map[string]interface{})}
_, err := coll.Find(content["query"]).Apply(change, result)
return err
}
// We only support handful op types. This function helps us to process supported
// ops in a universal way.
//
// We do not canonicalize the ops in OpsReader because we hope ops reader to do
// its job honestly and the consumer of these ops decide how to further process
// the original ops.
func canonicalizeOp(op *Op) *Op {
if op.Type != Command {
return op
}
cmd := op.Content["command"].(map[string]interface{})
for _, name := range []string{"findandmodify", "count"} {
collName, exist := cmd[name]
if !exist {
continue
}
op.Type = OpType("command." + name)
op.Collection = collName.(string)
op.Content = cmd
return op
}
return nil
}
func (e *OpsExecutor) Execute(op *Op) error {
op = canonicalizeOp(op)
if op == nil {
return NotSupported
}
e.statsCollector.StartOp(op.Type)
defer e.statsCollector.EndOp()
content := op.Content
coll := e.session.DB(op.Database).C(op.Collection)
return e.subExecutes[op.Type](content, coll)
}