summaryrefslogtreecommitdiff
path: root/src/mongo/dbtests/indexcatalogtests.cpp
blob: bf9b61cf5d39886989def961cdfc9a0312d8d796 (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
// indexcatalogtests.cpp : index_catalog.{h,cpp} unit tests.

/**
 *    Copyright (C) 2013 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/>.
 */

#include "mongo/db/catalog/index_catalog.h"
#include "mongo/db/db.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/catalog/collection.h"
#include "mongo/db/operation_context_impl.h"

#include "mongo/dbtests/dbtests.h"

namespace IndexCatalogTests {

    static const char* const _ns = "unittests.indexcatalog";

    class IndexIteratorTests {
    public:
        IndexIteratorTests() {
            OperationContextImpl txn;
            Client::WriteContext ctx(&txn, _ns);

            _db = ctx.ctx().db();
            _coll = _db->createCollection(&txn, _ns);
            _catalog = _coll->getIndexCatalog();
        }

        ~IndexIteratorTests() {
            OperationContextImpl txn;
            Client::WriteContext ctx(&txn, _ns);

            _db->dropCollection(&txn, _ns);
        }

        void run() {
            OperationContextImpl txn;
            Client::WriteContext ctx(&txn, _ns);

            int numFinishedIndexesStart = _catalog->numIndexesReady();

            BSONObjBuilder b1;
            b1.append("key", BSON("x" << 1));
            b1.append("ns", _ns);
            b1.append("name", "_x_0");
            _catalog->createIndex(&txn, b1.obj(), true);

            BSONObjBuilder b2;
            b2.append("key", BSON("y" << 1));
            b2.append("ns", _ns);
            b2.append("name", "_y_0");
            _catalog->createIndex(&txn, b2.obj(), true);

            ASSERT_TRUE(_catalog->numIndexesReady() == numFinishedIndexesStart+2);

            IndexCatalog::IndexIterator ii = _catalog->getIndexIterator(false);
            int indexesIterated = 0;
            bool foundIndex = false;
            while (ii.more()) {
                IndexDescriptor* indexDesc = ii.next();
                indexesIterated++;
                BSONObjIterator boit(indexDesc->infoObj());
                while (boit.more() && !foundIndex) {
                    BSONElement e = boit.next();
                    if (str::equals(e.fieldName(), "name") &&
                            str::equals(e.valuestrsafe(), "_y_0")) {
                        foundIndex = true;
                        break;
                    }
                }
            }

            ASSERT_TRUE(indexesIterated == _catalog->numIndexesReady());
            ASSERT_TRUE(foundIndex);
        }

    private:
        IndexCatalog* _catalog;
        Collection* _coll;
        Database* _db;
    };

    class IndexCatalogTests : public Suite {
    public:
        IndexCatalogTests() : Suite( "indexcatalogtests" ) {
        }
        void setupTests() {
            add<IndexIteratorTests>();
        }
    } indexCatalogTests;
}