Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

$maxScan for limiting how much to scan SERVER-1015

  • Loading branch information...
commit aa82a787890de97c2613e719b114f570b5c34d06 1 parent e0d8e85
@erh erh authored
Showing with 27 additions and 2 deletions.
  1. +5 −0 db/query.cpp
  2. +8 −2 db/query.h
  3. +14 −0 jstests/maxscan.js
View
5 db/query.cpp
@@ -527,6 +527,11 @@ namespace mongo {
if( 0 ) {
cout << "SCANNING this: " << this << " key: " << _c->currKey() << " obj: " << _c->current() << endl;
}
+
+ if ( _pq.getMaxScan() && _nscanned >= _pq.getMaxScan() ){
+ finish();
+ return;
+ }
_nscanned++;
if ( !_matcher->matches(_c->currKey(), _c->currLoc() , &_details ) ) {
View
10 db/query.h
@@ -181,7 +181,8 @@ namespace mongo {
const BSONObj& getMax() const { return _max; }
const BSONObj& getOrder() const { return _order; }
const BSONElement& getHint() const { return _hint; }
-
+ int getMaxScan() const { return _maxScan; }
+
bool couldBeCommand() const {
/* we assume you are using findOne() for running a cmd... */
return _ntoreturn == 1 && strstr( _ns , ".$cmd" );
@@ -243,6 +244,7 @@ namespace mongo {
_explain = false;
_snapshot = false;
_returnKey = false;
+ _maxScan = 0;
}
void _initTop( const BSONObj& top ){
@@ -272,6 +274,9 @@ namespace mongo {
_hint = e;
else if ( strcmp( "$returnKey" , name ) == 0 )
_returnKey = e.trueValue();
+ else if ( strcmp( "$maxScan" , name ) == 0 )
+ _maxScan = e.numberInt();
+
}
@@ -310,8 +315,9 @@ namespace mongo {
BSONObj _max;
BSONElement _hint;
BSONObj _order;
+ int _maxScan;
};
-
+
} // namespace mongo
View
14 jstests/maxscan.js
@@ -0,0 +1,14 @@
+
+t = db.maxscan;
+t.drop();
+
+N = 100;
+for ( i=0; i<N; i++ ){
+ t.insert( { _id : i , x : i % 10 } );
+}
+
+assert.eq( N , t.find().itcount() , "A" )
+assert.eq( 50 , t.find()._addSpecial( "$maxScan" , 50 ).itcount() , "B" )
+
+assert.eq( 10 , t.find( { x : 2 } ).itcount() , "C" )
+assert.eq( 5 , t.find( { x : 2 } )._addSpecial( "$maxScan" , 50 ).itcount() , "D" )
Please sign in to comment.
Something went wrong with that request. Please try again.