summaryrefslogtreecommitdiff
path: root/src/mongo/db/catalog/database_impl.h
blob: b5ee0179b3a8daa235333a30ce5f24a1a89af5ce (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
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
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    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
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    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 Server Side 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/database.h"

#include "mongo/platform/random.h"

namespace mongo {

class DatabaseImpl : public Database {
public:
    explicit DatabaseImpl(StringData name, DatabaseCatalogEntry* dbEntry, uint64_t epoch);

    // must call close first
    ~DatabaseImpl();

    void init(OperationContext*) final;


    // closes files and other cleanup see below.
    void close(OperationContext* opCtx) final;

    const std::string& name() const final {
        return _name;
    }

    void clearTmpCollections(OperationContext* opCtx) final;

    /**
     * Sets a new profiling level for the database and returns the outcome.
     *
     * @param opCtx Operation context which to use for creating the profiling collection.
     * @param newLevel New profiling level to use.
     */
    Status setProfilingLevel(OperationContext* opCtx, int newLevel) final;

    int getProfilingLevel() const final {
        return _profile;
    }
    const char* getProfilingNS() const final {
        return _profileName.c_str();
    }

    void setDropPending(OperationContext* opCtx, bool dropPending) final;

    bool isDropPending(OperationContext* opCtx) const final;

    void getStats(OperationContext* opCtx, BSONObjBuilder* output, double scale = 1) final;

    const DatabaseCatalogEntry* getDatabaseCatalogEntry() const final;

    /**
     * dropCollection() will refuse to drop system collections. Use dropCollectionEvenIfSystem() if
     * that is required.
     *
     * If we are applying a 'drop' oplog entry on a secondary, 'dropOpTime' will contain the optime
     * of the oplog entry.
     */
    Status dropCollection(OperationContext* opCtx,
                          StringData fullns,
                          repl::OpTime dropOpTime) final;
    Status dropCollectionEvenIfSystem(OperationContext* opCtx,
                                      const NamespaceString& fullns,
                                      repl::OpTime dropOpTime) final;

    Status dropView(OperationContext* opCtx, StringData fullns) final;

    Status userCreateNS(OperationContext* opCtx,
                        const NamespaceString& fullns,
                        CollectionOptions collectionOptions,
                        bool createDefaultIndexes,
                        const BSONObj& idIndex) final;

    Collection* createCollection(OperationContext* opCtx,
                                 StringData ns,
                                 const CollectionOptions& options = CollectionOptions(),
                                 bool createDefaultIndexes = true,
                                 const BSONObj& idIndex = BSONObj()) final;

    Status createView(OperationContext* opCtx,
                      StringData viewName,
                      const CollectionOptions& options) final;

    /**
     * @param ns - this is fully qualified, which is maybe not ideal ???
     */
    Collection* getCollection(OperationContext* opCtx, StringData ns) const final;

    Collection* getCollection(OperationContext* opCtx, const NamespaceString& ns) const;

    /**
     * Get the view catalog, which holds the definition for all views created on this database. You
     * must be holding a database lock to use this accessor.
     */
    ViewCatalog* getViewCatalog() final {
        return &_views;
    }

    Collection* getOrCreateCollection(OperationContext* opCtx, const NamespaceString& nss) final;

    /**
     * Renames the fully qualified namespace 'fromNS' to the fully qualified namespace 'toNS'.
     * Illegal to call unless both 'fromNS' and 'toNS' are within this database. Returns an error if
     * 'toNS' already exists or 'fromNS' does not exist.
     */
    Status renameCollection(OperationContext* opCtx,
                            StringData fromNS,
                            StringData toNS,
                            bool stayTemp) final;

    /**
     * Physically drops the specified opened database and removes it from the server's metadata. It
     * doesn't notify the replication subsystem or do any other consistency checks, so it should
     * not be used directly from user commands.
     *
     * Must be called with the specified database locked in X mode.
     */
    static void dropDatabase(OperationContext* opCtx, Database* db);

    static Status validateDBName(StringData dbname);

    const std::string& getSystemViewsName() const final {
        return _viewsName;
    }

    StatusWith<NamespaceString> makeUniqueCollectionNamespace(OperationContext* opCtx,
                                                              StringData collectionNameModel) final;

    void checkForIdIndexesAndDropPendingCollections(OperationContext* opCtx) final;

    iterator begin() const final {
        return iterator(_collections.begin());
    }

    iterator end() const final {
        return iterator(_collections.end());
    }

    uint64_t epoch() const {
        return _epoch;
    }

private:
    /**
     * Gets or creates collection instance from existing metadata,
     * Returns NULL if invalid
     *
     * Note: This does not add the collection to _collections map, that must be done
     * by the caller, who takes onership of the Collection*
     */
    Collection* _getOrCreateCollectionInstance(OperationContext* opCtx, const NamespaceString& nss);

    /**
     * Throws if there is a reason 'ns' cannot be created as a user collection.
     */
    void _checkCanCreateCollection(OperationContext* opCtx,
                                   const NamespaceString& nss,
                                   const CollectionOptions& options);

    /**
     * Completes a collection drop by removing all the indexes and removing the collection itself
     * from the storage engine.
     *
     * This is called from dropCollectionEvenIfSystem() to drop the collection immediately on
     * unreplicated collection drops.
     */
    Status _finishDropCollection(OperationContext* opCtx,
                                 const NamespaceString& fullns,
                                 Collection* collection);

    class AddCollectionChange;
    class RemoveCollectionChange;
    class RenameCollectionChange;

    const std::string _name;  // "dbname"

    DatabaseCatalogEntry* _dbEntry;  // not owned here

    const uint64_t _epoch;

    const std::string _profileName;  // "dbname.system.profile"
    const std::string _viewsName;    // "dbname.system.views"

    int _profile;  // 0=off.

    // If '_dropPending' is true, this Database is in the midst of a two-phase drop. No new
    // collections may be created in this Database.
    // This variable may only be read/written while the database is locked in MODE_X.
    bool _dropPending = false;

    // Random number generator used to create unique collection namespaces suitable for temporary
    // collections.
    // Lazily created on first call to makeUniqueCollectionNamespace().
    // This variable may only be read/written while the database is locked in MODE_X.
    std::unique_ptr<PseudoRandom> _uniqueCollectionNamespacePseudoRandom;

    CollectionMap _collections;

    DurableViewCatalogImpl _durableViews;  // interface for system.views operations
    ViewCatalog _views;                    // in-memory representation of _durableViews
};

}  // namespace mongo