diff options
Diffstat (limited to 'src/mongo/db/exec/group.h')
-rw-r--r-- | src/mongo/db/exec/group.h | 197 |
1 files changed, 100 insertions, 97 deletions
diff --git a/src/mongo/db/exec/group.h b/src/mongo/db/exec/group.h index b9f525b95bb..49f5c881d84 100644 --- a/src/mongo/db/exec/group.h +++ b/src/mongo/db/exec/group.h @@ -34,135 +34,138 @@ namespace mongo { - class Collection; +class Collection; - /** - * A description of a request for a group operation. Copyable. - */ - struct GroupRequest { - // Namespace to operate on (e.g. "foo.bar"). - std::string ns; +/** + * A description of a request for a group operation. Copyable. + */ +struct GroupRequest { + // Namespace to operate on (e.g. "foo.bar"). + std::string ns; - // A predicate describing the set of documents to group. - BSONObj query; + // A predicate describing the set of documents to group. + BSONObj query; - // The field(s) to group by. Alternative to "keyFunctionCode". Empty if "keyFunctionCode" - // is being used instead. - BSONObj keyPattern; + // The field(s) to group by. Alternative to "keyFunctionCode". Empty if "keyFunctionCode" + // is being used instead. + BSONObj keyPattern; - // A Javascript function that maps a document to a key object. Alternative to "keyPattern". - // Empty is "keyPattern" is being used instead. - std::string keyFunctionCode; + // A Javascript function that maps a document to a key object. Alternative to "keyPattern". + // Empty is "keyPattern" is being used instead. + std::string keyFunctionCode; - // A Javascript function that takes a (input document, group result) pair and - // updates the group result document. - std::string reduceCode; + // A Javascript function that takes a (input document, group result) pair and + // updates the group result document. + std::string reduceCode; - // Scope for the reduce function. Optional. - BSONObj reduceScope; + // Scope for the reduce function. Optional. + BSONObj reduceScope; - // The initial value for the group result. - BSONObj initial; + // The initial value for the group result. + BSONObj initial; - // A Javascript function that "finalizes" a group result. Optional. - std::string finalize; + // A Javascript function that "finalizes" a group result. Optional. + std::string finalize; - // Whether this is an explain of a group. - bool explain; - }; + // Whether this is an explain of a group. + bool explain; +}; - /** - * Stage used by the group command. Consumes input documents from its child stage (returning - * NEED_TIME once for each document produced by the child), returns ADVANCED exactly once with - * the entire group result, then returns EOF. - * - * Only created through the getExecutorGroup path. - */ - class GroupStage: public PlanStage { - MONGO_DISALLOW_COPYING(GroupStage); - public: - GroupStage(OperationContext* txn, - const GroupRequest& request, - WorkingSet* workingSet, - PlanStage* child); - virtual ~GroupStage() { } +/** + * Stage used by the group command. Consumes input documents from its child stage (returning + * NEED_TIME once for each document produced by the child), returns ADVANCED exactly once with + * the entire group result, then returns EOF. + * + * Only created through the getExecutorGroup path. + */ +class GroupStage : public PlanStage { + MONGO_DISALLOW_COPYING(GroupStage); - virtual StageState work(WorkingSetID* out); - virtual bool isEOF(); - virtual void saveState(); - virtual void restoreState(OperationContext* opCtx); - virtual void invalidate(OperationContext* txn, const RecordId& dl, InvalidationType type); +public: + GroupStage(OperationContext* txn, + const GroupRequest& request, + WorkingSet* workingSet, + PlanStage* child); + virtual ~GroupStage() {} - virtual std::vector<PlanStage*> getChildren() const; + virtual StageState work(WorkingSetID* out); + virtual bool isEOF(); + virtual void saveState(); + virtual void restoreState(OperationContext* opCtx); + virtual void invalidate(OperationContext* txn, const RecordId& dl, InvalidationType type); - virtual StageType stageType() const { return STAGE_GROUP; } + virtual std::vector<PlanStage*> getChildren() const; - virtual PlanStageStats* getStats(); + virtual StageType stageType() const { + return STAGE_GROUP; + } - virtual const CommonStats* getCommonStats() const; + virtual PlanStageStats* getStats(); - virtual const SpecificStats* getSpecificStats() const; + virtual const CommonStats* getCommonStats() const; - static const char* kStageType; + virtual const SpecificStats* getSpecificStats() const; - private: - /** - * Keeps track of what this group is currently doing so that it can do the right thing on - * the next call to work(). - */ - enum GroupState { - // Need to initialize the underlying Javascript machinery. - GroupState_Initializing, + static const char* kStageType; - // Retrieving the next document from the child stage and processing it. - GroupState_ReadingFromChild, +private: + /** + * Keeps track of what this group is currently doing so that it can do the right thing on + * the next call to work(). + */ + enum GroupState { + // Need to initialize the underlying Javascript machinery. + GroupState_Initializing, - // Results have been returned. - GroupState_Done - }; + // Retrieving the next document from the child stage and processing it. + GroupState_ReadingFromChild, - // Initializes _scope, _reduceFunction and _keyFunction using the global scripting engine. - void initGroupScripting(); + // Results have been returned. + GroupState_Done + }; - // Updates _groupMap and _scope to account for the group key associated with this object. - // Returns an error status if an error occurred, else Status::OK(). - Status processObject(const BSONObj& obj); + // Initializes _scope, _reduceFunction and _keyFunction using the global scripting engine. + void initGroupScripting(); - // Finalize the results for this group operation. Returns an owned BSONObj with the results - // array. - BSONObj finalizeResults(); + // Updates _groupMap and _scope to account for the group key associated with this object. + // Returns an error status if an error occurred, else Status::OK(). + Status processObject(const BSONObj& obj); - // Transactional context for read locks. Not owned by us. - OperationContext* _txn; + // Finalize the results for this group operation. Returns an owned BSONObj with the results + // array. + BSONObj finalizeResults(); - GroupRequest _request; + // Transactional context for read locks. Not owned by us. + OperationContext* _txn; - // The WorkingSet we annotate with results. Not owned by us. - WorkingSet* _ws; + GroupRequest _request; - CommonStats _commonStats; - GroupStats _specificStats; + // The WorkingSet we annotate with results. Not owned by us. + WorkingSet* _ws; - std::unique_ptr<PlanStage> _child; + CommonStats _commonStats; + GroupStats _specificStats; - // Current state for this stage. - GroupState _groupState; + std::unique_ptr<PlanStage> _child; - // The Scope object that all script operations for this group stage will use. Initialized - // by initGroupScripting(). Owned here. - std::unique_ptr<Scope> _scope; + // Current state for this stage. + GroupState _groupState; - // The reduce function for the group operation. Initialized by initGroupScripting(). Owned - // by _scope. - ScriptingFunction _reduceFunction; + // The Scope object that all script operations for this group stage will use. Initialized + // by initGroupScripting(). Owned here. + std::unique_ptr<Scope> _scope; - // The key function for the group operation if one was provided by the user, else 0. - // Initialized by initGroupScripting(). Owned by _scope. - ScriptingFunction _keyFunction; + // The reduce function for the group operation. Initialized by initGroupScripting(). Owned + // by _scope. + ScriptingFunction _reduceFunction; - // Map from group key => group index. The group index is used to index into "$arr", a - // variable owned by _scope which contains the group data for this key. - std::map<BSONObj, int, BSONObjCmp> _groupMap; - }; + // The key function for the group operation if one was provided by the user, else 0. + // Initialized by initGroupScripting(). Owned by _scope. + ScriptingFunction _keyFunction; + + // Map from group key => group index. The group index is used to index into "$arr", a + // variable owned by _scope which contains the group data for this key. + std::map<BSONObj, int, BSONObjCmp> _groupMap; +}; } // namespace mongo |