2
* Copyright (C) 2012 10gen Inc.
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.
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.
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/>.
21
#include "mongo/base/disallow_copying.h"
22
#include "mongo/base/string_data.h"
23
#include "mongo/db/jsobj.h"
24
#include "mongo/s/chunk_version.h"
29
* This class represents the layout and contents of documents contained in the
30
* config.chunks collection. All manipulation of documents coming from that
31
* collection should be done with this class.
35
* // Contact the config. 'conn' has been obtained before.
37
* BSONObj query = QUERY(ChunkType::exampleField("exampleFieldName"));
38
* exampleDoc = conn->findOne(ChunkType::ConfigNS, query);
40
* // Process the response.
41
* ChunkType exampleType;
43
* if (!exampleType.parseBSON(exampleDoc, &errMsg) || !exampleType.isValid(&errMsg)) {
44
* // Can't use 'exampleType'. Take action.
46
* // use 'exampleType'
50
MONGO_DISALLOW_COPYING(ChunkType);
54
// schema declarations
57
// Name of the chunks collection in the config server.
58
static const std::string ConfigNS;
60
// Field names and types in the chunks collection type.
61
static const BSONField<std::string> name;
62
static const BSONField<std::string> ns;
63
static const BSONField<BSONObj> min;
64
static const BSONField<BSONObj> max;
65
static const BSONField<BSONArray> version;
66
static const BSONField<std::string> shard;
67
static const BSONField<bool> jumbo;
68
static const BSONField<Date_t> DEPRECATED_lastmod;
69
static const BSONField<OID> DEPRECATED_epoch;
72
// chunks type methods
79
* Returns true if all the mandatory fields are present and have valid
80
* representations. Otherwise returns false and fills in the optional 'errMsg' string.
82
bool isValid(std::string* errMsg) const;
85
* Returns the BSON representation of the entry.
87
BSONObj toBSON() const;
90
* Clears and populates the internal state using the 'source' BSON object if the
91
* latter contains valid values. Otherwise sets errMsg and returns false.
93
bool parseBSON(const BSONObj& source, std::string* errMsg);
96
* Clears the internal state.
101
* Copies all the fields present in 'this' to 'other'.
103
void cloneTo(ChunkType* other) const;
106
* Returns a string representation of the current internal state.
108
std::string toString() const;
111
// individual field accessors
115
void setName(const StringData& name) {
116
_name = name.toString();
120
void unsetName() { _isNameSet = false; }
122
bool isNameSet() const { return _isNameSet; }
124
// Calling get*() methods when the member is not set results in undefined behavior
125
const std::string getName() const {
130
void setNS(const StringData& ns) {
135
void unsetNS() { _isNsSet = false; }
137
bool isNSSet() const { return _isNsSet; }
139
// Calling get*() methods when the member is not set results in undefined behavior
140
const std::string getNS() const {
145
void setMin(const BSONObj& min) {
146
_min = min.getOwned();
150
void unsetMin() { _isMinSet = false; }
152
bool isMinSet() const { return _isMinSet; }
154
// Calling get*() methods when the member is not set results in undefined behavior
155
const BSONObj getMin() const {
160
void setMax(const BSONObj& max) {
161
_max = max.getOwned();
165
void unsetMax() { _isMaxSet = false; }
167
bool isMaxSet() const { return _isMaxSet; }
169
// Calling get*() methods when the member is not set results in undefined behavior
170
const BSONObj getMax() const {
175
void setVersion(const ChunkVersion& version) {
177
_isVersionSet = true;
180
void unsetVersion() { _isVersionSet = false; }
182
bool isVersionSet() const { return _isVersionSet; }
184
// Calling get*() methods when the member is not set results in undefined behavior
185
const ChunkVersion& getVersion() const {
186
dassert(_isVersionSet);
190
void setShard(const StringData& shard) {
191
_shard = shard.toString();
195
void unsetShard() { _isShardSet = false; }
197
bool isShardSet() const { return _isShardSet; }
199
// Calling get*() methods when the member is not set results in undefined behavior
200
const std::string getShard() const {
201
dassert(_isShardSet);
206
void setJumbo(bool jumbo) {
211
void unsetJumbo() { _isJumboSet = false; }
213
bool isJumboSet() const {
214
return _isJumboSet || jumbo.hasDefault();
217
// Calling get*() methods when the member is not set and has no default results in undefined
219
bool getJumbo() const {
223
dassert(jumbo.hasDefault());
224
return jumbo.getDefault();
229
// Convention: (M)andatory, (O)ptional, (S)pecial rule.
230
std::string _name; // (M) chunk's id
232
std::string _ns; // (M) collection this chunk is in
234
BSONObj _min; // (M) first key of the range, inclusive
236
BSONObj _max; // (M) last key of the range, non-inclusive
238
ChunkVersion _version; // (M) version of this chunk
240
std::string _shard; // (M) shard this chunk lives in
242
bool _jumbo; // (O) too big to move?