summaryrefslogtreecommitdiff
path: root/src/mongo/s
diff options
context:
space:
mode:
authorShaun Verch <shaun.verch@10gen.com>2013-01-17 11:50:29 +0000
committerShaun Verch <shaun.verch@10gen.com>2013-01-17 15:56:57 +0000
commit588a5937884a07c72af6e40a98cd592eedebc74e (patch)
tree46846baf1734035594aecf323bb93cc38efcba98 /src/mongo/s
parent04296d4f271aacfaaa8fc7c822e07dba76ae4a50 (diff)
downloadmongo-588a5937884a07c72af6e40a98cd592eedebc74e.tar.gz
SERVER-939 Update LockpingsType with new method of recording field presence and handling defaults
Diffstat (limited to 'src/mongo/s')
-rw-r--r--src/mongo/s/type_lockpings.cpp42
-rw-r--r--src/mongo/s/type_lockpings.h69
2 files changed, 78 insertions, 33 deletions
diff --git a/src/mongo/s/type_lockpings.cpp b/src/mongo/s/type_lockpings.cpp
index 97a42af1c29..b43ced50af4 100644
--- a/src/mongo/s/type_lockpings.cpp
+++ b/src/mongo/s/type_lockpings.cpp
@@ -13,7 +13,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
#include "mongo/s/type_lockpings.h"
#include "mongo/s/field_parser.h"
@@ -25,8 +24,8 @@ namespace mongo {
const std::string LockpingsType::ConfigNS = "config.lockpings";
- BSONField<string> LockpingsType::process("_id");
- BSONField<Date_t> LockpingsType::ping("ping");
+ const BSONField<std::string> LockpingsType::process("_id");
+ const BSONField<Date_t> LockpingsType::ping("ping");
LockpingsType::LockpingsType() {
clear();
@@ -41,11 +40,12 @@ namespace mongo {
errMsg = &dummy;
}
- if (_process.empty()) {
+ // All the mandatory fields must be present.
+ if (!_isProcessSet) {
*errMsg = stream() << "missing " << process.name() << " field";
return false;
}
- if (_ping.millis == 0) {
+ if (!_isPingSet) {
*errMsg = stream() << "missing " << ping.name() << " field";
return false;
}
@@ -56,8 +56,8 @@ namespace mongo {
BSONObj LockpingsType::toBSON() const {
BSONObjBuilder builder;
- if (!_process.empty()) builder.append(process(), _process);
- if (_ping.millis > 0ULL) builder.append(ping(), _ping);
+ if (_isProcessSet) builder.append(process(), _process);
+ if (_isPingSet) builder.append(ping(), _ping);
return builder.obj();
}
@@ -65,24 +65,44 @@ namespace mongo {
bool LockpingsType::parseBSON(BSONObj source, string* errMsg) {
clear();
- string dummy;
+ std::string dummy;
if (!errMsg) errMsg = &dummy;
- if (!FieldParser::extract(source, process, "", &_process, errMsg)) return false;
- if (!FieldParser::extract(source, ping, 0ULL, &_ping, errMsg)) return false;
+ FieldParser::FieldState fieldState;
+ fieldState = FieldParser::extract(source, process, "", &_process, errMsg);
+ if (fieldState == FieldParser::FIELD_INVALID) return false;
+ _isProcessSet = fieldState == FieldParser::FIELD_VALID;
+
+ fieldState = FieldParser::extract(source, ping, 0, &_ping, errMsg);
+ if (fieldState == FieldParser::FIELD_INVALID) return false;
+ _isPingSet = fieldState == FieldParser::FIELD_VALID;
return true;
}
void LockpingsType::clear() {
+
_process.clear();
+ _isProcessSet = false;
+
_ping = 0ULL;
+ _isPingSet = false;
+
}
- void LockpingsType::cloneTo(LockpingsType* other) {
+ void LockpingsType::cloneTo(LockpingsType* other) const {
other->clear();
+
other->_process = _process;
+ other->_isProcessSet = _isProcessSet;
+
other->_ping = _ping;
+ other->_isPingSet = _isPingSet;
+
+ }
+
+ std::string LockpingsType::toString() const {
+ return toBSON().toString();
}
} // namespace mongo
diff --git a/src/mongo/s/type_lockpings.h b/src/mongo/s/type_lockpings.h
index 40311e8cb5c..306be64814c 100644
--- a/src/mongo/s/type_lockpings.h
+++ b/src/mongo/s/type_lockpings.h
@@ -33,22 +33,18 @@ namespace mongo {
*
* // Contact the config. 'conn' has been obtained before.
* DBClientBase* conn;
- * unique_ptr<DbClientCursor> cursor;
- * BSONObj query = QUERY(LockpingsType::name("localhost:27017"));
- * cursor.reset(conn->query(LockpingsType::ConfigNS, query, ...));
+ * BSONObj query = QUERY(LockpingsType::exampleField("exampleFieldName"));
+ * exampleDoc = conn->findOne(LockpingsType::ConfigNS, query);
*
* // Process the response.
- * while (cursor->more()) {
- * lockPingDoc = cursor->next();
- * LockpingsType lockPing;
- * lockPing.fromBSON(lockPingDoc);
- * if (! lockPing.isValid()) {
- * // Can't use 'lockPing'. Take action.
- * }
- * // use 'lockPing'
+ * LockpingsType exampleType;
+ * string errMsg;
+ * if (!exampleType.parseBSON(exampleDoc, &errMsg) || !exampleType.isValid(&errMsg)) {
+ * // Can't use 'exampleType'. Take action.
* }
+ * // use 'exampleType'
+ *
*/
-
class LockpingsType {
MONGO_DISALLOW_COPYING(LockpingsType);
public:
@@ -61,8 +57,8 @@ namespace mongo {
static const std::string ConfigNS;
// Field names and types in the lockpings collection type.
- static BSONField<string> process; // string describing the process holding the lock
- static BSONField<Date_t> ping; // last time the holding process updated this document
+ static const BSONField<std::string> process;
+ static const BSONField<Date_t> ping;
//
// lockpings type methods
@@ -96,7 +92,7 @@ namespace mongo {
/**
* Copies all the fields present in 'this' to 'other'.
*/
- void cloneTo(LockpingsType* other);
+ void cloneTo(LockpingsType* other) const;
/**
* Returns a string representation of the current internal state.
@@ -107,16 +103,45 @@ namespace mongo {
// individual field accessors
//
- void setProcess(const StringData& process) { _process = process.toString(); }
- const std::string& getProcess() const { return _process; }
+ // Mandatory Fields
+ void setProcess(const StringData& process) {
+ _process = process.toString();
+ _isProcessSet = true;
+ }
+
+ void unsetProcess() { _isProcessSet = false; }
+
+ bool isProcessSet() { return _isProcessSet; }
+
+ // Calling get*() methods when the member is not set results in undefined behavior
+ const std::string& getProcess() const {
+ dassert(_isProcessSet);
+ return _process;
+ }
+
+ void setPing(const Date_t ping) {
+ _ping = ping;
+ _isPingSet = true;
+ }
+
+ void unsetPing() { _isPingSet = false; }
+
+ bool isPingSet() { return _isPingSet; }
+
+ // Calling get*() methods when the member is not set results in undefined behavior
+ const Date_t getPing() const {
+ dassert(_isPingSet);
+ return _ping;
+ }
- void setPing(const Date_t& time) { _ping = time; }
- Date_t getPing() const { return _ping; }
+ // Optional Fields
private:
// Convention: (M)andatory, (O)ptional, (S)pecial rule.
- std::string _process; // (M) string describing the process holding the lock
- Date_t _ping; // (M) last time the holding process updated this document
+ std::string _process; // (M) string describing the process holding the lock
+ bool _isProcessSet;
+ Date_t _ping; // (M) last time the holding process updated this document
+ bool _isPingSet;
};
-} // namespace mongo
+} // namespace mongo