1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include <boost/optional.hpp>
34 #include <string>
35 
36 #include "mongo/db/jsobj.h"
37 #include "mongo/s/shard_id.h"
38 
39 namespace mongo {
40 
41 class BSONObj;
42 class Status;
43 template <typename T>
44 class StatusWith;
45 
46 
47 /**
48  * This class represents the layout and contents of documents contained in the config.databases
49  * collection. All manipulation of documents coming from that collection should be done with
50  * this class.
51  */
52 class DatabaseType {
53 public:
54     // Name of the databases collection in the config server.
55     static const std::string ConfigNS;
56 
57     static const BSONField<std::string> name;
58     static const BSONField<std::string> primary;
59     static const BSONField<bool> sharded;
60 
61 
62     /**
63      * Constructs a new DatabaseType object from BSON. Also does validation of the contents.
64      */
65     static StatusWith<DatabaseType> fromBSON(const BSONObj& source);
66 
67     /**
68      * Returns OK if all fields have been set. Otherwise returns NoSuchKey and information
69      * about what is the first field which is missing.
70      */
71     Status validate() const;
72 
73     /**
74      * Returns the BSON representation of the entry.
75      */
76     BSONObj toBSON() const;
77 
78     /**
79      * Returns a std::string representation of the current internal state.
80      */
81     std::string toString() const;
82 
getName()83     const std::string& getName() const {
84         return _name.get();
85     }
86     void setName(const std::string& name);
87 
getPrimary()88     const ShardId& getPrimary() const {
89         return _primary.get();
90     }
91     void setPrimary(const ShardId& primary);
92 
getSharded()93     bool getSharded() const {
94         return _sharded.get();
95     }
setSharded(bool sharded)96     void setSharded(bool sharded) {
97         _sharded = sharded;
98     }
99 
100 private:
101     // Requred database name
102     boost::optional<std::string> _name;
103 
104     // Required primary shard (must be set even if the database is sharded, because there
105     // might be collections, which are unsharded).
106     boost::optional<ShardId> _primary;
107 
108     // Required whether sharding is enabled for this database. Even though this field is of
109     // type optional, it is only used as an indicator that the value was explicitly set.
110     boost::optional<bool> _sharded;
111 };
112 
113 }  // namespace mongo
114