/** * Copyright (C) 2020-present MongoDB, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the Server Side Public License, version 1, * as published by MongoDB, Inc. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * Server Side Public License for more details. * * You should have received a copy of the Server Side Public License * along with this program. If not, see * . * * As a special exception, the copyright holders give permission to link the * code of portions of this program with the OpenSSL library under certain * conditions as described in each individual source file and distribute * linked combinations including the program with the OpenSSL library. You * must comply with the Server Side Public License in all respects for * all of the code used other than as permitted herein. If you modify file(s) * with this exception, you may extend this exception to your version of the * file(s), but you are not obligated to do so. If you do not wish to do so, * delete this exception statement from your version. If you delete this * exception statement from all source files in the program, then also delete * it in the license file. */ #include "mongo/db/cst/c_node.h" #include "mongo/bson/bsontypes.h" #include "mongo/db/query/datetime/date_time_support.h" #include "mongo/util/hex.h" #include "mongo/util/overloaded_visitor.h" #include #include #include #include namespace mongo { using namespace std::string_literals; namespace { auto tabs(int num) { std::string out; for (; num > 0; num--) out += "\t"; return out; } auto printFieldname(const CNode::Fieldname& fieldname) { return stdx::visit( OverloadedVisitor{ [](const KeyFieldname& key) { return ""; }, [](const UserFieldname& user) { return ""; }, [](const FieldnamePath& path) { return stdx::visit( OverloadedVisitor{ [&](const ProjectionPath& projPath) { return ""; }, [&](const PositionalProjectionPath& posProjPath) { return ""; }, [&](const SortPath& sortPath) { return ""; }}, path); }}, fieldname); } auto printNonZeroKey(const NonZeroKey& nonZeroKey) { return stdx::visit( OverloadedVisitor{ [](const int& keyInt) { return "int "s + std::to_string(keyInt); }, [](const long long& keyLong) { return "long "s + std::to_string(keyLong); }, [](const double& keyDouble) { return "double "s + std::to_string(keyDouble); }, [](const Decimal128& keyDecimal) { return "decimal "s + keyDecimal.toString(); }}, nonZeroKey); } template auto printValue(const T& payload) { return stdx::visit( OverloadedVisitor{ [](const CNode::ArrayChildren&) { return ""s; }, [](const CNode::ObjectChildren&) { return ""s; }, [](const CompoundInclusionKey&) { return ""s; }, [](const CompoundExclusionKey&) { return ""s; }, [](const CompoundInconsistentKey&) { return ""s; }, [](const KeyValue& value) { return ""; }, [](const NonZeroKey& nonZeroKey) { return ""; }, [](const ValuePath& valuePath) { return stdx::visit( OverloadedVisitor{[&](const AggregationPath& aggPath) { return ""; }, [&](const AggregationVariablePath& aggVarPath) { return ""; }}, valuePath); }, [](const UserDouble& userDouble) { return ""; }, [](const UserString& userString) { return ""; }, [](const UserBinary& userBinary) { return ""; }, [](const UserUndefined& userUndefined) { return ""s; }, [](const UserObjectId& userObjectId) { return ""; }, [](const UserBoolean& userBoolean) { return ""; }, [](const UserDate& userDate) { return ""; }, [](const UserNull& userNull) { return ""s; }, [](const UserRegex& userRegex) { return ""; }, [](const UserDBPointer& userDBPointer) { return ""; }, [](const UserJavascript& userJavascript) { return ""; }, [](const UserSymbol& userSymbol) { return ""; }, [](const UserJavascriptWithScope& userJavascriptWithScope) { return ""; }, [](const UserInt& userInt) { return ""; }, [](const UserTimestamp& userTimestamp) { return ""; }, [](const UserLong& userLong) { return ""; }, [](const UserDecimal& userDecimal) { return ""; }, [](const UserMinKey& userMinKey) { return ""s; }, [](const UserMaxKey& userMaxKey) { return ""s; }}, payload); } } // namespace std::string CNode::toStringHelper(int numTabs) const { return stdx::visit( OverloadedVisitor{ [numTabs](const ArrayChildren& children) { return std::accumulate(children.cbegin(), children.cend(), tabs(numTabs) + "[\n", [numTabs](auto&& string, auto&& child) { return string + child.toStringHelper(numTabs + 1) + "\n"; }) + tabs(numTabs) + "]"; }, [numTabs](const ObjectChildren& children) { return std::accumulate(children.cbegin(), children.cend(), tabs(numTabs) + "{\n", [numTabs](auto&& string, auto&& childpair) { return string + tabs(numTabs) + printFieldname(childpair.first) + " :\n" + childpair.second.toStringHelper(numTabs + 1) + "\n"; }) + tabs(numTabs) + "}"; }, [numTabs](const CompoundInclusionKey& compoundKey) { return tabs(numTabs) + "\n" + compoundKey.obj->toStringHelper(numTabs + 1); }, [numTabs](const CompoundExclusionKey& compoundKey) { return tabs(numTabs) + "\n" + compoundKey.obj->toStringHelper(numTabs + 1); }, [numTabs](const CompoundInconsistentKey& compoundKey) { return tabs(numTabs) + "\n" + compoundKey.obj->toStringHelper(numTabs + 1); }, [this, numTabs](auto&&) { return tabs(numTabs) + printValue(payload); }}, payload); } std::pair CNode::toBsonWithArrayIndicator() const { auto addChild = [](auto&& bson, auto&& fieldname, auto&& child) { // This is a non-compound field. pull the BSONElement out of it's BSONObj shell and add it. if (auto [childBson, isArray] = child.toBsonWithArrayIndicator(); !childBson.isEmpty() && childBson.firstElementFieldNameStringData().empty()) return bson.addField( childBson .replaceFieldNames(BSON(std::forward(fieldname) << "")) .firstElement()); // This field is an array. Reconstruct with BSONArray and add it. else if (isArray) return bson.addField( BSON(std::forward(fieldname) << BSONArray{childBson}) .firstElement()); // This field is an object. Add it directly. else return bson.addField( BSON(std::forward(fieldname) << childBson).firstElement()); }; return stdx::visit( OverloadedVisitor{ // Build an array which will lose its identity and appear as a BSONObj [&](const ArrayChildren& children) { return std::pair{ std::accumulate(children.cbegin(), children.cend(), BSONObj{}, [&, fieldCount = 0u](auto&& bson, auto&& child) mutable { return addChild(std::forward(bson), std::to_string(fieldCount++), std::forward(child)); }), true}; }, // Build an object in a BSONObj. [&](const ObjectChildren& children) { return std::pair{std::accumulate(children.cbegin(), children.cend(), BSONObj{}, [&](auto&& bson, auto&& childPair) { return addChild( std::forward(bson), printFieldname(childPair.first), childPair.second); }), false}; }, // Build a compound inclusion key wrapper in a BSONObj. [&](const CompoundInclusionKey& compoundKey) { return std::pair{addChild(BSONObj{}, ""s, *compoundKey.obj), false}; }, // Build a compound exclusion key wrapper in a BSONObj. [&](const CompoundExclusionKey& compoundKey) { return std::pair{addChild(BSONObj{}, ""s, *compoundKey.obj), false}; }, // Build a compound exclusion key wrapper in a BSONObj. [&](const CompoundInconsistentKey& compoundKey) { return std::pair{ addChild(BSONObj{}, ""s, *compoundKey.obj), false}; }, // Build a non-compound field in a BSONObj shell. [this](auto&&) { return std::pair{BSON("" << printValue(payload)), false}; }}, payload); } bool CNode::isNumber() const { return stdx::visit(OverloadedVisitor{ [](const UserLong&) { return true; }, [](const UserDouble&) { return true; }, [](const UserDecimal&) { return true; }, [](const UserInt&) { return true; }, [](auto&&) { return false; }, }, payload); } int CNode::numberInt() const { // BSONElement has no safeNumberInt, so use CNode::numberLong which uses // BSONElement::safeNumberLong. We don't want to use BSONElement::numberInt because it has // undefined behavior for certain inputs (for one example, NaN). // safeNumberLong returns the LLONG_MIN/LLONG_MAX when the original value is too big/small // for a long long, so imitate that behavior here for int. long long val = numberLong(); constexpr int max = std::numeric_limits::max(); constexpr int min = std::numeric_limits::min(); if (val > static_cast(max)) return max; if (val < static_cast(min)) return min; return static_cast(val); } long long CNode::numberLong() const { return stdx::visit( OverloadedVisitor{[](const UserDouble& userDouble) { return (BSON("" << userDouble).firstElement()).safeNumberLong(); }, [](const UserInt& userInt) { return (BSON("" << userInt).firstElement()).safeNumberLong(); }, [](const UserLong& userLong) { return userLong; }, [](const UserDecimal& userDecimal) { return (BSON("" << userDecimal).firstElement()).safeNumberLong(); }, [](auto&&) -> UserLong { MONGO_UNREACHABLE }}, payload); } } // namespace mongo