Skip to content

Commit f470795

Browse files
authored
Merge pull request #17 from globalsign/feature/fmpwizard-count-maxtime-hint
Support index hints & deadlines for Count
2 parents d3b6a6e + e7068d7 commit f470795

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ script:
4646
- (cd bson && go test -check.v)
4747
- go test -check.v -fast
4848
- (cd txn && go test -check.v)
49+
- make stopdb
4950

5051
# vim:sw=4:ts=4:et

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
1818
* Fixes timezone handling ([details](https://github.com/go-mgo/mgo/pull/464))
1919
* Improved multi-document transaction performance ([details](https://github.com/globalsign/mgo/pull/10), [more](https://github.com/globalsign/mgo/pull/11), [more](https://github.com/globalsign/mgo/pull/16))
2020
* Fixes cursor timeouts ([detials](https://jira.mongodb.org/browse/SERVER-24899))
21+
* Support index hints and timeouts for count queries ([details](https://github.com/globalsign/mgo/pull/17))
2122

2223
---
2324

2425
### Thanks to
2526
* @BenLubar
2627
* @carter2000
2728
* @cezarsa
28-
* @eaglerayp
2929
* @drichelson
30+
* @eaglerayp
31+
* @fmpwizard
3032
* @jameinel
3133
* @Reenjii
3234
* @smoya

session.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4086,10 +4086,12 @@ func (iter *Iter) getMoreCmd() *queryOp {
40864086
}
40874087

40884088
type countCmd struct {
4089-
Count string
4090-
Query interface{}
4091-
Limit int32 ",omitempty"
4092-
Skip int32 ",omitempty"
4089+
Count string
4090+
Query interface{}
4091+
Limit int32 ",omitempty"
4092+
Skip int32 ",omitempty"
4093+
Hint bson.D `bson:"hint,omitempty"`
4094+
MaxTimeMS int `bson:"maxTimeMS,omitempty"`
40934095
}
40944096

40954097
// Count returns the total number of documents in the result set.
@@ -4111,8 +4113,12 @@ func (q *Query) Count() (n int, err error) {
41114113
if query == nil {
41124114
query = bson.D{}
41134115
}
4116+
// not checking the error because if type assertion fails, we
4117+
// simply want a Zero bson.D
4118+
hint, _ := q.op.options.Hint.(bson.D)
41144119
result := struct{ N int }{}
4115-
err = session.DB(dbname).Run(countCmd{cname, query, limit, op.skip}, &result)
4120+
err = session.DB(dbname).Run(countCmd{cname, query, limit, op.skip, hint, op.options.MaxTimeMS}, &result)
4121+
41164122
return result.N, err
41174123
}
41184124

session_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,49 @@ func (s *S) TestCountSkipLimit(c *C) {
12751275
c.Assert(n, Equals, 4)
12761276
}
12771277

1278+
func (s *S) TestCountMaxTimeMS(c *C) {
1279+
if !s.versionAtLeast(2, 6) {
1280+
c.Skip("SetMaxTime only supported in 2.6+")
1281+
}
1282+
1283+
session, err := mgo.Dial("localhost:40001")
1284+
c.Assert(err, IsNil)
1285+
defer session.Close()
1286+
1287+
coll := session.DB("mydb").C("mycoll")
1288+
1289+
ns := make([]int, 100000)
1290+
for _, n := range ns {
1291+
err := coll.Insert(M{"n": n})
1292+
c.Assert(err, IsNil)
1293+
}
1294+
_, err = coll.Find(M{"n": M{"$gt": 1}}).SetMaxTime(1 * time.Millisecond).Count()
1295+
e := err.(*mgo.QueryError)
1296+
// We hope this query took longer than 1 ms, which triggers an error code 50
1297+
c.Assert(e.Code, Equals, 50)
1298+
1299+
}
1300+
1301+
func (s *S) TestCountHint(c *C) {
1302+
if !s.versionAtLeast(2, 6) {
1303+
c.Skip("Not implemented until mongo 2.5.5 https://jira.mongodb.org/browse/SERVER-2677")
1304+
}
1305+
1306+
session, err := mgo.Dial("localhost:40001")
1307+
c.Assert(err, IsNil)
1308+
defer session.Close()
1309+
1310+
coll := session.DB("mydb").C("mycoll")
1311+
err = coll.Insert(M{"n": 1})
1312+
c.Assert(err, IsNil)
1313+
1314+
_, err = coll.Find(M{"n": M{"$gt": 1}}).Hint("does_not_exists").Count()
1315+
e := err.(*mgo.QueryError)
1316+
// If Hint wasn't doing anything, then Count would ignore the non existent index hint
1317+
// and return the normal ount. But we instead get an error code 2: bad hint
1318+
c.Assert(e.Code, Equals, 2)
1319+
}
1320+
12781321
func (s *S) TestQueryExplain(c *C) {
12791322
session, err := mgo.Dial("localhost:40001")
12801323
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)