35 #include "mongo/db/jsobj.h" 36 #include "mongo/db/operation_context.h" 46 class SavedCursorRegistry;
49 template <
class BtreeLayout>
51 template <
class BtreeLayout>
58 template <
class BtreeLayout>
71 typedef typename BtreeLayout::LocType
LocType;
86 const std::string& indexName,
143 const BSONObj& rawKey,
165 int direction)
const;
167 bool exists(OperationContext*
opCtx,
const KeyDataType&
key)
const;
174 long long* unusedCount,
177 unsigned depth)
const;
181 const int keyOffset)
const;
183 BSONObj
getKey(OperationContext*
opCtx,
const DiskLoc& bucketLoc,
const int keyOffset)
const;
205 int direction)
const;
211 int direction)
const;
214 const BSONObj& savedKey,
218 int* keyOffsetInOut)
const;
249 int direction)
const;
273 prevChildBucket(header.prevChildBucket),
274 recordLoc(header.recordLoc),
275 data(bucket->
data + header.keyDataOfs()) {}
324 static BucketType*
btreemod(OperationContext* opCtx, BucketType*
bucket);
333 const KeyDataType&
key,
334 const DiskLoc prevChildBucket);
354 const KeyDataType&
key,
359 void _pack(OperationContext* opCtx, BucketType*
bucket,
const DiskLoc thisLoc,
int& refPos);
366 std::pair<DiskLoc, int>& bestParent)
const;
370 const KeyDataType&
key,
374 bool* foundOut)
const;
383 std::pair<DiskLoc, int>& bestParent)
const;
389 int direction)
const;
393 bool keyIsUsed(OperationContext* opCtx,
const DiskLoc& loc,
const int& pos)
const;
400 int direction)
const;
404 const KeyDataType&
key,
408 const int direction)
const;
412 long long* unusedCount,
415 unsigned depth)
const;
422 const int leftIndex);
431 void split(OperationContext* opCtx,
436 const KeyDataType&
key,
443 const KeyDataType&
key,
453 const KeyDataType&
key,
465 const KeyDataType&
key,
529 bool _keyIsAt(
const BSONObj& savedKey,
544 const KeyDataType&
key,
548 BucketType*
childForPos(OperationContext* opCtx, BucketType*
bucket,
int pos)
const;
553 BucketType*
getBucket(OperationContext* opCtx,
const RecordId dl)
const;
555 BucketType*
getRoot(OperationContext* opCtx)
const;
561 BucketType* curBucket,
562 int64_t nBucketsInCurrentLevel,
563 std::vector<int64_t>* nKeysInLevel,
564 std::vector<FullKey>* selectedKeys)
const;
Status _insert(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc, const KeyDataType &key, const DiskLoc recordLoc, bool dupsAllowed, const DiskLoc leftChild, const DiskLoc rightChild)
Definition: btree_logic.cpp:2171
std::string _indexName
Definition: btree_logic.h:581
void replaceWithNextChild(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc)
Definition: btree_logic.cpp:1290
FullKey(const BucketType *bucket, int i)
Definition: btree_logic.h:271
int ofs
Definition: btree_interface.cpp:337
void restorePosition(OperationContext *opCtx, const BSONObj &savedKey, const DiskLoc &savedLoc, int direction, DiskLoc *bucketInOut, int *keyOffsetInOut) const
Definition: btree_logic.cpp:1149
BucketType * _getModifiableBucket(DiskLoc loc)
Definition: btree_logic.cpp:210
bool locate(OperationContext *opCtx, const BSONObj &key, const DiskLoc &recordLoc, const int direction, int *posOut, DiskLoc *bucketLocOut) const
Navigates down the tree and locates the bucket and position containing a record with the specified <k...
Definition: btree_logic.cpp:2304
void recordRandomWalk(OperationContext *opCtx, PseudoRandom *prng, BucketType *curBucket, int64_t nBucketsInCurrentLevel, std::vector< int64_t > *nKeysInLevel, std::vector< FullKey > *selectedKeys) const
Does a random walk through the tree, recording information about the walk along the way...
Definition: btree_logic.cpp:1992
This is the logic for manipulating the Btree.
Definition: btree_logic.h:59
bool customFind(OperationContext *opCtx, int low, int high, const IndexSeekPoint &seekPoint, int direction, DiskLoc *thisLocInOut, int *keyOfsInOut, std::pair< DiskLoc, int > &bestParent) const
Definition: btree_logic.cpp:849
void delKeyAtPos(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc, int p)
May delete the bucket 'bucket' rendering 'bucketLoc' invalid.
Definition: btree_logic.cpp:1196
void doBalanceLeftToRight(OperationContext *opCtx, BucketType *bucket, const DiskLoc thisLoc, int leftIndex, int split, BucketType *l, const DiskLoc lchild, BucketType *r, const DiskLoc rchild)
Definition: btree_logic.cpp:1478
Collection *const const NamespaceString & ns
Definition: collection_info_cache_impl.cpp:53
void _packReadyForMod(BucketType *bucket, int &refPos)
Version when write intent already declared.
Definition: btree_logic.cpp:535
void setInternalKey(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc, int keypos, const DiskLoc recordLoc, const KeyDataType &key, const DiskLoc lchild, const DiskLoc rchild)
Definition: btree_logic.cpp:1678
Ordering _ordering
Definition: btree_logic.h:579
bool wouldCreateDup(OperationContext *opCtx, const KeyDataType &key, const DiskLoc self) const
Definition: btree_logic.cpp:977
BSONObj getKey(OperationContext *opCtx, const DiskLoc &bucketLoc, const int keyOffset) const
Definition: btree_logic.cpp:1900
OperationContext * _opCtx
Definition: btree_logic.h:131
KeyDataType data
Definition: btree_logic.h:285
int indexInParent(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc) const
Definition: btree_logic.cpp:1439
SavedCursorRegistry * _cursorRegistry
Definition: btree_logic.h:577
static void _unalloc(BucketType *bucket, int bytes)
Definition: btree_logic.cpp:307
void split(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc, int keypos, const DiskLoc recordLoc, const KeyDataType &key, const DiskLoc lchild, const DiskLoc rchild)
Definition: btree_logic.cpp:1763
HeadManager * _headManager
Definition: btree_logic.h:571
static bool mayDropKey(BucketType *bucket, int index, int refPos)
With this implementation, refPos == 0 disregards effect of refPos.
Definition: btree_logic.cpp:491
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
static int lowWaterMark()
Definition: btree_logic.cpp:291
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
Builder(BtreeLogic *logic, OperationContext *opCtx, bool dupsAllowed)
Definition: btree_logic.cpp:88
bool _keyIsAt(const BSONObj &savedKey, const DiskLoc &savedLoc, BucketType *bucket, int keyPos) const
Definition: btree_logic.cpp:1177
void advance(OperationContext *opCtx, DiskLoc *bucketLocInOut, int *posInOut, int direction) const
Definition: btree_logic.cpp:684
BucketType * getRoot(OperationContext *opCtx) const
Definition: btree_logic.cpp:2401
bool keyIsUsed(OperationContext *opCtx, const DiskLoc &loc, const int &pos) const
Definition: btree_logic.cpp:2297
BtreeLayout::KeyType KeyDataType
Definition: btree_logic.h:65
bool pushBack(BucketType *bucket, const DiskLoc recordLoc, const KeyDataType &key, const DiskLoc prevChild)
Tries to push key into bucket.
Definition: btree_logic.cpp:390
long long fullValidate(OperationContext *, long long *unusedCount, bool strict, bool dumpBuckets, unsigned depth) const
Definition: btree_logic.cpp:2023
static void markUnused(BucketType *bucket, int keypos)
Definition: btree_logic.cpp:252
RecordStore * _recordStore
Definition: btree_logic.h:574
BucketType * getBucket(OperationContext *opCtx, const DiskLoc dl) const
Definition: btree_logic.h:550
BSONObj key
Definition: btree_interface.cpp:334
bool mayBalanceWithNeighbors(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc)
Definition: btree_logic.cpp:1589
Status initAsEmpty(OperationContext *opCtx)
Returns OK if the index was uninitialized before, error status otherwise.
Definition: btree_logic.cpp:1845
bool isEmpty(OperationContext *opCtx) const
Definition: btree_logic.cpp:1649
const KeyHeaderType & header
Definition: btree_logic.h:278
BucketType * _getBucket(DiskLoc loc)
Definition: btree_logic.cpp:215
static void setNotPacked(BucketType *bucket)
Definition: btree_logic.cpp:326
BucketType * childForPos(OperationContext *opCtx, BucketType *bucket, int pos) const
Definition: btree_logic.cpp:2412
static void setPacked(BucketType *bucket)
Definition: btree_logic.cpp:331
void doMergeChildren(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc, int leftIndex)
Definition: btree_logic.cpp:1394
static int splitPos(BucketType *bucket, int keypos)
In the standard btree algorithm, we would split based on the existing keys and the new key...
Definition: btree_logic.cpp:607
Builder * newBuilder(OperationContext *opCtx, bool dupsAllowed)
Caller owns the returned pointer.
Definition: btree_logic.cpp:82
static bool isHead(BucketType *bucket)
Definition: btree_logic.cpp:2381
static int _alloc(BucketType *bucket, int bytes)
We allocate space from the end of the buffer for data.
Definition: btree_logic.cpp:316
represents a disk location/offset on disk in a database.
Definition: diskloc.h:53
void advanceToImpl(OperationContext *opCtx, DiskLoc *thisLocInOut, int *keyOfsInOut, const IndexSeekPoint &seekPoint, int direction) const
find smallest/biggest value greater-equal/less-equal than specified
Definition: btree_logic.cpp:722
void customLocate(OperationContext *opCtx, DiskLoc *locInOut, int *keyOfsInOut, const IndexSeekPoint &seekPoint, int direction) const
Definition: btree_logic.cpp:672
RecordId toRecordId() const
Definition: diskloc.h:185
static char * dataAt(BucketType *bucket, short ofs)
Definition: btree_logic.cpp:258
static void _delKeyAtPos(BucketType *bucket, int keypos, bool mayEmpty=false)
Definition: btree_logic.cpp:336
SavedCursorRegistry * savedCursors() const
Definition: btree_logic.h:237
bool basicInsert(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc, int &keypos, const KeyDataType &key, const DiskLoc recordLoc)
Durability note:
Definition: btree_logic.cpp:436
virtual const RecordId getHead(OperationContext *opCtx) const =0
std::shared_ptr< void > data
Definition: ephemeral_for_test_record_store_test.cpp:74
Status insert(OperationContext *opCtx, const BSONObj &rawKey, const DiskLoc &value, bool dupsAllowed)
Definition: btree_logic.cpp:2151
BtreeLogic * _logic
Definition: btree_logic.h:124
Status touch(OperationContext *opCtx) const
Definition: btree_logic.cpp:2018
void doBalanceChildren(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc, int leftIndex)
Definition: btree_logic.cpp:1562
static DiskLoc fromRecordId(RecordId id)
Definition: diskloc.h:168
bool canMergeChildren(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc, const int leftIndex)
Definition: btree_logic.cpp:1310
static int _packedDataSize(BucketType *bucket, int refPos)
Definition: btree_logic.cpp:497
void delBucket(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc)
Definition: btree_logic.cpp:1126
IndexKeyEntry getRandomEntry(OperationContext *opCtx) const
Returns a pseudo-random element from the tree.
Definition: btree_logic.cpp:1923
static void dumpBucket(const BucketType *bucket, int indentLength=0)
Definition: btree_logic.cpp:1870
std::unique_ptr< KeyDataOwnedType > _keyLast
Definition: btree_logic.h:128
DiskLoc getRootLoc(OperationContext *opCtx) const
Definition: btree_logic.cpp:2407
const bool _isUnique
Definition: btree_logic.h:584
DiskLoc getHead(OperationContext *opCtx) const
Definition: btree_logic.h:191
int _rebalancedSeparatorPos(OperationContext *opCtx, BucketType *bucket, int leftIndex)
This implementation must respect the meaning and value of lowWaterMark.
Definition: btree_logic.cpp:1340
DiskLoc getDiskLoc(OperationContext *opCtx, const DiskLoc &bucketLoc, const int keyOffset) const
Definition: btree_logic.cpp:1891
void deleteInternalKey(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc, int keypos)
This function replaces the specified key (k) by either the prev or next key in the btree (k')...
Definition: btree_logic.cpp:1253
An abstraction used for storing documents in a collection or entries in an index. ...
Definition: record_store.h:282
DiskLoc bucket
Definition: btree_interface.cpp:336
bool unindex(OperationContext *opCtx, const BSONObj &key, const DiskLoc &recordLoc)
Definition: btree_logic.cpp:1632
bool isUnique() const
Definition: btree_logic.h:251
void advanceTo(OperationContext *, DiskLoc *thisLocInOut, int *keyOfsInOut, const IndexSeekPoint &seekPoint, int direction) const
Definition: btree_logic.cpp:703
BtreeLogic(HeadManager *head, RecordStore *store, SavedCursorRegistry *cursors, const Ordering &ordering, const std::string &indexName, bool isUnique)
'head' manages the catalog information.
Definition: btree_logic.h:82
static FullKey getFullKey(const BucketType *bucket, int i)
Definition: btree_logic.cpp:226
static int totalDataSize(BucketType *bucket)
Definition: btree_logic.cpp:270
DiskLoc _rightLeafLoc
Definition: btree_logic.h:126
static void popBack(BucketType *bucket, DiskLoc *recordLocOut, KeyDataType *keyDataOut)
Pull rightmost key from the bucket and set its prevChild pointer to be the nextChild for the whole bu...
Definition: btree_logic.cpp:363
BtreeLayout::KeyType KeyDataType
Definition: btree_logic.h:102
static void assertValid(const std::string &ns, BucketType *bucket, const Ordering &ordering, bool force=false)
Definition: btree_logic.cpp:2091
DiskLoc newBucket(BucketType *leftSib, DiskLoc leftSibLoc)
Creates and returns a new empty bucket to the right of leftSib, maintaining the internal consistency ...
Definition: btree_logic.cpp:164
Tool to construct custom tree shapes for tests.
Definition: btree_logic.h:52
long long _fullValidate(OperationContext *opCtx, const DiskLoc bucketLoc, long long *unusedCount, bool strict, bool dumpBuckets, unsigned depth) const
Definition: btree_logic.cpp:2032
std::string dupKeyError(const KeyDataType &key) const
Definition: btree_logic.cpp:1002
BtreeLayout::BucketType BucketType
Definition: btree_logic.h:74
DiskLoc _locate(OperationContext *opCtx, const DiskLoc &bucketLoc, const KeyDataType &key, int *posOut, bool *foundOut, const DiskLoc &recordLoc, const int direction) const
Recursively walk down the btree, looking for a match of key and recordLoc.
Definition: btree_logic.cpp:2329
BtreeLayout::FixedWidthKeyType KeyHeaderType
Definition: btree_logic.h:62
static void setKey(BucketType *bucket, int i, const DiskLoc recordLoc, const KeyDataType &key, const DiskLoc prevChildBucket)
Definition: btree_logic.cpp:647
const RecordStore * getRecordStore() const
Definition: btree_logic.h:233
void doBalanceRightToLeft(OperationContext *opCtx, BucketType *bucket, const DiskLoc thisLoc, int leftIndex, int split, BucketType *l, const DiskLoc lchild, BucketType *r, const DiskLoc rchild)
Definition: btree_logic.cpp:1518
static void init(BucketType *bucket)
Definition: btree_logic.cpp:296
BtreeLayout::KeyOwnedType KeyDataOwnedType
Definition: btree_logic.h:68
void _pack(OperationContext *opCtx, BucketType *bucket, const DiskLoc thisLoc, int &refPos)
When we delete things, we just leave empty space until the node is full and then we repack it...
Definition: btree_logic.cpp:518
BtreeLayout::LocType LocType
Definition: btree_logic.h:71
BtreeLayout::KeyOwnedType KeyDataOwnedType
Definition: btree_logic.h:101
void dropFront(BucketType *bucket, int nDrop, int &refpos)
Definition: btree_logic.cpp:662
Status dupKeyCheck(OperationContext *opCtx, const BSONObj &key, const DiskLoc &loc) const
Definition: btree_logic.cpp:965
An abstraction for setting and getting data about the 'head' of an index.
Definition: head_manager.h:41
bool _dupsAllowed
Definition: btree_logic.h:127
Describes a query that can be compared against an IndexKeyEntry in a way that allows expressing exclu...
Definition: index_entry_comparison.h:104
Definition: btree_logic.h:99
int customBSONCmp(const BSONObj &inIndex_left, const IndexSeekPoint &seekPoint_right, int direction) const
NOTE: Currently the Ordering implementation assumes a compound index will not have more keys than an ...
Definition: btree_logic.cpp:903
Status _find(OperationContext *opCtx, BucketType *bucket, const KeyDataType &key, const DiskLoc &recordLoc, bool errorIfDup, int *keyPositionOut, bool *foundOut) const
Find a key within this btree bucket.
Definition: btree_logic.cpp:1025
Status addKey(const BSONObj &key, const DiskLoc &loc)
Definition: btree_logic.cpp:120
Represents a single item in an index.
Definition: index_entry_comparison.h:45
void deallocBucket(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc)
Definition: btree_logic.cpp:1140
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
void skipUnusedKeys(OperationContext *opCtx, DiskLoc *loc, int *pos, int direction) const
Definition: btree_logic.cpp:693
Class that stores active cursors that have been saved (as part of yielding) to allow them to be inval...
Definition: record_store_v1_base.h:104
DiskLoc _addBucket(OperationContext *opCtx)
Definition: btree_logic.cpp:1855
const LocType & prevChildBucket
Definition: btree_logic.h:281
This is an in memory wrapper for the variable length data associated with a KeyHeaderType.
Definition: btree_logic.h:270
static KeyHeaderType & getKeyHeader(BucketType *bucket, int i)
Definition: btree_logic.cpp:239
static BucketType * btreemod(OperationContext *opCtx, BucketType *bucket)
Definition: btree_logic.cpp:263
bool exists(OperationContext *opCtx, const KeyDataType &key) const
Definition: btree_logic.cpp:945
void insertHere(OperationContext *opCtx, const DiskLoc bucketLoc, int pos, const KeyDataType &key, const DiskLoc recordLoc, const DiskLoc leftChild, const DiskLoc rightChild)
insert a key in this bucket, splitting if necessary.
Definition: btree_logic.cpp:1712
void truncateTo(BucketType *bucket, int N, int &refPos)
Definition: btree_logic.cpp:585
Ordering ordering() const
Definition: btree_logic.h:243
bool tryBalanceChildren(OperationContext *opCtx, BucketType *bucket, const DiskLoc bucketLoc, int leftIndex)
Definition: btree_logic.cpp:1463
void fixParentPtrs(OperationContext *trans, BucketType *bucket, const DiskLoc bucketLoc, int firstIndex=0, int lastIndex=-1)
This can cause a lot of additional page writes when we assign buckets to different parents...
Definition: btree_logic.cpp:1658
static LocType & childLocForPos(BucketType *bucket, int pos)
Definition: btree_logic.cpp:2419
Definition: btree_logic.cpp:106
This class is made friend of BtreeLogic so we can add whatever private method accesses we need to it...
Definition: btree_logic.h:50
const LocType & recordLoc
Definition: btree_logic.h:282
static void reserveKeysFront(BucketType *bucket, int nAdd)
Definition: btree_logic.cpp:637