|
| 1 | +/** |
| 2 | + * Copyright (C) 2015 MongoDB Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 6 | + * as published by the Free Software Foundation. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU Affero General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU Affero General Public License |
| 14 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + * |
| 16 | + * As a special exception, the copyright holders give permission to link the |
| 17 | + * code of portions of this program with the OpenSSL library under certain |
| 18 | + * conditions as described in each individual source file and distribute |
| 19 | + * linked combinations including the program with the OpenSSL library. You |
| 20 | + * must comply with the GNU Affero General Public License in all respects |
| 21 | + * for all of the code used other than as permitted herein. If you modify |
| 22 | + * file(s) with this exception, you may extend this exception to your |
| 23 | + * version of the file(s), but you are not obligated to do so. If you do not |
| 24 | + * wish to do so, delete this exception statement from your version. If you |
| 25 | + * delete this exception statement from all source files in the program, |
| 26 | + * then also delete it in the license file. |
| 27 | + */ |
| 28 | + |
| 29 | +#pragma once |
| 30 | + |
| 31 | +#include <string> |
| 32 | + |
| 33 | +#include "mongo/base/disallow_copying.h" |
| 34 | +#include "mongo/base/string_data.h" |
| 35 | +#include "mongo/db/namespace_string.h" |
| 36 | + |
| 37 | +namespace mongo { |
| 38 | + |
| 39 | +class BSONObj; |
| 40 | +class CollectionMetadata; |
| 41 | +class OperationContext; |
| 42 | + |
| 43 | +/** |
| 44 | + * Contains all sharding-related runtime state for a given collection. One such object is assigned |
| 45 | + * to each sharded collection known on a mongod instance. A set of these objects is linked off the |
| 46 | + * instance's sharding state. |
| 47 | + * |
| 48 | + * Synchronization rules: In order to look-up this object in the instance's sharding map, one must |
| 49 | + * have some lock on the respective collection. |
| 50 | + */ |
| 51 | +class CollectionShardingState { |
| 52 | + MONGO_DISALLOW_COPYING(CollectionShardingState); |
| 53 | + |
| 54 | +public: |
| 55 | + /** |
| 56 | + * Instantiates a new per-collection sharding state with some initial metadata. |
| 57 | + */ |
| 58 | + CollectionShardingState(NamespaceString nss, |
| 59 | + std::unique_ptr<CollectionMetadata> initialMetadata); |
| 60 | + ~CollectionShardingState(); |
| 61 | + |
| 62 | + /** |
| 63 | + * Obtains the sharding state for the specified collection. If it does not exist, it will be |
| 64 | + * created and will remain active until the collection is dropped or unsharded. |
| 65 | + * |
| 66 | + * Must be called with some lock held on the specific collection being looked up and the |
| 67 | + * returned pointer should never be stored. |
| 68 | + */ |
| 69 | + static CollectionShardingState* get(OperationContext* txn, const std::string& ns); |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the chunk metadata for the collection. |
| 73 | + */ |
| 74 | + std::shared_ptr<CollectionMetadata> getMetadata() const { |
| 75 | + return _metadata; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Set a new metadata to be used for this collection. |
| 80 | + */ |
| 81 | + void setMetadata(std::shared_ptr<CollectionMetadata> newMetadata); |
| 82 | + |
| 83 | +private: |
| 84 | + // Namespace to which this state belongs. |
| 85 | + const NamespaceString _nss; |
| 86 | + |
| 87 | + // Contains all the chunks associated with this collection. This value is always non-null. |
| 88 | + std::shared_ptr<CollectionMetadata> _metadata; |
| 89 | +}; |
| 90 | + |
| 91 | +} // namespace mongo |
0 commit comments