1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
/**
* Copyright (C) 2014 MongoDB Inc.
*
* 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.
*
* 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.
*
* 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/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include "mongo/db/catalog/index_catalog_entry.h"
#include "mongo/db/diskloc.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/storage/record.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/structure/btree/btree_ondisk.h"
#include "mongo/db/structure/btree/key.h"
#include "mongo/db/structure/btree/bucket_deletion_notification.h"
#include "mongo/db/structure/head_manager.h"
namespace mongo {
class BucketDeletionNotification;
/**
* This is the logic for manipulating the Btree. It is (mostly) independent of the on-disk
* format.
*/
template <class BtreeLayout>
class BtreeLogic {
public:
// AKA _keyNode
typedef typename BtreeLayout::FixedWidthKeyType KeyHeaderType;
// AKA Key
typedef typename BtreeLayout::KeyType KeyDataType;
// AKA KeyOwned
typedef typename BtreeLayout::KeyOwnedType KeyDataOwnedType;
// AKA Loc
typedef typename BtreeLayout::LocType LocType;
// AKA BucketBasics or BtreeBucket, either one.
typedef typename BtreeLayout::BucketType BucketType;
/**
* 'head' manages the catalog information.
* 'store' allocates and frees buckets.
* 'ordering' is meta-information we store in the catalog.
* 'indexName' is a string identifying the index that we use to print errors with.
*/
BtreeLogic(HeadManager* head,
RecordStore* store,
const Ordering& ordering,
const string& indexName,
BucketDeletionNotification* bucketDeletion)
: _headManager(head),
_recordStore(store),
_ordering(ordering),
_indexName(indexName),
_bucketDeletion(bucketDeletion) {
}
//
// Public-facing
//
class Builder {
public:
typedef typename BtreeLayout::KeyOwnedType KeyDataOwnedType;
typedef typename BtreeLayout::KeyType KeyDataType;
Status addKey(const BSONObj& key, const DiskLoc& loc);
// XXX: status, outparam for # keys?
unsigned long long commit(bool mayInterrupt);
private:
friend class BtreeLogic;
Builder(BtreeLogic* logic, OperationContext* trans, bool dupsAllowed);
// Direct ports of functionality
void newBucket();
void buildNextLevel(DiskLoc loc, bool mayInterrupt);
void mayCommitProgressDurably();
BucketType* _getModifiableBucket(DiskLoc loc);
BucketType* _getBucket(DiskLoc loc);
// Direct ports of functionality
// Not owned.
BtreeLogic* _logic;
// Direct port of names.
DiskLoc _cur;
DiskLoc _first;
BucketType* _b;
bool _committed;
bool _dupsAllowed;
long long _numAdded;
auto_ptr<KeyDataOwnedType> _keyLast;
// Not owned.
OperationContext* _trans;
};
/**
* Caller owns the returned pointer.
* 'this' must outlive the returned pointer.
*/
Builder* newBuilder(OperationContext* trans, bool dupsAllowed);
Status dupKeyCheck(const BSONObj& key, const DiskLoc& loc) const;
Status insert(OperationContext* trans,
const BSONObj& rawKey,
const DiskLoc& value,
bool dupsAllowed);
/**
* Navigates down the tree and locates the bucket and position containing a record with
* the specified <key, recordLoc> combination.
*
* @return true if the exact <key, recordLoc> was found. Otherwise, false and the
* bucketLocOut would contain the bucket containing key which is before or after the
* searched one (dependent on the direction).
*/
bool locate(const BSONObj& key,
const DiskLoc& recordLoc,
const int direction,
int* posOut,
DiskLoc* bucketLocOut) const;
void advance(DiskLoc* bucketLocInOut, int* posInOut, int direction) const;
bool exists(const KeyDataType& key) const;
bool unindex(OperationContext* trans,
const BSONObj& key,
const DiskLoc& recordLoc);
bool isEmpty() const;
long long fullValidate(long long *unusedCount,
bool strict,
bool dumpBuckets,
unsigned depth);
DiskLoc getDiskLoc(const DiskLoc& bucketLoc, const int keyOffset);
BSONObj getKey(const DiskLoc& bucketLoc, const int keyOffset);
//
// Composite key navigation methods
//
void customLocate(DiskLoc* locInOut,
int* keyOfsInOut,
const BSONObj& keyBegin,
int keyBeginLen,
bool afterKey,
const vector<const BSONElement*>& keyEnd,
const vector<bool>& keyEndInclusive,
int direction) const;
void advanceTo(DiskLoc* thisLocInOut,
int* keyOfsInOut,
const BSONObj &keyBegin,
int keyBeginLen,
bool afterKey,
const vector<const BSONElement*>& keyEnd,
const vector<bool>& keyEndInclusive,
int direction) const;
void restorePosition(const BSONObj& savedKey,
const DiskLoc& savedLoc,
int direction,
DiskLoc* bucketInOut,
int* keyOffsetInOut) const;
bool keyIsAt(const BSONObj& savedKey,
const DiskLoc& savedLoc,
BucketType* bucket,
int keyPos) const;
//
// Creation and deletion
//
/**
* Returns OK if the index was uninitialized before, error status otherwise.
*/
Status initAsEmpty(OperationContext* trans);
private:
friend class BtreeLogic::Builder;
/**
* This is an in memory wrapper for the variable length data associated with a
* KeyHeaderType. It points to on-disk data but is not itself on-disk data.
*
* This object and its BSONObj 'key' will become invalid if the KeyHeaderType data that owns
* this it is moved within the btree. In general, a KeyWrapper should not be expected to be
* valid after a write.
*/
struct FullKey {
FullKey(const BucketType* bucket, int i)
: header(getKeyHeader(bucket, i)),
prevChildBucket(header.prevChildBucket),
recordLoc(header.recordLoc),
data(bucket->data + header.keyDataOfs()) { }
// This is actually a reference to something on-disk.
const KeyHeaderType& header;
// These are actually in 'header'.
const LocType& prevChildBucket;
const LocType& recordLoc;
// This is *not* memory-mapped but its members point to something on-disk.
KeyDataType data;
};
//
// Functions that depend on the templated type info but nothing in 'this'.
//
static int headerSize();
static int bodySize();
static int lowWaterMark();
static LocType& childLocForPos(BucketType* bucket, int pos);
static FullKey getFullKey(const BucketType* bucket, int i);
static KeyHeaderType& getKeyHeader(BucketType* bucket, int i);
static const KeyHeaderType& getKeyHeader(const BucketType* bucket, int i);
static char* dataAt(BucketType* bucket, short ofs);
static void markUnused(BucketType* bucket, int keypos);
static int totalDataSize(BucketType* bucket);
static void init(BucketType* bucket);
static int _alloc(BucketType* bucket, int bytes);
static void _unalloc(BucketType* bucket, int bytes);
static void _delKeyAtPos(BucketType* bucket, int keypos, bool mayEmpty = false);
static void popBack(BucketType* bucket, DiskLoc* recordLocOut, KeyDataType *keyDataOut);
static bool mayDropKey(BucketType* bucket, int index, int refPos);
static int packedDataSize(BucketType* bucket, int refPos);
static void setPacked(BucketType* bucket);
static void setNotPacked(BucketType* bucket);
static BucketType* btreemod(OperationContext* trans, BucketType* bucket);
static int splitPos(BucketType* bucket, int keypos);
static void reserveKeysFront(BucketType* bucket, int nAdd);
static void setKey(BucketType* bucket,
int i,
const DiskLoc recordLoc,
const KeyDataType &key,
const DiskLoc prevChildBucket);
static bool isHead(BucketType* bucket);
static void dump(BucketType* bucket, int depth = 0);
static void assertValid(const std::string& ns,
BucketType* bucket,
const Ordering& ordering,
bool force = false);
//
// 'this'-specific helpers (require record store, catalog information, or ordering, or type
// information).
//
bool basicInsert(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
int& keypos,
const KeyDataType& key,
const DiskLoc recordLoc);
void dropFront(BucketType* bucket, int nDrop, int& refpos);
void _pack(OperationContext* trans, BucketType* bucket, const DiskLoc thisLoc, int &refPos);
void customLocate(DiskLoc* locInOut,
int* keyOfsInOut,
const BSONObj& keyBegin,
int keyBeginLen,
bool afterKey,
const vector<const BSONElement*>& keyEnd,
const vector<bool>& keyEndInclusive,
int direction,
pair<DiskLoc, int>& bestParent) const;
Status _find(BucketType* bucket,
const KeyDataType& key,
const DiskLoc& recordLoc,
bool errorIfDup,
int* keyPositionOut,
bool* foundOut) const;
bool customFind(int low,
int high,
const BSONObj& keyBegin,
int keyBeginLen,
bool afterKey,
const vector<const BSONElement*>& keyEnd,
const vector<bool>& keyEndInclusive,
const Ordering& order,
int direction,
DiskLoc* thisLocInOut,
int* keyOfsInOut,
pair<DiskLoc, int>& bestParent) const;
void advanceToImpl(DiskLoc* thisLocInOut,
int* keyOfsInOut,
const BSONObj &keyBegin,
int keyBeginLen,
bool afterKey,
const vector<const BSONElement*>& keyEnd,
const vector<bool>& keyEndInclusive,
int direction) const;
bool wouldCreateDup(const KeyDataType& key, const DiskLoc self) const;
bool keyIsUsed(const DiskLoc& loc, const int& pos) const;
void skipUnusedKeys(DiskLoc* loc, int* pos, int direction) const;
DiskLoc advance(const DiskLoc& bucketLoc, int* posInOut, int direction) const;
DiskLoc _locate(const DiskLoc& bucketLoc,
const KeyDataType& key,
int* posOut,
bool* foundOut,
const DiskLoc& recordLoc,
const int direction) const;
long long fullValidate(const DiskLoc bucketLoc,
long long *unusedCount,
bool strict,
bool dumpBuckets,
unsigned depth);
DiskLoc addBucket(OperationContext* trans);
bool canMergeChildren(BucketType* bucket,
const DiskLoc bucketLoc,
const int leftIndex);
// has to look in children of 'bucket' and requires record store
int rebalancedSeparatorPos(BucketType* bucket,
const DiskLoc bucketLoc,
int leftIndex);
void _packReadyForMod(BucketType* bucket, int &refPos);
void truncateTo(BucketType* bucket, int N, int &refPos);
void split(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
int keypos,
const DiskLoc recordLoc,
const KeyDataType& key,
const DiskLoc lchild,
const DiskLoc rchild);
Status _insert(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
const KeyDataType& key,
const DiskLoc recordLoc,
bool dupsAllowed,
const DiskLoc leftChild,
const DiskLoc rightChild);
// TODO take a BucketType*?
void insertHere(OperationContext* trans,
const DiskLoc bucketLoc,
int pos,
const KeyDataType& key,
const DiskLoc recordLoc,
const DiskLoc leftChild,
const DiskLoc rightChild);
string dupKeyError(const KeyDataType& key) const;
void setInternalKey(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
int keypos,
const DiskLoc recordLoc,
const KeyDataType& key,
const DiskLoc lchild,
const DiskLoc rchild);
void fix(OperationContext* trans, const DiskLoc bucketLoc, const DiskLoc child);
void fixParentPtrs(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
int firstIndex = 0,
int lastIndex = -1);
bool mayBalanceWithNeighbors(OperationContext* trans, BucketType* bucket, const DiskLoc bucketLoc);
void doBalanceChildren(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
int leftIndex);
void doBalanceLeftToRight(OperationContext* trans,
BucketType* bucket,
const DiskLoc thisLoc,
int leftIndex,
int split,
BucketType* l,
const DiskLoc lchild,
BucketType* r,
const DiskLoc rchild);
void doBalanceRightToLeft(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
int leftIndex,
int split,
BucketType* l,
const DiskLoc lchild,
BucketType* r,
const DiskLoc rchild);
bool tryBalanceChildren(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
int leftIndex);
int indexInParent(BucketType* bucket, const DiskLoc bucketLoc) const;
void doMergeChildren(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
int leftIndex);
void replaceWithNextChild(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc);
void deleteInternalKey(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
int keypos);
void delKeyAtPos(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc,
int p);
void delBucket(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc);
void deallocBucket(OperationContext* trans,
BucketType* bucket,
const DiskLoc bucketLoc);
// TODO 'this' for _ordering(?)
int 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) const;
// TODO needs 'this' for _ordering for sanity check
bool _pushBack(BucketType* bucket,
const DiskLoc recordLoc,
const KeyDataType& key,
const DiskLoc prevChild);
void pushBack(BucketType* bucket,
const DiskLoc recordLoc,
const KeyDataType& key,
const DiskLoc prevChild) {
invariant(_pushBack(bucket, recordLoc, key, prevChild));
}
BucketType* childForPos(BucketType* bucket, int pos) const;
BucketType* getBucket(const DiskLoc dl) const;
BucketType* getRoot() const;
DiskLoc getRootLoc() const;
//
// Data
//
// Not owned here.
HeadManager* _headManager;
// Not owned here.
RecordStore* _recordStore;
Ordering _ordering;
string _indexName;
// Not owned here
BucketDeletionNotification* _bucketDeletion;
};
} // namespace mongo
|