2
* Copyright (C) 2011 10gen Inc.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Affero General Public License, version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Affero General Public License for more details.
13
* You should have received a copy of the GNU Affero General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include "db/pipeline/document_source.h"
22
#include "db/pipeline/expression.h"
23
#include "db/pipeline/value.h"
27
const char DocumentSourceFilter::filterName[] = "$filter";
29
DocumentSourceFilter::~DocumentSourceFilter() {
32
const char *DocumentSourceFilter::getSourceName() const {
36
bool DocumentSourceFilter::coalesce(
37
const intrusive_ptr<DocumentSource> &pNextSource) {
39
/* we only know how to coalesce other filters */
40
DocumentSourceFilter *pDocFilter =
41
dynamic_cast<DocumentSourceFilter *>(pNextSource.get());
46
Two adjacent filters can be combined by creating a conjunction of
49
intrusive_ptr<ExpressionNary> pAnd(ExpressionAnd::create());
50
pAnd->addOperand(pFilter);
51
pAnd->addOperand(pDocFilter->pFilter);
57
void DocumentSourceFilter::optimize() {
58
pFilter = pFilter->optimize();
61
void DocumentSourceFilter::sourceToBson(
62
BSONObjBuilder *pBuilder, bool explain) const {
63
pFilter->addToBsonObj(pBuilder, filterName, false);
66
bool DocumentSourceFilter::accept(const Document& pDocument) const {
67
Value pValue(pFilter->evaluate(pDocument));
68
return pValue.coerceToBool();
71
intrusive_ptr<DocumentSource> DocumentSourceFilter::createFromBson(
72
BSONElement *pBsonElement,
73
const intrusive_ptr<ExpressionContext> &pCtx) {
74
uassert(15946, "a document filter expression must be an object",
75
pBsonElement->type() == Object);
77
Expression::ObjectCtx oCtx(0);
78
intrusive_ptr<Expression> pExpression(
79
Expression::parseObject(pBsonElement, &oCtx));
80
intrusive_ptr<DocumentSourceFilter> pFilter(
81
DocumentSourceFilter::create(pExpression, pCtx));
86
intrusive_ptr<DocumentSourceFilter> DocumentSourceFilter::create(
87
const intrusive_ptr<Expression> &pFilter,
88
const intrusive_ptr<ExpressionContext> &pExpCtx) {
89
intrusive_ptr<DocumentSourceFilter> pSource(
90
new DocumentSourceFilter(pFilter, pExpCtx));
94
DocumentSourceFilter::DocumentSourceFilter(
95
const intrusive_ptr<Expression> &pTheFilter,
96
const intrusive_ptr<ExpressionContext> &pExpCtx):
97
DocumentSourceFilterBase(pExpCtx),
101
void DocumentSourceFilter::toMatcherBson(BSONObjBuilder *pBuilder) const {
102
pFilter->toMatcherBson(pBuilder);