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/>.
16
#include "mongo/s/type_shard.h"
18
#include "mongo/s/field_parser.h"
19
#include "mongo/util/mongoutils/str.h"
23
using mongoutils::str::stream;
25
const std::string ShardType::ConfigNS = "config.shards";
27
const BSONField<std::string> ShardType::name("_id");
28
const BSONField<std::string> ShardType::host("host");
29
const BSONField<bool> ShardType::draining("draining");
30
const BSONField<long long> ShardType::maxSize("maxSize");
31
const BSONField<BSONArray> ShardType::tags("tags");
33
ShardType::ShardType() {
37
ShardType::~ShardType() {
40
bool ShardType::isValid(std::string* errMsg) const {
46
// All the mandatory fields must be present.
48
*errMsg = stream() << "missing " << name.name() << " field";
52
*errMsg = stream() << "missing " << host.name() << " field";
59
BSONObj ShardType::toBSON() const {
60
BSONObjBuilder builder;
62
if (_isNameSet) builder.append(name(), _name);
63
if (_isHostSet) builder.append(host(), _host);
64
if (_isDrainingSet) builder.append(draining(), _draining);
65
if (_isMaxSizeSet) builder.append(maxSize(), _maxSize);
66
if (_isTagsSet) builder.append(tags(), _tags);
71
bool ShardType::parseBSON(const BSONObj& source, string* errMsg) {
75
if (!errMsg) errMsg = &dummy;
77
FieldParser::FieldState fieldState;
78
fieldState = FieldParser::extract(source, name, &_name, errMsg);
79
if (fieldState == FieldParser::FIELD_INVALID) return false;
80
_isNameSet = fieldState == FieldParser::FIELD_SET;
82
fieldState = FieldParser::extract(source, host, &_host, errMsg);
83
if (fieldState == FieldParser::FIELD_INVALID) return false;
84
_isHostSet = fieldState == FieldParser::FIELD_SET;
86
fieldState = FieldParser::extract(source, draining, &_draining, errMsg);
87
if (fieldState == FieldParser::FIELD_INVALID) return false;
88
_isDrainingSet = fieldState == FieldParser::FIELD_SET;
90
fieldState = FieldParser::extract(source, maxSize, &_maxSize, errMsg);
91
if (fieldState == FieldParser::FIELD_INVALID) return false;
92
_isMaxSizeSet = fieldState == FieldParser::FIELD_SET;
94
fieldState = FieldParser::extract(source, tags, &_tags, errMsg);
95
if (fieldState == FieldParser::FIELD_INVALID) return false;
96
_isTagsSet = fieldState == FieldParser::FIELD_SET;
101
void ShardType::clear() {
110
_isDrainingSet = false;
113
_isMaxSizeSet = false;
120
void ShardType::cloneTo(ShardType* other) const {
123
other->_name = _name;
124
other->_isNameSet = _isNameSet;
126
other->_host = _host;
127
other->_isHostSet = _isHostSet;
129
other->_draining = _draining;
130
other->_isDrainingSet = _isDrainingSet;
132
other->_maxSize = _maxSize;
133
other->_isMaxSizeSet = _isMaxSizeSet;
135
other->_tags = _tags;
136
other->_isTagsSet = _isTagsSet;
140
std::string ShardType::toString() const {
141
return toBSON().toString();