1 2 /** 3 * Copyright (C) 2018-present MongoDB, Inc. 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the Server Side Public License, version 1, 7 * as published by MongoDB, Inc. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * Server Side Public License for more details. 13 * 14 * You should have received a copy of the Server Side Public License 15 * along with this program. If not, see 16 * <http://www.mongodb.com/licensing/server-side-public-license>. 17 * 18 * As a special exception, the copyright holders give permission to link the 19 * code of portions of this program with the OpenSSL library under certain 20 * conditions as described in each individual source file and distribute 21 * linked combinations including the program with the OpenSSL library. You 22 * must comply with the Server Side Public License in all respects for 23 * all of the code used other than as permitted herein. If you modify file(s) 24 * with this exception, you may extend this exception to your version of the 25 * file(s), but you are not obligated to do so. If you do not wish to do so, 26 * delete this exception statement from your version. If you delete this 27 * exception statement from all source files in the program, then also delete 28 * it in the license file. 29 */ 30 31 #pragma once 32 33 #include <boost/optional.hpp> 34 #include <set> 35 #include <string> 36 37 #include "mongo/db/pipeline/document.h" 38 #include "mongo/db/pipeline/variables.h" 39 40 namespace mongo { 41 class ParsedDeps; 42 43 /** 44 * This struct allows components in an agg pipeline to report what they need from their input. 45 */ 46 struct DepsTracker { 47 /** 48 * Represents what metadata is available on documents that are input to the pipeline. 49 */ 50 enum MetadataAvailable { kNoMetadata = 0, kTextScore = 1 }; 51 52 DepsTracker(MetadataAvailable metadataAvailable = kNoMetadata) _metadataAvailableDepsTracker53 : _metadataAvailable(metadataAvailable) {} 54 55 /** 56 * Returns a projection object covering the dependencies tracked by this class. 57 */ 58 BSONObj toProjection() const; 59 60 boost::optional<ParsedDeps> toParsedDeps() const; 61 hasNoRequirementsDepsTracker62 bool hasNoRequirements() const { 63 return fields.empty() && !needWholeDocument && !_needTextScore; 64 } 65 66 /** 67 * Returns 'true' if any of the DepsTracker's variables appear in the passed 'ids' set. 68 */ hasVariableReferenceToDepsTracker69 bool hasVariableReferenceTo(const std::set<Variables::Id>& ids) const { 70 std::vector<Variables::Id> match; 71 std::set_intersection( 72 vars.begin(), vars.end(), ids.begin(), ids.end(), std::back_inserter(match)); 73 return !match.empty(); 74 } 75 getMetadataAvailableDepsTracker76 MetadataAvailable getMetadataAvailable() const { 77 return _metadataAvailable; 78 } 79 isTextScoreAvailableDepsTracker80 bool isTextScoreAvailable() const { 81 return _metadataAvailable & MetadataAvailable::kTextScore; 82 } 83 getNeedTextScoreDepsTracker84 bool getNeedTextScore() const { 85 return _needTextScore; 86 } 87 setNeedTextScoreDepsTracker88 void setNeedTextScore(bool needTextScore) { 89 if (needTextScore && !isTextScoreAvailable()) { 90 uasserted( 91 40218, 92 "pipeline requires text score metadata, but there is no text score available"); 93 } 94 _needTextScore = needTextScore; 95 } 96 getNeedSortKeyDepsTracker97 bool getNeedSortKey() const { 98 return _needSortKey; 99 } 100 setNeedSortKeyDepsTracker101 void setNeedSortKey(bool needSortKey) { 102 // We don't expect to ever unset '_needSortKey'. 103 invariant(!_needSortKey || needSortKey); 104 _needSortKey = needSortKey; 105 } 106 107 std::set<std::string> fields; // Names of needed fields in dotted notation. 108 std::set<Variables::Id> vars; // IDs of referenced variables. 109 bool needWholeDocument = false; // If true, ignore 'fields'; the whole document is needed. 110 111 private: 112 /** 113 * Appends the meta projections for the sort key and/or text score to 'bb' if necessary. Returns 114 * true if either type of metadata was needed, and false otherwise. 115 */ 116 bool _appendMetaProjections(BSONObjBuilder* bb) const; 117 118 MetadataAvailable _metadataAvailable; 119 bool _needTextScore = false; // if true, add a {$meta: "textScore"} to the projection. 120 bool _needSortKey = false; // if true, add a {$meta: "sortKey"} to the projection. 121 }; 122 123 /** 124 * This class is designed to quickly extract the needed fields from a BSONObj into a Document. 125 * It should only be created by a call to DepsTracker::ParsedDeps 126 */ 127 class ParsedDeps { 128 public: 129 Document extractFields(const BSONObj& input) const; 130 131 private: 132 friend struct DepsTracker; // so it can call constructor ParsedDeps(Document && fields)133 explicit ParsedDeps(Document&& fields) : _fields(std::move(fields)), _nFields(_fields.size()) {} 134 135 Document _fields; 136 int _nFields; // Cache the number of top-level fields needed. 137 }; 138 } 139