summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/document_source_filter.cpp
blob: de0736beee58756c105d0675cc1b58ce324ad0a1 (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
93
94
95
96
97
98
99
100
101
102
103
104
/**
*    Copyright (C) 2011 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/>.
*/

#include "pch.h"

#include "db/pipeline/document_source.h"

#include "db/jsobj.h"
#include "db/pipeline/expression.h"
#include "db/pipeline/value.h"

namespace mongo {

    const char DocumentSourceFilter::filterName[] = "$filter";

    DocumentSourceFilter::~DocumentSourceFilter() {
    }

    const char *DocumentSourceFilter::getSourceName() const {
        return filterName;
    }

    bool DocumentSourceFilter::coalesce(
        const intrusive_ptr<DocumentSource> &pNextSource) {

        /* we only know how to coalesce other filters */
        DocumentSourceFilter *pDocFilter =
            dynamic_cast<DocumentSourceFilter *>(pNextSource.get());
        if (!pDocFilter)
            return false;

        /*
          Two adjacent filters can be combined by creating a conjunction of
          their predicates.
         */
        intrusive_ptr<ExpressionNary> pAnd(ExpressionAnd::create());
        pAnd->addOperand(pFilter);
        pAnd->addOperand(pDocFilter->pFilter);
        pFilter = pAnd;

        return true;
    }

    void DocumentSourceFilter::optimize() {
        pFilter = pFilter->optimize();
    }

    void DocumentSourceFilter::sourceToBson(
        BSONObjBuilder *pBuilder, bool explain) const {
        pFilter->addToBsonObj(pBuilder, filterName, false);
    }

    bool DocumentSourceFilter::accept(const Document& pDocument) const {
        Value pValue(pFilter->evaluate(pDocument));
        return pValue.coerceToBool();
    }

    intrusive_ptr<DocumentSource> DocumentSourceFilter::createFromBson(
        BSONElement *pBsonElement,
        const intrusive_ptr<ExpressionContext> &pCtx) {
        uassert(15946, "a document filter expression must be an object",
                pBsonElement->type() == Object);

        Expression::ObjectCtx oCtx(0);
        intrusive_ptr<Expression> pExpression(
            Expression::parseObject(pBsonElement, &oCtx));
        intrusive_ptr<DocumentSourceFilter> pFilter(
            DocumentSourceFilter::create(pExpression, pCtx));

        return pFilter;
    }

    intrusive_ptr<DocumentSourceFilter> DocumentSourceFilter::create(
        const intrusive_ptr<Expression> &pFilter,
        const intrusive_ptr<ExpressionContext> &pExpCtx) {
        intrusive_ptr<DocumentSourceFilter> pSource(
            new DocumentSourceFilter(pFilter, pExpCtx));
        return pSource;
    }

    DocumentSourceFilter::DocumentSourceFilter(
        const intrusive_ptr<Expression> &pTheFilter,
        const intrusive_ptr<ExpressionContext> &pExpCtx):
        DocumentSourceFilterBase(pExpCtx),
        pFilter(pTheFilter) {
    }

    void DocumentSourceFilter::toMatcherBson(BSONObjBuilder *pBuilder) const {
        pFilter->toMatcherBson(pBuilder);
    }
}