summaryrefslogtreecommitdiff
path: root/db/dbcommands.cpp
diff options
context:
space:
mode:
authorAaron <aaron@10gen.com>2009-04-30 16:25:36 -0400
committerAaron <aaron@10gen.com>2009-04-30 16:25:36 -0400
commitaf59c4ea9e47fdc77a2ae19e625971fa7245424b (patch)
tree8646591c24a4f56eb9b0820c852eac0aa37214b8 /db/dbcommands.cpp
parentde01cc8c1aedadb1acfff8d6eb575e28aaf89a86 (diff)
downloadmongo-af59c4ea9e47fdc77a2ae19e625971fa7245424b.tar.gz
core implementation of min/max query parameters
Diffstat (limited to 'db/dbcommands.cpp')
-rw-r--r--db/dbcommands.cpp89
1 files changed, 1 insertions, 88 deletions
diff --git a/db/dbcommands.cpp b/db/dbcommands.cpp
index 9b980dec29e..272c31dde33 100644
--- a/db/dbcommands.cpp
+++ b/db/dbcommands.cpp
@@ -34,6 +34,7 @@
#include "instance.h"
#include "lasterror.h"
#include "security.h"
+#include "queryoptimizer.h"
namespace mongo {
@@ -821,94 +822,6 @@ namespace mongo {
}
} cmdFileMD5;
- bool indexWorks( const BSONObj &idxPattern, const BSONObj &sampleKey, int direction, int firstSignificantField ) {
- BSONObjIterator p( idxPattern );
- BSONObjIterator k( sampleKey );
- int i = 0;
- while( 1 ) {
- BSONElement pe = p.next();
- BSONElement ke = k.next();
- if ( pe.eoo() && ke.eoo() )
- return true;
- if ( pe.eoo() || ke.eoo() )
- return false;
- if ( strcmp( pe.fieldName(), ke.fieldName() ) != 0 )
- return false;
- if ( ( i == firstSignificantField ) && !( ( direction > 0 ) == ( pe.number() > 0 ) ) )
- return false;
- ++i;
- }
- return false;
- }
-
- // NOTE min, max, and keyPattern will be updated to be consistent with the selected index.
- const IndexDetails *indexDetailsForRange( const char *ns, string &errmsg, BSONObj &min, BSONObj &max, BSONObj &keyPattern ) {
- if ( ns[ 0 ] == '\0' || min.isEmpty() || max.isEmpty() ) {
- errmsg = "invalid command syntax (note: min and max are required)";
- return 0;
- }
-
- setClient( ns );
- const IndexDetails *id = 0;
- NamespaceDetails *d = nsdetails( ns );
- if ( !d ) {
- errmsg = "ns not found";
- return 0;
- }
-
- if ( keyPattern.isEmpty() ) {
- BSONObjIterator i( min );
- BSONObjIterator a( max );
- int direction = 0;
- int firstSignificantField = 0;
- while( 1 ) {
- BSONElement ie = i.next();
- BSONElement ae = a.next();
- if ( ie.eoo() && ae.eoo() )
- break;
- if ( ie.eoo() || ae.eoo() || strcmp( ie.fieldName(), ae.fieldName() ) != 0 ) {
- errmsg = "min and max keys do not share pattern";
- return 0;
- }
- int cmp = ie.woCompare( ae );
- if ( cmp < 0 )
- direction = 1;
- if ( cmp > 0 )
- direction = -1;
- if ( direction != 0 )
- break;
- ++firstSignificantField;
- }
- for (int i = 0; i < d->nIndexes; i++ ) {
- IndexDetails& ii = d->indexes[i];
- if ( indexWorks( ii.keyPattern(), min, direction, firstSignificantField ) ) {
- id = &ii;
- keyPattern = ii.keyPattern();
- break;
- }
- }
-
- } else {
- for (int i = 0; i < d->nIndexes; i++ ) {
- IndexDetails& ii = d->indexes[i];
- if( ii.keyPattern().woCompare(keyPattern) == 0 ) {
- id = &ii;
- break;
- }
- }
- }
-
- if ( !id ) {
- errmsg = "no index found for specified keyPattern";
- return 0;
- }
-
- min = min.extractFieldsUnDotted( keyPattern );
- max = max.extractFieldsUnDotted( keyPattern );
-
- return id;
- }
-
class CmdMedianKey : public Command {
public:
CmdMedianKey() : Command( "medianKey" ) {}