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 #include "mongo/platform/basic.h"
32 
33 #include "mongo/db/exec/pipeline_proxy.h"
34 
35 
36 #include "mongo/db/pipeline/document_source.h"
37 #include "mongo/db/pipeline/expression_context.h"
38 #include "mongo/db/pipeline/pipeline_d.h"
39 #include "mongo/stdx/memory.h"
40 
41 namespace mongo {
42 
43 using boost::intrusive_ptr;
44 using std::shared_ptr;
45 using std::unique_ptr;
46 using std::vector;
47 using stdx::make_unique;
48 
49 const char* PipelineProxyStage::kStageType = "PIPELINE_PROXY";
50 
PipelineProxyStage(OperationContext * opCtx,std::unique_ptr<Pipeline,Pipeline::Deleter> pipeline,WorkingSet * ws)51 PipelineProxyStage::PipelineProxyStage(OperationContext* opCtx,
52                                        std::unique_ptr<Pipeline, Pipeline::Deleter> pipeline,
53                                        WorkingSet* ws)
54     : PlanStage(kStageType, opCtx),
55       _pipeline(std::move(pipeline)),
56       _includeMetaData(_pipeline->getContext()->needsMerge),  // send metadata to merger
57       _includeSortKey(_includeMetaData && !_pipeline->getContext()->from34Mongos),
58       _ws(ws) {
59     // We take over responsibility for disposing of the Pipeline, since it is required that
60     // doDispose() will be called before destruction of this PipelineProxyStage.
61     _pipeline.get_deleter().dismissDisposal();
62 }
63 
doWork(WorkingSetID * out)64 PlanStage::StageState PipelineProxyStage::doWork(WorkingSetID* out) {
65     if (!out) {
66         return PlanStage::FAILURE;
67     }
68 
69     if (!_stash.empty()) {
70         *out = _ws->allocate();
71         WorkingSetMember* member = _ws->get(*out);
72         member->obj = Snapshotted<BSONObj>(SnapshotId(), _stash.back());
73         _stash.pop_back();
74         member->transitionToOwnedObj();
75         return PlanStage::ADVANCED;
76     }
77 
78     if (boost::optional<BSONObj> next = getNextBson()) {
79         *out = _ws->allocate();
80         WorkingSetMember* member = _ws->get(*out);
81         member->obj = Snapshotted<BSONObj>(SnapshotId(), *next);
82         member->transitionToOwnedObj();
83         return PlanStage::ADVANCED;
84     }
85 
86     return PlanStage::IS_EOF;
87 }
88 
isEOF()89 bool PipelineProxyStage::isEOF() {
90     if (!_stash.empty())
91         return false;
92 
93     if (boost::optional<BSONObj> next = getNextBson()) {
94         _stash.push_back(*next);
95         return false;
96     }
97 
98     return true;
99 }
100 
doDetachFromOperationContext()101 void PipelineProxyStage::doDetachFromOperationContext() {
102     _pipeline->detachFromOperationContext();
103 }
104 
doReattachToOperationContext()105 void PipelineProxyStage::doReattachToOperationContext() {
106     _pipeline->reattachToOperationContext(getOpCtx());
107 }
108 
doDispose()109 void PipelineProxyStage::doDispose() {
110     _pipeline->dispose(getOpCtx());
111 }
112 
getStats()113 unique_ptr<PlanStageStats> PipelineProxyStage::getStats() {
114     unique_ptr<PlanStageStats> ret =
115         make_unique<PlanStageStats>(CommonStats(kStageType), STAGE_PIPELINE_PROXY);
116     ret->specific = make_unique<CollectionScanStats>();
117     return ret;
118 }
119 
getNextBson()120 boost::optional<BSONObj> PipelineProxyStage::getNextBson() {
121     if (auto next = _pipeline->getNext()) {
122         if (_includeMetaData) {
123             return next->toBsonWithMetaData(_includeSortKey);
124         } else {
125             return next->toBson();
126         }
127     }
128 
129     return boost::none;
130 }
131 
getLatestOplogTimestamp() const132 Timestamp PipelineProxyStage::getLatestOplogTimestamp() const {
133     return PipelineD::getLatestOplogTimestamp(_pipeline.get());
134 }
135 
getPlanSummaryStr() const136 std::string PipelineProxyStage::getPlanSummaryStr() const {
137     return PipelineD::getPlanSummaryStr(_pipeline.get());
138 }
139 
getPlanSummaryStats(PlanSummaryStats * statsOut) const140 void PipelineProxyStage::getPlanSummaryStats(PlanSummaryStats* statsOut) const {
141     invariant(statsOut);
142     PipelineD::getPlanSummaryStats(_pipeline.get(), statsOut);
143     statsOut->nReturned = getCommonStats()->advanced;
144 }
145 }  // namespace mongo
146