2008-07-30 20:08:54 +02:00
|
|
|
// btreecursor.cpp
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-07-30 20:08:54 +02: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-07-30 20:08:54 +02: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-07-30 20:08:54 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
2010-04-27 21:27:52 +02:00
|
|
|
#include "pch.h"
|
2008-07-30 20:08:54 +02:00
|
|
|
#include "btree.h"
|
|
|
|
#include "pdfile.h"
|
2008-12-09 15:54:51 +01:00
|
|
|
#include "jsobj.h"
|
2010-11-04 14:00:39 +01:00
|
|
|
#include "curop-inl.h"
|
2008-07-30 20:08:54 +02:00
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
extern int otherTraceLevel;
|
2008-07-30 20:08:54 +02:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
BtreeCursor::BtreeCursor( NamespaceDetails *_d, int _idxNo, const IndexDetails &_id,
|
2010-07-16 10:49:15 +02:00
|
|
|
const BSONObj &_startKey, const BSONObj &_endKey, bool endKeyInclusive, int _direction ) :
|
2011-01-04 06:40:41 +01:00
|
|
|
d(_d), idxNo(_idxNo),
|
|
|
|
startKey( _startKey ),
|
|
|
|
endKey( _endKey ),
|
|
|
|
_endKeyInclusive( endKeyInclusive ),
|
|
|
|
_multikey( d->isMultikey( idxNo ) ),
|
|
|
|
indexDetails( _id ),
|
|
|
|
_order( _id.keyPattern() ),
|
|
|
|
_ordering( Ordering::make( _order ) ),
|
|
|
|
_direction( _direction ),
|
|
|
|
_spec( _id.getSpec() ),
|
|
|
|
_independentFieldRanges( false ),
|
|
|
|
_nscanned( 0 ) {
|
2009-08-11 22:42:27 +02:00
|
|
|
audit();
|
|
|
|
init();
|
2010-10-19 05:25:56 +02:00
|
|
|
dassert( _dups.size() == 0 );
|
2009-08-11 22:42:27 +02:00
|
|
|
}
|
|
|
|
|
2010-07-16 10:49:15 +02:00
|
|
|
BtreeCursor::BtreeCursor( NamespaceDetails *_d, int _idxNo, const IndexDetails& _id, const shared_ptr< FieldRangeVector > &_bounds, int _direction )
|
2009-08-11 22:42:27 +02:00
|
|
|
:
|
2011-01-04 06:40:41 +01:00
|
|
|
d(_d), idxNo(_idxNo),
|
|
|
|
_endKeyInclusive( true ),
|
|
|
|
_multikey( d->isMultikey( idxNo ) ),
|
|
|
|
indexDetails( _id ),
|
|
|
|
_order( _id.keyPattern() ),
|
|
|
|
_ordering( Ordering::make( _order ) ),
|
|
|
|
_direction( _direction ),
|
|
|
|
_bounds( ( assert( _bounds.get() ), _bounds ) ),
|
|
|
|
_boundsIterator( new FieldRangeVector::Iterator( *_bounds ) ),
|
|
|
|
_spec( _id.getSpec() ),
|
|
|
|
_independentFieldRanges( true ),
|
|
|
|
_nscanned( 0 ) {
|
2010-07-16 14:36:26 +02:00
|
|
|
massert( 13384, "BtreeCursor FieldRangeVector constructor doesn't accept special indexes", !_spec.getType() );
|
2009-08-11 22:42:27 +02:00
|
|
|
audit();
|
2010-10-19 15:13:57 +02:00
|
|
|
startKey = _bounds->startKey();
|
2010-07-16 18:45:59 +02:00
|
|
|
_boundsIterator->advance( startKey ); // handles initialization
|
2010-08-18 20:42:58 +02:00
|
|
|
_boundsIterator->prepDive();
|
|
|
|
pair< DiskLoc, int > noBestParent;
|
|
|
|
bucket = indexDetails.head;
|
|
|
|
keyOfs = 0;
|
2010-11-10 17:49:28 +01:00
|
|
|
indexDetails.head.btree()->customLocate( bucket, keyOfs, startKey, 0, false, _boundsIterator->cmp(), _boundsIterator->inc(), _ordering, _direction, noBestParent );
|
2010-07-16 10:49:15 +02:00
|
|
|
skipAndCheck();
|
2010-10-19 05:25:56 +02:00
|
|
|
dassert( _dups.size() == 0 );
|
2009-08-11 22:42:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BtreeCursor::audit() {
|
2009-07-07 19:17:53 +02:00
|
|
|
dassert( d->idxNo((IndexDetails&) indexDetails) == idxNo );
|
|
|
|
|
2009-02-20 17:08:22 +01:00
|
|
|
if ( otherTraceLevel >= 12 ) {
|
|
|
|
if ( otherTraceLevel >= 200 ) {
|
|
|
|
out() << "::BtreeCursor() qtl>200. validating entire index." << endl;
|
2010-10-19 15:13:57 +02:00
|
|
|
indexDetails.head.btree()->fullValidate(indexDetails.head, _order);
|
2009-02-20 17:08:22 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
out() << "BTreeCursor(). dumping head bucket" << endl;
|
|
|
|
indexDetails.head.btree()->dump();
|
|
|
|
}
|
|
|
|
}
|
2009-08-11 22:42:27 +02:00
|
|
|
}
|
2009-02-24 16:27:07 +01:00
|
|
|
|
2009-08-11 22:42:27 +02:00
|
|
|
void BtreeCursor::init() {
|
2011-01-04 06:40:41 +01:00
|
|
|
if ( _spec.getType() ) {
|
2010-02-25 05:44:49 +01:00
|
|
|
startKey = _spec.getType()->fixKey( startKey );
|
|
|
|
endKey = _spec.getType()->fixKey( endKey );
|
2010-07-07 21:26:20 +02:00
|
|
|
}
|
2009-08-11 22:42:27 +02:00
|
|
|
bool found;
|
2009-02-20 17:08:22 +01:00
|
|
|
bucket = indexDetails.head.btree()->
|
2011-01-04 06:40:41 +01:00
|
|
|
locate(indexDetails, indexDetails.head, startKey, _ordering, keyOfs, found, _direction > 0 ? minDiskLoc : maxDiskLoc, _direction);
|
2010-08-04 05:09:16 +02:00
|
|
|
if ( ok() ) {
|
|
|
|
_nscanned = 1;
|
2011-01-04 06:40:41 +01:00
|
|
|
}
|
2010-07-16 10:49:15 +02:00
|
|
|
skipUnusedKeys( false );
|
|
|
|
checkEnd();
|
2009-09-07 22:19:10 +02:00
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-07-07 21:26:20 +02:00
|
|
|
void BtreeCursor::skipAndCheck() {
|
2010-07-13 01:33:02 +02:00
|
|
|
skipUnusedKeys( true );
|
2010-07-07 21:26:20 +02:00
|
|
|
while( 1 ) {
|
|
|
|
if ( !skipOutOfRangeKeysAndCheckEnd() ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while( skipOutOfRangeKeysAndCheckEnd() );
|
2010-07-13 01:33:02 +02:00
|
|
|
if ( !skipUnusedKeys( true ) ) {
|
2010-07-07 21:26:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-07-16 10:49:15 +02:00
|
|
|
bool BtreeCursor::skipOutOfRangeKeysAndCheckEnd() {
|
|
|
|
if ( !ok() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int ret = _boundsIterator->advance( currKeyNode().key );
|
|
|
|
if ( ret == -2 ) {
|
|
|
|
bucket = DiskLoc();
|
|
|
|
return false;
|
2011-01-04 06:40:41 +01:00
|
|
|
}
|
|
|
|
else if ( ret == -1 ) {
|
2010-08-04 05:09:16 +02:00
|
|
|
++_nscanned;
|
2010-07-16 10:49:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
2010-08-04 05:09:16 +02:00
|
|
|
++_nscanned;
|
2010-08-18 20:42:58 +02:00
|
|
|
advanceTo( currKeyNode().key, ret, _boundsIterator->after(), _boundsIterator->cmp(), _boundsIterator->inc() );
|
2010-07-16 10:49:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
/* skip unused keys. */
|
2010-07-13 01:33:02 +02:00
|
|
|
bool BtreeCursor::skipUnusedKeys( bool mayJump ) {
|
2009-01-15 16:17:11 +01:00
|
|
|
int u = 0;
|
|
|
|
while ( 1 ) {
|
|
|
|
if ( !ok() )
|
|
|
|
break;
|
2010-11-04 01:55:28 +01:00
|
|
|
const BtreeBucket *b = bucket.btree();
|
|
|
|
const _KeyNode& kn = b->k(keyOfs);
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( kn.isUsed() )
|
|
|
|
break;
|
2010-11-10 17:49:28 +01:00
|
|
|
bucket = b->advance(bucket, keyOfs, _direction, "skipUnusedKeys");
|
2009-01-15 16:17:11 +01:00
|
|
|
u++;
|
2010-08-04 05:09:16 +02:00
|
|
|
//don't include unused keys in nscanned
|
|
|
|
//++_nscanned;
|
2010-07-13 01:33:02 +02:00
|
|
|
if ( mayJump && ( u % 10 == 0 ) ) {
|
2010-07-08 08:30:53 +02:00
|
|
|
skipOutOfRangeKeysAndCheckEnd();
|
|
|
|
}
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
|
|
|
if ( u > 10 )
|
|
|
|
OCCASIONALLY log() << "btree unused skipped:" << u << '\n';
|
2010-07-07 21:26:20 +02:00
|
|
|
return u;
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2008-07-30 20:08:54 +02:00
|
|
|
|
2010-07-07 21:26:20 +02:00
|
|
|
// Return a value in the set {-1, 0, 1} to represent the sign of parameter i.
|
2009-01-15 16:17:11 +01:00
|
|
|
int sgn( int i ) {
|
|
|
|
if ( i == 0 )
|
|
|
|
return 0;
|
|
|
|
return i > 0 ? 1 : -1;
|
|
|
|
}
|
2008-12-09 15:54:51 +01:00
|
|
|
|
2009-01-27 21:35:28 +01:00
|
|
|
// Check if the current key is beyond endKey.
|
2009-01-15 16:17:11 +01:00
|
|
|
void BtreeCursor::checkEnd() {
|
|
|
|
if ( bucket.isNull() )
|
|
|
|
return;
|
2009-04-06 23:32:40 +02:00
|
|
|
if ( !endKey.isEmpty() ) {
|
2010-10-19 15:13:57 +02:00
|
|
|
int cmp = sgn( endKey.woCompare( currKey(), _order ) );
|
2010-11-10 17:49:28 +01:00
|
|
|
if ( ( cmp != 0 && cmp != _direction ) ||
|
2011-01-04 06:40:41 +01:00
|
|
|
( cmp == 0 && !_endKeyInclusive ) )
|
2009-04-06 23:32:40 +02:00
|
|
|
bucket = DiskLoc();
|
|
|
|
}
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-08-18 20:42:58 +02:00
|
|
|
void BtreeCursor::advanceTo( const BSONObj &keyBegin, int keyBeginLen, bool afterKey, const vector< const BSONElement * > &keyEnd, const vector< bool > &keyEndInclusive) {
|
2010-11-10 17:49:28 +01:00
|
|
|
bucket.btree()->advanceTo( bucket, keyOfs, keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, _ordering, _direction );
|
2010-07-07 21:26:20 +02:00
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
bool BtreeCursor::advance() {
|
2009-12-22 21:22:37 +01:00
|
|
|
killCurrentOp.checkForInterrupt();
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( bucket.isNull() )
|
|
|
|
return false;
|
2010-07-16 10:49:15 +02:00
|
|
|
|
2010-11-10 17:49:28 +01:00
|
|
|
bucket = bucket.btree()->advance(bucket, keyOfs, _direction, "BtreeCursor::advance");
|
2011-01-04 06:40:41 +01:00
|
|
|
|
2010-07-16 10:49:15 +02:00
|
|
|
if ( !_independentFieldRanges ) {
|
|
|
|
skipUnusedKeys( false );
|
|
|
|
checkEnd();
|
2010-08-04 05:09:16 +02:00
|
|
|
if ( ok() ) {
|
|
|
|
++_nscanned;
|
|
|
|
}
|
2011-01-04 06:40:41 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-08-04 05:09:16 +02:00
|
|
|
skipAndCheck();
|
2010-07-16 10:49:15 +02:00
|
|
|
}
|
|
|
|
return ok();
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2008-07-30 20:08:54 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
void BtreeCursor::noteLocation() {
|
|
|
|
if ( !eof() ) {
|
|
|
|
BSONObj o = bucket.btree()->keyAt(keyOfs).copy();
|
|
|
|
keyAtKeyOfs = o;
|
|
|
|
locAtKeyOfs = bucket.btree()->k(keyOfs).recordLoc;
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
/* Since the last noteLocation(), our key may have moved around, and that old cached
|
|
|
|
information may thus be stale and wrong (although often it is right). We check
|
|
|
|
that here; if we have moved, we have to search back for where we were at.
|
|
|
|
|
|
|
|
i.e., after operations on the index, the BtreeCursor's cached location info may
|
|
|
|
be invalid. This function ensures validity, so you should call it before using
|
|
|
|
the cursor if other writers have used the database since the last noteLocation
|
|
|
|
call.
|
2008-12-29 02:28:49 +01:00
|
|
|
*/
|
2009-01-15 16:17:11 +01:00
|
|
|
void BtreeCursor::checkLocation() {
|
|
|
|
if ( eof() )
|
|
|
|
return;
|
2008-12-29 02:28:49 +01:00
|
|
|
|
2010-11-10 17:49:28 +01:00
|
|
|
_multikey = d->isMultikey(idxNo);
|
2009-07-07 19:17:53 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( keyOfs >= 0 ) {
|
2010-11-04 01:55:28 +01:00
|
|
|
const BtreeBucket *b = bucket.btree();
|
2009-01-15 16:17:11 +01:00
|
|
|
|
|
|
|
assert( !keyAtKeyOfs.isEmpty() );
|
|
|
|
|
|
|
|
// Note keyAt() returns an empty BSONObj if keyOfs is now out of range,
|
|
|
|
// which is possible as keys may have been deleted.
|
2010-05-19 19:34:06 +02:00
|
|
|
int x = 0;
|
|
|
|
while( 1 ) {
|
|
|
|
if ( b->keyAt(keyOfs).woEqual(keyAtKeyOfs) &&
|
2011-01-04 06:40:41 +01:00
|
|
|
b->k(keyOfs).recordLoc == locAtKeyOfs ) {
|
|
|
|
if ( !b->k(keyOfs).isUsed() ) {
|
|
|
|
/* we were deleted but still exist as an unused
|
|
|
|
marker key. advance.
|
|
|
|
*/
|
|
|
|
skipUnusedKeys( false );
|
|
|
|
}
|
|
|
|
return;
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2010-05-19 19:34:06 +02:00
|
|
|
|
2011-01-04 06:40:41 +01:00
|
|
|
/* we check one key earlier too, in case a key was just deleted. this is
|
2010-05-19 19:34:06 +02:00
|
|
|
important so that multi updates are reasonably fast.
|
|
|
|
*/
|
|
|
|
if( keyOfs == 0 || x++ )
|
|
|
|
break;
|
|
|
|
keyOfs--;
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
|
|
|
}
|
2008-12-29 02:28:49 +01:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
/* normally we don't get to here. when we do, old position is no longer
|
|
|
|
valid and we must refind where we left off (which is expensive)
|
|
|
|
*/
|
2008-07-30 20:08:54 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
bool found;
|
2008-07-30 20:08:54 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
/* TODO: Switch to keep indexdetails and do idx.head! */
|
2010-11-10 17:49:28 +01:00
|
|
|
bucket = indexDetails.head.btree()->locate(indexDetails, indexDetails.head, keyAtKeyOfs, _ordering, keyOfs, found, locAtKeyOfs, _direction);
|
2009-01-15 16:17:11 +01:00
|
|
|
RARELY log() << " key seems to have moved in the index, refinding. found:" << found << endl;
|
2009-11-23 07:29:38 +01:00
|
|
|
if ( ! bucket.isNull() )
|
2010-07-13 01:33:02 +02:00
|
|
|
skipUnusedKeys( false );
|
2009-11-23 06:38:53 +01:00
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2009-01-15 16:17:11 +01:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------- */
|
|
|
|
|
2009-02-13 17:25:52 +01:00
|
|
|
struct BtreeCursorUnitTest {
|
|
|
|
BtreeCursorUnitTest() {
|
2009-01-15 16:17:11 +01:00
|
|
|
assert( minDiskLoc.compare(maxDiskLoc) < 0 );
|
|
|
|
}
|
|
|
|
} btut;
|
2009-01-14 23:09:51 +01:00
|
|
|
|
|
|
|
} // namespace mongo
|