diff options
author | Qingyang Chen <qingyang.chen@10gen.com> | 2015-06-19 17:21:47 -0400 |
---|---|---|
committer | Qingyang Chen <qingyang.chen@10gen.com> | 2015-07-06 18:00:21 -0400 |
commit | 32f04880f175fd3a3ce848ca074be72adc6d638c (patch) | |
tree | 040ef2400f925fe39878bdd6b605bce9a83c8290 /src/mongo/db/matcher | |
parent | 8567716e593426868f161a7e5e822486ca93336c (diff) | |
download | mongo-32f04880f175fd3a3ce848ca074be72adc6d638c.tar.gz |
SERVER-16889 StatusWithMatchExpression replace ME* with unique_ptr<ME>
Diffstat (limited to 'src/mongo/db/matcher')
-rw-r--r-- | src/mongo/db/matcher/expression_algo_test.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser.cpp | 250 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser.h | 2 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser_array_test.cpp | 29 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser_geo.cpp | 14 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser_geo_test.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser_leaf_test.cpp | 59 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser_test.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser_text.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser_text_test.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser_tree.cpp | 15 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_parser_tree_test.cpp | 13 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_where.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/matcher/expression_where_noop.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/matcher/matcher.cpp | 9 |
15 files changed, 174 insertions, 245 deletions
diff --git a/src/mongo/db/matcher/expression_algo_test.cpp b/src/mongo/db/matcher/expression_algo_test.cpp index a108a7a7f3b..9effbf6b0b9 100644 --- a/src/mongo/db/matcher/expression_algo_test.cpp +++ b/src/mongo/db/matcher/expression_algo_test.cpp @@ -49,7 +49,7 @@ public: ParsedMatchExpression(const std::string& str) : _obj(fromjson(str)) { StatusWithMatchExpression result = MatchExpressionParser::parse(_obj); ASSERT_OK(result.getStatus()); - _expr.reset(result.getValue()); + _expr = std::move(result.getValue()); } const MatchExpression* get() const { diff --git a/src/mongo/db/matcher/expression_parser.cpp b/src/mongo/db/matcher/expression_parser.cpp index e5fe8ec8cfc..2f27c157f9c 100644 --- a/src/mongo/db/matcher/expression_parser.cpp +++ b/src/mongo/db/matcher/expression_parser.cpp @@ -36,6 +36,7 @@ #include "mongo/db/matcher/expression_array.h" #include "mongo/db/matcher/expression_leaf.h" #include "mongo/db/matcher/expression_tree.h" +#include "mongo/stdx/memory.h" #include "mongo/util/mongoutils/str.h" @@ -74,14 +75,14 @@ StatusWithMatchExpression MatchExpressionParser::_parseComparison(const char* na if (MatchExpression::EQ != cmp->matchType() && RegEx == e.type()) { std::stringstream ss; ss << "Can't have RegEx as arg to predicate over field '" << name << "'."; - return StatusWithMatchExpression(Status(ErrorCodes::BadValue, ss.str())); + return {Status(ErrorCodes::BadValue, ss.str())}; } Status s = temp->init(name, e); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; - return StatusWithMatchExpression(temp.release()); + return {std::move(temp)}; } StatusWithMatchExpression MatchExpressionParser::_parseSubField(const BSONObj& context, @@ -103,13 +104,11 @@ StatusWithMatchExpression MatchExpressionParser::_parseSubField(const BSONObj& c case -1: // $where cannot be a sub-expression because it works on top-level documents only. if (mongoutils::str::equals("$where", e.fieldName())) { - return StatusWithMatchExpression(ErrorCodes::BadValue, - "$where cannot be applied to a field"); + return {Status(ErrorCodes::BadValue, "$where cannot be applied to a field")}; } - return StatusWithMatchExpression(ErrorCodes::BadValue, - mongoutils::str::stream() - << "unknown operator: " << e.fieldName()); + return {Status(ErrorCodes::BadValue, + mongoutils::str::stream() << "unknown operator: " << e.fieldName())}; case BSONObj::LT: return _parseComparison(name, new LTMatchExpression(), e); case BSONObj::LTE: @@ -122,51 +121,50 @@ StatusWithMatchExpression MatchExpressionParser::_parseSubField(const BSONObj& c if (RegEx == e.type()) { // Just because $ne can be rewritten as the negation of an // equality does not mean that $ne of a regex is allowed. See SERVER-1705. - return StatusWithMatchExpression( - Status(ErrorCodes::BadValue, "Can't have regex as arg to $ne.")); + return {Status(ErrorCodes::BadValue, "Can't have regex as arg to $ne.")}; } StatusWithMatchExpression s = _parseComparison(name, new EqualityMatchExpression(), e); if (!s.isOK()) return s; - std::unique_ptr<NotMatchExpression> n(new NotMatchExpression()); - Status s2 = n->init(s.getValue()); + std::unique_ptr<NotMatchExpression> n = stdx::make_unique<NotMatchExpression>(); + Status s2 = n->init(s.getValue().release()); if (!s2.isOK()) - return StatusWithMatchExpression(s2); - return StatusWithMatchExpression(n.release()); + return s2; + return {std::move(n)}; } case BSONObj::Equality: return _parseComparison(name, new EqualityMatchExpression(), e); case BSONObj::opIN: { if (e.type() != Array) - return StatusWithMatchExpression(ErrorCodes::BadValue, "$in needs an array"); - std::unique_ptr<InMatchExpression> temp(new InMatchExpression()); + return {Status(ErrorCodes::BadValue, "$in needs an array")}; + std::unique_ptr<InMatchExpression> temp = stdx::make_unique<InMatchExpression>(); Status s = temp->init(name); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; s = _parseArrayFilterEntries(temp->getArrayFilterEntries(), e.Obj()); if (!s.isOK()) - return StatusWithMatchExpression(s); - return StatusWithMatchExpression(temp.release()); + return s; + return {std::move(temp)}; } case BSONObj::NIN: { if (e.type() != Array) - return StatusWithMatchExpression(ErrorCodes::BadValue, "$nin needs an array"); - std::unique_ptr<InMatchExpression> temp(new InMatchExpression()); + return {Status(ErrorCodes::BadValue, "$nin needs an array")}; + std::unique_ptr<InMatchExpression> temp = stdx::make_unique<InMatchExpression>(); Status s = temp->init(name); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; s = _parseArrayFilterEntries(temp->getArrayFilterEntries(), e.Obj()); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; - std::unique_ptr<NotMatchExpression> temp2(new NotMatchExpression()); + std::unique_ptr<NotMatchExpression> temp2 = stdx::make_unique<NotMatchExpression>(); s = temp2->init(temp.release()); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; - return StatusWithMatchExpression(temp2.release()); + return {std::move(temp2)}; } case BSONObj::opSIZE: { @@ -191,30 +189,31 @@ StatusWithMatchExpression MatchExpressionParser::_parseSubField(const BSONObj& c size = -1; } } else { - return StatusWithMatchExpression(ErrorCodes::BadValue, "$size needs a number"); + return {Status(ErrorCodes::BadValue, "$size needs a number")}; } - std::unique_ptr<SizeMatchExpression> temp(new SizeMatchExpression()); + std::unique_ptr<SizeMatchExpression> temp = stdx::make_unique<SizeMatchExpression>(); Status s = temp->init(name, size); if (!s.isOK()) - return StatusWithMatchExpression(s); - return StatusWithMatchExpression(temp.release()); + return s; + return {std::move(temp)}; } case BSONObj::opEXISTS: { if (e.eoo()) - return StatusWithMatchExpression(ErrorCodes::BadValue, "$exists can't be eoo"); - std::unique_ptr<ExistsMatchExpression> temp(new ExistsMatchExpression()); + return {Status(ErrorCodes::BadValue, "$exists can't be eoo")}; + std::unique_ptr<ExistsMatchExpression> temp = + stdx::make_unique<ExistsMatchExpression>(); Status s = temp->init(name); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; if (e.trueValue()) - return StatusWithMatchExpression(temp.release()); - std::unique_ptr<NotMatchExpression> temp2(new NotMatchExpression()); + return {std::move(temp)}; + std::unique_ptr<NotMatchExpression> temp2 = stdx::make_unique<NotMatchExpression>(); s = temp2->init(temp.release()); if (!s.isOK()) - return StatusWithMatchExpression(s); - return StatusWithMatchExpression(temp2.release()); + return s; + return {std::move(temp2)}; } case BSONObj::opTYPE: @@ -231,10 +230,10 @@ StatusWithMatchExpression MatchExpressionParser::_parseSubField(const BSONObj& c while (i.more()) { BSONElement temp = i.next(); if (temp.getGtLtOp(-1) == BSONObj::opREGEX) - return StatusWithMatchExpression(NULL); + return {nullptr}; } - return StatusWithMatchExpression(ErrorCodes::BadValue, "$options needs a $regex"); + return {Status(ErrorCodes::BadValue, "$options needs a $regex")}; } case BSONObj::opREGEX: { @@ -252,8 +251,8 @@ StatusWithMatchExpression MatchExpressionParser::_parseSubField(const BSONObj& c return expressionParserGeoCallback(name, x, context); } - return StatusWithMatchExpression(ErrorCodes::BadValue, - mongoutils::str::stream() << "not handled: " << e.fieldName()); + return {Status(ErrorCodes::BadValue, + mongoutils::str::stream() << "not handled: " << e.fieldName())}; } StatusWithMatchExpression MatchExpressionParser::_parse(const BSONObj& obj, int level) { @@ -261,10 +260,10 @@ StatusWithMatchExpression MatchExpressionParser::_parse(const BSONObj& obj, int mongoutils::str::stream ss; ss << "exceeded maximum query tree depth of " << kMaximumTreeDepth << " at " << obj.toString(); - return StatusWithMatchExpression(ErrorCodes::BadValue, ss); + return {Status(ErrorCodes::BadValue, ss)}; } - std::unique_ptr<AndMatchExpression> root(new AndMatchExpression()); + std::unique_ptr<AndMatchExpression> root = stdx::make_unique<AndMatchExpression>(); bool topLevel = (level == 0); level++; @@ -278,64 +277,64 @@ StatusWithMatchExpression MatchExpressionParser::_parse(const BSONObj& obj, int // TODO: optimize if block? if (mongoutils::str::equals("or", rest)) { if (e.type() != Array) - return StatusWithMatchExpression(ErrorCodes::BadValue, "$or needs an array"); - std::unique_ptr<OrMatchExpression> temp(new OrMatchExpression()); + return {Status(ErrorCodes::BadValue, "$or needs an array")}; + std::unique_ptr<OrMatchExpression> temp = stdx::make_unique<OrMatchExpression>(); Status s = _parseTreeList(e.Obj(), temp.get(), level); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; root->add(temp.release()); } else if (mongoutils::str::equals("and", rest)) { if (e.type() != Array) - return StatusWithMatchExpression(ErrorCodes::BadValue, "and needs an array"); - std::unique_ptr<AndMatchExpression> temp(new AndMatchExpression()); + return {Status(ErrorCodes::BadValue, "and needs an array")}; + std::unique_ptr<AndMatchExpression> temp = stdx::make_unique<AndMatchExpression>(); Status s = _parseTreeList(e.Obj(), temp.get(), level); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; root->add(temp.release()); } else if (mongoutils::str::equals("nor", rest)) { if (e.type() != Array) - return StatusWithMatchExpression(ErrorCodes::BadValue, "and needs an array"); - std::unique_ptr<NorMatchExpression> temp(new NorMatchExpression()); + return {Status(ErrorCodes::BadValue, "and needs an array")}; + std::unique_ptr<NorMatchExpression> temp = stdx::make_unique<NorMatchExpression>(); Status s = _parseTreeList(e.Obj(), temp.get(), level); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; root->add(temp.release()); } else if (mongoutils::str::equals("atomic", rest) || mongoutils::str::equals("isolated", rest)) { if (!topLevel) - return StatusWithMatchExpression( - ErrorCodes::BadValue, "$atomic/$isolated has to be at the top level"); + return {Status(ErrorCodes::BadValue, + "$atomic/$isolated has to be at the top level")}; if (e.trueValue()) root->add(new AtomicMatchExpression()); } else if (mongoutils::str::equals("where", rest)) { StatusWithMatchExpression s = _whereCallback->parseWhere(e); if (!s.isOK()) return s; - root->add(s.getValue()); + root->add(s.getValue().release()); } else if (mongoutils::str::equals("text", rest)) { if (e.type() != Object) { - return StatusWithMatchExpression(ErrorCodes::BadValue, - "$text expects an object"); + return {Status(ErrorCodes::BadValue, "$text expects an object")}; } StatusWithMatchExpression s = expressionParserTextCallback(e.Obj()); if (!s.isOK()) { return s; } - root->add(s.getValue()); + root->add(s.getValue().release()); } else if (mongoutils::str::equals("comment", rest)) { } else if (mongoutils::str::equals("ref", rest) || mongoutils::str::equals("id", rest) || mongoutils::str::equals("db", rest)) { // DBRef fields. - std::unique_ptr<ComparisonMatchExpression> eq(new EqualityMatchExpression()); + std::unique_ptr<ComparisonMatchExpression> eq = + stdx::make_unique<EqualityMatchExpression>(); Status s = eq->init(e.fieldName(), e); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; root->add(eq.release()); } else { - return StatusWithMatchExpression( - ErrorCodes::BadValue, - mongoutils::str::stream() << "unknown top level operator: " << e.fieldName()); + return {Status(ErrorCodes::BadValue, + mongoutils::str::stream() + << "unknown top level operator: " << e.fieldName())}; } continue; @@ -344,7 +343,7 @@ StatusWithMatchExpression MatchExpressionParser::_parse(const BSONObj& obj, int if (_isExpressionDocument(e, false)) { Status s = _parseSub(e.fieldName(), e.Obj(), root.get(), level); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; continue; } @@ -352,25 +351,26 @@ StatusWithMatchExpression MatchExpressionParser::_parse(const BSONObj& obj, int StatusWithMatchExpression result = _parseRegexElement(e.fieldName(), e); if (!result.isOK()) return result; - root->add(result.getValue()); + root->add(result.getValue().release()); continue; } - std::unique_ptr<ComparisonMatchExpression> eq(new EqualityMatchExpression()); + std::unique_ptr<ComparisonMatchExpression> eq = + stdx::make_unique<EqualityMatchExpression>(); Status s = eq->init(e.fieldName(), e); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; root->add(eq.release()); } if (root->numChildren() == 1) { - const MatchExpression* real = root->getChild(0); + std::unique_ptr<MatchExpression> real(root->getChild(0)); root->clearAndRelease(); - return StatusWithMatchExpression(const_cast<MatchExpression*>(real)); + return {std::move(real)}; } - return StatusWithMatchExpression(root.release()); + return {std::move(root)}; } Status MatchExpressionParser::_parseSub(const char* name, @@ -407,7 +407,7 @@ Status MatchExpressionParser::_parseSub(const char* name, StatusWithMatchExpression s = expressionParserGeoCallback(name, firstElt.getGtLtOp(), sub); if (s.isOK()) { - root->add(s.getValue()); + root->add(s.getValue().release()); } // Propagate geo parsing result to caller. @@ -425,7 +425,7 @@ Status MatchExpressionParser::_parseSub(const char* name, return s.getStatus(); if (s.getValue()) - root->add(s.getValue()); + root->add(s.getValue().release()); } return Status::OK(); @@ -493,47 +493,42 @@ bool MatchExpressionParser::_isDBRefDocument(const BSONObj& obj, bool allowIncom StatusWithMatchExpression MatchExpressionParser::_parseMOD(const char* name, const BSONElement& e) { if (e.type() != Array) - return StatusWithMatchExpression(ErrorCodes::BadValue, - "malformed mod, needs to be an array"); + return {Status(ErrorCodes::BadValue, "malformed mod, needs to be an array")}; BSONObjIterator i(e.Obj()); if (!i.more()) - return StatusWithMatchExpression(ErrorCodes::BadValue, - "malformed mod, not enough elements"); + return {Status(ErrorCodes::BadValue, "malformed mod, not enough elements")}; BSONElement d = i.next(); if (!d.isNumber()) - return StatusWithMatchExpression(ErrorCodes::BadValue, - "malformed mod, divisor not a number"); + return {Status(ErrorCodes::BadValue, "malformed mod, divisor not a number")}; if (!i.more()) - return StatusWithMatchExpression(ErrorCodes::BadValue, - "malformed mod, not enough elements"); + return {Status(ErrorCodes::BadValue, "malformed mod, not enough elements")}; BSONElement r = i.next(); if (!d.isNumber()) - return StatusWithMatchExpression(ErrorCodes::BadValue, - "malformed mod, remainder not a number"); + return {Status(ErrorCodes::BadValue, "malformed mod, remainder not a number")}; if (i.more()) - return StatusWithMatchExpression(ErrorCodes::BadValue, "malformed mod, too many elements"); + return {Status(ErrorCodes::BadValue, "malformed mod, too many elements")}; - std::unique_ptr<ModMatchExpression> temp(new ModMatchExpression()); + std::unique_ptr<ModMatchExpression> temp = stdx::make_unique<ModMatchExpression>(); Status s = temp->init(name, d.numberInt(), r.numberInt()); if (!s.isOK()) - return StatusWithMatchExpression(s); - return StatusWithMatchExpression(temp.release()); + return s; + return {std::move(temp)}; } StatusWithMatchExpression MatchExpressionParser::_parseRegexElement(const char* name, const BSONElement& e) { if (e.type() != RegEx) - return StatusWithMatchExpression(ErrorCodes::BadValue, "not a regex"); + return {Status(ErrorCodes::BadValue, "not a regex")}; - std::unique_ptr<RegexMatchExpression> temp(new RegexMatchExpression()); + std::unique_ptr<RegexMatchExpression> temp = stdx::make_unique<RegexMatchExpression>(); Status s = temp->init(name, e.regex(), e.regexFlags()); if (!s.isOK()) - return StatusWithMatchExpression(s); - return StatusWithMatchExpression(temp.release()); + return s; + return {std::move(temp)}; } StatusWithMatchExpression MatchExpressionParser::_parseRegexDocument(const char* name, @@ -552,15 +547,13 @@ StatusWithMatchExpression MatchExpressionParser::_parseRegexDocument(const char* regex = e.regex(); regexOptions = e.regexFlags(); } else { - return StatusWithMatchExpression(ErrorCodes::BadValue, - "$regex has to be a string"); + return {Status(ErrorCodes::BadValue, "$regex has to be a string")}; } break; case BSONObj::opOPTIONS: if (e.type() != String) - return StatusWithMatchExpression(ErrorCodes::BadValue, - "$options has to be a string"); + return {Status(ErrorCodes::BadValue, "$options has to be a string")}; regexOptions = e.String(); break; default: @@ -568,11 +561,11 @@ StatusWithMatchExpression MatchExpressionParser::_parseRegexDocument(const char* } } - std::unique_ptr<RegexMatchExpression> temp(new RegexMatchExpression()); + std::unique_ptr<RegexMatchExpression> temp = stdx::make_unique<RegexMatchExpression>(); Status s = temp->init(name, regex, regexOptions); if (!s.isOK()) - return StatusWithMatchExpression(s); - return StatusWithMatchExpression(temp.release()); + return s; + return {std::move(temp)}; } Status MatchExpressionParser::_parseArrayFilterEntries(ArrayFilterEntries* entries, @@ -587,7 +580,7 @@ Status MatchExpressionParser::_parseArrayFilterEntries(ArrayFilterEntries* entri } if (e.type() == RegEx) { - std::unique_ptr<RegexMatchExpression> r(new RegexMatchExpression()); + std::unique_ptr<RegexMatchExpression> r = stdx::make_unique<RegexMatchExpression>(); Status s = r->init("", e); if (!s.isOK()) return s; @@ -639,14 +632,14 @@ StatusWithMatchExpression MatchExpressionParser::_parseType(const char* name, return s; } - return temp.release(); + return {std::move(temp)}; } StatusWithMatchExpression MatchExpressionParser::_parseElemMatch(const char* name, const BSONElement& e, int level) { if (e.type() != Object) - return StatusWithMatchExpression(ErrorCodes::BadValue, "$elemMatch needs an Object"); + return {Status(ErrorCodes::BadValue, "$elemMatch needs an Object")}; BSONObj obj = e.Obj(); @@ -676,19 +669,20 @@ StatusWithMatchExpression MatchExpressionParser::_parseElemMatch(const char* nam AndMatchExpression theAnd; Status s = _parseSub("", obj, &theAnd, level); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; - std::unique_ptr<ElemMatchValueMatchExpression> temp(new ElemMatchValueMatchExpression()); + std::unique_ptr<ElemMatchValueMatchExpression> temp = + stdx::make_unique<ElemMatchValueMatchExpression>(); s = temp->init(name); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; for (size_t i = 0; i < theAnd.numChildren(); i++) { temp->add(theAnd.getChild(i)); } theAnd.clearAndRelease(); - return StatusWithMatchExpression(temp.release()); + return {std::move(temp)}; } // DBRef value case @@ -700,31 +694,31 @@ StatusWithMatchExpression MatchExpressionParser::_parseElemMatch(const char* nam StatusWithMatchExpression subRaw = _parse(obj, level); if (!subRaw.isOK()) return subRaw; - std::unique_ptr<MatchExpression> sub(subRaw.getValue()); + std::unique_ptr<MatchExpression> sub = std::move(subRaw.getValue()); // $where is not supported under $elemMatch because $where // applies to top-level document, not array elements in a field. if (hasNode(sub.get(), MatchExpression::WHERE)) { - return StatusWithMatchExpression(ErrorCodes::BadValue, - "$elemMatch cannot contain $where expression"); + return {Status(ErrorCodes::BadValue, "$elemMatch cannot contain $where expression")}; } - std::unique_ptr<ElemMatchObjectMatchExpression> temp(new ElemMatchObjectMatchExpression()); + std::unique_ptr<ElemMatchObjectMatchExpression> temp = + stdx::make_unique<ElemMatchObjectMatchExpression>(); Status status = temp->init(name, sub.release()); if (!status.isOK()) - return StatusWithMatchExpression(status); + return status; - return StatusWithMatchExpression(temp.release()); + return {std::move(temp)}; } StatusWithMatchExpression MatchExpressionParser::_parseAll(const char* name, const BSONElement& e, int level) { if (e.type() != Array) - return StatusWithMatchExpression(ErrorCodes::BadValue, "$all needs an array"); + return {Status(ErrorCodes::BadValue, "$all needs an array")}; BSONObj arr = e.Obj(); - std::unique_ptr<AndMatchExpression> myAnd(new AndMatchExpression()); + std::unique_ptr<AndMatchExpression> myAnd = stdx::make_unique<AndMatchExpression>(); BSONObjIterator i(arr); if (arr.firstElement().type() == Object && @@ -737,73 +731,71 @@ StatusWithMatchExpression MatchExpressionParser::_parseAll(const char* name, if (hopefullyElemMatchElement.type() != Object) { // $all : [ { $elemMatch : ... }, 5 ] - return StatusWithMatchExpression(ErrorCodes::BadValue, - "$all/$elemMatch has to be consistent"); + return {Status(ErrorCodes::BadValue, "$all/$elemMatch has to be consistent")}; } BSONObj hopefullyElemMatchObj = hopefullyElemMatchElement.Obj(); if (!mongoutils::str::equals("$elemMatch", hopefullyElemMatchObj.firstElement().fieldName())) { // $all : [ { $elemMatch : ... }, { x : 5 } ] - return StatusWithMatchExpression(ErrorCodes::BadValue, - "$all/$elemMatch has to be consistent"); + return {Status(ErrorCodes::BadValue, "$all/$elemMatch has to be consistent")}; } StatusWithMatchExpression inner = _parseElemMatch(name, hopefullyElemMatchObj.firstElement(), level); if (!inner.isOK()) return inner; - myAnd->add(inner.getValue()); + myAnd->add(inner.getValue().release()); } - return StatusWithMatchExpression(myAnd.release()); + return {std::move(myAnd)}; } while (i.more()) { BSONElement e = i.next(); if (e.type() == RegEx) { - std::unique_ptr<RegexMatchExpression> r(new RegexMatchExpression()); + std::unique_ptr<RegexMatchExpression> r = stdx::make_unique<RegexMatchExpression>(); Status s = r->init(name, e); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; myAnd->add(r.release()); } else if (e.type() == Object && e.Obj().firstElement().getGtLtOp(-1) != -1) { - return StatusWithMatchExpression(ErrorCodes::BadValue, "no $ expressions in $all"); + return {Status(ErrorCodes::BadValue, "no $ expressions in $all")}; } else { - std::unique_ptr<EqualityMatchExpression> x(new EqualityMatchExpression()); + std::unique_ptr<EqualityMatchExpression> x = + stdx::make_unique<EqualityMatchExpression>(); Status s = x->init(name, e); if (!s.isOK()) - return StatusWithMatchExpression(s); + return s; myAnd->add(x.release()); } } if (myAnd->numChildren() == 0) { - return StatusWithMatchExpression(new FalseMatchExpression()); + return {stdx::make_unique<FalseMatchExpression>()}; } - return StatusWithMatchExpression(myAnd.release()); + return {std::move(myAnd)}; } StatusWithMatchExpression MatchExpressionParser::WhereCallback::parseWhere( const BSONElement& where) const { - return StatusWithMatchExpression(ErrorCodes::NoWhereParseContext, - "no context for parsing $where"); + return {Status(ErrorCodes::NoWhereParseContext, "no context for parsing $where")}; } // Geo StatusWithMatchExpression expressionParserGeoCallbackDefault(const char* name, int type, const BSONObj& section) { - return StatusWithMatchExpression(ErrorCodes::BadValue, "geo not linked in"); + return {Status(ErrorCodes::BadValue, "geo not linked in")}; } MatchExpressionParserGeoCallback expressionParserGeoCallback = expressionParserGeoCallbackDefault; // Text StatusWithMatchExpression expressionParserTextCallbackDefault(const BSONObj& queryObj) { - return StatusWithMatchExpression(ErrorCodes::BadValue, "$text not linked in"); + return {Status(ErrorCodes::BadValue, "$text not linked in")}; } MatchExpressionParserTextCallback expressionParserTextCallback = diff --git a/src/mongo/db/matcher/expression_parser.h b/src/mongo/db/matcher/expression_parser.h index ea331467c2f..e9bb3cf7200 100644 --- a/src/mongo/db/matcher/expression_parser.h +++ b/src/mongo/db/matcher/expression_parser.h @@ -41,7 +41,7 @@ namespace mongo { class OperationContext; -typedef StatusWith<MatchExpression*> StatusWithMatchExpression; +typedef StatusWith<std::unique_ptr<MatchExpression>> StatusWithMatchExpression; class MatchExpressionParser { public: diff --git a/src/mongo/db/matcher/expression_parser_array_test.cpp b/src/mongo/db/matcher/expression_parser_array_test.cpp index 01bd6301d73..29da1ee21c3 100644 --- a/src/mongo/db/matcher/expression_parser_array_test.cpp +++ b/src/mongo/db/matcher/expression_parser_array_test.cpp @@ -50,7 +50,6 @@ TEST(MatchExpressionParserArrayTest, Size1) { ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2)))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1)))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2 << 3)))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, SizeAsString) { @@ -63,7 +62,6 @@ TEST(MatchExpressionParserArrayTest, SizeAsString) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2)))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSONArray()))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1)))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, SizeWithDouble) { @@ -76,7 +74,6 @@ TEST(MatchExpressionParserArrayTest, SizeWithDouble) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1)))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSONArray()))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2 << 3)))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, SizeBad) { @@ -96,7 +93,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchArr1) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2)))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSON("x" << 1))))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSON("x" << 1 << "y" << 2))))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, ElemMatchAnd) { @@ -109,7 +105,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchAnd) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2)))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSON("x" << 1))))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSON("x" << 1 << "y" << 2))))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, ElemMatchNor) { @@ -121,7 +116,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchNor) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2)))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSON("x" << 1))))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSON("x" << 2 << "y" << 2))))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, ElemMatchOr) { @@ -134,7 +128,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchOr) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2)))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSON("x" << 1))))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSON("x" << 1 << "y" << 2))))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, ElemMatchVal1) { @@ -145,7 +138,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchVal1) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << 1))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(4)))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(6)))); - delete result.getValue(); } // with explicit $eq @@ -168,7 +160,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchDBRef1) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << match))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(notMatch)))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(match)))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, ElemMatchDBRef2) { @@ -190,7 +181,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchDBRef2) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << match))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(notMatch)))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(match)))); - delete result.getValue(); } // Additional fields after $ref and $id. @@ -217,7 +207,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchDBRef3) { BSON("x" << BSON_ARRAY(BSON("$ref" << "coll" << "$id" << oid << "foo" << 12345 << "bar" << 678))))); - delete result.getValue(); } // Query with DBRef fields out of order. @@ -244,7 +233,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchDBRef4) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << match))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(notMatch)))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(match)))); - delete result.getValue(); } // Query with DBRef fields out of order. @@ -274,7 +262,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchDBRef5) { BSON("x" << BSON_ARRAY(BSON("$ref" << "coll" << "$id" << oid << "foo" << 12345 << "bar" << 678))))); - delete result.getValue(); } // Incomplete DBRef - $id missing. @@ -303,7 +290,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchDBRef6) { BSON("x" << BSON_ARRAY(BSON("$ref" << "coll" << "$id" << oid << "foo" << 12345 << "bar" << 678))))); - delete result.getValue(); } // Incomplete DBRef - $ref missing. @@ -331,7 +317,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchDBRef7) { BSON("x" << BSON_ARRAY(BSON("$ref" << "coll" << "$id" << oid << "foo" << 12345 << "bar" << 678))))); - delete result.getValue(); } // Incomplete DBRef - $db only. @@ -366,7 +351,6 @@ TEST(MatchExpressionParserArrayTest, ElemMatchDBRef8) { << "$id" << oid << "$db" << "db" << "foo" << 12345 << "bar" << 678))))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, All1) { @@ -383,7 +367,6 @@ TEST(MatchExpressionParserArrayTest, All1) { ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2)))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2 << 3)))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(2 << 3)))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, AllNull) { @@ -398,7 +381,6 @@ TEST(MatchExpressionParserArrayTest, AllNull) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1)))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSONNULL))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSONNULL)))); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, AllBadArg) { @@ -445,7 +427,6 @@ TEST(MatchExpressionParserArrayTest, AllRegex1) { ASSERT(!result.getValue()->matchesSingleElement(notMatchFirst["a"])); ASSERT(!result.getValue()->matchesSingleElement(notMatchSecond["a"])); ASSERT(result.getValue()->matchesSingleElement(matchesBoth["a"])); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, AllRegex2) { @@ -469,7 +450,6 @@ TEST(MatchExpressionParserArrayTest, AllRegex2) { ASSERT(!result.getValue()->matchesSingleElement(notMatchFirst["a"])); ASSERT(result.getValue()->matchesSingleElement(matchesBoth["a"])); - delete result.getValue(); } TEST(MatchExpressionParserArrayTest, AllNonArray) { @@ -484,7 +464,6 @@ TEST(MatchExpressionParserArrayTest, AllNonArray) { ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(5)))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 4))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(4)))); - delete result.getValue(); } @@ -504,7 +483,6 @@ TEST(MatchExpressionParserArrayTest, AllElemMatch1) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(1 << 2)))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSON("x" << 1))))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSON("x" << 1 << "y" << 2))))); - delete result.getValue(); } // $all and $elemMatch on dotted field. @@ -541,7 +519,6 @@ TEST(MatchExpressionParserArrayTest, AllElemMatch2) { // x is an array. ASSERT(result.getValue()->matchesBSON( BSON("x" << BSON_ARRAY(BSON("y" << BSON_ARRAY(BSON("x" << 1 << "z" << 1))))))); - delete result.getValue(); } // Check the structure of the resulting MatchExpression, and make sure that the paths @@ -551,7 +528,7 @@ TEST(MatchExpressionParserArrayTest, AllElemMatch3) { StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> expr(result.getValue()); + std::unique_ptr<MatchExpression> expr = std::move(result.getValue()); // Root node should be an AND with one child. ASSERT_EQUALS(MatchExpression::AND, expr->matchType()); @@ -629,7 +606,6 @@ TEST(MatchExpressionParserArrayTest, AllEmptyString) { << ""))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSONNULL << "")))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSONObj() << "")))); - delete result.getValue(); } // $all with ISO date. @@ -652,7 +628,6 @@ TEST(MatchExpressionParserArrayTest, AllISODate) { ASSERT(result.getValue()->matchesBSON(BSON("x" << match))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSONNULL << match)))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSONObj() << match)))); - delete result.getValue(); } // $all on array element with empty string. @@ -672,7 +647,6 @@ TEST(MatchExpressionParserArrayTest, AllDottedEmptyString) { << ""))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSONNULL << "")))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSONObj() << "")))); - delete result.getValue(); } // $all on array element with ISO date. @@ -697,6 +671,5 @@ TEST(MatchExpressionParserArrayTest, AllDottedISODate) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << match))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSONNULL << match)))); ASSERT(result.getValue()->matchesBSON(BSON("x" << BSON_ARRAY(BSONObj() << match)))); - delete result.getValue(); } } diff --git a/src/mongo/db/matcher/expression_parser_geo.cpp b/src/mongo/db/matcher/expression_parser_geo.cpp index f740a93abac..d706ef65193 100644 --- a/src/mongo/db/matcher/expression_parser_geo.cpp +++ b/src/mongo/db/matcher/expression_parser_geo.cpp @@ -33,23 +33,25 @@ #include "mongo/base/init.h" #include "mongo/db/jsobj.h" #include "mongo/db/matcher/expression_geo.h" +#include "mongo/stdx/memory.h" #include "mongo/util/mongoutils/str.h" namespace mongo { using std::unique_ptr; +using stdx::make_unique; StatusWithMatchExpression expressionParserGeoCallbackReal(const char* name, int type, const BSONObj& section) { if (BSONObj::opWITHIN == type || BSONObj::opGEO_INTERSECTS == type) { - unique_ptr<GeoExpression> gq(new GeoExpression(name)); + unique_ptr<GeoExpression> gq = make_unique<GeoExpression>(name); Status parseStatus = gq->parseFrom(section); if (!parseStatus.isOK()) return StatusWithMatchExpression(parseStatus); - unique_ptr<GeoMatchExpression> e(new GeoMatchExpression()); + unique_ptr<GeoMatchExpression> e = make_unique<GeoMatchExpression>(); // Until the index layer accepts non-BSON predicates, or special indices are moved into // stages, we have to clean up the raw object so it can be passed down to the index @@ -59,15 +61,15 @@ StatusWithMatchExpression expressionParserGeoCallbackReal(const char* name, Status s = e->init(name, gq.release(), bob.obj()); if (!s.isOK()) return StatusWithMatchExpression(s); - return StatusWithMatchExpression(e.release()); + return {std::move(e)}; } else { verify(BSONObj::opNEAR == type); - unique_ptr<GeoNearExpression> nq(new GeoNearExpression(name)); + unique_ptr<GeoNearExpression> nq = make_unique<GeoNearExpression>(name); Status s = nq->parseFrom(section); if (!s.isOK()) { return StatusWithMatchExpression(s); } - unique_ptr<GeoNearMatchExpression> e(new GeoNearMatchExpression()); + unique_ptr<GeoNearMatchExpression> e = make_unique<GeoNearMatchExpression>(); // Until the index layer accepts non-BSON predicates, or special indices are moved into // stages, we have to clean up the raw object so it can be passed down to the index // layer. @@ -76,7 +78,7 @@ StatusWithMatchExpression expressionParserGeoCallbackReal(const char* name, s = e->init(name, nq.release(), bob.obj()); if (!s.isOK()) return StatusWithMatchExpression(s); - return StatusWithMatchExpression(e.release()); + return {std::move(e)}; } } diff --git a/src/mongo/db/matcher/expression_parser_geo_test.cpp b/src/mongo/db/matcher/expression_parser_geo_test.cpp index f86366754a9..b280e54d208 100644 --- a/src/mongo/db/matcher/expression_parser_geo_test.cpp +++ b/src/mongo/db/matcher/expression_parser_geo_test.cpp @@ -44,7 +44,6 @@ TEST(MatchExpressionParserGeo, WithinBox) { StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(!result.getValue()->matchesBSON(fromjson("{a: [3,4]}"))); ASSERT(result.getValue()->matchesBSON(fromjson("{a: [4,4]}"))); @@ -60,9 +59,8 @@ TEST(MatchExpressionParserGeoNear, ParseNear) { StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); - MatchExpression* exp = result.getValue(); + MatchExpression* exp = result.getValue().get(); ASSERT_EQUALS(MatchExpression::GEO_NEAR, exp->matchType()); GeoNearMatchExpression* gnexp = static_cast<GeoNearMatchExpression*>(exp); diff --git a/src/mongo/db/matcher/expression_parser_leaf_test.cpp b/src/mongo/db/matcher/expression_parser_leaf_test.cpp index 1d8e7e0e8d8..47c239dfa01 100644 --- a/src/mongo/db/matcher/expression_parser_leaf_test.cpp +++ b/src/mongo/db/matcher/expression_parser_leaf_test.cpp @@ -49,7 +49,6 @@ TEST(MatchExpressionParserLeafTest, SimpleEQ2) { BSONObj query = BSON("x" << BSON("$eq" << 2)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 1))); ASSERT(result.getValue()->matchesBSON(BSON("x" << 2))); @@ -66,7 +65,6 @@ TEST(MatchExpressionParserLeafTest, SimpleGT1) { BSONObj query = BSON("x" << BSON("$gt" << 2)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 2))); ASSERT(result.getValue()->matchesBSON(BSON("x" << 3))); @@ -76,7 +74,6 @@ TEST(MatchExpressionParserLeafTest, SimpleLT1) { BSONObj query = BSON("x" << BSON("$lt" << 2)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << 1))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 2))); @@ -87,7 +84,6 @@ TEST(MatchExpressionParserLeafTest, SimpleGTE1) { BSONObj query = BSON("x" << BSON("$gte" << 2)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 1))); ASSERT(result.getValue()->matchesBSON(BSON("x" << 2))); @@ -98,7 +94,6 @@ TEST(MatchExpressionParserLeafTest, SimpleLTE1) { BSONObj query = BSON("x" << BSON("$lte" << 2)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << 1))); ASSERT(result.getValue()->matchesBSON(BSON("x" << 2))); @@ -109,7 +104,6 @@ TEST(MatchExpressionParserLeafTest, SimpleNE1) { BSONObj query = BSON("x" << BSON("$ne" << 2)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << 1))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 2))); @@ -120,7 +114,6 @@ TEST(MatchExpressionParserLeafTest, SimpleModBad1) { BSONObj query = BSON("x" << BSON("$mod" << BSON_ARRAY(3 << 2))); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy1(result.getValue()); query = BSON("x" << BSON("$mod" << BSON_ARRAY(3))); result = MatchExpressionParser::parse(query); @@ -147,7 +140,6 @@ TEST(MatchExpressionParserLeafTest, SimpleMod1) { BSONObj query = BSON("x" << BSON("$mod" << BSON_ARRAY(3 << 2))); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << 5))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 4))); @@ -158,7 +150,6 @@ TEST(MatchExpressionParserLeafTest, SimpleModNotNumber) { BSONObj query = BSON("x" << BSON("$mod" << BSON_ARRAY(2 << "r"))); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << 2))); ASSERT(result.getValue()->matchesBSON(BSON("x" << 4))); @@ -172,7 +163,6 @@ TEST(MatchExpressionParserLeafTest, SimpleIN1) { BSONObj query = BSON("x" << BSON("$in" << BSON_ARRAY(2 << 3))); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 1))); ASSERT(result.getValue()->matchesBSON(BSON("x" << 2))); @@ -187,7 +177,6 @@ TEST(MatchExpressionParserLeafTest, INSingleDBRef) { << "db")))); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); OID oidx = OID::gen(); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON("$ref" @@ -250,7 +239,6 @@ TEST(MatchExpressionParserLeafTest, INMultipleDBRef) { << "db")))); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); OID oidx = OID::gen(); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON("$ref" @@ -350,7 +338,6 @@ TEST(MatchExpressionParserLeafTest, INDBRefWithOptionalField1) { << "$id" << oid << "foo" << 12345)))); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); OID oidx = OID::gen(); ASSERT(!result.getValue()->matchesBSON(BSON("x" << BSON("$ref" @@ -452,7 +439,6 @@ TEST(MatchExpressionParserLeafTest, INRegexStuff) { BSONObj query = BSON("a" << operand.obj()); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); BSONObj matchFirst = BSON("a" << "ax"); @@ -478,7 +464,6 @@ TEST(MatchExpressionParserLeafTest, SimpleNIN1) { BSONObj query = BSON("x" << BSON("$nin" << BSON_ARRAY(2 << 3))); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << 1))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 2))); @@ -498,7 +483,6 @@ TEST(MatchExpressionParserLeafTest, Regex1) { BSONObj query = b.obj(); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << "abc"))); @@ -515,7 +499,6 @@ TEST(MatchExpressionParserLeafTest, Regex2) { << "i")); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << "abc"))); @@ -533,7 +516,6 @@ TEST(MatchExpressionParserLeafTest, Regex3) { StatusWithMatchExpression result = MatchExpressionParser::parse(query); log() << "result: " << result.getStatus() << endl; ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << "abc"))); @@ -578,7 +560,6 @@ TEST(MatchExpressionParserLeafTest, ExistsYes1) { BSONObj query = BSON("x" << b.obj()); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << "abc"))); @@ -592,7 +573,6 @@ TEST(MatchExpressionParserLeafTest, ExistsNO1) { BSONObj query = BSON("x" << b.obj()); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(!result.getValue()->matchesBSON(BSON("x" << "abc"))); @@ -604,7 +584,6 @@ TEST(MatchExpressionParserLeafTest, Type1) { BSONObj query = BSON("x" << BSON("$type" << String)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << "abc"))); @@ -615,7 +594,6 @@ TEST(MatchExpressionParserLeafTest, Type2) { BSONObj query = BSON("x" << BSON("$type" << (double)NumberDouble)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(result.getValue()->matchesBSON(BSON("x" << 5.3))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 5))); @@ -625,7 +603,6 @@ TEST(MatchExpressionParserLeafTest, TypeDoubleOperator) { BSONObj query = BSON("x" << BSON("$type" << 1.5)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 5.3))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 5))); @@ -635,7 +612,6 @@ TEST(MatchExpressionParserLeafTest, TypeNull) { BSONObj query = BSON("x" << BSON("$type" << jstNULL)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(!result.getValue()->matchesBSON(BSONObj())); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 5))); @@ -650,7 +626,6 @@ TEST(MatchExpressionParserLeafTest, TypeBadType) { BSONObj query = BSON("x" << b.obj()); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - std::unique_ptr<MatchExpression> destroy(result.getValue()); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 5.3))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 5))); @@ -663,12 +638,12 @@ TEST(MatchExpressionParserLeafTest, TypeBad) { } TEST(MatchExpressionParserLeafTest, TypeBadString) { - ASSERT_NOT_OK(MatchExpressionParser::parse(fromjson("{a: {$type: null}}"))); - ASSERT_NOT_OK(MatchExpressionParser::parse(fromjson("{a: {$type: true}}"))); - ASSERT_NOT_OK(MatchExpressionParser::parse(fromjson("{a: {$type: {}}}}"))); + ASSERT_NOT_OK(MatchExpressionParser::parse(fromjson("{a: {$type: null}}")).getStatus()); + ASSERT_NOT_OK(MatchExpressionParser::parse(fromjson("{a: {$type: true}}")).getStatus()); + ASSERT_NOT_OK(MatchExpressionParser::parse(fromjson("{a: {$type: {}}}}")).getStatus()); ASSERT_NOT_OK(MatchExpressionParser::parse( - fromjson("{a: {$type: ObjectId('000000000000000000000000')}}"))); - ASSERT_NOT_OK(MatchExpressionParser::parse(fromjson("{a: {$type: []}}"))); + fromjson("{a: {$type: ObjectId('000000000000000000000000')}}")).getStatus()); + ASSERT_NOT_OK(MatchExpressionParser::parse(fromjson("{a: {$type: []}}")).getStatus()); } TEST(MatchExpressionParserLeafTest, TypeStringnameDouble) { @@ -676,7 +651,7 @@ TEST(MatchExpressionParserLeafTest, TypeStringnameDouble) { MatchExpressionParser::parse(fromjson("{a: {$type: 'double'}}")); ASSERT(typeNumberDouble.isOK()); TypeMatchExpression* tmeNumberDouble = - static_cast<TypeMatchExpression*>(typeNumberDouble.getValue()); + static_cast<TypeMatchExpression*>(typeNumberDouble.getValue().get()); ASSERT(tmeNumberDouble->getData() == NumberDouble); ASSERT_TRUE(tmeNumberDouble->matchesBSON(fromjson("{a: 5.4}"))); ASSERT_FALSE(tmeNumberDouble->matchesBSON(fromjson("{a: NumberInt(5)}"))); @@ -686,7 +661,8 @@ TEST(MatchExpressionParserLeafTest, TypeStringnameNumberInt) { StatusWithMatchExpression typeNumberInt = MatchExpressionParser::parse(fromjson("{a: {$type: 'int'}}")); ASSERT(typeNumberInt.isOK()); - TypeMatchExpression* tmeNumberInt = static_cast<TypeMatchExpression*>(typeNumberInt.getValue()); + TypeMatchExpression* tmeNumberInt = + static_cast<TypeMatchExpression*>(typeNumberInt.getValue().get()); ASSERT(tmeNumberInt->getData() == NumberInt); ASSERT_TRUE(tmeNumberInt->matchesBSON(fromjson("{a: NumberInt(5)}"))); ASSERT_FALSE(tmeNumberInt->matchesBSON(fromjson("{a: 5.4}"))); @@ -697,7 +673,7 @@ TEST(MatchExpressionParserLeafTest, TypeStringnameNumberLong) { MatchExpressionParser::parse(fromjson("{a: {$type: 'long'}}")); ASSERT(typeNumberLong.isOK()); TypeMatchExpression* tmeNumberLong = - static_cast<TypeMatchExpression*>(typeNumberLong.getValue()); + static_cast<TypeMatchExpression*>(typeNumberLong.getValue().get()); ASSERT(tmeNumberLong->getData() == NumberLong); ASSERT_TRUE(tmeNumberLong->matchesBSON(BSON("a" << static_cast<long long>(-1)))); ASSERT_FALSE(tmeNumberLong->matchesBSON(fromjson("{a: true}"))); @@ -707,7 +683,8 @@ TEST(MatchExpressionParserLeafTest, TypeStringnameString) { StatusWithMatchExpression typeString = MatchExpressionParser::parse(fromjson("{a: {$type: 'string'}}")); ASSERT(typeString.isOK()); - TypeMatchExpression* tmeString = static_cast<TypeMatchExpression*>(typeString.getValue()); + TypeMatchExpression* tmeString = + static_cast<TypeMatchExpression*>(typeString.getValue().get()); ASSERT(tmeString->getData() == String); ASSERT_TRUE(tmeString->matchesBSON(fromjson("{a: 'hello world'}"))); ASSERT_FALSE(tmeString->matchesBSON(fromjson("{a: 5.4}"))); @@ -717,7 +694,8 @@ TEST(MatchExpressionParserLeafTest, TypeStringnamejstOID) { StatusWithMatchExpression typejstOID = MatchExpressionParser::parse(fromjson("{a: {$type: 'objectId'}}")); ASSERT(typejstOID.isOK()); - TypeMatchExpression* tmejstOID = static_cast<TypeMatchExpression*>(typejstOID.getValue()); + TypeMatchExpression* tmejstOID = + static_cast<TypeMatchExpression*>(typejstOID.getValue().get()); ASSERT(tmejstOID->getData() == jstOID); ASSERT_TRUE(tmejstOID->matchesBSON(fromjson("{a: ObjectId('000000000000000000000000')}"))); ASSERT_FALSE(tmejstOID->matchesBSON(fromjson("{a: 'hello world'}"))); @@ -727,7 +705,8 @@ TEST(MatchExpressionParserLeafTest, TypeStringnamejstNULL) { StatusWithMatchExpression typejstNULL = MatchExpressionParser::parse(fromjson("{a: {$type: 'null'}}")); ASSERT(typejstNULL.isOK()); - TypeMatchExpression* tmejstNULL = static_cast<TypeMatchExpression*>(typejstNULL.getValue()); + TypeMatchExpression* tmejstNULL = + static_cast<TypeMatchExpression*>(typejstNULL.getValue().get()); ASSERT(tmejstNULL->getData() == jstNULL); ASSERT_TRUE(tmejstNULL->matchesBSON(fromjson("{a: null}"))); ASSERT_FALSE(tmejstNULL->matchesBSON(fromjson("{a: true}"))); @@ -737,7 +716,7 @@ TEST(MatchExpressionParserLeafTest, TypeStringnameBool) { StatusWithMatchExpression typeBool = MatchExpressionParser::parse(fromjson("{a: {$type: 'bool'}}")); ASSERT(typeBool.isOK()); - TypeMatchExpression* tmeBool = static_cast<TypeMatchExpression*>(typeBool.getValue()); + TypeMatchExpression* tmeBool = static_cast<TypeMatchExpression*>(typeBool.getValue().get()); ASSERT(tmeBool->getData() == Bool); ASSERT_TRUE(tmeBool->matchesBSON(fromjson("{a: true}"))); ASSERT_FALSE(tmeBool->matchesBSON(fromjson("{a: null}"))); @@ -747,7 +726,8 @@ TEST(MatchExpressionParserLeafTest, TypeStringnameObject) { StatusWithMatchExpression typeObject = MatchExpressionParser::parse(fromjson("{a: {$type: 'object'}}")); ASSERT(typeObject.isOK()); - TypeMatchExpression* tmeObject = static_cast<TypeMatchExpression*>(typeObject.getValue()); + TypeMatchExpression* tmeObject = + static_cast<TypeMatchExpression*>(typeObject.getValue().get()); ASSERT(tmeObject->getData() == Object); ASSERT_TRUE(tmeObject->matchesBSON(fromjson("{a: {}}"))); ASSERT_FALSE(tmeObject->matchesBSON(fromjson("{a: []}"))); @@ -757,7 +737,8 @@ TEST(MatchExpressionParserLeafTest, TypeStringnameArray) { StatusWithMatchExpression typeArray = MatchExpressionParser::parse(fromjson("{a: {$type: 'array'}}")); ASSERT(typeArray.isOK()); - TypeMatchExpression* tmeArray = static_cast<TypeMatchExpression*>(typeArray.getValue()); + TypeMatchExpression* tmeArray = + static_cast<TypeMatchExpression*>(typeArray.getValue().get()); ASSERT(tmeArray->getData() == Array); ASSERT_TRUE(tmeArray->matchesBSON(fromjson("{a: [[]]}"))); ASSERT_FALSE(tmeArray->matchesBSON(fromjson("{a: {}}"))); diff --git a/src/mongo/db/matcher/expression_parser_test.cpp b/src/mongo/db/matcher/expression_parser_test.cpp index 5af0c7a2843..4927cd29229 100644 --- a/src/mongo/db/matcher/expression_parser_test.cpp +++ b/src/mongo/db/matcher/expression_parser_test.cpp @@ -46,8 +46,6 @@ TEST(MatchExpressionParserTest, SimpleEQ1) { ASSERT(result.getValue()->matchesBSON(BSON("x" << 2))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 3))); - - delete result.getValue(); } TEST(MatchExpressionParserTest, Multiple1) { @@ -60,20 +58,16 @@ TEST(MatchExpressionParserTest, Multiple1) { ASSERT(!result.getValue()->matchesBSON(BSON("x" << 6 << "y" << 7))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 5 << "y" << 9))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 5 << "y" << 4))); - - delete result.getValue(); } TEST(AtomicMatchExpressionTest, Simple1) { BSONObj query = BSON("x" << 5 << "$atomic" << BSON("$gt" << 5 << "$lt" << 8)); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - delete result.getValue(); query = BSON("x" << 5 << "$isolated" << 1); result = MatchExpressionParser::parse(query); ASSERT_TRUE(result.isOK()); - delete result.getValue(); query = BSON("x" << 5 << "y" << BSON("$isolated" << 1)); result = MatchExpressionParser::parse(query); diff --git a/src/mongo/db/matcher/expression_parser_text.cpp b/src/mongo/db/matcher/expression_parser_text.cpp index de46f9e1169..a01529bee91 100644 --- a/src/mongo/db/matcher/expression_parser_text.cpp +++ b/src/mongo/db/matcher/expression_parser_text.cpp @@ -88,7 +88,7 @@ StatusWithMatchExpression expressionParserTextCallbackReal(const BSONObj& queryO if (!s.isOK()) { return StatusWithMatchExpression(s); } - return StatusWithMatchExpression(e.release()); + return {std::move(e)}; } MONGO_INITIALIZER(MatchExpressionParserText)(::mongo::InitializerContext* context) { diff --git a/src/mongo/db/matcher/expression_parser_text_test.cpp b/src/mongo/db/matcher/expression_parser_text_test.cpp index fe5eaebc0f7..33ab47af773 100644 --- a/src/mongo/db/matcher/expression_parser_text_test.cpp +++ b/src/mongo/db/matcher/expression_parser_text_test.cpp @@ -47,7 +47,7 @@ TEST(MatchExpressionParserText, Basic) { ASSERT_EQUALS(MatchExpression::TEXT, result.getValue()->matchType()); std::unique_ptr<TextMatchExpression> textExp( - static_cast<TextMatchExpression*>(result.getValue())); + static_cast<TextMatchExpression*>(result.getValue().release())); ASSERT_EQUALS(textExp->getQuery(), "awesome"); ASSERT_EQUALS(textExp->getLanguage(), "english"); ASSERT_EQUALS(textExp->getCaseSensitive(), fts::FTSQuery::caseSensitiveDefault); @@ -68,7 +68,7 @@ TEST(MatchExpressionParserText, CaseSensitiveTrue) { ASSERT_EQUALS(MatchExpression::TEXT, result.getValue()->matchType()); std::unique_ptr<TextMatchExpression> textExp( - static_cast<TextMatchExpression*>(result.getValue())); + static_cast<TextMatchExpression*>(result.getValue().release())); ASSERT_EQUALS(textExp->getCaseSensitive(), true); } @@ -80,7 +80,7 @@ TEST(MatchExpressionParserText, CaseSensitiveFalse) { ASSERT_EQUALS(MatchExpression::TEXT, result.getValue()->matchType()); std::unique_ptr<TextMatchExpression> textExp( - static_cast<TextMatchExpression*>(result.getValue())); + static_cast<TextMatchExpression*>(result.getValue().release())); ASSERT_EQUALS(textExp->getCaseSensitive(), false); } diff --git a/src/mongo/db/matcher/expression_parser_tree.cpp b/src/mongo/db/matcher/expression_parser_tree.cpp index 9a3149a8c5e..26ad561e756 100644 --- a/src/mongo/db/matcher/expression_parser_tree.cpp +++ b/src/mongo/db/matcher/expression_parser_tree.cpp @@ -35,6 +35,7 @@ #include "mongo/db/matcher/expression_array.h" #include "mongo/db/matcher/expression_leaf.h" #include "mongo/db/matcher/expression_tree.h" +#include "mongo/stdx/memory.h" #include "mongo/util/mongoutils/str.h" namespace mongo { @@ -59,7 +60,7 @@ Status MatchExpressionParser::_parseTreeList(const BSONObj& arr, if (!sub.isOK()) return sub.getStatus(); - out->add(sub.getValue()); + out->add(sub.getValue().release()); } return Status::OK(); } @@ -71,11 +72,11 @@ StatusWithMatchExpression MatchExpressionParser::_parseNot(const char* name, StatusWithMatchExpression s = _parseRegexElement(name, e); if (!s.isOK()) return s; - std::unique_ptr<NotMatchExpression> n(new NotMatchExpression()); - Status s2 = n->init(s.getValue()); + std::unique_ptr<NotMatchExpression> n = stdx::make_unique<NotMatchExpression>(); + Status s2 = n->init(s.getValue().release()); if (!s2.isOK()) return StatusWithMatchExpression(s2); - return StatusWithMatchExpression(n.release()); + return {std::move(n)}; } if (e.type() != Object) @@ -85,7 +86,7 @@ StatusWithMatchExpression MatchExpressionParser::_parseNot(const char* name, if (notObject.isEmpty()) return StatusWithMatchExpression(ErrorCodes::BadValue, "$not cannot be empty"); - std::unique_ptr<AndMatchExpression> theAnd(new AndMatchExpression()); + std::unique_ptr<AndMatchExpression> theAnd = stdx::make_unique<AndMatchExpression>(); Status s = _parseSub(name, notObject, theAnd.get(), level); if (!s.isOK()) return StatusWithMatchExpression(s); @@ -96,11 +97,11 @@ StatusWithMatchExpression MatchExpressionParser::_parseNot(const char* name, if (theAnd->getChild(i)->matchType() == MatchExpression::REGEX) return StatusWithMatchExpression(ErrorCodes::BadValue, "$not cannot have a regex"); - std::unique_ptr<NotMatchExpression> theNot(new NotMatchExpression()); + std::unique_ptr<NotMatchExpression> theNot = stdx::make_unique<NotMatchExpression>(); s = theNot->init(theAnd.release()); if (!s.isOK()) return StatusWithMatchExpression(s); - return StatusWithMatchExpression(theNot.release()); + return {std::move(theNot)}; } } diff --git a/src/mongo/db/matcher/expression_parser_tree_test.cpp b/src/mongo/db/matcher/expression_parser_tree_test.cpp index a650803d283..6e4530a1a29 100644 --- a/src/mongo/db/matcher/expression_parser_tree_test.cpp +++ b/src/mongo/db/matcher/expression_parser_tree_test.cpp @@ -48,8 +48,6 @@ TEST(MatchExpressionParserTreeTest, OR1) { ASSERT(result.getValue()->matchesBSON(BSON("y" << 2))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 3))); ASSERT(!result.getValue()->matchesBSON(BSON("y" << 1))); - - delete result.getValue(); } TEST(MatchExpressionParserTreeTest, OREmbedded) { @@ -62,8 +60,6 @@ TEST(MatchExpressionParserTreeTest, OREmbedded) { ASSERT(result.getValue()->matchesBSON(BSON("y" << 2))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 3))); ASSERT(!result.getValue()->matchesBSON(BSON("y" << 1))); - - delete result.getValue(); } @@ -78,8 +74,6 @@ TEST(MatchExpressionParserTreeTest, AND1) { ASSERT(!result.getValue()->matchesBSON(BSON("y" << 1))); ASSERT(result.getValue()->matchesBSON(BSON("x" << 1 << "y" << 2))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 2 << "y" << 2))); - - delete result.getValue(); } TEST(MatchExpressionParserTreeTest, NOREmbedded) { @@ -91,8 +85,6 @@ TEST(MatchExpressionParserTreeTest, NOREmbedded) { ASSERT(!result.getValue()->matchesBSON(BSON("y" << 2))); ASSERT(result.getValue()->matchesBSON(BSON("x" << 3))); ASSERT(result.getValue()->matchesBSON(BSON("y" << 1))); - - delete result.getValue(); } TEST(MatchExpressionParserTreeTest, NOT1) { @@ -102,8 +94,6 @@ TEST(MatchExpressionParserTreeTest, NOT1) { ASSERT(result.getValue()->matchesBSON(BSON("x" << 2))); ASSERT(!result.getValue()->matchesBSON(BSON("x" << 8))); - - delete result.getValue(); } // Test a deep match tree that is not deep enough to hit the maximum depth limit. @@ -122,7 +112,6 @@ TEST(MatchExpressionParserTreeTest, MaximumTreeDepthNotExceed) { BSONObj query = fromjson(ss.str()); StatusWithMatchExpression result = MatchExpressionParser::parse(query); ASSERT(result.isOK()); - delete result.getValue(); } // Test a tree that exceeds the maximum depth limit. @@ -193,7 +182,5 @@ TEST(MatchExpressionParserLeafTest, NotRegex1) { << "ABC"))); ASSERT(result.getValue()->matchesBSON(BSON("x" << "AC"))); - - delete result.getValue(); } } diff --git a/src/mongo/db/matcher/expression_where.cpp b/src/mongo/db/matcher/expression_where.cpp index 22985eb10e4..21c276a10b6 100644 --- a/src/mongo/db/matcher/expression_where.cpp +++ b/src/mongo/db/matcher/expression_where.cpp @@ -189,7 +189,7 @@ StatusWithMatchExpression WhereCallbackReal::parseWhere(const BSONElement& where Status s = exp->init(_dbName, where.valuestr(), BSONObj()); if (!s.isOK()) return StatusWithMatchExpression(s); - return StatusWithMatchExpression(exp.release()); + return {std::move(exp)}; } if (where.type() == CodeWScope) { @@ -197,7 +197,7 @@ StatusWithMatchExpression WhereCallbackReal::parseWhere(const BSONElement& where exp->init(_dbName, where.codeWScopeCode(), BSONObj(where.codeWScopeScopeDataUnsafe())); if (!s.isOK()) return StatusWithMatchExpression(s); - return StatusWithMatchExpression(exp.release()); + return {std::move(exp)}; } return StatusWithMatchExpression(ErrorCodes::BadValue, "$where got bad type"); diff --git a/src/mongo/db/matcher/expression_where_noop.cpp b/src/mongo/db/matcher/expression_where_noop.cpp index fcda8d6a006..a9db9b0413b 100644 --- a/src/mongo/db/matcher/expression_where_noop.cpp +++ b/src/mongo/db/matcher/expression_where_noop.cpp @@ -123,14 +123,14 @@ StatusWithMatchExpression WhereCallbackNoop::parseWhere(const BSONElement& where Status s = exp->init(where.valuestr()); if (!s.isOK()) return StatusWithMatchExpression(s); - return StatusWithMatchExpression(exp.release()); + return {std::move(exp)}; } if (where.type() == CodeWScope) { Status s = exp->init(where.codeWScopeCode()); if (!s.isOK()) return StatusWithMatchExpression(s); - return StatusWithMatchExpression(exp.release()); + return {std::move(exp)}; } return StatusWithMatchExpression(ErrorCodes::BadValue, "$where got bad type"); diff --git a/src/mongo/db/matcher/matcher.cpp b/src/mongo/db/matcher/matcher.cpp index da7e68e2f09..df9a7fe71e1 100644 --- a/src/mongo/db/matcher/matcher.cpp +++ b/src/mongo/db/matcher/matcher.cpp @@ -43,12 +43,13 @@ namespace mongo { Matcher::Matcher(const BSONObj& pattern, const MatchExpressionParser::WhereCallback& whereCallback) : _pattern(pattern) { - StatusWithMatchExpression result = MatchExpressionParser::parse(pattern, whereCallback); + StatusWithMatchExpression statusWithMatcher = + MatchExpressionParser::parse(pattern, whereCallback); uassert(16810, - mongoutils::str::stream() << "bad query: " << result.getStatus().toString(), - result.isOK()); + mongoutils::str::stream() << "bad query: " << statusWithMatcher.getStatus().toString(), + statusWithMatcher.isOK()); - _expression.reset(result.getValue()); + _expression = std::move(statusWithMatcher.getValue()); } bool Matcher::matches(const BSONObj& doc, MatchDetails* details) const { |