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
|
/**
* Copyright (C) 2012 10gen 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/>.
*/
#include "mongo/db/btreeposition.h"
#include "mongo/db/btree.h"
#include "mongo/db/index.h"
#include "mongo/db/pdfile.h"
namespace mongo {
std::ostream& operator<<( std::ostream& stream, const BtreeKeyLocation& loc ) {
return stream << BSON( "bucket" << loc.bucket.toString() <<
"index" << loc.pos ).jsonString();
}
LogicalBtreePosition::LogicalBtreePosition( const IndexDetails& indexDetails,
Ordering ordering,
const BtreeKeyLocation& initialLocation ) :
_indexDetails( &indexDetails ),
_ordering( ordering ),
_initialLocation( initialLocation ),
_initialLocationValid() {
fassert( 16494, _indexDetails->version() == 1 );
}
void LogicalBtreePosition::init() {
if ( _initialLocation.bucket.isNull() ) {
// Abort if the initial location is not a valid bucket.
return;
}
// Store the key and record referenced at the supplied initial location.
BucketBasics<V1>::KeyNode keyNode =
_initialLocation.bucket.btree<V1>()->keyNode( _initialLocation.pos );
_key = keyNode.key.toBson().getOwned();
_record = keyNode.recordLoc;
_initialLocationValid = true;
}
BtreeKeyLocation LogicalBtreePosition::currentLocation() const {
if ( _initialLocation.bucket.isNull() ) {
// Abort if the initial location is not a valid bucket.
return BtreeKeyLocation();
}
// If the initial location has not been invalidated ...
if ( _initialLocationValid ) {
const BtreeBucket<V1>* bucket = _initialLocation.bucket.btree<V1>();
if ( // ... and the bucket was not marked as invalid ...
bucket->getN() != bucket->INVALID_N_SENTINEL &&
// ... and the initial location index is valid for the bucket ...
_initialLocation.pos < bucket->getN() ) {
BucketBasics<V1>::KeyNode keyNode = bucket->keyNode( _initialLocation.pos );
if ( // ... and the record location equals the initial record location ...
keyNode.recordLoc == _record &&
// ... and the key equals the initial key ...
keyNode.key.toBson().binaryEqual( _key ) ) {
// ... then the initial location is the current location, so return it.
return _initialLocation;
}
}
}
// Relocate the key and record location retrieved from _initialLocation.
BtreeKeyLocation ret;
bool found;
ret.bucket = _indexDetails->head.btree<V1>()->locate( *_indexDetails,
_indexDetails->head,
_key,
_ordering,
ret.pos,
found,
_record,
1 // Forward direction means the next
// ordered key will be returned if
// the requested key is missing.
);
return ret;
}
} // namespace mongo
|