Skip to content

Commit

Permalink
SERVER-939 Update LockpingsType with new method of recording field pr…
Browse files Browse the repository at this point in the history
…esence and handling defaults
  • Loading branch information
sverch committed Jan 17, 2013
1 parent 04296d4 commit 588a593
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 33 deletions.
42 changes: 31 additions & 11 deletions src/mongo/s/type_lockpings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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();
Expand All @@ -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;
}
Expand All @@ -56,33 +56,53 @@ 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();
}

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
69 changes: 47 additions & 22 deletions src/mongo/s/type_lockpings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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

0 comments on commit 588a593

Please sign in to comment.