Skip to content

Commit

Permalink
SERVER-939 Introduced type for config.collection docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Lerner committed Nov 9, 2012
1 parent 7e7c9c3 commit e0efa26
Show file tree
Hide file tree
Showing 9 changed files with 734 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/mongo/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Import("darwin windows solaris linux nix")
env.SConscript(['base/SConscript',
'db/auth/SConscript',
'platform/SConscript',
's/SConscript',
'unittest/SConscript'])

def add_exe( v ):
Expand Down
4 changes: 3 additions & 1 deletion src/mongo/bson/bson_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include <string>

#include "mongo/bson/bsonobj.h"

namespace mongo {

/**
Expand Down Expand Up @@ -87,7 +89,7 @@ namespace mongo {
return _name;
}

string operator()() const {
std::string operator()() const {
return _name;
}

Expand Down
12 changes: 12 additions & 0 deletions src/mongo/s/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- mode: python -*-

Import("env")

env.StaticLibrary('config', ['field_parser.cpp',
'type_collection.cpp'],
LIBDEPS=['$BUILD_DIR/mongo/base/base',
'$BUILD_DIR/mongo/bson'])

env.CppUnitTest('field_parser_test', 'field_parser_test.cpp', LIBDEPS=['config'])

env.CppUnitTest('type_collection_test', 'type_collection_test.cpp', LIBDEPS=['config'])
112 changes: 112 additions & 0 deletions src/mongo/s/field_parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* Copyright (C) 2012 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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/field_parser.h"
#include "mongo/bson/bsontypes.h"

namespace mongo {

bool FieldParser::extract(BSONObj doc,
const BSONField<bool>& field,
bool def,
bool* out) {
BSONElement elem = doc[field.name()];
if (elem.eoo()) {
*out = def;
return true;
}

if (elem.type() == Bool) {
*out = elem.boolean();
return true;
}

return false;
}

bool FieldParser::extract(BSONObj doc,
const BSONField<BSONObj>& field,
const BSONObj& def,
BSONObj* out) {
BSONElement elem = doc[field.name()];
if (elem.eoo()) {
*out = def;
return true;
}

if (elem.type() == Object) {
*out = elem.embeddedObject();
return true;
}

return false;
}

bool FieldParser::extract(BSONObj doc,
const BSONField<Date_t>& field,
const Date_t def,
Date_t* out) {
BSONElement elem = doc[field.name()];
if (elem.eoo()) {
*out = def;
return true;
}

if (elem.type() == Date) {
*out = elem.date();
return true;
}

return false;
}

bool FieldParser::extract(BSONObj doc,
const BSONField<string>& field,
const string& def,
string* out) {
BSONElement elem = doc[field.name()];
if (elem.eoo()) {
*out = def;
return true;
}

if (elem.type() == String) {
*out = elem.valuestr();
return true;
}

return false;
}

bool FieldParser::extract(BSONObj doc,
const BSONField<OID>& field,
const OID& def,
OID* out) {
BSONElement elem = doc[field.name()];
if (elem.eoo()) {
*out = def;
return true;
}

if (elem.type() == jstOID) {
*out = elem.__oid();
return true;
}

return false;
}

} // namespace mongo
61 changes: 61 additions & 0 deletions src/mongo/s/field_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (C) 2012 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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/>.
*/

#pragma once

#include <string>

#include "mongo/bson/bson_field.h"
#include "mongo/bson/oid.h"
#include "mongo/bson/util/misc.h" // for Date_t

namespace mongo {

class FieldParser {
public:
/**
* Returns true and fill in 'out' with the contents of the field described by 'field'
* or with the value in 'def', depending on whether the field is present and has the
* correct type in 'doc' or not, respectively. Otherwise, if the field exists but has
* the wrong type, returns false.
*/
static bool extract(BSONObj doc,
const BSONField<bool>& field,
bool def,
bool* out);

static bool extract(BSONObj doc,
const BSONField<BSONObj>& field,
const BSONObj& def,
BSONObj* out);

static bool extract(BSONObj doc,
const BSONField<Date_t>& field,
const Date_t def,
Date_t* out);

static bool extract(BSONObj doc,
const BSONField<string>& field,
const string& def,
string* out);

static bool extract(BSONObj doc,
const BSONField<OID>& field,
const OID& def,
OID* out);
};

} // namespace mongo
124 changes: 124 additions & 0 deletions src/mongo/s/field_parser_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* Copyright (C) 2012 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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 <string>

#include "mongo/bson/bson_field.h"
#include "mongo/bson/oid.h"
#include "mongo/bson/util/misc.h" // for Date_t
#include "mongo/db/jsobj.h"
#include "mongo/s/field_parser.h"
#include "mongo/unittest/unittest.h"

namespace {

class ExtractionFixture : public mongo::unittest::Test {
protected:
mongo::BSONObj doc;

bool valBool;
mongo::BSONObj valObj;
mongo::Date_t valDate;
std::string valString;
mongo::OID valOID;

static mongo::BSONField<bool> aBool;
static mongo::BSONField<mongo::BSONObj> anObj;
static mongo::BSONField<mongo::Date_t> aDate;
static mongo::BSONField<std::string> aString;
static mongo::BSONField<mongo::OID> anOID;

void setUp() {
valBool = true;
valObj = BSON("a" << 1);
valDate = 1ULL;
valString = "a string";
valOID = mongo::OID::gen();

doc = BSON(aBool(valBool) <<
anObj(valObj) <<
aDate(valDate) <<
aString(valString) <<
anOID(valOID));
}

void tearDown() {
}
};

mongo::BSONField<bool> ExtractionFixture::aBool("aBool");
mongo::BSONField<mongo::BSONObj> ExtractionFixture::anObj("anObj");
mongo::BSONField<mongo::Date_t> ExtractionFixture::aDate("aDate");
mongo::BSONField<std::string> ExtractionFixture::aString("aString");
mongo::BSONField<mongo::OID> ExtractionFixture::anOID("anOID");

TEST_F(ExtractionFixture, GetBool) {
mongo::BSONField<bool> notThere("otherBool");
mongo::BSONField<bool> wrongType(aString.name());
bool val;
ASSERT_TRUE(mongo::FieldParser::extract(doc, aBool, false, &val));
ASSERT_EQUALS(val, valBool);
ASSERT_TRUE(mongo::FieldParser::extract(doc, notThere, true, &val));
ASSERT_EQUALS(val, true);
ASSERT_FALSE(mongo::FieldParser::extract(doc, wrongType, true, &val));
}

TEST_F(ExtractionFixture, GetBSONObj) {
mongo::BSONField<mongo::BSONObj> notThere("otherObj");
mongo::BSONField<mongo::BSONObj> wrongType(aString.name());
mongo::BSONObj val;
ASSERT_TRUE(mongo::FieldParser::extract(doc, anObj, mongo::BSONObj(), &val));
ASSERT_EQUALS(val, valObj);
ASSERT_TRUE(mongo::FieldParser::extract(doc, notThere, BSON("b" << 1), &val));
ASSERT_EQUALS(val, BSON("b" << 1));
ASSERT_FALSE(mongo::FieldParser::extract(doc, wrongType, BSON("b" << 1), &val));
}

TEST_F(ExtractionFixture, GetDate) {
mongo::BSONField<mongo::Date_t> notThere("otherDate");
mongo::BSONField<mongo::Date_t> wrongType(aString.name());
mongo::Date_t val;
ASSERT_TRUE(mongo::FieldParser::extract(doc, aDate, time(0), &val));
ASSERT_EQUALS(val, valDate);
ASSERT_TRUE(mongo::FieldParser::extract(doc, notThere, 99ULL, &val));
ASSERT_EQUALS(val, 99ULL);
ASSERT_FALSE(mongo::FieldParser::extract(doc, wrongType, 99ULL, &val));
}

TEST_F(ExtractionFixture, GetString) {
mongo::BSONField<std::string> notThere("otherString");
mongo::BSONField<std::string> wrongType(aBool.name());
std::string val;
ASSERT_TRUE(mongo::FieldParser::extract(doc, aString, "", &val));
ASSERT_EQUALS(val, valString);
ASSERT_TRUE(mongo::FieldParser::extract(doc, notThere, "abc", &val));
ASSERT_EQUALS(val, "abc");
ASSERT_FALSE(mongo::FieldParser::extract(doc, wrongType, "abc", &val));
}

TEST_F(ExtractionFixture, GetOID) {
mongo::BSONField<mongo::OID> notThere("otherOID");
mongo::BSONField<mongo::OID> wrongType(aString.name());
mongo::OID defOID = mongo::OID::gen();
mongo::OID val;
ASSERT_TRUE(mongo::FieldParser::extract(doc, anOID, mongo::OID(), &val));
ASSERT_EQUALS(val, valOID);
ASSERT_TRUE(mongo::FieldParser::extract(doc, notThere, defOID, &val));
ASSERT_EQUALS(val, defOID);
ASSERT_FALSE(mongo::FieldParser::extract(doc, wrongType, defOID, &val));
}

} // unnamed namespace
Loading

0 comments on commit e0efa26

Please sign in to comment.