Files
mongo/db/queryoptimizer.cpp

140 lines
4.2 KiB
C++
Raw Normal View History

2008-11-15 19:04:01 -05:00
/* queryoptimizer.cpp */
2008-11-14 16:19:47 -05:00
/**
* Copyright (C) 2008 10gen Inc.
2008-12-28 20:28:49 -05:00
*
2008-11-14 16:19:47 -05: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-28 20:28:49 -05:00
*
2008-11-14 16:19:47 -05: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-28 20:28:49 -05:00
*
2008-11-14 16:19:47 -05: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/>.
*/
#include "stdafx.h"
#include "query.h"
#include "pdfile.h"
#include "jsobj.h"
#include "../util/builder.h"
#include <time.h>
#include "btree.h"
#include "../util/lruishmap.h"
#include "json.h"
#include "repl.h"
#include "replset.h"
#include "scanandorder.h"
#include "queryoptimizer.h"
2009-01-14 17:09:51 -05:00
namespace mongo {
2009-02-17 15:53:19 -05:00
FieldBound::FieldBound( BSONElement e ) :
lower_( minKey.firstElement() ),
upper_( maxKey.firstElement() ) {
if ( e.eoo() )
return;
if ( e.type() == RegEx ) {
const char *r = e.simpleRegex();
if ( r ) {
lower_ = addObj( BSON( "" << r ) ).firstElement();
upper_ = addObj( BSON( "" << simpleRegexEnd( r ) ) ).firstElement();
}
return;
}
switch( e.getGtLtOp() ) {
case JSMatcher::Equality:
lower_ = e;
upper_ = e;
break;
case JSMatcher::LT:
case JSMatcher::LTE:
upper_ = e;
break;
case JSMatcher::GT:
case JSMatcher::GTE:
lower_ = e;
break;
2009-02-17 19:39:35 -05:00
case JSMatcher::opIN: {
massert( "$in requires array", e.type() == Array );
BSONElement max = minKey.firstElement();
BSONElement min = maxKey.firstElement();
BSONObjIterator i( e.embeddedObject() );
while( i.more() ) {
BSONElement f = i.next();
if ( f.eoo() )
break;
if ( max.woCompare( f, false ) < 0 )
max = f;
if ( min.woCompare( f, false ) > 0 )
min = f;
}
lower_ = min;
upper_ = max;
}
2009-02-17 15:53:19 -05:00
default:
break;
}
}
FieldBound &FieldBound::operator&=( const FieldBound &other ) {
if ( other.upper_.woCompare( upper_, false ) < 0 )
upper_ = other.upper_;
if ( other.lower_.woCompare( lower_, false ) > 0 )
lower_ = other.lower_;
2009-02-17 19:39:35 -05:00
for( vector< BSONObj >::const_iterator i = other.objData_.begin(); i != other.objData_.end(); ++i )
objData_.push_back( *i );
2009-02-17 15:53:19 -05:00
massert( "Invalid bounds", lower_.woCompare( upper_, false ) <= 0 );
return *this;
}
string FieldBound::simpleRegexEnd( string regex ) {
++regex[ regex.length() - 1 ];
return regex;
}
BSONObj FieldBound::addObj( BSONObj o ) {
objData_.push_back( o );
return o;
}
FieldBoundSet::FieldBoundSet( BSONObj query ) :
query_( query.copy() ) {
BSONObjIterator i( query_ );
while( i.more() ) {
BSONElement e = i.next();
if ( e.eoo() )
break;
2009-02-17 19:39:35 -05:00
if ( getGtLtOp( e ) == JSMatcher::Equality ) {
2009-02-17 15:53:19 -05:00
bounds_[ e.fieldName() ] &= FieldBound( e );
2009-02-17 19:39:35 -05:00
}
2009-02-17 15:53:19 -05:00
else {
BSONObjIterator i( e.embeddedObject() );
while( i.more() ) {
BSONElement f = i.next();
if ( f.eoo() )
break;
bounds_[ e.fieldName() ] &= FieldBound( f );
}
}
}
}
QueryPlan QueryOptimizer::getPlan(
const char *ns,
BSONObj* query,
BSONObj* order,
BSONObj* hint)
{
QueryPlan plan;
2008-11-14 16:19:47 -05:00
return plan;
}
2009-01-14 17:09:51 -05:00
} // namespace mongo