Skip to content

Commit

Permalink
updated cxx driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Mar 11, 2013
1 parent 271a04c commit 6dd6236
Show file tree
Hide file tree
Showing 108 changed files with 4,174 additions and 1,480 deletions.
18 changes: 13 additions & 5 deletions mongo-cxx-driver/src/SConscript.client
Expand Up @@ -13,7 +13,7 @@ env.Command(['mongo/db/auth/action_type.h', 'mongo/db/auth/action_type.cpp'],
['mongo/db/auth/generate_action_types.py', 'mongo/db/auth/action_types.txt'],
'$PYTHON $SOURCES $TARGETS')

clientSource = [
clientSourceBasic = [
'mongo/base/configuration_variable_manager.cpp',
'mongo/base/error_codes.cpp',
'mongo/base/global_initializer.cpp',
Expand All @@ -26,9 +26,9 @@ clientSource = [
'mongo/base/parse_number.cpp',
'mongo/base/status.cpp',
'mongo/base/string_data.cpp',
'mongo/bson/util/bson_extract.cpp',
'mongo/bson/bson_validate.cpp',
'mongo/bson/oid.cpp',
'mongo/bson/util/bson_extract.cpp',
'mongo/buildinfo.cpp',
'mongo/client/clientAndShell.cpp',
'mongo/client/clientOnly.cpp',
Expand All @@ -40,6 +40,7 @@ clientSource = [
'mongo/client/distlock.cpp',
'mongo/client/gridfs.cpp',
'mongo/client/model.cpp',
'mongo/client/sasl_client_authenticate.cpp',
'mongo/client/syncclusterconnection.cpp',
'mongo/db/jsobj.cpp',
'mongo/db/json.cpp',
Expand Down Expand Up @@ -83,11 +84,18 @@ clientSource = [
'mongo/util/timer.cpp',
'mongo/util/trace.cpp',
'mongo/util/util.cpp',
'mongo/util/version.cpp',
]

clientSourceSasl = ['mongo/client/sasl_client_authenticate_impl.cpp',
'mongo/util/gsasl_session.cpp']

clientSourceAll = clientSourceBasic + clientSourceSasl

if env['MONGO_BUILD_SASL_CLIENT']:
clientSource.extend(['mongo/client/sasl_client_authenticate.cpp',
'mongo/util/gsasl_session.cpp'])
clientSource = clientSourceAll
else:
clientSource = clientSourceBasic

exampleSourceMap = [
('authTest', 'mongo/client/examples/authTest.cpp'),
Expand Down Expand Up @@ -154,7 +162,7 @@ env.Install(
'mongo/db/auth/generate_action_types.py',
'mongo/db/auth/action_types.txt',
'#buildscripts/make_archive.py',
clientSource,
clientSourceAll,
clientHeaders,
[source for (target, source) in exampleSourceMap],
'mongo/bson/bsondemo/bsondemo.cpp',
Expand Down
17 changes: 14 additions & 3 deletions mongo-cxx-driver/src/mongo/base/counter.h
Expand Up @@ -22,12 +22,23 @@
#include "mongo/platform/cstdint.h"

namespace mongo {

/**
* A 64bit (atomic) counter.
*
* The constructor allows setting the start value, and increment([int]) is used to change it.
*
* The value can be returned using get() or the (long long) function operator.
*/
class Counter64 {
public:


/** Atomically increment. */
void increment( uint64_t n = 1 ) { _counter.addAndFetch(n); }


/** Atomically decrement. */
void decrement( uint64_t n = 1 ) { _counter.subtractAndFetch(n); }

/** Return the current value */
long long get() const { return _counter.load(); }

operator long long() const { return get(); }
Expand Down
33 changes: 16 additions & 17 deletions mongo-cxx-driver/src/mongo/base/parse_number.cpp
Expand Up @@ -74,33 +74,32 @@ namespace mongo {
* "inputBase" is not 0, *outputBase is set to "inputBase". Otherwise, if "stringValue" starts
* with "0x" or "0X", sets outputBase to 16, or if it starts with 0, sets outputBase to 8.
*
* Returns the substring of "stringValue" with the base-indicating prefix stripped off.
* Returns stringValue, unless it sets *outputBase to 16, in which case it will strip off the
* "0x" or "0X" prefix, if present.
*/
static inline StringData _extractBase(
const StringData& stringValue, int inputBase, int* outputBase) {

const StringData hexPrefixLower("0x", StringData::LiteralTag());
const StringData hexPrefixUpper("0X", StringData::LiteralTag());
if (inputBase == 0) {
if (stringValue.size() == 0) {
*outputBase = inputBase;
return stringValue;
if (stringValue.size() > 2 && (stringValue.startsWith(hexPrefixLower) ||
stringValue.startsWith(hexPrefixUpper))) {
*outputBase = 16;
return stringValue.substr(2);
}
if (stringValue[0] == '0') {
if (stringValue.size() > 1 && (stringValue[1] == 'x' || stringValue[1] == 'X')) {
*outputBase = 16;
return stringValue.substr(2);
}
if (stringValue.size() > 1 && stringValue[0] == '0') {
*outputBase = 8;
return stringValue.substr(1);
return stringValue;
}
*outputBase = 10;
return stringValue;
}
else {
*outputBase = inputBase;
if (inputBase == 16) {
StringData prefix = stringValue.substr(0, 2);
if (prefix == "0x" || prefix == "0X")
return stringValue.substr(2);
if (inputBase == 16 && (stringValue.startsWith(hexPrefixLower) ||
stringValue.startsWith(hexPrefixUpper))) {
return stringValue.substr(2);
}
return stringValue;
}
Expand All @@ -115,12 +114,12 @@ namespace mongo {
if (base == 1 || base < 0 || base > 36)
return Status(ErrorCodes::BadValue, "Invalid base", 0);

if (stringValue.size() == 0)
return Status(ErrorCodes::FailedToParse, "Empty string");

bool isNegative = false;
StringData str = _extractBase(_extractSign(stringValue, &isNegative), base, &base);

if (str.empty())
return Status(ErrorCodes::FailedToParse, "No digits");

NumberType n(0);
if (isNegative) {
if (limits::is_signed) {
Expand Down
26 changes: 22 additions & 4 deletions mongo-cxx-driver/src/mongo/base/string_data-inl.h
Expand Up @@ -61,8 +61,11 @@ namespace mongo {
dest[size()] = 0;
}

inline size_t StringData::find( char c ) const {
const void* x = memchr( _data, c, size() );
inline size_t StringData::find( char c, size_t fromPos ) const {
if ( fromPos >= size() )
return string::npos;

const void* x = memchr( _data + fromPos, c, _size - fromPos );
if ( x == 0 )
return string::npos;
return static_cast<size_t>( static_cast<const char*>(x) - _data );
Expand Down Expand Up @@ -91,10 +94,25 @@ namespace mongo {
if ( pos > size() )
throw std::out_of_range( "out of range" );

if ( pos + n > size() )
// truncate to end of string
if ( n > size() - pos )
n = size() - pos;

return StringData( _data + pos, n );
}

}
inline bool StringData::startsWith( const StringData& prefix ) const {
// TODO: Investigate an optimized implementation.
return substr(0, prefix.size()) == prefix;
}

inline bool StringData::endsWith( const StringData& suffix ) const {
// TODO: Investigate an optimized implementation.
const size_t thisSize = size();
const size_t suffixSize = suffix.size();
if (suffixSize > thisSize)
return false;
return substr(thisSize - suffixSize) == suffix;
}

} // namespace mongo
19 changes: 15 additions & 4 deletions mongo-cxx-driver/src/mongo/base/string_data.h
Expand Up @@ -56,7 +56,7 @@ namespace mongo {

/**
* Constructs a StringData explicitly, for the case where the length of the string is
* already known. 'c' must be a pointer to a null-terminated string, and strlenOfc must
* already known. 'c' must be a pointer to a null-terminated string, and len must
* be the length that strlen(c) would return, a.k.a the index of the terminator in c.
*/
StringData( const char* c, size_t len )
Expand Down Expand Up @@ -96,16 +96,27 @@ namespace mongo {
// finders
//

size_t find( char c ) const;
size_t find( char c , size_t fromPos = 0 ) const;
size_t find( const StringData& needle ) const;

/**
* Returns true if 'prefix' is a substring of this instance, anchored at position 0.
*/
bool startsWith( const StringData& prefix ) const;

/**
* Returns true if 'suffix' is a substring of this instance, anchored at the end.
*/
bool endsWith( const StringData& suffix ) const;

//
// accessors
//

/**
* this is not guaranteed to be null-terminated,
* if you use this without all using size(), you are likely doing something wrong
* Get the pointer to the first byte of StringData. This is not guaranteed to be
* null-terminated, so if using this without checking size(), you are likely doing
* something wrong.
*/
const char* rawData() const { return _data; }

Expand Down
18 changes: 15 additions & 3 deletions mongo-cxx-driver/src/mongo/bson/bson_field.h
Expand Up @@ -74,8 +74,11 @@ namespace mongo {
template<typename T>
class BSONField {
public:
BSONField(const std::string& name, const std::string& longName="")
: _name(name), _longName(longName) {}
BSONField(const std::string& name)
: _name(name), _defaultSet(false) {}

BSONField(const std::string& name, const T& defaultVal)
: _name(name), _default(defaultVal), _defaultSet(true) {}

BSONFieldValue<T> make(const T& t) const {
return BSONFieldValue<T>(_name, t);
Expand All @@ -89,6 +92,14 @@ namespace mongo {
return _name;
}

const T& getDefault() const {
return _default;
}

const bool hasDefault() const {
return _defaultSet;
}

std::string operator()() const {
return _name;
}
Expand All @@ -109,7 +120,8 @@ namespace mongo {

private:
std::string _name;
std::string _longName;
T _default;
bool _defaultSet;
};

} // namespace mongo

0 comments on commit 6dd6236

Please sign in to comment.