Files
mongo/db/btree.cpp

1860 lines
73 KiB
C++
Raw Normal View History

2008-06-06 09:43:15 -04:00
// btree.cpp
/**
* Copyright (C) 2008 10gen Inc.
2008-12-28 20:28:49 -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
*
* 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
*
* 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 15:27:52 -04:00
#include "pch.h"
#include "db.h"
2008-06-06 09:43:15 -04:00
#include "btree.h"
#include "pdfile.h"
#include "json.h"
#include "clientcursor.h"
#include "client.h"
#include "dbhelpers.h"
#include "curop-inl.h"
2010-02-01 10:38:00 -05:00
#include "stats/counters.h"
2010-12-24 12:20:33 -05:00
#include "dur_commitjob.h"
2011-04-11 18:21:48 -04:00
#include "btreebuilder.h"
2008-06-06 09:43:15 -04:00
2009-01-14 17:09:51 -05:00
namespace mongo {
2011-04-22 18:16:43 -04:00
BOOST_STATIC_ASSERT( Record::HeaderSize == 16 );
BOOST_STATIC_ASSERT( Record::HeaderSize + BtreeData_V1::BucketSize == 8192 );
#define VERIFYTHISLOC dassert( thisLoc.btree<V>() == this );
2011-01-04 00:40:41 -05:00
_KeyNode& _KeyNode::writing() const {
return *getDur().writing( const_cast< _KeyNode* >( this ) );
}
template< class V >
BucketBasics<V>::KeyNode::KeyNode(const BucketBasics<V>& bb, const _KeyNode &k) :
2011-01-04 00:40:41 -05:00
prevChildBucket(k.prevChildBucket),
recordLoc(k.recordLoc), key(bb.data+k.keyDataOfs())
{ }
// BucketBasics::lowWaterMark()
//
// We define this value as the maximum number of bytes such that, if we have
// fewer than this many bytes, we must be able to either merge with or receive
// keys from any neighboring node. If our utilization goes below this value we
// know we can bring up the utilization with a simple operation. Ignoring the
// 90/10 split policy which is sometimes employed and our 'unused' nodes, this
// is a lower bound on bucket utilization for non root buckets.
2011-01-04 00:40:41 -05:00
//
// Note that the exact value here depends on the implementation of
// rebalancedSeparatorPos(). The conditions for lowWaterMark - 1 are as
// follows: We know we cannot merge with the neighbor, so the total data size
// for us, the neighbor, and the separator must be at least
// BtreeBucket<V>::bodySize() + 1. We must be able to accept one key of any
// allowed size, so our size plus storage for that additional key must be
// <= BtreeBucket<V>::bodySize() / 2. This way, with the extra key we'll have a
// new bucket data size < half the total data size and by the implementation
// of rebalancedSeparatorPos() the key must be added.
2010-11-05 17:15:39 -04:00
static const int split_debug = 0;
static const int insert_debug = 0;
extern int otherTraceLevel;
/**
2011-01-04 00:40:41 -05:00
* this error is ok/benign when doing a background indexing -- that logic in pdfile checks explicitly
* for the 10287 error code.
*/
2011-01-04 00:40:41 -05:00
static void alreadyInIndex() {
2010-02-11 14:49:55 -05:00
// we don't use massert() here as that does logging and this is 'benign' - see catches in _indexRecord()
throw MsgAssertionException(10287, "btree: key+recloc already in index");
}
/* BucketBasics --------------------------------------------------- */
template< class V >
void BucketBasics<V>::assertWritable() {
2011-01-04 00:40:41 -05:00
if( cmdLine.dur )
2011-05-02 18:16:03 -04:00
dur::assertAlreadyDeclared(this, V::BucketSize);
2010-12-24 12:20:33 -05:00
}
template< class V >
string BtreeBucket<V>::bucketSummary() const {
2010-10-05 21:01:40 -04:00
stringstream ss;
ss << " Bucket info:" << endl;
2011-05-02 18:16:03 -04:00
ss << " n: " << this->n << endl;
ss << " parent: " << this->parent.toString() << endl;
ss << " nextChild: " << this->parent.toString() << endl;
ss << " flags:" << this->flags << endl;
ss << " emptySize: " << this->emptySize << " topSize: " << this->topSize << endl;
2010-10-05 21:01:40 -04:00
return ss.str();
2009-02-02 18:18:22 -05:00
}
2011-01-04 00:40:41 -05:00
template< class V >
int BucketBasics<V>::Size() const {
2011-05-02 18:16:03 -04:00
return V::BucketSize;
}
template< class V >
void BucketBasics<V>::_shape(int level, stringstream& ss) const {
for ( int i = 0; i < level; i++ ) ss << ' ';
ss << "*\n";
for ( int i = 0; i < this->n; i++ ) {
if ( !k(i).prevChildBucket.isNull() ) {
DiskLoc ll = k(i).prevChildBucket;
ll.btree<V>()->_shape(level+1,ss);
}
}
2011-05-02 18:16:03 -04:00
if ( !this->nextChild.isNull() ) {
DiskLoc ll = this->nextChild;
ll.btree<V>()->_shape(level+1,ss);
}
}
int bt_fv=0;
int bt_dmp=0;
template< class V >
void BtreeBucket<V>::dumpTree(const DiskLoc &thisLoc, const BSONObj &order) const {
bt_dmp=1;
fullValidate(thisLoc, order);
bt_dmp=0;
}
template< class V >
long long BtreeBucket<V>::fullValidate(const DiskLoc& thisLoc, const BSONObj &order, long long *unusedCount, bool strict) const {
{
bool f = false;
assert( f = true );
massert( 10281 , "assert is misdefined", f);
}
2009-12-22 15:22:37 -05:00
killCurrentOp.checkForInterrupt();
2011-05-02 18:16:03 -04:00
this->assertValid(order, true);
2008-12-28 20:28:49 -05:00
if ( bt_dmp ) {
out() << thisLoc.toString() << ' ';
((BtreeBucket *) this)->dump();
}
2008-12-28 20:28:49 -05:00
// keycount
long long kc = 0;
2008-12-28 20:28:49 -05:00
2011-05-02 18:16:03 -04:00
for ( int i = 0; i < this->n; i++ ) {
const _KeyNode& kn = this->k(i);
2008-12-28 20:28:49 -05:00
2010-06-28 22:16:07 -07:00
if ( kn.isUsed() ) {
kc++;
2011-01-04 00:40:41 -05:00
}
else {
2010-06-28 22:16:07 -07:00
if ( unusedCount ) {
++( *unusedCount );
}
}
if ( !kn.prevChildBucket.isNull() ) {
DiskLoc left = kn.prevChildBucket;
2011-04-22 18:16:43 -04:00
const BtreeBucket *b = left.btree<V>();
if ( strict ) {
2011-01-04 00:40:41 -05:00
assert( b->parent == thisLoc );
}
else {
wassert( b->parent == thisLoc );
}
kc += b->fullValidate(kn.prevChildBucket, order, unusedCount, strict);
}
}
2011-05-02 18:16:03 -04:00
if ( !this->nextChild.isNull() ) {
DiskLoc ll = this->nextChild;
const BtreeBucket *b = ll.btree<V>();
if ( strict ) {
2011-01-04 00:40:41 -05:00
assert( b->parent == thisLoc );
}
else {
wassert( b->parent == thisLoc );
}
2011-05-02 18:16:03 -04:00
kc += b->fullValidate(this->nextChild, order, unusedCount, strict);
}
return kc;
}
int nDumped = 0;
template< class V >
void BucketBasics<V>::assertValid(const Ordering &order, bool force) const {
if ( !debug && !force )
return;
2011-05-03 16:24:42 -04:00
{
int foo = this->n;
wassert( foo >= 0 && this->n < Size() );
foo = this->emptySize;
wassert( foo >= 0 && this->emptySize < V::BucketSize );
wassert( this->topSize >= this->n && this->topSize <= V::BucketSize );
}
2010-08-22 10:30:26 -04:00
// this is very slow so don't do often
{
static int _k;
2011-01-04 00:40:41 -05:00
if( ++_k % 128 )
2010-08-22 10:30:26 -04:00
return;
}
DEV {
// slow:
2011-05-02 18:16:03 -04:00
for ( int i = 0; i < this->n-1; i++ ) {
2011-04-11 18:21:48 -04:00
Key k1 = keyNode(i).key;
Key k2 = keyNode(i+1).key;
int z = k1.woCompare(k2, order); //OK
if ( z > 0 ) {
out() << "ERROR: btree key order corrupt. Keys:" << endl;
if ( ++nDumped < 5 ) {
2011-05-02 18:16:03 -04:00
for ( int j = 0; j < this->n; j++ ) {
out() << " " << keyNode(j).key.toString() << endl;
}
2011-04-22 18:16:43 -04:00
((BtreeBucket<V> *) this)->dump();
2008-12-28 20:28:49 -05:00
}
wassert(false);
break;
2008-12-28 20:28:49 -05:00
}
else if ( z == 0 ) {
if ( !(k(i).recordLoc < k(i+1).recordLoc) ) {
out() << "ERROR: btree key order corrupt (recordloc's wrong). Keys:" << endl;
out() << " k(" << i << "):" << keyNode(i).key.toString() << " RL:" << k(i).recordLoc.toString() << endl;
out() << " k(" << i+1 << "):" << keyNode(i+1).key.toString() << " RL:" << k(i+1).recordLoc.toString() << endl;
wassert( k(i).recordLoc < k(i+1).recordLoc );
}
2008-12-28 20:28:49 -05:00
}
}
}
else {
//faster:
2011-05-02 18:16:03 -04:00
if ( this->n > 1 ) {
2011-04-11 18:21:48 -04:00
Key k1 = keyNode(0).key;
2011-05-02 18:16:03 -04:00
Key k2 = keyNode(this->n-1).key;
int z = k1.woCompare(k2, order);
//wassert( z <= 0 );
if ( z > 0 ) {
problem() << "btree keys out of order" << '\n';
ONCE {
2011-04-22 18:16:43 -04:00
((BtreeBucket<V> *) this)->dump();
}
assert(false);
}
}
2008-12-28 20:28:49 -05:00
}
}
2008-06-06 09:43:15 -04:00
template< class V >
inline void BucketBasics<V>::markUnused(int keypos) {
2011-05-02 18:16:03 -04:00
assert( keypos >= 0 && keypos < this->n );
k(keypos).setUnused();
}
template< class V >
inline int BucketBasics<V>::totalDataSize() const {
2011-05-02 18:16:03 -04:00
return (int) (Size() - (this->data-(char*)this));
}
template< class V >
void BucketBasics<V>::init() {
2011-05-02 18:16:03 -04:00
this->_init();
this->parent.Null();
this->nextChild.Null();
this->flags = Packed;
this->n = 0;
this->emptySize = totalDataSize();
this->topSize = 0;
}
/** see _alloc */
template< class V >
inline void BucketBasics<V>::_unalloc(int bytes) {
2011-05-02 18:16:03 -04:00
this->topSize -= bytes;
this->emptySize += bytes;
}
/**
* we allocate space from the end of the buffer for data.
* the keynodes grow from the front.
*/
template< class V >
inline int BucketBasics<V>::_alloc(int bytes) {
2011-05-06 19:37:23 -04:00
assert( this->emptySize >= bytes );
2011-05-02 18:16:03 -04:00
this->topSize += bytes;
this->emptySize -= bytes;
int ofs = totalDataSize() - this->topSize;
assert( ofs > 0 );
return ofs;
}
template< class V >
void BucketBasics<V>::_delKeyAtPos(int keypos, bool mayEmpty) {
// TODO This should be keypos < n
2011-05-02 18:16:03 -04:00
assert( keypos >= 0 && keypos <= this->n );
assert( childForPos(keypos).isNull() );
// TODO audit cases where nextChild is null
2011-05-02 18:16:03 -04:00
assert( ( mayEmpty && this->n > 0 ) || this->n > 1 || this->nextChild.isNull() );
this->emptySize += sizeof(_KeyNode);
this->n--;
for ( int j = keypos; j < this->n; j++ )
k(j) = k(j+1);
setNotPacked();
}
/**
2011-01-04 00:40:41 -05:00
* pull rightmost key from the bucket. this version requires its right child to be null so it
* does not bother returning that value.
*/
template< class V >
void BucketBasics<V>::popBack(DiskLoc& recLoc, Key& key) {
2011-05-02 18:16:03 -04:00
massert( 10282 , "n==0 in btree popBack()", this->n > 0 );
assert( k(this->n-1).isUsed() ); // no unused skipping in this function at this point - btreebuilder doesn't require that
KeyNode kn = keyNode(this->n-1);
2009-09-24 12:11:55 -04:00
recLoc = kn.recordLoc;
key = kn.key;
2011-04-11 18:21:48 -04:00
int keysize = kn.key.dataSize();
2009-09-26 04:18:39 -04:00
2011-05-02 18:16:03 -04:00
massert( 10283 , "rchild not null in btree popBack()", this->nextChild.isNull());
2009-09-26 04:18:39 -04:00
2011-01-04 00:40:41 -05:00
// weirdly, we also put the rightmost down pointer in nextchild, even when bucket isn't full.
2011-05-02 18:16:03 -04:00
this->nextChild = kn.prevChildBucket;
2009-09-26 04:18:39 -04:00
2011-05-02 18:16:03 -04:00
this->n--;
// This is risky because the key we are returning points to this unalloc'ed memory,
// and we are assuming that the last key points to the last allocated
// bson region.
2011-05-02 18:16:03 -04:00
this->emptySize += sizeof(_KeyNode);
_unalloc(keysize);
2009-09-24 12:11:55 -04:00
}
/** add a key. must be > all existing. be careful to set next ptr right. */
template< class V >
bool BucketBasics<V>::_pushBack(const DiskLoc recordLoc, const Key& key, const Ordering &order, const DiskLoc prevChild) {
2011-04-11 18:21:48 -04:00
int bytesNeeded = key.dataSize() + sizeof(_KeyNode);
2011-05-02 18:16:03 -04:00
if ( bytesNeeded > this->emptySize )
return false;
2011-05-02 18:16:03 -04:00
assert( bytesNeeded <= this->emptySize );
if( this->n ) {
const KeyNode klast = keyNode(this->n-1);
2011-04-20 09:30:56 -04:00
if( klast.key.woCompare(key, order) > 0 ) {
log() << "btree bucket corrupt? consider reindexing or running validate command" << endl;
//cout << keyNode(n-1).key.toString() << endl;
//cout << key.toString() << endl;
assert(false);
}
}
2011-05-02 18:16:03 -04:00
this->emptySize -= sizeof(_KeyNode);
_KeyNode& kn = k(this->n++);
kn.prevChildBucket = prevChild;
kn.recordLoc = recordLoc;
2011-04-11 18:21:48 -04:00
kn.setKeyDataOfs( (short) _alloc(key.dataSize()) );
2011-04-20 09:30:56 -04:00
short ofs = kn.keyDataOfs();
char *p = dataAt(ofs);
2011-04-11 18:21:48 -04:00
memcpy(p, key.data(), key.dataSize());
2011-04-20 09:30:56 -04:00
return true;
}
2010-11-23 10:12:31 -05:00
/* durability note
2011-01-04 00:40:41 -05:00
we do separate intent declarations herein. arguably one could just declare
the whole bucket given we do group commits. this is something we could investigate
2010-11-23 10:12:31 -05:00
later as to what is faster under what situations.
*/
2011-01-04 00:40:41 -05:00
/** insert a key in a bucket with no complexity -- no splits required
@return false if a split is required.
*/
template< class V >
bool BucketBasics<V>::basicInsert(const DiskLoc thisLoc, int &keypos, const DiskLoc recordLoc, const Key& key, const Ordering &order) const {
2011-05-02 18:16:03 -04:00
assert( keypos >= 0 && keypos <= this->n );
2011-04-11 18:21:48 -04:00
int bytesNeeded = key.dataSize() + sizeof(_KeyNode);
2011-05-02 18:16:03 -04:00
if ( bytesNeeded > this->emptySize ) {
_pack(thisLoc, order, keypos);
2011-05-02 18:16:03 -04:00
if ( bytesNeeded > this->emptySize )
return false;
}
2010-09-27 12:35:22 -04:00
BucketBasics *b;
{
const char *p = (const char *) &k(keypos);
2011-05-02 18:16:03 -04:00
const char *q = (const char *) &k(this->n+1);
// declare that we will write to [k(keypos),k(n)]
2011-01-04 00:40:41 -05:00
// todo: this writes a medium amount to the journal. we may want to add a verb "shift" to the redo log so
// we can log a very small amount.
b = (BucketBasics*) getDur().writingAtOffset((void *) this, p-(char*)this, q-p);
// e.g. n==3, keypos==2
// 1 4 9
// ->
// 1 4 _ 9
2011-05-02 18:16:03 -04:00
for ( int j = this->n; j > keypos; j-- ) // make room
b->k(j) = b->k(j-1);
}
2010-09-27 12:35:22 -04:00
2011-05-02 18:16:03 -04:00
getDur().declareWriteIntent(&b->emptySize, sizeof(this->emptySize)+sizeof(this->topSize)+sizeof(this->n));
2010-09-27 12:35:22 -04:00
b->emptySize -= sizeof(_KeyNode);
b->n++;
// This _KeyNode was marked for writing above.
2010-09-27 12:35:22 -04:00
_KeyNode& kn = b->k(keypos);
kn.prevChildBucket.Null();
kn.recordLoc = recordLoc;
2011-04-11 18:21:48 -04:00
kn.setKeyDataOfs((short) b->_alloc(key.dataSize()) );
2010-09-27 12:35:22 -04:00
char *p = b->dataAt(kn.keyDataOfs());
2011-04-11 18:21:48 -04:00
getDur().declareWriteIntent(p, key.dataSize());
memcpy(p, key.data(), key.dataSize());
return true;
}
2008-06-06 09:43:15 -04:00
/**
* With this implementation, refPos == 0 disregards effect of refPos.
* index > 0 prevents creation of an empty bucket.
*/
template< class V >
bool BucketBasics<V>::mayDropKey( int index, int refPos ) const {
2010-10-26 11:42:06 -07:00
return index > 0 && ( index != refPos ) && k( index ).isUnused() && k( index ).prevChildBucket.isNull();
}
2011-01-04 00:40:41 -05:00
template< class V >
int BucketBasics<V>::packedDataSize( int refPos ) const {
2011-05-02 18:16:03 -04:00
if ( this->flags & Packed ) {
return V::BucketSize - this->emptySize - headerSize();
2010-10-26 11:42:06 -07:00
}
int size = 0;
2011-05-02 18:16:03 -04:00
for( int j = 0; j < this->n; ++j ) {
2010-10-26 11:42:06 -07:00
if ( mayDropKey( j, refPos ) ) {
continue;
}
2011-04-11 18:21:48 -04:00
size += keyNode( j ).key.dataSize() + sizeof( _KeyNode );
2010-10-26 11:42:06 -07:00
}
return size;
}
2011-01-04 00:40:41 -05:00
/**
* when we delete things we just leave empty space until the node is
* full and then we repack it.
*/
template< class V >
void BucketBasics<V>::_pack(const DiskLoc thisLoc, const Ordering &order, int &refPos) const {
2011-05-02 18:16:03 -04:00
if ( this->flags & Packed )
return;
2010-12-24 12:20:33 -05:00
VERIFYTHISLOC
2011-01-04 00:40:41 -05:00
/** TODO perhaps this can be optimized. for example if packing does no write, we can skip intent decl.
an empirical approach is probably best than just adding new code : perhaps the bucket would need
declaration anyway within the group commit interval, in which case we would just be adding
2010-12-24 12:20:33 -05:00
code and complexity without benefit.
*/
2011-04-22 18:16:43 -04:00
thisLoc.btreemod<V>()->_packReadyForMod(order, refPos);
}
2010-12-24 12:20:33 -05:00
/** version when write intent already declared */
template< class V >
void BucketBasics<V>::_packReadyForMod( const Ordering &order, int &refPos ) {
2010-12-24 12:20:33 -05:00
assertWritable();
2011-05-02 18:16:03 -04:00
if ( this->flags & Packed )
return;
2008-06-06 09:43:15 -04:00
int tdz = totalDataSize();
2011-05-02 18:16:03 -04:00
char temp[V::BucketSize];
int ofs = tdz;
2011-05-02 18:16:03 -04:00
this->topSize = 0;
2010-06-28 22:16:07 -07:00
int i = 0;
2011-05-02 18:16:03 -04:00
for ( int j = 0; j < this->n; j++ ) {
2010-10-26 11:42:06 -07:00
if( mayDropKey( j, refPos ) ) {
2010-06-28 22:16:07 -07:00
continue; // key is unused and has no children - drop it
}
if( i != j ) {
2010-07-08 01:41:46 -07:00
if ( refPos == j ) {
2010-07-07 22:25:09 -07:00
refPos = i; // i < j so j will never be refPos again
}
2010-06-28 22:16:07 -07:00
k( i ) = k( j );
}
short ofsold = k(i).keyDataOfs();
2011-04-11 18:21:48 -04:00
int sz = keyNode(i).key.dataSize();
ofs -= sz;
2011-05-02 18:16:03 -04:00
this->topSize += sz;
memcpy(temp+ofs, dataAt(ofsold), sz);
2010-06-28 22:16:07 -07:00
k(i).setKeyDataOfsSavingUse( ofs );
++i;
}
2011-05-02 18:16:03 -04:00
if ( refPos == this->n ) {
2010-07-08 01:41:46 -07:00
refPos = i;
}
2011-05-02 18:16:03 -04:00
this->n = i;
int dataUsed = tdz - ofs;
2011-05-02 18:16:03 -04:00
memcpy(this->data + ofs, temp + ofs, dataUsed);
2010-12-24 12:20:33 -05:00
// assertWritable();
// TEMP TEST getDur().declareWriteIntent(this, sizeof(*this));
2011-05-02 18:16:03 -04:00
this->emptySize = tdz - dataUsed - this->n * sizeof(_KeyNode);
2011-05-03 16:24:42 -04:00
{
int foo = this->emptySize;
assert( foo >= 0 );
}
2008-06-06 09:43:15 -04:00
setPacked();
2011-01-04 00:40:41 -05:00
assertValid( order );
}
template< class V >
inline void BucketBasics<V>::truncateTo(int N, const Ordering &order, int &refPos) {
2010-12-24 12:20:33 -05:00
dbMutex.assertWriteLocked();
assertWritable();
2011-05-02 18:16:03 -04:00
this->n = N;
setNotPacked();
_packReadyForMod( order, refPos );
2008-12-28 20:28:49 -05:00
}
/**
* In the standard btree algorithm, we would split based on the
* existing keys _and_ the new key. But that's more work to
* implement, so we split the existing keys and then add the new key.
*
* There are several published heuristic algorithms for doing splits,
* but basically what you want are (1) even balancing between the two
* sides and (2) a small split key so the parent can have a larger
* branching factor.
*
* We just have a simple algorithm right now: if a key includes the
* halfway point (or 10% way point) in terms of bytes, split on that key;
* otherwise split on the key immediately to the left of the halfway
* point (or 10% point).
*
* This function is expected to be called on a packed bucket.
*/
template< class V >
int BucketBasics<V>::splitPos( int keypos ) const {
2011-05-02 18:16:03 -04:00
assert( this->n > 2 );
int split = 0;
int rightSize = 0;
2011-01-04 00:40:41 -05:00
// when splitting a btree node, if the new key is greater than all the other keys, we should not do an even split, but a 90/10 split.
// see SERVER-983
// TODO I think we only want to do the 90% split on the rhs node of the tree.
2011-05-02 18:16:03 -04:00
int rightSizeLimit = ( this->topSize + sizeof( _KeyNode ) * this->n ) / ( keypos == this->n ? 10 : 2 );
for( int i = this->n - 1; i > -1; --i ) {
2011-04-11 18:21:48 -04:00
rightSize += keyNode( i ).key.dataSize() + sizeof( _KeyNode );
if ( rightSize > rightSizeLimit ) {
split = i;
break;
}
}
// safeguards - we must not create an empty bucket
if ( split < 1 ) {
split = 1;
2011-01-04 00:40:41 -05:00
}
2011-05-02 18:16:03 -04:00
else if ( split > this->n - 2 ) {
split = this->n - 2;
}
2011-01-04 00:40:41 -05:00
return split;
}
2011-01-04 00:40:41 -05:00
template< class V >
void BucketBasics<V>::reserveKeysFront( int nAdd ) {
2011-05-02 18:16:03 -04:00
assert( this->emptySize >= int( sizeof( _KeyNode ) * nAdd ) );
this->emptySize -= sizeof( _KeyNode ) * nAdd;
for( int i = this->n - 1; i > -1; --i ) {
k( i + nAdd ) = k( i );
}
2011-05-02 18:16:03 -04:00
this->n += nAdd;
}
2011-01-04 00:40:41 -05:00
template< class V >
void BucketBasics<V>::setKey( int i, const DiskLoc recordLoc, const Key &key, const DiskLoc prevChildBucket ) {
_KeyNode &kn = k( i );
kn.recordLoc = recordLoc;
kn.prevChildBucket = prevChildBucket;
2011-04-11 18:21:48 -04:00
short ofs = (short) _alloc( key.dataSize() );
kn.setKeyDataOfs( ofs );
char *p = dataAt( ofs );
2011-04-11 18:21:48 -04:00
memcpy( p, key.data(), key.dataSize() );
}
2011-01-04 00:40:41 -05:00
template< class V >
void BucketBasics<V>::dropFront( int nDrop, const Ordering &order, int &refpos ) {
2011-05-02 18:16:03 -04:00
for( int i = nDrop; i < this->n; ++i ) {
k( i - nDrop ) = k( i );
}
2011-05-02 18:16:03 -04:00
this->n -= nDrop;
setNotPacked();
2011-01-04 00:40:41 -05:00
_packReadyForMod( order, refpos );
}
2011-01-04 00:40:41 -05:00
/* - BtreeBucket --------------------------------------------------- */
2008-12-28 20:28:49 -05:00
/** @return largest key in the subtree. */
template< class V >
void BtreeBucket<V>::findLargestKey(const DiskLoc& thisLoc, DiskLoc& largestLoc, int& largestKey) {
DiskLoc loc = thisLoc;
while ( 1 ) {
2011-04-22 18:16:43 -04:00
const BtreeBucket *b = loc.btree<V>();
if ( !b->nextChild.isNull() ) {
loc = b->nextChild;
continue;
}
2008-12-28 20:28:49 -05:00
assert(b->n>0);
largestLoc = loc;
largestKey = b->n-1;
break;
2008-12-28 20:28:49 -05:00
}
}
2011-01-04 00:40:41 -05:00
/**
* NOTE Currently the Ordering implementation assumes a compound index will
* not have more keys than an unsigned variable has bits. The same
* assumption is used in the implementation below with respect to the 'mask'
* variable.
2011-04-11 18:21:48 -04:00
*
* @param l a regular bsonobj
* @param rBegin composed partly of an existing bsonobj, and the remaining keys are taken from a vector of elements that frequently changes
*
* see
* jstests/index_check6.js
* https://jira.mongodb.org/browse/SERVER-371
*/
template< class V >
int BtreeBucket<V>::customBSONCmp( const BSONObj &l, const BSONObj &rBegin, int rBeginLen, bool rSup, const vector< const BSONElement * > &rEnd, const vector< bool > &rEndInclusive, const Ordering &o, int direction ) {
2010-07-07 12:26:20 -07:00
BSONObjIterator ll( l );
BSONObjIterator rr( rBegin );
2010-07-16 01:49:15 -07:00
vector< const BSONElement * >::const_iterator rr2 = rEnd.begin();
2010-08-18 11:42:58 -07:00
vector< bool >::const_iterator inc = rEndInclusive.begin();
2010-07-07 12:26:20 -07:00
unsigned mask = 1;
for( int i = 0; i < rBeginLen; ++i, mask <<= 1 ) {
BSONElement lll = ll.next();
BSONElement rrr = rr.next();
2010-07-16 01:49:15 -07:00
++rr2;
2010-08-18 11:42:58 -07:00
++inc;
2011-01-04 00:40:41 -05:00
2010-07-07 12:26:20 -07:00
int x = lll.woCompare( rrr, false );
if ( o.descending( mask ) )
x = -x;
if ( x != 0 )
return x;
}
2010-08-18 11:42:58 -07:00
if ( rSup ) {
return -direction;
}
2010-07-07 12:26:20 -07:00
for( ; ll.more(); mask <<= 1 ) {
BSONElement lll = ll.next();
2010-07-16 01:49:15 -07:00
BSONElement rrr = **rr2;
++rr2;
2010-07-07 12:26:20 -07:00
int x = lll.woCompare( rrr, false );
if ( o.descending( mask ) )
x = -x;
if ( x != 0 )
return x;
2010-08-18 11:42:58 -07:00
if ( !*inc ) {
return -direction;
}
++inc;
2010-07-07 12:26:20 -07:00
}
return 0;
}
template< class V >
bool BtreeBucket<V>::exists(const IndexDetails& idx, const DiskLoc &thisLoc, const Key& key, const Ordering& order) const {
2011-04-11 18:21:48 -04:00
int pos;
bool found;
DiskLoc b = locate(idx, thisLoc, key, order, pos, found, minDiskLoc);
2011-04-11 18:21:48 -04:00
// skip unused keys
while ( 1 ) {
if( b.isNull() )
break;
2011-04-22 18:16:43 -04:00
const BtreeBucket *bucket = b.btree<V>();
2011-04-11 18:21:48 -04:00
const _KeyNode& kn = bucket->k(pos);
if ( kn.isUsed() )
return bucket->keyAt(pos).woEqual(key);
b = bucket->advance(b, pos, 1, "BtreeBucket<V>::exists");
}
return false;
}
template< class V >
bool BtreeBucket<V>::wouldCreateDup(
2011-01-04 00:40:41 -05:00
const IndexDetails& idx, const DiskLoc &thisLoc,
2011-04-11 18:21:48 -04:00
const Key& key, const Ordering& order,
2011-01-04 00:40:41 -05:00
const DiskLoc &self) const {
2010-02-04 14:25:49 -05:00
int pos;
bool found;
DiskLoc b = locate(idx, thisLoc, key, order, pos, found, minDiskLoc);
while ( !b.isNull() ) {
// we skip unused keys
2011-04-22 18:16:43 -04:00
const BtreeBucket *bucket = b.btree<V>();
const _KeyNode& kn = bucket->k(pos);
2010-02-04 14:25:49 -05:00
if ( kn.isUsed() ) {
if( bucket->keyAt(pos).woEqual(key) )
return kn.recordLoc != self;
break;
}
b = bucket->advance(b, pos, 1, "BtreeBucket<V>::dupCheck");
2010-02-04 14:25:49 -05:00
}
return false;
}
template< class V >
string BtreeBucket<V>::dupKeyError( const IndexDetails& idx , const Key& key ) {
stringstream ss;
ss << "E11000 duplicate key error ";
ss << "index: " << idx.indexNamespace() << " ";
2011-04-11 18:21:48 -04:00
ss << "dup key: " << key.toString();
return ss.str();
}
/**
* Find a key withing this btree bucket.
*
2011-01-04 00:40:41 -05:00
* When duplicate keys are allowed, we use the DiskLoc of the record as if it were part of the
* key. That assures that even when there are many duplicates (e.g., 1 million) for a key,
* our performance is still good.
*
* assertIfDup: if the key exists (ignoring the recordLoc), uassert
*
* pos: for existing keys k0...kn-1.
* returns # it goes BEFORE. so key[pos-1] < key < key[pos]
* returns n if it goes after the last existing key.
* note result might be an Unused location!
*/
template< class V >
2011-05-02 18:16:03 -04:00
bool BtreeBucket<V>::find(const IndexDetails& idx, const Key& key, const DiskLoc &recordLoc,
const Ordering &order, int& pos, bool assertIfDup) const {
globalIndexCounters.btree( (char*)this );
2011-01-04 00:40:41 -05:00
// binary search for this key
bool dupsChecked = false;
int l=0;
2011-05-02 18:16:03 -04:00
int h=this->n-1;
while ( l <= h ) {
int m = (l+h)/2;
2011-05-02 18:16:03 -04:00
KeyNode M = this->keyNode(m);
int x = key.woCompare(M.key, order);
2011-01-04 00:40:41 -05:00
if ( x == 0 ) {
if( assertIfDup ) {
2011-01-04 00:40:41 -05:00
if( k(m).isUnused() ) {
// ok that key is there if unused. but we need to check that there aren't other
// entries for the key then. as it is very rare that we get here, we don't put any
// coding effort in here to make this particularly fast
2011-01-04 00:40:41 -05:00
if( !dupsChecked ) {
dupsChecked = true;
2011-04-22 18:16:43 -04:00
if( idx.head.btree<V>()->exists(idx, idx.head, key, order) ) {
if( idx.head.btree<V>()->wouldCreateDup(idx, idx.head, key, order, recordLoc) )
2010-02-11 14:49:55 -05:00
uasserted( ASSERT_ID_DUPKEY , dupKeyError( idx , key ) );
else
alreadyInIndex();
}
}
}
2010-02-11 14:49:55 -05:00
else {
2011-01-04 00:40:41 -05:00
if( M.recordLoc == recordLoc )
2010-02-11 14:49:55 -05:00
alreadyInIndex();
uasserted( ASSERT_ID_DUPKEY , dupKeyError( idx , key ) );
2010-02-11 14:49:55 -05:00
}
}
2009-01-26 17:23:45 -05:00
// dup keys allowed. use recordLoc as if it is part of the key
DiskLoc unusedRL = M.recordLoc;
unusedRL.GETOFS() &= ~1; // so we can test equality without the used bit messing us up
x = recordLoc.compare(unusedRL);
2009-01-26 17:23:45 -05:00
}
if ( x < 0 ) // key < M.key
h = m-1;
else if ( x > 0 )
l = m+1;
else {
2009-01-26 17:23:45 -05:00
// found it.
pos = m;
return true;
}
}
// not found
pos = l;
2011-05-02 18:16:03 -04:00
if ( pos != this->n ) {
2011-04-11 18:21:48 -04:00
Key keyatpos = keyNode(pos).key;
wassert( key.woCompare(keyatpos, order) <= 0 );
if ( pos > 0 ) {
wassert( keyNode(pos-1).key.woCompare(key, order) <= 0 );
}
2008-12-28 20:28:49 -05:00
}
return false;
}
2008-06-06 09:43:15 -04:00
template< class V >
void BtreeBucket<V>::delBucket(const DiskLoc thisLoc, const IndexDetails& id) {
2010-02-09 15:32:49 -05:00
ClientCursor::informAboutToDeleteBucket(thisLoc); // slow...
assert( !isHead() );
2008-12-28 20:28:49 -05:00
2011-05-02 18:16:03 -04:00
DiskLoc ll = this->parent;
const BtreeBucket *p = ll.btree<V>();
2010-10-26 18:52:22 -07:00
int parentIdx = indexInParent( thisLoc );
p->childForPos( parentIdx ).writing().Null();
deallocBucket( thisLoc, id );
}
2011-01-04 00:40:41 -05:00
template< class V >
void BtreeBucket<V>::deallocBucket(const DiskLoc thisLoc, const IndexDetails &id) {
#if 0
// as a temporary defensive measure, we zap the whole bucket, AND don't truly delete
// it (meaning it is ineligible for reuse).
memset(this, 0, Size());
2008-07-16 19:18:04 -04:00
#else
// defensive:
2011-05-02 18:16:03 -04:00
this->n = -1;
this->parent.Null();
string ns = id.indexNamespace();
2010-09-30 15:42:04 -04:00
theDataFileMgr._deleteRecord(nsdetails(ns.c_str()), ns.c_str(), thisLoc.rec(), thisLoc);
2008-07-16 19:18:04 -04:00
#endif
}
/** note: may delete the entire bucket! this invalid upon return sometimes. */
template< class V >
void BtreeBucket<V>::delKeyAtPos( const DiskLoc thisLoc, IndexDetails& id, int p, const Ordering &order) {
2011-05-02 18:16:03 -04:00
assert(this->n>0);
DiskLoc left = this->childForPos(p);
2011-05-02 18:16:03 -04:00
if ( this->n == 1 ) {
if ( left.isNull() && this->nextChild.isNull() ) {
this->_delKeyAtPos(p);
2010-10-26 11:42:06 -07:00
if ( isHead() ) {
// we don't delete the top bucket ever
2011-01-04 00:40:41 -05:00
}
else {
if ( !mayBalanceWithNeighbors( thisLoc, id, order ) ) {
2010-12-08 09:50:01 -08:00
// An empty bucket is only allowed as a transient state. If
// there are no neighbors to balance with, we delete ourself.
// This condition is only expected in legacy btrees.
delBucket(thisLoc, id);
}
2010-10-26 11:42:06 -07:00
}
return;
}
2010-12-08 09:50:01 -08:00
deleteInternalKey( thisLoc, p, id, order );
2008-12-28 20:28:49 -05:00
return;
}
2010-10-26 11:42:06 -07:00
if ( left.isNull() ) {
2011-05-02 18:16:03 -04:00
this->_delKeyAtPos(p);
mayBalanceWithNeighbors( thisLoc, id, order );
2011-01-04 00:40:41 -05:00
}
else {
2010-12-08 09:50:01 -08:00
deleteInternalKey( thisLoc, p, id, order );
2010-10-26 11:42:06 -07:00
}
2008-12-28 20:28:49 -05:00
}
2011-01-04 00:40:41 -05:00
2010-12-08 09:50:01 -08:00
/**
* This function replaces the specified key (k) by either the prev or next
* key in the btree (k'). We require that k have either a left or right
* child. If k has a left child, we set k' to the prev key of k, which must
* be a leaf present in the left child. If k does not have a left child, we
* set k' to the next key of k, which must be a leaf present in the right
* child. When we replace k with k', we copy k' over k (which may cause a
* split) and then remove k' from its original location. Because k' is
* stored in a descendent of k, replacing k by k' will not modify the
* storage location of the original k', and we can easily remove k' from
* its original location.
*
* This function is only needed in cases where k has a left or right child;
* in other cases a simpler key removal implementation is possible.
*
* NOTE on legacy btree structures:
* In legacy btrees, k' can be a nonleaf. In such a case we 'delete' k by
* marking it as an unused node rather than replacing it with k'. Also, k'
* may be a leaf but marked as an unused node. In such a case we replace
* k by k', preserving the key's unused marking. This function is only
* expected to mark a key as unused when handling a legacy btree.
*/
template< class V >
void BtreeBucket<V>::deleteInternalKey( const DiskLoc thisLoc, int keypos, IndexDetails &id, const Ordering &order ) {
2011-05-02 18:16:03 -04:00
DiskLoc lchild = this->childForPos( keypos );
DiskLoc rchild = this->childForPos( keypos + 1 );
2010-12-08 09:50:01 -08:00
assert( !lchild.isNull() || !rchild.isNull() );
int advanceDirection = lchild.isNull() ? 1 : -1;
int advanceKeyOfs = keypos;
DiskLoc advanceLoc = advance( thisLoc, advanceKeyOfs, advanceDirection, __FUNCTION__ );
// advanceLoc must be a descentant of thisLoc, because thisLoc has a
// child in the proper direction and all descendants of thisLoc must be
// nonempty because they are not the root.
2011-04-22 18:16:43 -04:00
if ( !advanceLoc.btree<V>()->childForPos( advanceKeyOfs ).isNull() ||
!advanceLoc.btree<V>()->childForPos( advanceKeyOfs + 1 ).isNull() ) {
2010-12-08 09:50:01 -08:00
// only expected with legacy btrees, see note above
2011-05-02 18:16:03 -04:00
this->markUnused( keypos );
2010-12-08 09:50:01 -08:00
return;
}
2011-01-04 00:40:41 -05:00
2011-04-22 18:16:43 -04:00
KeyNode kn = advanceLoc.btree<V>()->keyNode( advanceKeyOfs );
// Because advanceLoc is a descendant of thisLoc, updating thisLoc will
// not affect packing or keys of advanceLoc and kn will be stable
// during the following setInternalKey()
2011-05-02 18:16:03 -04:00
setInternalKey( thisLoc, keypos, kn.recordLoc, kn.key, order, this->childForPos( keypos ), this->childForPos( keypos + 1 ), id );
2011-04-22 18:16:43 -04:00
advanceLoc.btreemod<V>()->delKeyAtPos( advanceLoc, id, advanceKeyOfs, order );
2010-12-08 09:50:01 -08:00
}
2008-12-28 20:28:49 -05:00
2011-05-02 18:16:03 -04:00
#define BTREE(loc) (static_cast<DiskLoc>(loc).btree<V>())
#define BTREEMOD(loc) (static_cast<DiskLoc>(loc).btreemod<V>())
template< class V >
void BtreeBucket<V>::replaceWithNextChild( const DiskLoc thisLoc, IndexDetails &id ) {
2011-05-02 18:16:03 -04:00
assert( this->n == 0 && !this->nextChild.isNull() );
if ( this->parent.isNull() ) {
2010-10-26 11:42:06 -07:00
assert( id.head == thisLoc );
2011-05-02 18:16:03 -04:00
id.head.writing() = this->nextChild;
2011-01-04 00:40:41 -05:00
}
else {
2011-05-02 18:16:03 -04:00
DiskLoc ll = this->parent;
ll.btree<V>()->childForPos( indexInParent( thisLoc ) ).writing() = this->nextChild;
2010-10-26 11:42:06 -07:00
}
2011-05-02 18:16:03 -04:00
BTREE(this->nextChild)->parent.writing() = this->parent;
(static_cast<DiskLoc>(this->nextChild).btree<V>())->parent.writing() = this->parent;
2010-10-26 11:42:06 -07:00
ClientCursor::informAboutToDeleteBucket( thisLoc );
deallocBucket( thisLoc, id );
}
2011-01-04 00:40:41 -05:00
template< class V >
bool BtreeBucket<V>::canMergeChildren( const DiskLoc &thisLoc, int leftIndex ) const {
2011-05-02 18:16:03 -04:00
assert( leftIndex >= 0 && leftIndex < this->n );
DiskLoc leftNodeLoc = this->childForPos( leftIndex );
DiskLoc rightNodeLoc = this->childForPos( leftIndex + 1 );
2010-10-26 11:42:06 -07:00
if ( leftNodeLoc.isNull() || rightNodeLoc.isNull() ) {
// TODO if this situation is possible in long term implementation, maybe we should compact somehow anyway
return false;
}
int pos = 0;
{
2011-04-22 18:16:43 -04:00
const BtreeBucket *l = leftNodeLoc.btree<V>();
const BtreeBucket *r = rightNodeLoc.btree<V>();
2011-05-02 18:16:03 -04:00
if ( ( this->headerSize() + l->packedDataSize( pos ) + r->packedDataSize( pos ) + keyNode( leftIndex ).key.dataSize() + sizeof(_KeyNode) > unsigned( V::BucketSize ) ) ) {
2010-10-26 11:42:06 -07:00
return false;
}
}
return true;
}
2011-01-04 00:40:41 -05:00
/**
* This implementation must respect the meaning and value of lowWaterMark.
* Also see comments in splitPos().
*/
template< class V >
int BtreeBucket<V>::rebalancedSeparatorPos( const DiskLoc &thisLoc, int leftIndex ) const {
int split = -1;
int rightSize = 0;
2011-05-02 18:16:03 -04:00
const BtreeBucket *l = BTREE(this->childForPos( leftIndex ));
const BtreeBucket *r = BTREE(this->childForPos( leftIndex + 1 ));
int KNS = sizeof( _KeyNode );
2011-04-11 18:21:48 -04:00
int rightSizeLimit = ( l->topSize + l->n * KNS + keyNode( leftIndex ).key.dataSize() + KNS + r->topSize + r->n * KNS ) / 2;
// This constraint should be ensured by only calling this function
// if we go below the low water mark.
assert( rightSizeLimit < BtreeBucket<V>::bodySize() );
for( int i = r->n - 1; i > -1; --i ) {
2011-04-11 18:21:48 -04:00
rightSize += r->keyNode( i ).key.dataSize() + KNS;
if ( rightSize > rightSizeLimit ) {
split = l->n + 1 + i;
break;
}
}
if ( split == -1 ) {
2011-04-11 18:21:48 -04:00
rightSize += keyNode( leftIndex ).key.dataSize() + KNS;
if ( rightSize > rightSizeLimit ) {
split = l->n;
}
}
if ( split == -1 ) {
for( int i = l->n - 1; i > -1; --i ) {
2011-04-11 18:21:48 -04:00
rightSize += l->keyNode( i ).key.dataSize() + KNS;
if ( rightSize > rightSizeLimit ) {
split = i;
break;
}
}
}
// safeguards - we must not create an empty bucket
if ( split < 1 ) {
split = 1;
2011-01-04 00:40:41 -05:00
}
else if ( split > l->n + 1 + r->n - 2 ) {
split = l->n + 1 + r->n - 2;
}
2011-01-04 00:40:41 -05:00
return split;
}
2011-01-04 00:40:41 -05:00
template< class V >
void BtreeBucket<V>::doMergeChildren( const DiskLoc thisLoc, int leftIndex, IndexDetails &id, const Ordering &order ) {
2011-05-02 18:16:03 -04:00
DiskLoc leftNodeLoc = this->childForPos( leftIndex );
DiskLoc rightNodeLoc = this->childForPos( leftIndex + 1 );
2011-04-22 18:16:43 -04:00
BtreeBucket *l = leftNodeLoc.btreemod<V>();
BtreeBucket *r = rightNodeLoc.btreemod<V>();
int pos = 0;
l->_packReadyForMod( order, pos );
r->_packReadyForMod( order, pos ); // pack r in case there are droppable keys
2011-01-04 00:40:41 -05:00
// We know the additional keys below will fit in l because canMergeChildren()
// must be true.
2010-10-26 11:42:06 -07:00
int oldLNum = l->n;
{
KeyNode kn = keyNode( leftIndex );
l->pushBack( kn.recordLoc, kn.key, order, l->nextChild ); // left child's right child becomes old parent key's left child
}
2010-10-26 11:42:06 -07:00
for( int i = 0; i < r->n; ++i ) {
KeyNode kn = r->keyNode( i );
l->pushBack( kn.recordLoc, kn.key, order, kn.prevChildBucket );
}
l->nextChild = r->nextChild;
2010-10-26 11:42:06 -07:00
l->fixParentPtrs( leftNodeLoc, oldLNum );
r->delBucket( rightNodeLoc, id );
2011-05-02 18:16:03 -04:00
this->childForPos( leftIndex + 1 ) = leftNodeLoc;
this->childForPos( leftIndex ) = DiskLoc();
this->_delKeyAtPos( leftIndex, true );
if ( this->n == 0 ) {
2010-10-26 11:42:06 -07:00
// will trash this and thisLoc
2010-12-28 11:04:42 -08:00
// TODO To ensure all leaves are of equal height, we should ensure
// this is only called on the root.
2010-10-26 11:42:06 -07:00
replaceWithNextChild( thisLoc, id );
2011-01-04 00:40:41 -05:00
}
else {
2010-10-26 11:42:06 -07:00
// balance recursively - maybe we should do this even when n == 0?
mayBalanceWithNeighbors( thisLoc, id, order );
2010-10-26 11:42:06 -07:00
}
}
2011-01-04 00:40:41 -05:00
template< class V >
int BtreeBucket<V>::indexInParent( const DiskLoc &thisLoc ) const {
2011-05-02 18:16:03 -04:00
assert( !this->parent.isNull() );
const BtreeBucket *p = BTREE(this->parent);
2010-10-26 11:42:06 -07:00
if ( p->nextChild == thisLoc ) {
return p->n;
2011-01-04 00:40:41 -05:00
}
else {
2010-10-26 11:42:06 -07:00
for( int i = 0; i < p->n; ++i ) {
if ( p->k( i ).prevChildBucket == thisLoc ) {
return i;
}
}
}
out() << "ERROR: can't find ref to child bucket.\n";
out() << "child: " << thisLoc << "\n";
dump();
2011-05-02 18:16:03 -04:00
out() << "Parent: " << this->parent << "\n";
2010-10-26 11:42:06 -07:00
p->dump();
assert(false);
return -1; // just to compile
}
2011-01-04 00:40:41 -05:00
template< class V >
bool BtreeBucket<V>::tryBalanceChildren( const DiskLoc thisLoc, int leftIndex, IndexDetails &id, const Ordering &order ) const {
// If we can merge, then we must merge rather than balance to preserve
// bucket utilization constraints.
if ( canMergeChildren( thisLoc, leftIndex ) ) {
return false;
}
2011-04-22 18:16:43 -04:00
thisLoc.btreemod<V>()->doBalanceChildren( thisLoc, leftIndex, id, order );
return true;
}
2011-01-04 00:40:41 -05:00
template< class V >
void BtreeBucket<V>::doBalanceLeftToRight( const DiskLoc thisLoc, int leftIndex, int split,
2011-01-04 00:40:41 -05:00
BtreeBucket *l, const DiskLoc lchild,
BtreeBucket *r, const DiskLoc rchild,
IndexDetails &id, const Ordering &order ) {
// TODO maybe do some audits the same way pushBack() does?
// As a precondition, rchild + the old separator are <= half a body size,
// and lchild is at most completely full. Based on the value of split,
// rchild will get <= half of the total bytes which is at most 75%
// of a full body. So rchild will have room for the following keys:
int rAdd = l->n - split;
2010-11-22 11:11:39 -08:00
r->reserveKeysFront( rAdd );
for( int i = split + 1, j = 0; i < l->n; ++i, ++j ) {
KeyNode kn = l->keyNode( i );
r->setKey( j, kn.recordLoc, kn.key, kn.prevChildBucket );
}
{
KeyNode kn = keyNode( leftIndex );
r->setKey( rAdd - 1, kn.recordLoc, kn.key, l->nextChild ); // left child's right child becomes old parent key's left child
}
r->fixParentPtrs( rchild, 0, rAdd - 1 );
{
KeyNode kn = l->keyNode( split );
l->nextChild = kn.prevChildBucket;
// Because lchild is a descendant of thisLoc, updating thisLoc will
// not not affect packing or keys of lchild and kn will be stable
// during the following setInternalKey()
2011-01-04 00:40:41 -05:00
setInternalKey( thisLoc, leftIndex, kn.recordLoc, kn.key, order, lchild, rchild, id );
}
int zeropos = 0;
// lchild and rchild cannot be merged, so there must be >0 (actually more)
// keys to the left of split.
l->truncateTo( split, order, zeropos );
}
template< class V >
void BtreeBucket<V>::doBalanceRightToLeft( const DiskLoc thisLoc, int leftIndex, int split,
2011-01-04 00:40:41 -05:00
BtreeBucket *l, const DiskLoc lchild,
BtreeBucket *r, const DiskLoc rchild,
IndexDetails &id, const Ordering &order ) {
// As a precondition, lchild + the old separator are <= half a body size,
// and rchild is at most completely full. Based on the value of split,
// lchild will get less than half of the total bytes which is at most 75%
// of a full body. So lchild will have room for the following keys:
int lN = l->n;
{
KeyNode kn = keyNode( leftIndex );
l->pushBack( kn.recordLoc, kn.key, order, l->nextChild ); // left child's right child becomes old parent key's left child
}
for( int i = 0; i < split - lN - 1; ++i ) {
KeyNode kn = r->keyNode( i );
l->pushBack( kn.recordLoc, kn.key, order, kn.prevChildBucket );
}
{
KeyNode kn = r->keyNode( split - lN - 1 );
l->nextChild = kn.prevChildBucket;
// Child lN was lchild's old nextChild, and don't need to fix that one.
l->fixParentPtrs( lchild, lN + 1, l->n );
// Because rchild is a descendant of thisLoc, updating thisLoc will
// not affect packing or keys of rchild and kn will be stable
// during the following setInternalKey()
setInternalKey( thisLoc, leftIndex, kn.recordLoc, kn.key, order, lchild, rchild, id );
}
int zeropos = 0;
// lchild and rchild cannot be merged, so there must be >0 (actually more)
// keys to the right of split.
r->dropFront( split - lN, order, zeropos );
}
2011-01-04 00:40:41 -05:00
template< class V >
void BtreeBucket<V>::doBalanceChildren( const DiskLoc thisLoc, int leftIndex, IndexDetails &id, const Ordering &order ) {
2011-05-02 18:16:03 -04:00
DiskLoc lchild = this->childForPos( leftIndex );
DiskLoc rchild = this->childForPos( leftIndex + 1 );
int zeropos = 0;
2011-04-22 18:16:43 -04:00
BtreeBucket *l = lchild.btreemod<V>();
l->_packReadyForMod( order, zeropos );
2011-04-22 18:16:43 -04:00
BtreeBucket *r = rchild.btreemod<V>();
r->_packReadyForMod( order, zeropos );
int split = rebalancedSeparatorPos( thisLoc, leftIndex );
2011-01-04 00:40:41 -05:00
// By definition, if we are below the low water mark and cannot merge
// then we must actively balance.
assert( split != l->n );
if ( split < l->n ) {
doBalanceLeftToRight( thisLoc, leftIndex, split, l, lchild, r, rchild, id, order );
2011-01-04 00:40:41 -05:00
}
else {
doBalanceRightToLeft( thisLoc, leftIndex, split, l, lchild, r, rchild, id, order );
}
}
2011-01-04 00:40:41 -05:00
template< class V >
bool BtreeBucket<V>::mayBalanceWithNeighbors( const DiskLoc thisLoc, IndexDetails &id, const Ordering &order ) const {
2011-05-02 18:16:03 -04:00
if ( this->parent.isNull() ) { // we are root, there are no neighbors
return false;
2010-10-26 11:42:06 -07:00
}
2011-01-04 00:40:41 -05:00
2011-05-02 18:16:03 -04:00
if ( this->packedDataSize( 0 ) >= this->lowWaterMark() ) {
return false;
}
2011-01-04 00:40:41 -05:00
2011-05-02 18:16:03 -04:00
const BtreeBucket *p = BTREE(this->parent);
2010-10-26 11:42:06 -07:00
int parentIdx = indexInParent( thisLoc );
2011-01-04 00:40:41 -05:00
// TODO will missing neighbor case be possible long term? Should we try to merge/balance somehow in that case if so?
bool mayBalanceRight = ( ( parentIdx < p->n ) && !p->childForPos( parentIdx + 1 ).isNull() );
bool mayBalanceLeft = ( ( parentIdx > 0 ) && !p->childForPos( parentIdx - 1 ).isNull() );
2011-01-04 00:40:41 -05:00
// Balance if possible on one side - we merge only if absolutely necessary
// to preserve btree bucket utilization constraints since that's a more
// heavy duty operation (especially if we must re-split later).
2011-01-04 00:40:41 -05:00
if ( mayBalanceRight &&
2011-05-02 18:16:03 -04:00
p->tryBalanceChildren( this->parent, parentIdx, id, order ) ) {
return true;
}
2011-01-04 00:40:41 -05:00
if ( mayBalanceLeft &&
2011-05-02 18:16:03 -04:00
p->tryBalanceChildren( this->parent, parentIdx - 1, id, order ) ) {
return true;
2010-10-26 11:42:06 -07:00
}
2011-05-02 18:16:03 -04:00
BtreeBucket *pm = BTREEMOD(this->parent);
if ( mayBalanceRight ) {
2011-05-02 18:16:03 -04:00
pm->doMergeChildren( this->parent, parentIdx, id, order );
return true;
2011-01-04 00:40:41 -05:00
}
else if ( mayBalanceLeft ) {
2011-05-02 18:16:03 -04:00
pm->doMergeChildren( this->parent, parentIdx - 1, id, order );
return true;
2010-10-26 11:42:06 -07:00
}
2011-01-04 00:40:41 -05:00
return false;
2010-10-26 11:42:06 -07:00
}
2011-01-04 00:40:41 -05:00
/** remove a key from the index */
template< class V >
bool BtreeBucket<V>::unindex(const DiskLoc thisLoc, IndexDetails& id, const BSONObj& key, const DiskLoc recordLoc ) const {
int pos;
bool found;
2010-04-20 19:42:53 -04:00
DiskLoc loc = locate(id, thisLoc, key, Ordering::make(id.keyPattern()), pos, found, recordLoc, 1);
if ( found ) {
2011-03-08 14:12:18 -05:00
if ( key.objsize() > KeyMax ) {
OCCASIONALLY problem() << "unindex: key too large to index but was found for " << id.indexNamespace() << " reIndex suggested" << endl;
}
2011-04-22 18:16:43 -04:00
loc.btreemod<V>()->delKeyAtPos(loc, id, pos, Ordering::make(id.keyPattern()));
2011-03-08 14:12:18 -05:00
return true;
}
2008-12-28 20:28:49 -05:00
return false;
}
template< class V >
BtreeBucket<V> * BtreeBucket<V>::allocTemp() {
2011-05-02 18:16:03 -04:00
BtreeBucket *b = (BtreeBucket*) malloc(V::BucketSize);
b->init();
return b;
2008-12-28 20:28:49 -05:00
}
2008-06-06 09:43:15 -04:00
template< class V >
inline void BtreeBucket<V>::fix(const DiskLoc thisLoc, const DiskLoc child) {
if ( !child.isNull() ) {
if ( insert_debug )
out() << " " << child.toString() << ".parent=" << thisLoc.toString() << endl;
2011-04-22 18:16:43 -04:00
child.btree<V>()->parent.writing() = thisLoc;
}
2008-12-28 20:28:49 -05:00
}
2008-06-06 09:43:15 -04:00
/**
* This can cause a lot of additional page writes when we assign buckets to
* different parents. Maybe get rid of parent ptrs?
*/
template< class V >
void BtreeBucket<V>::fixParentPtrs(const DiskLoc thisLoc, int firstIndex, int lastIndex) const {
VERIFYTHISLOC
if ( lastIndex == -1 ) {
2011-05-02 18:16:03 -04:00
lastIndex = this->n;
}
for ( int i = firstIndex; i <= lastIndex; i++ ) {
2011-05-02 18:16:03 -04:00
fix(thisLoc, this->childForPos(i));
}
}
2008-06-06 09:43:15 -04:00
template< class V >
void BtreeBucket<V>::setInternalKey( const DiskLoc thisLoc, int keypos,
2011-04-11 18:21:48 -04:00
const DiskLoc recordLoc, const Key &key, const Ordering &order,
2011-01-04 00:40:41 -05:00
const DiskLoc lchild, const DiskLoc rchild, IndexDetails &idx ) {
2011-05-02 18:16:03 -04:00
this->childForPos( keypos ).Null();
2010-12-08 09:50:01 -08:00
// This may leave the bucket empty (n == 0) which is ok only as a
// transient state. In the instant case, the implementation of
// insertHere behaves correctly when n == 0 and as a side effect
// increments n.
2011-05-02 18:16:03 -04:00
this->_delKeyAtPos( keypos, true );
2011-01-04 00:40:41 -05:00
2010-12-08 09:50:01 -08:00
// Ensure we do not orphan neighbor's old child.
2011-05-02 18:16:03 -04:00
assert( this->childForPos( keypos ) == rchild );
2011-01-04 00:40:41 -05:00
2010-12-08 09:50:01 -08:00
// Just set temporarily - required to pass validation in insertHere()
2011-05-02 18:16:03 -04:00
this->childForPos( keypos ) = lchild;
2011-01-04 00:40:41 -05:00
insertHere( thisLoc, keypos, recordLoc, key, order, lchild, rchild, idx );
}
2011-01-04 00:40:41 -05:00
/**
* insert a key in this bucket, splitting if necessary.
2010-12-08 09:50:01 -08:00
* @keypos - where to insert the key in range 0..n. 0=make leftmost, n=make rightmost.
* NOTE this function may free some data, and as a result the value passed for keypos may
* be invalid after calling insertHere()
*
* Some of the write intent signaling below relies on the implementation of
* the optimized write intent code in basicInsert().
2011-01-04 00:40:41 -05:00
*/
template< class V >
void BtreeBucket<V>::insertHere( const DiskLoc thisLoc, int keypos,
2011-04-11 18:21:48 -04:00
const DiskLoc recordLoc, const Key& key, const Ordering& order,
2011-01-04 00:40:41 -05:00
const DiskLoc lchild, const DiskLoc rchild, IndexDetails& idx) const {
if ( insert_debug )
out() << " " << thisLoc.toString() << ".insertHere " << key.toString() << '/' << recordLoc.toString() << ' '
2011-01-04 00:40:41 -05:00
<< lchild.toString() << ' ' << rchild.toString() << " keypos:" << keypos << endl;
2011-05-06 14:02:08 -04:00
if ( !this->basicInsert(thisLoc, keypos, recordLoc, key, order) ) {
// If basicInsert() fails, the bucket will be packed as required by split().
2011-04-22 18:16:43 -04:00
thisLoc.btreemod<V>()->split(thisLoc, keypos, recordLoc, key, order, lchild, rchild, idx);
return;
}
{
const _KeyNode *_kn = &k(keypos);
_KeyNode *kn = (_KeyNode *) getDur().alreadyDeclared((_KeyNode*) _kn); // already declared intent in basicInsert()
2011-05-02 18:16:03 -04:00
if ( keypos+1 == this->n ) { // last key
if ( this->nextChild != lchild ) {
out() << "ERROR nextChild != lchild" << endl;
out() << " thisLoc: " << thisLoc.toString() << ' ' << idx.indexNamespace() << endl;
2011-05-02 18:16:03 -04:00
out() << " keyPos: " << keypos << " n:" << this->n << endl;
out() << " nextChild: " << this->nextChild.toString() << " lchild: " << lchild.toString() << endl;
out() << " recordLoc: " << recordLoc.toString() << " rchild: " << rchild.toString() << endl;
out() << " key: " << key.toString() << endl;
dump();
assert(false);
}
2011-05-02 18:16:03 -04:00
kn->prevChildBucket = this->nextChild;
assert( kn->prevChildBucket == lchild );
2011-05-02 18:16:03 -04:00
this->nextChild.writing() = rchild;
if ( !rchild.isNull() )
2011-05-02 18:16:03 -04:00
BTREE(rchild)->parent.writing() = thisLoc;
2008-12-28 20:28:49 -05:00
}
else {
kn->prevChildBucket = lchild;
if ( k(keypos+1).prevChildBucket != lchild ) {
out() << "ERROR k(keypos+1).prevChildBucket != lchild" << endl;
out() << " thisLoc: " << thisLoc.toString() << ' ' << idx.indexNamespace() << endl;
2011-05-02 18:16:03 -04:00
out() << " keyPos: " << keypos << " n:" << this->n << endl;
out() << " k(keypos+1).pcb: " << k(keypos+1).prevChildBucket.toString() << " lchild: " << lchild.toString() << endl;
out() << " recordLoc: " << recordLoc.toString() << " rchild: " << rchild.toString() << endl;
out() << " key: " << key.toString() << endl;
dump();
assert(false);
}
const DiskLoc *pc = &k(keypos+1).prevChildBucket;
*getDur().alreadyDeclared((DiskLoc*) pc) = rchild; // declared in basicInsert()
if ( !rchild.isNull() )
2011-04-22 18:16:43 -04:00
rchild.btree<V>()->parent.writing() = thisLoc;
2008-12-28 20:28:49 -05:00
}
return;
2008-12-28 20:28:49 -05:00
}
}
2008-12-28 20:28:49 -05:00
template< class V >
void BtreeBucket<V>::split(const DiskLoc thisLoc, int keypos, const DiskLoc recordLoc, const Key& key, const Ordering& order, const DiskLoc lchild, const DiskLoc rchild, IndexDetails& idx) {
2011-05-02 18:16:03 -04:00
this->assertWritable();
2010-12-24 12:20:33 -05:00
if ( split_debug )
out() << " " << thisLoc.toString() << ".split" << endl;
2011-05-02 18:16:03 -04:00
int split = this->splitPos( keypos );
2009-09-27 14:46:51 -04:00
DiskLoc rLoc = addBucket(idx);
2011-04-22 18:16:43 -04:00
BtreeBucket *r = rLoc.btreemod<V>();
if ( split_debug )
2011-05-02 18:16:03 -04:00
out() << " split:" << split << ' ' << keyNode(split).key.toString() << " this->n:" << this->n << endl;
for ( int i = split+1; i < this->n; i++ ) {
KeyNode kn = keyNode(i);
r->pushBack(kn.recordLoc, kn.key, order, kn.prevChildBucket);
}
2011-05-02 18:16:03 -04:00
r->nextChild = this->nextChild;
r->assertValid( order );
if ( split_debug )
2011-05-02 18:16:03 -04:00
out() << " this->new rLoc:" << rLoc.toString() << endl;
r = 0;
2011-04-22 18:16:43 -04:00
rLoc.btree<V>()->fixParentPtrs(rLoc);
{
KeyNode splitkey = keyNode(split);
2011-05-02 18:16:03 -04:00
this->nextChild = splitkey.prevChildBucket; // splitkey key gets promoted, its children will be thisLoc (l) and rLoc (r)
if ( split_debug ) {
out() << " splitkey key:" << splitkey.key.toString() << endl;
}
2011-01-04 00:40:41 -05:00
// Because thisLoc is a descendant of parent, updating parent will
2011-05-02 18:16:03 -04:00
// not affect packing or keys of thisLoc and splitkey will be stable
// during the following:
2011-05-02 18:16:03 -04:00
// promote splitkey to a parent this->node
if ( this->parent.isNull() ) {
// make a this->new this->parent if we were the root
2009-09-27 14:46:51 -04:00
DiskLoc L = addBucket(idx);
2011-04-22 18:16:43 -04:00
BtreeBucket *p = L.btreemod<V>();
p->pushBack(splitkey.recordLoc, splitkey.key, order, thisLoc);
p->nextChild = rLoc;
p->assertValid( order );
2011-05-02 18:16:03 -04:00
this->parent = idx.head.writing() = L;
if ( split_debug )
2011-05-02 18:16:03 -04:00
out() << " we were root, making this->new root:" << hex << this->parent.getOfs() << dec << endl;
rLoc.btree<V>()->parent.writing() = this->parent;
}
else {
// set this before calling _insert - if it splits it will do fixParent() logic and change the value.
2011-05-02 18:16:03 -04:00
rLoc.btree<V>()->parent.writing() = this->parent;
if ( split_debug )
out() << " promoting splitkey key " << splitkey.key.toString() << endl;
2011-05-02 18:16:03 -04:00
BTREE(this->parent)->_insert(this->parent, splitkey.recordLoc, splitkey.key, order, /*dupsallowed*/true, thisLoc, rLoc, idx);
}
}
2008-06-06 09:43:15 -04:00
2010-06-28 22:16:07 -07:00
int newpos = keypos;
// note this may trash splitkey.key. thus we had to promote it before finishing up here.
2011-05-02 18:16:03 -04:00
this->truncateTo(split, order, newpos);
2008-12-28 20:28:49 -05:00
2011-05-02 18:16:03 -04:00
// add our this->new key, there is room this->now
{
if ( keypos <= split ) {
if ( split_debug )
2011-05-02 18:16:03 -04:00
out() << " keypos<split, insertHere() the this->new key" << endl;
2010-06-28 22:16:07 -07:00
insertHere(thisLoc, newpos, recordLoc, key, order, lchild, rchild, idx);
2011-01-04 00:40:41 -05:00
}
else {
int kp = keypos-split-1;
assert(kp>=0);
2011-05-02 18:16:03 -04:00
BTREE(rLoc)->insertHere(rLoc, kp, recordLoc, key, order, lchild, rchild, idx);
}
2008-12-28 20:28:49 -05:00
}
if ( split_debug )
out() << " split end " << hex << thisLoc.getOfs() << dec << endl;
2008-12-28 20:28:49 -05:00
}
2008-06-06 09:43:15 -04:00
2011-05-02 18:16:03 -04:00
/** start a this->new index off, empty */
template< class V >
DiskLoc BtreeBucket<V>::addBucket(const IndexDetails& id) {
2010-10-19 11:21:18 -07:00
string ns = id.indexNamespace();
2011-05-02 18:16:03 -04:00
DiskLoc loc = theDataFileMgr.insert(ns.c_str(), 0, V::BucketSize, true);
BtreeBucket *b = BTREEMOD(loc);
2009-09-27 14:46:51 -04:00
b->init();
return loc;
2008-12-28 20:28:49 -05:00
}
2011-04-21 14:53:10 -04:00
void renameIndexNamespace(const char *oldNs, const char *newNs) {
2010-09-30 15:42:04 -04:00
renameNamespace( oldNs, newNs );
2009-09-24 12:11:55 -04:00
}
template< class V >
const DiskLoc BtreeBucket<V>::getHead(const DiskLoc& thisLoc) const {
DiskLoc p = thisLoc;
2011-05-02 18:16:03 -04:00
while ( !BTREE(p)->isHead() )
p = BTREE(p)->parent;
return p;
2008-12-28 20:28:49 -05:00
}
template< class V >
DiskLoc BtreeBucket<V>::advance(const DiskLoc& thisLoc, int& keyOfs, int direction, const char *caller) const {
2011-05-02 18:16:03 -04:00
if ( keyOfs < 0 || keyOfs >= this->n ) {
out() << "ASSERT failure BtreeBucket<V>::advance, caller: " << caller << endl;
out() << " thisLoc: " << thisLoc.toString() << endl;
2011-05-02 18:16:03 -04:00
out() << " keyOfs: " << keyOfs << " this->n:" << this->n << " direction: " << direction << endl;
out() << bucketSummary() << endl;
assert(false);
}
int adj = direction < 0 ? 1 : 0;
int ko = keyOfs + direction;
2011-05-02 18:16:03 -04:00
DiskLoc nextDown = this->childForPos(ko+adj);
if ( !nextDown.isNull() ) {
while ( 1 ) {
2011-05-02 18:16:03 -04:00
keyOfs = direction>0 ? 0 : BTREE(nextDown)->n - 1;
DiskLoc loc = BTREE(nextDown)->childForPos(keyOfs + adj);
if ( loc.isNull() )
break;
nextDown = loc;
2008-12-28 20:28:49 -05:00
}
return nextDown;
2008-12-28 20:28:49 -05:00
}
2011-05-02 18:16:03 -04:00
if ( ko < this->n && ko >= 0 ) {
keyOfs = ko;
return thisLoc;
}
2008-12-28 20:28:49 -05:00
// end of bucket. traverse back up.
DiskLoc childLoc = thisLoc;
2011-05-02 18:16:03 -04:00
DiskLoc ancestor = this->parent;
while ( 1 ) {
if ( ancestor.isNull() )
break;
2011-05-02 18:16:03 -04:00
const BtreeBucket *an = BTREE(ancestor);
for ( int i = 0; i < an->n; i++ ) {
if ( an->childForPos(i+adj) == childLoc ) {
keyOfs = i;
return ancestor;
}
}
assert( direction<0 || an->nextChild == childLoc );
2011-05-02 18:16:03 -04:00
// this->parent exhausted also, keep going up
childLoc = ancestor;
ancestor = an->parent;
}
2008-12-28 20:28:49 -05:00
return DiskLoc();
2008-12-28 20:28:49 -05:00
}
template< class V >
DiskLoc BtreeBucket<V>::locate(const IndexDetails& idx, const DiskLoc& thisLoc, const BSONObj& key, const Ordering &order, int& pos, bool& found, const DiskLoc &recordLoc, int direction) const {
2011-05-03 16:31:21 -04:00
KeyOwned k(key);
return locate(idx, thisLoc, k, order, pos, found, recordLoc, direction);
2011-04-11 18:21:48 -04:00
}
template< class V >
DiskLoc BtreeBucket<V>::locate(const IndexDetails& idx, const DiskLoc& thisLoc, const Key& key, const Ordering &order, int& pos, bool& found, const DiskLoc &recordLoc, int direction) const {
int p;
found = find(idx, key, recordLoc, order, p, /*assertIfDup*/ false);
if ( found ) {
pos = p;
return thisLoc;
}
2008-06-06 09:43:15 -04:00
2011-05-02 18:16:03 -04:00
DiskLoc child = this->childForPos(p);
if ( !child.isNull() ) {
2011-05-02 18:16:03 -04:00
DiskLoc l = BTREE(child)->locate(idx, child, key, order, pos, found, recordLoc, direction);
if ( !l.isNull() )
return l;
}
2008-12-28 20:28:49 -05:00
pos = p;
if ( direction < 0 )
return --pos == -1 ? DiskLoc() /*theend*/ : thisLoc;
else
2011-05-02 18:16:03 -04:00
return pos == this->n ? DiskLoc() /*theend*/ : thisLoc;
2008-12-28 20:28:49 -05:00
}
2011-01-04 00:40:41 -05:00
template< class V >
bool BtreeBucket<V>::customFind( int l, int h, const BSONObj &keyBegin, int keyBeginLen, bool afterKey, const vector< const BSONElement * > &keyEnd, const vector< bool > &keyEndInclusive, const Ordering &order, int direction, DiskLoc &thisLoc, int &keyOfs, pair< DiskLoc, int > &bestParent ) const {
2010-07-07 12:26:20 -07:00
while( 1 ) {
if ( l + 1 == h ) {
2010-07-07 13:08:53 -07:00
keyOfs = ( direction > 0 ) ? h : l;
2011-05-02 18:16:03 -04:00
DiskLoc next = BTREE(thisLoc)->k( h ).prevChildBucket;
2010-07-07 12:26:20 -07:00
if ( !next.isNull() ) {
bestParent = make_pair( thisLoc, keyOfs );
thisLoc = next;
return true;
2011-01-04 00:40:41 -05:00
}
else {
2010-07-07 12:26:20 -07:00
return false;
}
}
int m = l + ( h - l ) / 2;
2011-05-02 18:16:03 -04:00
int cmp = customBSONCmp( BTREE(thisLoc)->keyNode( m ).key.toBson(), keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction );
2010-07-07 12:26:20 -07:00
if ( cmp < 0 ) {
l = m;
2011-01-04 00:40:41 -05:00
}
else if ( cmp > 0 ) {
2010-07-07 12:26:20 -07:00
h = m;
2011-01-04 00:40:41 -05:00
}
else {
2010-07-07 13:08:53 -07:00
if ( direction < 0 ) {
l = m;
2011-01-04 00:40:41 -05:00
}
else {
2010-07-07 13:08:53 -07:00
h = m;
}
2010-07-07 12:26:20 -07:00
}
2011-01-04 00:40:41 -05:00
}
2010-07-07 12:26:20 -07:00
}
2011-01-04 00:40:41 -05:00
/**
* find smallest/biggest value greater-equal/less-equal than specified
* starting thisLoc + keyOfs will be strictly less than/strictly greater than keyBegin/keyBeginLen/keyEnd
* All the direction checks below allowed me to refactor the code, but possibly separate forward and reverse implementations would be more efficient
*/
template< class V >
void BtreeBucket<V>::advanceTo(DiskLoc &thisLoc, int &keyOfs, const BSONObj &keyBegin, int keyBeginLen, bool afterKey, const vector< const BSONElement * > &keyEnd, const vector< bool > &keyEndInclusive, const Ordering &order, int direction ) const {
2010-07-07 13:08:53 -07:00
int l,h;
bool dontGoUp;
if ( direction > 0 ) {
2010-07-07 13:41:35 -07:00
l = keyOfs;
2011-05-02 18:16:03 -04:00
h = this->n - 1;
2011-04-11 18:21:48 -04:00
dontGoUp = ( customBSONCmp( keyNode( h ).key.toBson(), keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction ) >= 0 );
2011-01-04 00:40:41 -05:00
}
else {
2010-07-07 13:41:35 -07:00
l = 0;
h = keyOfs;
2011-04-11 18:21:48 -04:00
dontGoUp = ( customBSONCmp( keyNode( l ).key.toBson(), keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction ) <= 0 );
2010-07-07 13:08:53 -07:00
}
2010-07-07 12:26:20 -07:00
pair< DiskLoc, int > bestParent;
2010-07-07 13:41:35 -07:00
if ( dontGoUp ) {
2010-07-07 12:26:20 -07:00
// this comparison result assures h > l
2010-08-18 11:42:58 -07:00
if ( !customFind( l, h, keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction, thisLoc, keyOfs, bestParent ) ) {
2010-07-07 12:26:20 -07:00
return;
}
2011-01-04 00:40:41 -05:00
}
else {
2011-05-02 18:16:03 -04:00
// go up this->parents until rightmost/leftmost node is >=/<= target or at top
while( !BTREE(thisLoc)->parent.isNull() ) {
thisLoc = BTREE(thisLoc)->parent;
2010-07-07 13:41:35 -07:00
if ( direction > 0 ) {
2011-05-02 18:16:03 -04:00
if ( customBSONCmp( BTREE(thisLoc)->keyNode( BTREE(thisLoc)->n - 1 ).key.toBson(), keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction ) >= 0 ) {
2010-07-07 13:41:35 -07:00
break;
}
2011-01-04 00:40:41 -05:00
}
else {
2011-05-02 18:16:03 -04:00
if ( customBSONCmp( BTREE(thisLoc)->keyNode( 0 ).key.toBson(), keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction ) <= 0 ) {
2010-07-07 13:41:35 -07:00
break;
2011-01-04 00:40:41 -05:00
}
2010-07-07 12:26:20 -07:00
}
}
}
2010-08-18 11:42:58 -07:00
customLocate( thisLoc, keyOfs, keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction, bestParent );
}
2011-01-04 00:40:41 -05:00
template< class V >
void BtreeBucket<V>::customLocate(DiskLoc &thisLoc, int &keyOfs, const BSONObj &keyBegin, int keyBeginLen, bool afterKey, const vector< const BSONElement * > &keyEnd, const vector< bool > &keyEndInclusive, const Ordering &order, int direction, pair< DiskLoc, int > &bestParent ) const {
2011-05-02 18:16:03 -04:00
if ( BTREE(thisLoc)->n == 0 ) {
2010-08-18 11:42:58 -07:00
thisLoc = DiskLoc();
return;
}
2010-07-07 13:41:35 -07:00
// go down until find smallest/biggest >=/<= target
2010-07-07 12:26:20 -07:00
while( 1 ) {
2010-08-18 11:42:58 -07:00
int l = 0;
2011-05-02 18:16:03 -04:00
int h = BTREE(thisLoc)->n - 1;
2010-07-07 13:41:35 -07:00
// leftmost/rightmost key may possibly be >=/<= search key
bool firstCheck;
if ( direction > 0 ) {
2011-05-02 18:16:03 -04:00
firstCheck = ( customBSONCmp( BTREE(thisLoc)->keyNode( 0 ).key.toBson(), keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction ) >= 0 );
2011-01-04 00:40:41 -05:00
}
else {
2011-05-02 18:16:03 -04:00
firstCheck = ( customBSONCmp( BTREE(thisLoc)->keyNode( h ).key.toBson(), keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction ) <= 0 );
2010-07-07 13:41:35 -07:00
}
if ( firstCheck ) {
DiskLoc next;
if ( direction > 0 ) {
2011-05-02 18:16:03 -04:00
next = BTREE(thisLoc)->k( 0 ).prevChildBucket;
2010-07-07 13:41:35 -07:00
keyOfs = 0;
2011-01-04 00:40:41 -05:00
}
else {
2011-05-02 18:16:03 -04:00
next = BTREE(thisLoc)->nextChild;
2010-07-07 13:41:35 -07:00
keyOfs = h;
}
2010-07-07 12:26:20 -07:00
if ( !next.isNull() ) {
2010-11-22 12:37:05 -08:00
bestParent = pair< DiskLoc, int >( thisLoc, keyOfs );
2010-07-07 12:26:20 -07:00
thisLoc = next;
continue;
2011-01-04 00:40:41 -05:00
}
else {
2010-07-07 12:26:20 -07:00
return;
}
}
2010-07-07 13:41:35 -07:00
bool secondCheck;
if ( direction > 0 ) {
2011-05-02 18:16:03 -04:00
secondCheck = ( customBSONCmp( BTREE(thisLoc)->keyNode( h ).key.toBson(), keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction ) < 0 );
2011-01-04 00:40:41 -05:00
}
else {
2011-05-02 18:16:03 -04:00
secondCheck = ( customBSONCmp( BTREE(thisLoc)->keyNode( 0 ).key.toBson(), keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction ) > 0 );
2010-07-07 13:41:35 -07:00
}
if ( secondCheck ) {
DiskLoc next;
if ( direction > 0 ) {
2011-05-02 18:16:03 -04:00
next = BTREE(thisLoc)->nextChild;
2011-01-04 00:40:41 -05:00
}
else {
2011-05-02 18:16:03 -04:00
next = BTREE(thisLoc)->k( 0 ).prevChildBucket;
2010-07-07 13:41:35 -07:00
}
2010-07-07 12:26:20 -07:00
if ( next.isNull() ) {
2011-05-02 18:16:03 -04:00
// if bestParent is this->null, we've hit the end and thisLoc gets set to DiskLoc()
2010-07-07 12:26:20 -07:00
thisLoc = bestParent.first;
keyOfs = bestParent.second;
return;
2011-01-04 00:40:41 -05:00
}
else {
2010-07-07 12:26:20 -07:00
thisLoc = next;
continue;
}
}
2010-08-18 11:42:58 -07:00
if ( !customFind( l, h, keyBegin, keyBeginLen, afterKey, keyEnd, keyEndInclusive, order, direction, thisLoc, keyOfs, bestParent ) ) {
2010-07-07 12:26:20 -07:00
return;
}
}
}
2011-01-04 00:40:41 -05:00
/** @thisLoc disk location of *this */
template< class V >
int BtreeBucket<V>::_insert(const DiskLoc thisLoc, const DiskLoc recordLoc,
2011-04-11 18:21:48 -04:00
const Key& key, const Ordering &order, bool dupsAllowed,
2010-11-22 12:37:05 -08:00
const DiskLoc lChild, const DiskLoc rChild, IndexDetails& idx) const {
2011-04-11 18:21:48 -04:00
if ( key.dataSize() > KeyMax ) {
problem() << "ERROR: key too large len:" << key.dataSize() << " max:" << KeyMax << ' ' << key.dataSize() << ' ' << idx.indexNamespace() << endl;
return 2;
2008-12-28 20:28:49 -05:00
}
2011-04-11 18:21:48 -04:00
assert( key.dataSize() > 0 );
int pos;
bool found = find(idx, key, recordLoc, order, pos, !dupsAllowed);
if ( insert_debug ) {
out() << " " << thisLoc.toString() << '.' << "_insert " <<
2011-01-04 00:40:41 -05:00
key.toString() << '/' << recordLoc.toString() <<
" l:" << lChild.toString() << " r:" << rChild.toString() << endl;
2011-05-02 18:16:03 -04:00
out() << " found:" << found << " pos:" << pos << " this->n:" << this->n << endl;
}
if ( found ) {
const _KeyNode& kn = k(pos);
if ( kn.isUnused() ) {
log(4) << "btree _insert: reusing unused key" << endl;
2011-05-02 18:16:03 -04:00
massert( 10285 , "_insert: reuse key but lchild is not this->null", lChild.isNull());
massert( 10286 , "_insert: reuse key but rchild is not this->null", rChild.isNull());
kn.writing().setUsed();
return 0;
}
2008-12-28 20:28:49 -05:00
2011-01-04 00:40:41 -05:00
DEV {
log() << "_insert(): key already exists in index (ok for background:true)\n";
2010-10-19 11:21:18 -07:00
log() << " " << idx.indexNamespace() << " thisLoc:" << thisLoc.toString() << '\n';
log() << " " << key.toString() << '\n';
log() << " " << "recordLoc:" << recordLoc.toString() << " pos:" << pos << endl;
2011-05-02 18:16:03 -04:00
log() << " old l r: " << this->childForPos(pos).toString() << ' ' << this->childForPos(pos+1).toString() << endl;
log() << " this->new l r: " << lChild.toString() << ' ' << rChild.toString() << endl;
2010-02-01 12:01:26 -05:00
}
2010-02-11 14:49:55 -05:00
alreadyInIndex();
2008-12-28 20:28:49 -05:00
}
DEBUGGING out() << "TEMP: key: " << key.toString() << endl;
2011-05-02 18:16:03 -04:00
DiskLoc child = this->childForPos(pos);
if ( insert_debug )
out() << " getChild(" << pos << "): " << child.toString() << endl;
2011-05-02 18:16:03 -04:00
// In current usage, rChild isNull() for a this->new key and false when we are
// promoting a split key. These are the only two cases where _insert()
// is called currently.
if ( child.isNull() || !rChild.isNull() ) {
2011-05-02 18:16:03 -04:00
// A this->new key will be inserted at the same tree height as an adjacent existing key.
insertHere(thisLoc, pos, recordLoc, key, order, lChild, rChild, idx);
return 0;
2008-12-28 20:28:49 -05:00
}
2011-04-22 18:16:43 -04:00
return child.btree<V>()->_insert(child, recordLoc, key, order, dupsAllowed, /*lchild*/DiskLoc(), /*rchild*/DiskLoc(), idx);
2008-12-28 20:28:49 -05:00
}
template< class V >
void BtreeBucket<V>::dump() const {
2011-05-02 18:16:03 -04:00
out() << "DUMP btreebucket this->n:" << this->n;
out() << " this->parent:" << hex << this->parent.getOfs() << dec;
for ( int i = 0; i < this->n; i++ ) {
out() << '\n';
KeyNode k = keyNode(i);
out() << '\t' << i << '\t' << k.key.toString() << "\tleft:" << hex <<
2011-01-04 00:40:41 -05:00
k.prevChildBucket.getOfs() << "\tRecLoc:" << k.recordLoc.toString() << dec;
if ( this->k(i).isUnused() )
out() << " UNUSED";
}
2011-05-02 18:16:03 -04:00
out() << " right:" << hex << this->nextChild.getOfs() << dec << endl;
}
2008-12-28 20:28:49 -05:00
/** todo: meaning of return code unclear clean up */
template< class V >
int BtreeBucket<V>::bt_insert(const DiskLoc thisLoc, const DiskLoc recordLoc,
2011-04-11 18:21:48 -04:00
const BSONObj& _key, const Ordering &order, bool dupsAllowed,
IndexDetails& idx, bool toplevel) const
{
KeyOwned key(_key);
if ( toplevel ) {
2011-04-11 18:21:48 -04:00
if ( key.dataSize() > KeyMax ) {
problem() << "Btree::insert: key too large to index, skipping " << idx.indexNamespace() << ' ' << key.dataSize() << ' ' << key.toString() << endl;
return 3;
}
}
2008-06-06 09:43:15 -04:00
2011-04-11 18:21:48 -04:00
int x = _insert(thisLoc, recordLoc, Key(key), order, dupsAllowed, DiskLoc(), DiskLoc(), idx);
2011-05-02 18:16:03 -04:00
this->assertValid( order );
return x;
}
template< class V >
void BtreeBucket<V>::shape(stringstream& ss) const {
2011-05-02 18:16:03 -04:00
this->_shape(0, ss);
}
2011-01-04 00:40:41 -05:00
template< class V >
int BtreeBucket<V>::getKeyMax() {
return KeyMax;
}
2011-01-04 00:40:41 -05:00
template< class V >
DiskLoc BtreeBucket<V>::findSingle( const IndexDetails& indexdetails , const DiskLoc& thisLoc, const BSONObj& key ) const {
2009-11-25 10:09:59 -05:00
int pos;
bool found;
2011-04-11 18:21:48 -04:00
// TODO: is it really ok here that the order is a default?
2011-05-02 18:16:03 -04:00
// for findById() use, yes. for checkNoIndexConflicts, this->no?
2010-04-20 19:42:53 -04:00
Ordering o = Ordering::make(BSONObj());
DiskLoc bucket = locate( indexdetails , indexdetails.head , key , o , pos , found , minDiskLoc );
2009-11-25 10:09:59 -05:00
if ( bucket.isNull() )
return bucket;
2011-04-22 18:16:43 -04:00
const BtreeBucket<V> *b = bucket.btree<V>();
2011-01-04 00:40:41 -05:00
while ( 1 ) {
const _KeyNode& knraw = b->k(pos);
if ( knraw.isUsed() )
break;
bucket = b->advance( bucket , pos , 1 , "findSingle" );
if ( bucket.isNull() )
return bucket;
2011-04-22 18:16:43 -04:00
b = bucket.btree<V>();
}
KeyNode kn = b->keyNode( pos );
2011-04-11 18:21:48 -04:00
if ( KeyOwned(key).woCompare( kn.key, o ) != 0 )
return DiskLoc();
2009-11-25 10:09:59 -05:00
return kn.recordLoc;
}
2008-06-06 09:43:15 -04:00
2011-05-02 18:16:03 -04:00
} // this->namespace mongo
#include "db.h"
#include "dbhelpers.h"
namespace mongo {
template< class V >
void BtreeBucket<V>::a_test(IndexDetails& id) {
2011-04-22 18:16:43 -04:00
BtreeBucket *b = id.head.btreemod<V>();
// record locs for testing
DiskLoc A(1, 20);
DiskLoc B(1, 30);
DiskLoc C(1, 40);
DiskLoc rl;
BSONObj key = fromjson("{x:9}");
2010-04-20 19:42:53 -04:00
BSONObj orderObj = fromjson("{}");
Ordering order = Ordering::make(orderObj);
b->bt_insert(id.head, A, key, order, true, id);
A.GETOFS() += 2;
b->bt_insert(id.head, A, key, order, true, id);
A.GETOFS() += 2;
b->bt_insert(id.head, A, key, order, true, id);
A.GETOFS() += 2;
b->bt_insert(id.head, A, key, order, true, id);
A.GETOFS() += 2;
assert( b->k(0).isUsed() );
// b->k(0).setUnused();
b->k(1).setUnused();
b->k(2).setUnused();
b->k(3).setUnused();
2010-04-20 19:42:53 -04:00
b->dumpTree(id.head, orderObj);
2009-09-24 12:11:55 -04:00
/* b->bt_insert(id.head, B, key, order, false, id);
b->k(1).setUnused();
b->dumpTree(id.head, order);
b->bt_insert(id.head, A, key, order, false, id);
b->dumpTree(id.head, order);
*/
// this should assert. does it? (it might "accidentally" though, not asserting proves a problem, asserting proves nothing)
b->bt_insert(id.head, C, key, order, false, id);
2010-04-20 19:42:53 -04:00
// b->dumpTree(id.head, order);
}
2011-05-02 18:16:03 -04:00
template class BucketBasics<V0>;
template class BucketBasics<V1>;
template class BtreeBucket<V0>;
template class BtreeBucket<V1>;
2009-09-24 12:11:55 -04:00
}