|
| 1 | +/** |
| 2 | + * Copyright (C) 2019-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + * |
| 17 | + * As a special exception, the copyright holders give permission to link the |
| 18 | + * code of portions of this program with the OpenSSL library under certain |
| 19 | + * conditions as described in each individual source file and distribute |
| 20 | + * linked combinations including the program with the OpenSSL library. You |
| 21 | + * must comply with the Server Side Public License in all respects for |
| 22 | + * all of the code used other than as permitted herein. If you modify file(s) |
| 23 | + * with this exception, you may extend this exception to your version of the |
| 24 | + * file(s), but you are not obligated to do so. If you do not wish to do so, |
| 25 | + * delete this exception statement from your version. If you delete this |
| 26 | + * exception statement from all source files in the program, then also delete |
| 27 | + * it in the license file. |
| 28 | + */ |
| 29 | + |
| 30 | +#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kQuery |
| 31 | + |
| 32 | +#include "mongo/platform/basic.h" |
| 33 | + |
| 34 | +#include "mongo/db/exec/shard_name.h" |
| 35 | + |
| 36 | +#include <memory> |
| 37 | + |
| 38 | +#include "mongo/db/exec/filter.h" |
| 39 | +#include "mongo/db/exec/scoped_timer.h" |
| 40 | +#include "mongo/db/exec/working_set_common.h" |
| 41 | +#include "mongo/db/exec/working_set_computed_data.h" |
| 42 | +#include "mongo/s/shard_key_pattern.h" |
| 43 | +#include "mongo/util/log.h" |
| 44 | + |
| 45 | +namespace mongo { |
| 46 | + |
| 47 | +using std::shared_ptr; |
| 48 | +using std::unique_ptr; |
| 49 | +using std::vector; |
| 50 | +using std::string; |
| 51 | + |
| 52 | +// static |
| 53 | +const char* ShardNameStage::kStageType = "SHARD_NAME"; |
| 54 | + |
| 55 | +ShardNameStage:: ShardNameStage(OperationContext* opCtx, |
| 56 | + ScopedCollectionMetadata metadata, |
| 57 | + WorkingSet* ws, |
| 58 | + PlanStage* child) |
| 59 | + : PlanStage(kStageType, opCtx), _ws(ws), _shardNamer(std::move(metadata)) { |
| 60 | + _children.emplace_back(child); |
| 61 | +} |
| 62 | + |
| 63 | +ShardNameStage::~ShardNameStage() {} |
| 64 | + |
| 65 | +bool ShardNameStage::isEOF() { |
| 66 | + return child()->isEOF(); |
| 67 | +} |
| 68 | + |
| 69 | +PlanStage::StageState ShardNameStage::doWork(WorkingSetID* out) { |
| 70 | + // If we've returned as many results as we're limited to, isEOF will be true. |
| 71 | + if (isEOF()) { |
| 72 | + return PlanStage::IS_EOF; |
| 73 | + } |
| 74 | + |
| 75 | + StageState status = child()->work(out); |
| 76 | + |
| 77 | + if (PlanStage::ADVANCED == status) { |
| 78 | + // If we're sharded make sure to add shardName to the output. |
| 79 | + if (_shardNamer.isCollectionSharded()) { |
| 80 | + WorkingSetMember* member = _ws->get(*out); |
| 81 | + const StringData shardName = _shardNamer.shardName(); |
| 82 | + |
| 83 | + // Populate the working set member with the shard name and return it. |
| 84 | + member->addComputed(new ShardNameComputedData(shardName)); |
| 85 | + } |
| 86 | + |
| 87 | + // If we're here either we have shard state and added the shardName, or we have no shard |
| 88 | + // state. Either way, we advance. |
| 89 | + return status; |
| 90 | + } |
| 91 | + |
| 92 | + return status; |
| 93 | +} |
| 94 | + |
| 95 | +unique_ptr<PlanStageStats> ShardNameStage::getStats() { |
| 96 | + _commonStats.isEOF = isEOF(); |
| 97 | + unique_ptr<PlanStageStats> ret = |
| 98 | + std::make_unique<PlanStageStats>(_commonStats, STAGE_SHARD_NAME); |
| 99 | + ret->children.emplace_back(child()->getStats()); |
| 100 | + return ret; |
| 101 | +} |
| 102 | + |
| 103 | +const SpecificStats* ShardNameStage::getSpecificStats() const { |
| 104 | + // No specific stats are tracked for the shard name stage. |
| 105 | + return nullptr; |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace mongo |
0 commit comments