summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorQingyang Chen <qingyang.chen@10gen.com>2015-06-04 14:11:43 -0400
committerDavid Storch <david.storch@10gen.com>2015-06-10 13:48:05 -0400
commit8fa3e031acda17e3ec75a2914f6713baeb0d2083 (patch)
treeb784e0c4b57d07847fc1184036b59f94408488ed /src
parent2400048495d870ead8ef09f2a1393f67c4b66a5e (diff)
downloadmongo-8fa3e031acda17e3ec75a2914f6713baeb0d2083.tar.gz
SERVER-16912 finish implementation of GeoMatchExpression::equivalent().
Closes #982 Signed-off-by: David Storch <david.storch@10gen.com>
Diffstat (limited to 'src')
-rw-r--r--src/mongo/db/matcher/expression_geo.cpp8
-rw-r--r--src/mongo/db/matcher/expression_geo_test.cpp108
2 files changed, 106 insertions, 10 deletions
diff --git a/src/mongo/db/matcher/expression_geo.cpp b/src/mongo/db/matcher/expression_geo.cpp
index 2db0da16e47..e8f0fac31d5 100644
--- a/src/mongo/db/matcher/expression_geo.cpp
+++ b/src/mongo/db/matcher/expression_geo.cpp
@@ -371,9 +371,7 @@ namespace mongo {
if ( path() != realOther->path() )
return false;
- // TODO:
- // return _query == realOther->_query;
- return false;
+ return _rawObj == realOther->_rawObj;
}
LeafMatchExpression* GeoMatchExpression::shallowClone() const {
@@ -428,9 +426,7 @@ namespace mongo {
if ( path() != realOther->path() )
return false;
- // TODO:
- // return _query == realOther->_query;
- return false;
+ return _rawObj == realOther->_rawObj;
}
LeafMatchExpression* GeoNearMatchExpression::shallowClone() const {
diff --git a/src/mongo/db/matcher/expression_geo_test.cpp b/src/mongo/db/matcher/expression_geo_test.cpp
index 21ec6785b60..67d51f5580c 100644
--- a/src/mongo/db/matcher/expression_geo_test.cpp
+++ b/src/mongo/db/matcher/expression_geo_test.cpp
@@ -37,15 +37,14 @@
#include "mongo/db/matcher/matcher.h"
#include "mongo/db/matcher/expression.h"
#include "mongo/db/matcher/expression_geo.h"
+#include "mongo/stdx/memory.h"
namespace mongo {
- using std::auto_ptr;
-
TEST( ExpressionGeoTest, Geo1 ) {
BSONObj query = fromjson("{loc:{$within:{$box:[{x: 4, y:4},[6,6]]}}}");
- auto_ptr<GeoExpression> gq(new GeoExpression);
+ std::unique_ptr<GeoExpression> gq(new GeoExpression);
ASSERT_OK( gq->parseFrom( query["loc"].Obj() ) );
GeoMatchExpression ge;
@@ -62,7 +61,7 @@ namespace mongo {
TEST(ExpressionGeoTest, GeoNear1) {
BSONObj query = fromjson("{loc:{$near:{$maxDistance:100, "
"$geometry:{type:\"Point\", coordinates:[0,0]}}}}");
- auto_ptr<GeoNearExpression> nq(new GeoNearExpression);
+ std::unique_ptr<GeoNearExpression> nq(new GeoNearExpression);
ASSERT_OK(nq->parseFrom(query["loc"].Obj()));
GeoNearMatchExpression gne;
@@ -74,4 +73,105 @@ namespace mongo {
ASSERT_EQUALS(gne.getData().maxDistance, 100);
}
+ std::unique_ptr<GeoMatchExpression> makeGeoMatchExpression(const BSONObj& locQuery) {
+ std::unique_ptr<GeoExpression> gq(new GeoExpression);
+ ASSERT_OK(gq->parseFrom(locQuery));
+
+ std::unique_ptr<GeoMatchExpression> ge = stdx::make_unique<GeoMatchExpression>();
+ ASSERT_OK(ge->init("a", gq.release(), locQuery));
+
+ return ge;
+ }
+
+ std::unique_ptr<GeoNearMatchExpression> makeGeoNearMatchExpression(const BSONObj& locQuery) {
+ std::unique_ptr<GeoNearExpression> nq(new GeoNearExpression);
+ ASSERT_OK(nq->parseFrom(locQuery));
+
+ std::unique_ptr<GeoNearMatchExpression> gne = stdx::make_unique<GeoNearMatchExpression>();
+ ASSERT_OK(gne->init("a", nq.release(), locQuery));
+
+ return gne;
+ }
+
+
+ /**
+ * A bunch of cases in which a geo expression is equivalent() to both itself or to another
+ * expression.
+ */
+ TEST(ExpressionGeoTest, GeoEquivalent) {
+ {
+ BSONObj query = fromjson("{$within: {$box: [{x: 4, y: 4}, [6, 6]]}}");
+ std::unique_ptr<GeoMatchExpression> ge(makeGeoMatchExpression(query));
+ ASSERT(ge->equivalent(ge.get()));
+ }
+ {
+ BSONObj query = fromjson("{$within: {$geometry: {type: 'Polygon',"
+ "coordinates: [[[0, 0], [3, 6], [6, 1], [0, 0]]]}}}");
+ std::unique_ptr<GeoMatchExpression> ge(makeGeoMatchExpression(query));
+ ASSERT(ge->equivalent(ge.get()));
+ }
+ {
+ BSONObj query1 = fromjson("{$within: {$geometry: {type: 'Polygon',"
+ "coordinates: [[[0, 0], [3, 6], [6, 1], [0, 0]]]}}}"),
+ query2 = fromjson("{$within: {$geometry: {type: 'Polygon',"
+ "coordinates: [[[0, 0], [3, 6], [6, 1], [0, 0]]]}}}");
+ std::unique_ptr<GeoMatchExpression> ge1(makeGeoMatchExpression(query1)),
+ ge2(makeGeoMatchExpression(query2));
+ ASSERT(ge1->equivalent(ge2.get()));
+ }
+ }
+
+ /**
+ * A bunch of cases in which a *geoNear* expression is equivalent both to itself or to
+ * another expression.
+ */
+ TEST(ExpressionGeoTest, GeoNearEquivalent) {
+ {
+ BSONObj query = fromjson("{$near: {$maxDistance: 100, "
+ "$geometry: {type: 'Point', coordinates: [0, 0]}}}");
+ std::unique_ptr<GeoNearMatchExpression> gne(makeGeoNearMatchExpression(query));
+ ASSERT(gne->equivalent(gne.get()));
+ }
+ {
+ BSONObj query = fromjson("{$near: {$minDistance: 10, $maxDistance: 100,"
+ "$geometry: {type: 'Point', coordinates: [0, 0]}}}");
+ std::unique_ptr<GeoNearMatchExpression> gne(makeGeoNearMatchExpression(query));
+ ASSERT(gne->equivalent(gne.get()));
+ }
+ {
+ BSONObj query1 = fromjson("{$near: {$maxDistance: 100, "
+ "$geometry: {type: 'Point', coordinates: [1, 0]}}}"),
+ query2 = fromjson("{$near: {$maxDistance: 100, "
+ "$geometry: {type: 'Point', coordinates: [1, 0]}}}");
+ std::unique_ptr<GeoNearMatchExpression> gne1(makeGeoNearMatchExpression(query1)),
+ gne2(makeGeoNearMatchExpression(query2));
+ ASSERT(gne1->equivalent(gne2.get()));
+ }
+ }
+
+ /**
+ * A geo expression being not equivalent to another expression.
+ */
+ TEST(ExpressionGeoTest, GeoNotEquivalent) {
+ BSONObj query1 = fromjson("{$within: {$geometry: {type: 'Polygon',"
+ "coordinates: [[[0, 0], [3, 6], [6, 1], [0, 0]]]}}}"),
+ query2 = fromjson("{$within: {$geometry: {type: 'Polygon',"
+ "coordinates: [[[0, 0], [3, 6], [6, 2], [0, 0]]]}}}");
+ std::unique_ptr<GeoMatchExpression> ge1(makeGeoMatchExpression(query1)),
+ ge2(makeGeoMatchExpression(query2));
+ ASSERT(!ge1->equivalent(ge2.get()));
+ }
+
+ /**
+ * A *geoNear* expression being not equivalent to another expression.
+ */
+ TEST(ExpressionGeoTest, GeoNearNotEquivalent) {
+ BSONObj query1 = fromjson("{$near: {$maxDistance: 100, "
+ "$geometry: {type: 'Point', coordinates: [0, 0]}}}"),
+ query2 = fromjson("{$near: {$maxDistance: 100, "
+ "$geometry: {type: 'Point', coordinates: [1, 0]}}}");
+ std::unique_ptr<GeoNearMatchExpression> gne1(makeGeoNearMatchExpression(query1)),
+ gne2(makeGeoNearMatchExpression(query2));
+ ASSERT(!gne1->equivalent(gne2.get()));
+ }
}