Skip to content

Commit

Permalink
CS-4068 add additional tests to within-in-matcher, allow points to be…
Browse files Browse the repository at this point in the history
… array or key/val
  • Loading branch information
Hari Khalsa committed Nov 30, 2012
1 parent f98f206 commit c0888b3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/mongo/db/matcher.cpp
Expand Up @@ -374,15 +374,15 @@ namespace mongo {
if (!strcmp(elt.fieldName(), "$box")) {
BSONObjIterator coordIt(elt.Obj());
BSONElement minE = coordIt.next();
if (Array != minE.type()) { break; }
if (!minE.isABSONObj()) { break; }
if (!coordIt.more()) { break; }
BSONElement maxE = coordIt.next();
if (Array != maxE.type()) { break; }
if (!maxE.isABSONObj()) { break; }
_geo.push_back(GeoMatcher::makeBox(e.fieldName(), minE.Obj(), maxE.Obj()));
} else if (!strcmp(elt.fieldName(), "$center")) {
BSONObjIterator coordIt(elt.Obj());
BSONElement center = coordIt.next();
if (Array != center.type()) { break; }
if (!center.isABSONObj()) { break; }
if (!coordIt.more()) { break; }
BSONElement radius = coordIt.next();
if (!radius.isNumber()) { break; }
Expand All @@ -393,7 +393,7 @@ namespace mongo {
bool valid = true;
while (coordIt.more()) {
BSONElement coord = coordIt.next();
if (Array != coord.type()) { valid = false; break; }
if (!coord.isABSONObj()) { valid = false; break; }
BSONObjIterator numIt(coord.Obj());
if (!numIt.more()) { valid = false; break; }
BSONElement x = numIt.next();
Expand Down Expand Up @@ -995,9 +995,9 @@ namespace mongo {
}
int matches = 0;
for (BSONElementSet::const_iterator i = s.begin(); i != s.end(); ++i) {
if (Array != i->type()) { continue; }
if (!i->isABSONObj()) { continue; }
Point p;
if (!GeoMatcher::pointFromArray(i->Obj(), &p)) { continue; }
if (!GeoMatcher::pointFrom(i->Obj(), &p)) { continue; }
if (it->containsPoint(p)) { ++matches; }
}
if (0 == matches) { return false; }
Expand Down
12 changes: 6 additions & 6 deletions src/mongo/db/matcher.h
Expand Up @@ -63,15 +63,15 @@ namespace mongo {
static GeoMatcher makeBox(const string& field, const BSONObj &min, const BSONObj &max) {
GeoMatcher m(field);
m._isBox = true;
pointFromArray(min, &m._box._min);
pointFromArray(max, &m._box._max);
pointFrom(min, &m._box._min);
pointFrom(max, &m._box._max);
return m;
}

static GeoMatcher makeCircle(const string& field, const BSONObj &center, double rad) {
GeoMatcher m(field);
m._isCircle = true;
pointFromArray(center, &m._center);
pointFrom(center, &m._center);
m._radius = rad;
return m;
}
Expand All @@ -85,7 +85,7 @@ namespace mongo {
while (coordIt.more()) {
BSONElement coord = coordIt.next();
Point p;
pointFromArray(coord.Obj(), &p);
pointFrom(coord.Obj(), &p);
points.push_back(p);
}
m._polygon = Polygon(points);
Expand Down Expand Up @@ -116,9 +116,9 @@ namespace mongo {
return ss.str();
}

// XXX: change to pointFromJSON and deal with x: foo, y:foo
static bool pointFromArray(const BSONObj o, Point *p) {
static bool pointFrom(const BSONObj o, Point *p) {
BSONObjIterator i(o);
if (!i.more()) { return false; }
BSONElement xe = i.next();
if (!i.more()) { return false; }
BSONElement ye = i.next();
Expand Down
10 changes: 6 additions & 4 deletions src/mongo/dbtests/matchertests.cpp
Expand Up @@ -135,11 +135,12 @@ namespace MatcherTests {
class WithinBox {
public:
void run() {
Matcher m(fromjson("{loc:{$within:{$box:[[4,4],[6,6]]}}}"));
Matcher m(fromjson("{loc:{$within:{$box:[{x:4,y:4},[6,6]]}}}"));
ASSERT(!m.matches(fromjson("{loc: [3,4]}")));
ASSERT(m.matches(fromjson("{loc: [4,4]}")));
ASSERT(m.matches(fromjson("{loc: [5,5]}")));
ASSERT(m.matches(fromjson("{loc: [5,5.1]}")));
ASSERT(m.matches(fromjson("{loc: {x: 5, y:5.1}}")));
}
};

Expand All @@ -149,17 +150,18 @@ namespace MatcherTests {
Matcher m(fromjson("{loc:{$within:{$polygon:[[0,0],[0,5],[5,5],[5,0]]}}}"));
ASSERT(m.matches(fromjson("{loc: [3,4]}")));
ASSERT(m.matches(fromjson("{loc: [4,4]}")));
ASSERT(m.matches(fromjson("{loc: [5,5]}")));
ASSERT(m.matches(fromjson("{loc: {x:5,y:5}}")));
ASSERT(!m.matches(fromjson("{loc: [5,5.1]}")));
ASSERT(!m.matches(fromjson("{loc: {}}")));
}
};

class WithinCenter {
public:
void run() {
Matcher m(fromjson("{loc:{$within:{$center:[[30,30],10]}}}"));
Matcher m(fromjson("{loc:{$within:{$center:[{x:30,y:30},10]}}}"));
ASSERT(!m.matches(fromjson("{loc: [3,4]}")));
ASSERT(m.matches(fromjson("{loc: [30,30]}")));
ASSERT(m.matches(fromjson("{loc: {x:30,y:30}}")));
ASSERT(m.matches(fromjson("{loc: [20,30]}")));
ASSERT(m.matches(fromjson("{loc: [30,20]}")));
ASSERT(m.matches(fromjson("{loc: [40,30]}")));
Expand Down

0 comments on commit c0888b3

Please sign in to comment.