basic support for EXPLAIN and EVAL

This commit is contained in:
Kamil Dziedzic
2017-09-06 11:22:45 +02:00
parent 6fa8dc5161
commit 185076f8ec
6 changed files with 52 additions and 3 deletions

View File

@@ -84,6 +84,8 @@ func TestExplain(t *testing.T) {
"insert": "Cannot explain cmd: insert",
"mapreduce": "Cannot explain cmd: mapReduce",
"update": "<nil>",
"explain": "Cannot explain cmd: explain",
"eval": "Cannot explain cmd: eval",
}
expectError := map[string]string{}

View File

@@ -81,20 +81,31 @@ func (f *Fingerprint) Fingerprint(doc proto.SystemProfile) (string, error) {
switch doc.Op {
case "remove", "update":
op = doc.Op
ns := strings.Split(doc.Ns, ".")
ns := strings.SplitN(doc.Ns, ".", 2)
if len(ns) == 2 {
collection = ns[1]
}
case "insert":
op = doc.Op
ns := strings.Split(doc.Ns, ".")
ns := strings.SplitN(doc.Ns, ".", 2)
if len(ns) == 2 {
collection = ns[1]
}
retKeys = []string{}
case "query":
// EXPLAIN MongoDB 2.6:
// "query" : {
// "query" : {
//
// },
// "$explain" : true
// },
if _, ok := doc.Query.Map()["$explain"]; ok {
op = "explain"
break
}
op = "find"
ns := strings.Split(doc.Ns, ".")
ns := strings.SplitN(doc.Ns, ".", 2)
if len(ns) == 2 {
collection = ns[1]
}
@@ -140,6 +151,12 @@ func (f *Fingerprint) Fingerprint(doc proto.SystemProfile) (string, error) {
}
case "geoNear":
retKeys = []string{}
case "explain":
retKeys = []string{}
case "$eval":
op = "eval"
collection = ""
retKeys = []string{}
}
}

View File

@@ -113,6 +113,16 @@ func TestFingerprints(t *testing.T) {
"distinct_3.2.16": "DISTINCT coll a,b",
"distinct_3.4.7": "DISTINCT coll a,b",
"distinct_3.5.11": "DISTINCT coll a,b",
"explain_2.6.12": "EXPLAIN",
"explain_3.0.15": "EXPLAIN",
"explain_3.2.16": "EXPLAIN",
"explain_3.4.7": "EXPLAIN",
"explain_3.5.11": "EXPLAIN",
"eval_2.6.12": "EVAL",
"eval_3.0.15": "EVAL",
"eval_3.2.16": "EVAL",
"eval_3.4.7": "EVAL",
"eval_3.5.11": "EVAL",
"find_empty_2.6.12": "FIND coll",
"find_empty_3.0.15": "FIND coll",
"find_empty_3.2.16": "FIND coll",

View File

@@ -120,6 +120,22 @@ func (self ExampleQuery) ExplainCmd() bson.D {
if cmd.Len() == 0 {
cmd = self.Query
}
// MongoDB 2.6:
//
// "query" : {
// "query" : {
//
// },
// "$explain" : true
// },
if _, ok := cmd.Map()["$explain"]; ok {
cmd = BsonD{
{"explain", ""},
}
break
}
if cmd.Len() == 0 || cmd[0].Name != "find" {
var filter interface{}
if cmd.Len() > 0 && cmd[0].Name == "query" {

View File

@@ -0,0 +1 @@
db.eval("db")

View File

@@ -0,0 +1,3 @@
var coll = db.coll
coll.find().explain()