Skip to content

Commit c832bc7

Browse files
committed
SERVER-17364 Unify handling of child stages into PlanStage base class
This is prep for adding more methods that need to propagate to children.
1 parent f64b6c5 commit c832bc7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+540
-1617
lines changed

src/mongo/db/exec/SConscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ env.Library(
5858
"oplogstart.cpp",
5959
"or.cpp",
6060
"pipeline_proxy.cpp",
61+
"plan_stage.cpp",
6162
"projection.cpp",
6263
"projection_exec.cpp",
6364
"queued_data_stage.cpp",

src/mongo/db/exec/and_hash.cpp

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,25 @@ const size_t AndHashStage::kLookAheadWorks = 10;
5555
const char* AndHashStage::kStageType = "AND_HASH";
5656

5757
AndHashStage::AndHashStage(WorkingSet* ws, const Collection* collection)
58-
: _collection(collection),
58+
: PlanStage(kStageType),
59+
_collection(collection),
5960
_ws(ws),
6061
_hashingChildren(true),
6162
_currentChild(0),
62-
_commonStats(kStageType),
6363
_memUsage(0),
6464
_maxMemUsage(kDefaultMaxMemUsageBytes) {}
6565

6666
AndHashStage::AndHashStage(WorkingSet* ws, const Collection* collection, size_t maxMemUsage)
67-
: _collection(collection),
67+
: PlanStage(kStageType),
68+
_collection(collection),
6869
_ws(ws),
6970
_hashingChildren(true),
7071
_currentChild(0),
71-
_commonStats(kStageType),
7272
_memUsage(0),
7373
_maxMemUsage(maxMemUsage) {}
7474

75-
AndHashStage::~AndHashStage() {
76-
for (size_t i = 0; i < _children.size(); ++i) {
77-
delete _children[i];
78-
}
79-
}
80-
8175
void AndHashStage::addChild(PlanStage* child) {
82-
_children.push_back(child);
76+
_children.emplace_back(child);
8377
}
8478

8579
size_t AndHashStage::getMemUsage() const {
@@ -137,7 +131,7 @@ PlanStage::StageState AndHashStage::work(WorkingSetID* out) {
137131
// a result. If it's EOF this whole stage will be EOF. If it produces a
138132
// result we cache it for later.
139133
for (size_t i = 0; i < _children.size(); ++i) {
140-
PlanStage* child = _children[i];
134+
auto& child = _children[i];
141135
for (size_t j = 0; j < kLookAheadWorks; ++j) {
142136
StageState childStatus = child->work(&_lookAheadResults[i]);
143137

@@ -428,33 +422,12 @@ PlanStage::StageState AndHashStage::hashOtherChildren(WorkingSetID* out) {
428422
}
429423
}
430424

431-
void AndHashStage::saveState() {
432-
++_commonStats.yields;
433-
434-
for (size_t i = 0; i < _children.size(); ++i) {
435-
_children[i]->saveState();
436-
}
437-
}
438-
439-
void AndHashStage::restoreState(OperationContext* opCtx) {
440-
++_commonStats.unyields;
441-
442-
for (size_t i = 0; i < _children.size(); ++i) {
443-
_children[i]->restoreState(opCtx);
444-
}
445-
}
446-
447-
void AndHashStage::invalidate(OperationContext* txn, const RecordId& dl, InvalidationType type) {
448-
++_commonStats.invalidates;
449-
425+
void AndHashStage::doInvalidate(OperationContext* txn, const RecordId& dl, InvalidationType type) {
426+
// TODO remove this since calling isEOF is illegal inside of doInvalidate().
450427
if (isEOF()) {
451428
return;
452429
}
453430

454-
for (size_t i = 0; i < _children.size(); ++i) {
455-
_children[i]->invalidate(txn, dl, type);
456-
}
457-
458431
// Invalidation can happen to our warmup results. If that occurs just
459432
// flag it and forget about it.
460433
for (size_t i = 0; i < _lookAheadResults.size(); ++i) {
@@ -500,10 +473,6 @@ void AndHashStage::invalidate(OperationContext* txn, const RecordId& dl, Invalid
500473
}
501474
}
502475

503-
vector<PlanStage*> AndHashStage::getChildren() const {
504-
return _children;
505-
}
506-
507476
unique_ptr<PlanStageStats> AndHashStage::getStats() {
508477
_commonStats.isEOF = isEOF();
509478

@@ -519,10 +488,6 @@ unique_ptr<PlanStageStats> AndHashStage::getStats() {
519488
return ret;
520489
}
521490

522-
const CommonStats* AndHashStage::getCommonStats() const {
523-
return &_commonStats;
524-
}
525-
526491
const SpecificStats* AndHashStage::getSpecificStats() const {
527492
return &_specificStats;
528493
}

src/mongo/db/exec/and_hash.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ class AndHashStage : public PlanStage {
5858
*/
5959
AndHashStage(WorkingSet* ws, const Collection* collection, size_t maxMemUsage);
6060

61-
virtual ~AndHashStage();
62-
6361
void addChild(PlanStage* child);
6462

6563
/**
@@ -71,20 +69,14 @@ class AndHashStage : public PlanStage {
7169
virtual StageState work(WorkingSetID* out);
7270
virtual bool isEOF();
7371

74-
virtual void saveState();
75-
virtual void restoreState(OperationContext* opCtx);
76-
virtual void invalidate(OperationContext* txn, const RecordId& dl, InvalidationType type);
77-
78-
virtual std::vector<PlanStage*> getChildren() const;
72+
virtual void doInvalidate(OperationContext* txn, const RecordId& dl, InvalidationType type);
7973

8074
virtual StageType stageType() const {
8175
return STAGE_AND_HASH;
8276
}
8377

8478
virtual std::unique_ptr<PlanStageStats> getStats();
8579

86-
virtual const CommonStats* getCommonStats() const;
87-
8880
virtual const SpecificStats* getSpecificStats() const;
8981

9082
static const char* kStageType;
@@ -102,9 +94,6 @@ class AndHashStage : public PlanStage {
10294
// Not owned by us.
10395
WorkingSet* _ws;
10496

105-
// The stages we read from. Owned by us.
106-
std::vector<PlanStage*> _children;
107-
10897
// We want to see if any of our children are EOF immediately. This requires working them a
10998
// few times to see if they hit EOF or if they produce a result. If they produce a result,
11099
// we place that result here.
@@ -127,7 +116,6 @@ class AndHashStage : public PlanStage {
127116
size_t _currentChild;
128117

129118
// Stats
130-
CommonStats _commonStats;
131119
AndHashStats _specificStats;
132120

133121
// The usage in bytes of all buffered data that we're holding.

src/mongo/db/exec/and_sorted.cpp

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,16 @@ using stdx::make_unique;
4545
const char* AndSortedStage::kStageType = "AND_SORTED";
4646

4747
AndSortedStage::AndSortedStage(WorkingSet* ws, const Collection* collection)
48-
: _collection(collection),
48+
: PlanStage(kStageType),
49+
_collection(collection),
4950
_ws(ws),
5051
_targetNode(numeric_limits<size_t>::max()),
5152
_targetId(WorkingSet::INVALID_ID),
52-
_isEOF(false),
53-
_commonStats(kStageType) {}
53+
_isEOF(false) {}
5454

55-
AndSortedStage::~AndSortedStage() {
56-
for (size_t i = 0; i < _children.size(); ++i) {
57-
delete _children[i];
58-
}
59-
}
6055

6156
void AndSortedStage::addChild(PlanStage* child) {
62-
_children.push_back(child);
57+
_children.emplace_back(child);
6358
}
6459

6560
bool AndSortedStage::isEOF() {
@@ -160,7 +155,7 @@ PlanStage::StageState AndSortedStage::moveTowardTargetLoc(WorkingSetID* out) {
160155

161156
// We have nodes that haven't hit _targetLoc yet.
162157
size_t workingChildNumber = _workingTowardRep.front();
163-
PlanStage* next = _children[workingChildNumber];
158+
auto& next = _children[workingChildNumber];
164159
WorkingSetID id = WorkingSet::INVALID_ID;
165160
StageState state = next->work(&id);
166161

@@ -253,33 +248,15 @@ PlanStage::StageState AndSortedStage::moveTowardTargetLoc(WorkingSetID* out) {
253248
}
254249
}
255250

256-
void AndSortedStage::saveState() {
257-
++_commonStats.yields;
258-
259-
for (size_t i = 0; i < _children.size(); ++i) {
260-
_children[i]->saveState();
261-
}
262-
}
263-
264-
void AndSortedStage::restoreState(OperationContext* opCtx) {
265-
++_commonStats.unyields;
266-
267-
for (size_t i = 0; i < _children.size(); ++i) {
268-
_children[i]->restoreState(opCtx);
269-
}
270-
}
271-
272-
void AndSortedStage::invalidate(OperationContext* txn, const RecordId& dl, InvalidationType type) {
273-
++_commonStats.invalidates;
274251

252+
void AndSortedStage::doInvalidate(OperationContext* txn,
253+
const RecordId& dl,
254+
InvalidationType type) {
255+
// TODO remove this since calling isEOF is illegal inside of doInvalidate().
275256
if (isEOF()) {
276257
return;
277258
}
278259

279-
for (size_t i = 0; i < _children.size(); ++i) {
280-
_children[i]->invalidate(txn, dl, type);
281-
}
282-
283260
if (dl == _targetLoc) {
284261
// We're in the middle of moving children forward until they hit _targetLoc, which is no
285262
// longer a valid target. If it's a deletion we can't AND it with anything, if it's a
@@ -298,10 +275,6 @@ void AndSortedStage::invalidate(OperationContext* txn, const RecordId& dl, Inval
298275
}
299276
}
300277

301-
vector<PlanStage*> AndSortedStage::getChildren() const {
302-
return _children;
303-
}
304-
305278
unique_ptr<PlanStageStats> AndSortedStage::getStats() {
306279
_commonStats.isEOF = isEOF();
307280

@@ -314,10 +287,6 @@ unique_ptr<PlanStageStats> AndSortedStage::getStats() {
314287
return ret;
315288
}
316289

317-
const CommonStats* AndSortedStage::getCommonStats() const {
318-
return &_commonStats;
319-
}
320-
321290
const SpecificStats* AndSortedStage::getSpecificStats() const {
322291
return &_specificStats;
323292
}

src/mongo/db/exec/and_sorted.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,20 @@ namespace mongo {
5454
class AndSortedStage : public PlanStage {
5555
public:
5656
AndSortedStage(WorkingSet* ws, const Collection* collection);
57-
virtual ~AndSortedStage();
5857

5958
void addChild(PlanStage* child);
6059

6160
virtual StageState work(WorkingSetID* out);
6261
virtual bool isEOF();
6362

64-
virtual void saveState();
65-
virtual void restoreState(OperationContext* opCtx);
66-
virtual void invalidate(OperationContext* txn, const RecordId& dl, InvalidationType type);
67-
68-
virtual std::vector<PlanStage*> getChildren() const;
63+
virtual void doInvalidate(OperationContext* txn, const RecordId& dl, InvalidationType type);
6964

7065
virtual StageType stageType() const {
7166
return STAGE_AND_SORTED;
7267
}
7368

7469
virtual std::unique_ptr<PlanStageStats> getStats();
7570

76-
virtual const CommonStats* getCommonStats() const;
77-
7871
virtual const SpecificStats* getSpecificStats() const;
7972

8073
static const char* kStageType;
@@ -93,9 +86,6 @@ class AndSortedStage : public PlanStage {
9386
// Not owned by us.
9487
WorkingSet* _ws;
9588

96-
// Owned by us.
97-
std::vector<PlanStage*> _children;
98-
9989
// The current node we're AND-ing against.
10090
size_t _targetNode;
10191
RecordId _targetLoc;
@@ -110,7 +100,6 @@ class AndSortedStage : public PlanStage {
110100
bool _isEOF;
111101

112102
// Stats
113-
CommonStats _commonStats;
114103
AndSortedStats _specificStats;
115104
};
116105

0 commit comments

Comments
 (0)