summaryrefslogtreecommitdiff
path: root/src/mongo/db/exec/and_hash.h
blob: 5189e3816735e01deed43daee5bc6f13771ff233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 *    Copyright (C) 2013 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <boost/scoped_ptr.hpp>
#include <vector>

#include "mongo/db/diskloc.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/exec/plan_stage.h"
#include "mongo/db/matcher/expression.h"
#include "mongo/platform/unordered_set.h"

namespace mongo {

    /**
     * Reads from N children, each of which must have a valid DiskLoc.  Uses a hash table to
     * intersect the outputs of the N children, and outputs the intersection.
     *
     * Preconditions: Valid DiskLoc.  More than one child.
     *
     * Any DiskLoc that we keep a reference to that is invalidated before we are able to return it
     * is fetched and added to the WorkingSet as "flagged for further review."  Because this stage
     * operates with DiskLocs, we are unable to evaluate the AND for the invalidated DiskLoc, and it
     * must be fully matched later.
     */
    class AndHashStage : public PlanStage {
    public:
        AndHashStage(WorkingSet* ws, const MatchExpression* filter);
        virtual ~AndHashStage();

        void addChild(PlanStage* child);

        virtual StageState work(WorkingSetID* out);
        virtual bool isEOF();

        virtual void prepareToYield();
        virtual void recoverFromYield();
        virtual void invalidate(const DiskLoc& dl);

        virtual PlanStageStats* getStats();

    private:
        StageState readFirstChild();
        StageState hashOtherChildren();

        // Not owned by us.
        WorkingSet* _ws;

        // Not owned by us.
        const MatchExpression* _filter;

        // The stages we read from.  Owned by us.
        vector<PlanStage*> _children;

        // _dataMap is filled out by the first child and probed by subsequent children.
        typedef unordered_map<DiskLoc, WorkingSetID, DiskLoc::Hasher> DataMap;
        DataMap _dataMap;

        // Keeps track of what elements from _dataMap subsequent children have seen.
        typedef unordered_set<DiskLoc, DiskLoc::Hasher> SeenMap;
        SeenMap _seenMap;

        // Iterator over the members of _dataMap that survive.
        DataMap::iterator _resultIterator;

        // True if we're still scanning _children for results.
        bool _shouldScanChildren;

        // Which child are we currently working on?
        size_t _currentChild;

        // Stats
        CommonStats _commonStats;
        AndHashStats _specificStats;
    };

}  // namespace mongo