summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/index_bounds_builder_test.cpp
diff options
context:
space:
mode:
authorDavid Storch <david.storch@10gen.com>2014-02-12 17:21:12 -0500
committerDavid Storch <david.storch@10gen.com>2014-02-14 19:17:18 -0500
commitb855e3e54b2cbe9e93ab97866b37beef53d12696 (patch)
tree91a0632c365c38628c4c5b3749083ecdf144a932 /src/mongo/db/query/index_bounds_builder_test.cpp
parent5a7ecde80c028947a9b29e82fe461013991e3d07 (diff)
downloadmongo-b855e3e54b2cbe9e93ab97866b37beef53d12696.tar.gz
SERVER-12532 negated predicates can use an index
Diffstat (limited to 'src/mongo/db/query/index_bounds_builder_test.cpp')
-rw-r--r--src/mongo/db/query/index_bounds_builder_test.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/mongo/db/query/index_bounds_builder_test.cpp b/src/mongo/db/query/index_bounds_builder_test.cpp
index 7712daad4e5..4be2f4dea22 100644
--- a/src/mongo/db/query/index_bounds_builder_test.cpp
+++ b/src/mongo/db/query/index_bounds_builder_test.cpp
@@ -989,4 +989,66 @@ namespace {
ASSERT(!testSingleInterval(bounds));
}
+ //
+ // Complementing bounds for negations
+ //
+
+ /**
+ * Get a BSONObj which represents the interval from
+ * MinKey to 'end'.
+ */
+ BSONObj minKeyIntObj(int end) {
+ BSONObjBuilder bob;
+ bob.appendMinKey("");
+ bob.appendNumber("", end);
+ return bob.obj();
+ }
+
+ /**
+ * Get a BSONObj which represents the interval from
+ * 'start' to MaxKey.
+ */
+ BSONObj maxKeyIntObj(int start) {
+ BSONObjBuilder bob;
+ bob.appendNumber("", start);
+ bob.appendMaxKey("");
+ return bob.obj();
+ }
+
+ // Expected oil: [MinKey, 3), (3, MaxKey]
+ TEST(IndexBoundsBuilderTest, SimpleNE) {
+ IndexEntry testIndex = IndexEntry(BSONObj());
+ BSONObj obj = BSON("a" << BSON("$ne" << 3));
+ auto_ptr<MatchExpression> expr(parseMatchExpression(obj));
+ BSONElement elt = obj.firstElement();
+ OrderedIntervalList oil;
+ IndexBoundsBuilder::BoundsTightness tightness;
+ IndexBoundsBuilder::translate(expr.get(), elt, testIndex, &oil, &tightness);
+ ASSERT_EQUALS(oil.name, "a");
+ ASSERT_EQUALS(oil.intervals.size(), 2U);
+ ASSERT_EQUALS(Interval::INTERVAL_EQUALS, oil.intervals[0].compare(
+ Interval(minKeyIntObj(3), true, false)));
+ ASSERT_EQUALS(Interval::INTERVAL_EQUALS, oil.intervals[1].compare(
+ Interval(maxKeyIntObj(3), false, true)));
+ ASSERT_EQUALS(tightness, IndexBoundsBuilder::EXACT);
+ }
+
+ TEST(IndexBoundsBuilderTest, IntersectWithNE) {
+ IndexEntry testIndex = IndexEntry(BSONObj());
+ vector<BSONObj> toIntersect;
+ toIntersect.push_back(fromjson("{a: {$gt: 1}}"));
+ toIntersect.push_back(fromjson("{a: {$ne: 2}}}"));
+ toIntersect.push_back(fromjson("{a: {$lte: 6}}"));
+ OrderedIntervalList oil;
+ IndexBoundsBuilder::BoundsTightness tightness;
+ testTranslateAndIntersect(toIntersect, &oil, &tightness);
+ ASSERT_EQUALS(oil.name, "a");
+ ASSERT_EQUALS(oil.intervals.size(), 2U);
+ ASSERT_EQUALS(Interval::INTERVAL_EQUALS, oil.intervals[0].compare(
+ Interval(BSON("" << 1 << "" << 2), false, false)));
+ ASSERT_EQUALS(Interval::INTERVAL_EQUALS, oil.intervals[1].compare(
+ Interval(BSON("" << 2 << "" << 6), false, true)));
+ ASSERT_EQUALS(tightness, IndexBoundsBuilder::EXACT);
+ }
+
} // namespace