0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00
mongodb/db/matcher.h

135 lines
3.1 KiB
C
Raw Normal View History

2008-11-30 02:01:58 +01:00
// matcher.h
2008-11-18 21:47:37 +01:00
/* JSMatcher is our boolean expression evaluator for "where" clauses */
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
2008-11-18 21:47:37 +01:00
* 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.
2008-12-29 02:28:49 +01:00
*
2008-11-18 21:47:37 +01:00
* 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.
2008-12-29 02:28:49 +01:00
*
2008-11-18 21:47:37 +01:00
* 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
2008-12-29 02:28:49 +01:00
#include <pcrecpp.h>
2008-12-29 02:28:49 +01:00
class RegexMatcher {
2008-11-18 21:47:37 +01:00
public:
2008-12-29 02:28:49 +01:00
const char *fieldName;
pcrecpp::RE *re;
2008-11-18 21:47:37 +01:00
bool inIndex;
2008-12-29 02:28:49 +01:00
RegexMatcher() {
re = 0;
inIndex = false;
}
~RegexMatcher() {
delete re;
}
2008-11-18 21:47:37 +01:00
};
2008-12-29 02:28:49 +01:00
class BasicMatcher {
2008-11-18 21:47:37 +01:00
public:
BSONElement toMatch;
2008-12-29 02:28:49 +01:00
int compareOp;
2008-11-18 21:47:37 +01:00
bool inIndex;
};
// SQL where clause equivalent
2008-12-29 02:28:49 +01:00
class Where;
2008-11-18 21:47:37 +01:00
/* Match BSON objects against a query pattern.
e.g.
db.foo.find( { a : 3 } );
{ a : 3 } is the pattern object.
GT/LT:
2008-12-29 02:28:49 +01:00
{ a : { $gt : 3 } }
2008-11-18 21:47:37 +01:00
Not equal:
2008-12-29 02:28:49 +01:00
{ a : { $ne : 3 } }
2008-11-18 21:47:37 +01:00
TODO: we should rewrite the matcher to be more an AST style.
*/
2008-12-29 02:28:49 +01:00
class JSMatcher : boost::noncopyable {
int matchesDotted(
const char *fieldName,
BSONElement& toMatch, BSONObj& obj,
int compareOp, bool *deep, bool isArr = false);
struct element_lt
{
bool operator()(const BSONElement& l, const BSONElement& r) const
{
int x = (int) l.type() - (int) r.type();
if ( x < 0 ) return true;
if ( x > 0 ) return false;
return compareElementValues(l,r) < 0;
}
};
2008-11-18 21:47:37 +01:00
public:
2008-12-29 02:28:49 +01:00
enum {
Equality = 0,
LT = 0x1,
LTE = 0x3,
GTE = 0x6,
GT = 0x4,
opIN = 0x8, // { x : { $in : [1,2,3] } }
2008-11-18 21:47:37 +01:00
NE = 0x9
2008-12-29 02:28:49 +01:00
};
2008-11-18 21:47:37 +01:00
2008-12-29 02:28:49 +01:00
static int opDirection(int op) {
return op <= LTE ? -1 : 1;
}
2008-11-18 21:47:37 +01:00
2008-12-29 02:28:49 +01:00
JSMatcher(BSONObj& pattern, BSONObj indexKeyPattern);
2008-11-18 21:47:37 +01:00
2008-12-29 02:28:49 +01:00
~JSMatcher();
2008-11-18 21:47:37 +01:00
2008-12-29 02:28:49 +01:00
/* deep - means we looked into arrays for a match
2008-11-18 21:47:37 +01:00
*/
2008-12-29 02:28:49 +01:00
bool matches(BSONObj& j, bool *deep = 0);
2008-11-18 21:47:37 +01:00
2008-12-29 02:28:49 +01:00
int getN() {
return n;
}
2008-11-18 21:47:37 +01:00
private:
void addBasic(BSONElement e, int c, BSONObj& indexKeyPattern) {
BasicMatcher bm;
bm.toMatch = e;
bm.compareOp = c;
bm.inIndex = indexKeyPattern.hasElement(e.fieldName());
basics.push_back(bm);
n++;
}
2008-12-29 02:28:49 +01:00
int valuesMatch(BSONElement& l, BSONElement& r, int op);
2008-11-18 21:47:37 +01:00
2008-12-29 02:28:49 +01:00
set<BSONElement,element_lt> *in; // set if query uses $in
Where *where; // set if query uses $where
BSONObj& jsobj; // the query pattern. e.g., { name: "joe" }
2008-11-18 21:47:37 +01:00
vector<BasicMatcher> basics;
2008-12-29 02:28:49 +01:00
int n; // # of basicmatcher items
2008-11-18 21:47:37 +01:00
2008-12-29 02:28:49 +01:00
RegexMatcher regexs[4];
int nRegex;
2008-11-18 21:47:37 +01:00
2008-12-29 02:28:49 +01:00
// so we delete the mem when we're done:
BSONObjBuilder *builders[8];
int nBuilders;
2008-11-18 21:47:37 +01:00
bool checkInIndex;
};