summaryrefslogtreecommitdiff
path: root/src/mongo/dbtests/btreebuildertests.cpp
blob: bd668c6c45facf76d9f52fcfe98ebf51a2676fbc (plain)
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
//@file btreebuildertests.cpp : mongo/db/btreebuilder.{h,cpp} tests

/**
 *    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/>.
 *
 *    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.
 */

#include "mongo/db/btreebuilder.h"

#include "mongo/db/btreecursor.h"
#include "mongo/db/catalog/index_catalog.h"
#include "mongo/db/pdfile.h"
#include "mongo/db/structure/collection.h"
#include "mongo/platform/cstdint.h"

#include "mongo/dbtests/dbtests.h"

namespace BtreeBuilderTests {

    static const char* const _ns = "unittests.btreebuilder";
    DBDirectClient _client;

    /**
     * Test fixture for a write locked test using collection _ns.  Includes functionality to
     * partially construct a new IndexDetails in a manner that supports proper cleanup in
     * dropCollection().
     */
    class IndexBuildBase {
    public:
        IndexBuildBase() :
            _ctx( _ns ) {
            _client.createCollection( _ns );
        }
        ~IndexBuildBase() {
            _client.dropCollection( _ns );
        }
    protected:
        /** @return IndexDetails for a new index on a:1, with the info field populated. */
        IndexDetails& addIndexWithInfo() {
            BSONObj indexInfo = BSON( "v" << 1 <<
                                      "key" << BSON( "a" << 1 ) <<
                                      "ns" << _ns <<
                                      "name" << "a_1" );
            int32_t lenWHdr = indexInfo.objsize() + Record::HeaderSize;
            const char* systemIndexes = "unittests.system.indexes";
            DiskLoc infoLoc = allocateSpaceForANewRecord( systemIndexes,
                                                          nsdetails( systemIndexes ),
                                                          lenWHdr,
                                                          false );
            Record* infoRecord = reinterpret_cast<Record*>( getDur().writingPtr( infoLoc.rec(),
                                                                                 lenWHdr ) );
            memcpy( infoRecord->data(), indexInfo.objdata(), indexInfo.objsize() );
            addRecordToRecListInExtent( infoRecord, infoLoc );

            Collection* collection = _ctx.ctx().db()->getCollection( _ns );
            verify( collection );

            IndexCatalog::IndexBuildBlock  blk( collection->getIndexCatalog(), "a_1", infoLoc );
            IndexDetails* id = blk.indexDetails();
            blk.success();
            return *id;
        }
    private:
        Client::WriteContext _ctx;
    };

    /**
     * BtreeBuilder::commit() constructs a btree from the keys provided to BtreeBuilder::addKey().
     */
    class Commit : public IndexBuildBase {
    public:
        void run() {
            IndexDetails& id = addIndexWithInfo();
            // Create a btree builder.
            BtreeBuilder<V1> builder( false, id );
            // Add some keys to the builder, in order.
            int32_t nKeys = 1000;
            for( int32_t i = 0; i < nKeys; ++i ) {
                BSONObj key = BSON( "a" << i );
                builder.addKey( key, /* dummy location */ DiskLoc() );
            }
            // The root of the index has not yet been set.
            ASSERT( id.head.isNull() );
            // Call commit on the builder to finish building the btree.
            builder.commit( true );
            // The root of the index is now set.
            ASSERT( !id.head.isNull() );
            // Create a cursor over the index.
            scoped_ptr<BtreeCursor> cursor(
                    BtreeCursor::make( nsdetails( _ns ),
                                       id,
                                       BSON( "" << -1 ),    // startKey below minimum key value.
                                       BSON( "" << nKeys ), // endKey above maximum key value.
                                       true,                // endKeyInclusive true.
                                       1                    // direction forward.
                                       ) );
            // Check that the keys in the index are the expected ones.
            int32_t expectedKey = 0;
            for( ; cursor->ok(); cursor->advance(), ++expectedKey ) {
                ASSERT_EQUALS( expectedKey, cursor->currKey().firstElement().number() );
            }
            ASSERT_EQUALS( nKeys, expectedKey );
        }
    };

    /**
     * BtreeBuilder::commit() is interrupted if there is a request to kill the current operation.
     */
    class InterruptCommit : public IndexBuildBase {
    public:
        InterruptCommit( bool mayInterrupt ) :
            _mayInterrupt( mayInterrupt ) {
        }
        void run() {
            IndexDetails& id = addIndexWithInfo();
            // Create a btree builder.
            BtreeBuilder<V1> builder( false, id );
            // Add some keys to the builder, in order.  We need enough keys to build an internal
            // node in order to check for an interrupt.
            int32_t nKeys = 1000;
            for( int32_t i = 0; i < nKeys; ++i ) {
                BSONObj key = BSON( "a" << i );
                builder.addKey( key, /* dummy location */ DiskLoc() );
            }
            // The root of the index has not yet been set.
            ASSERT( id.head.isNull() );
            // Register a request to kill the current operation.
            cc().curop()->kill();
            if ( _mayInterrupt ) {
                // Call commit on the builder, which will be aborted due to the kill request.
                ASSERT_THROWS( builder.commit( _mayInterrupt ), UserException );
                // The root of the index is not set because commit() did not complete.
                ASSERT( id.head.isNull() );
            }
            else {
                // Call commit on the builder, which will not be aborted because mayInterrupt is
                // false.
                builder.commit( _mayInterrupt );
                // The root of the index is set because commit() completed.
                ASSERT( !id.head.isNull() );
            }
        }
    private:
        bool _mayInterrupt;
    };

    class BtreeBuilderTests : public Suite {
    public:
        BtreeBuilderTests() :
            Suite( "btreebuilder" ) {
        }

        void setupTests() {
            add<Commit>();
            add<InterruptCommit>( false );
            add<InterruptCommit>( true );
        }
    } btreeBuilderTests;

} // namespace BtreeBuilderTests