Skip to content

Commit

Permalink
Fixed some upstream differences that weren't merged in correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbenet committed Jun 28, 2011
1 parent f82e859 commit c0e8e2a
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 178 deletions.
140 changes: 12 additions & 128 deletions src/bson-inl.h
@@ -1,6 +1,6 @@
/** @file bsoninlines.h
a goal here is that the most common bson methods can be used inline-only, a la boost.
thus some things are inline that wouldn't necessarily be otherwise.
a goal here is that the most common bson methods can be used inline-only, a
la boost. thus some things are inline that wouldn't necessarily be otherwise.
*/

/* Copyright 2009 10gen Inc.
Expand Down Expand Up @@ -32,124 +32,8 @@

namespace bson {

/* must be same type when called, unless both sides are #s
this large function is in header to facilitate inline-only use of bson
*/
inline int compareElementValues(const BSONElement& l, const BSONElement& r) {
int f;
double x;

switch ( l.type() ) {
case EOO:
case Undefined: // EOO and Undefined are same canonicalType
case jstNULL:
case MaxKey:
case MinKey:
f = l.canonicalType() - r.canonicalType();
if ( f<0 ) return -1;
return f==0 ? 0 : 1;
case Bool:
return *l.value() - *r.value();
case Timestamp:
// unsigned compare for timestamps - note they are not really dates but (ordinal + time_t)
if ( l.date() < r.date() )
return -1;
return l.date() == r.date() ? 0 : 1;
case Date:
{
long long a = (long long) l.Date().millis;
long long b = (long long) r.Date().millis;
if( a < b )
return -1;
return a == b ? 0 : 1;
}
case NumberLong:
if( r.type() == NumberLong ) {
long long L = l._numberLong();
long long R = r._numberLong();
if( L < R ) return -1;
if( L == R ) return 0;
return 1;
}
// else fall through
case NumberInt:
case NumberDouble: {
double left = l.number();
double right = r.number();
bool lNan = !( left <= std::numeric_limits< double >::max() &&
left >= -std::numeric_limits< double >::max() );
bool rNan = !( right <= numeric_limits< double >::max() &&
right >= -numeric_limits< double >::max() );
if ( lNan ) {
if ( rNan ) {
return 0;
}
else {
return -1;
}
}
else if ( rNan ) {
return 1;
}
x = left - right;
if ( x < 0 ) return -1;
return x == 0 ? 0 : 1;
}
case jstOID:
return memcmp(l.value(), r.value(), 12);
case Code:
case Symbol:
case String:
/* todo: a utf sort order version one day... */
{
// we use memcmp as we allow zeros in UTF8 strings
int lsz = l.valuestrsize();
int rsz = r.valuestrsize();
int common = min(lsz, rsz);
int res = memcmp(l.valuestr(), r.valuestr(), common);
if( res )
return res;
// longer string is the greater one
return lsz-rsz;
}
case Object:
case Array:
return l.embeddedObject().woCompare( r.embeddedObject() );
case DBRef: {
int lsz = l.valuesize();
int rsz = r.valuesize();
if ( lsz - rsz != 0 ) return lsz - rsz;
return memcmp(l.value(), r.value(), lsz);
}
case BinData: {
int lsz = l.objsize(); // our bin data size in bytes, not including the subtype byte
int rsz = r.objsize();
if ( lsz - rsz != 0 ) return lsz - rsz;
return memcmp(l.value()+4, r.value()+4, lsz+1);
}
case RegEx: {
int c = strcmp(l.regex(), r.regex());
if ( c )
return c;
return strcmp(l.regexFlags(), r.regexFlags());
}
case CodeWScope : {
f = l.canonicalType() - r.canonicalType();
if ( f )
return f;
f = strcmp( l.codeWScopeCode() , r.codeWScopeCode() );
if ( f )
return f;
f = strcmp( l.codeWScopeScopeData() , r.codeWScopeScopeData() );
if ( f )
return f;
return 0;
}
default:
assert( false);
}
return -1;
}
/* TODO(jbenet) evaluate whether 'int bson::compareElementValues(const
bson::BSONElement&, const bson::BSONElement&)' does belong here. */

/* wo = "well ordered" */
inline int BSONElement::woCompare( const BSONElement &e,
Expand Down Expand Up @@ -249,7 +133,7 @@ namespace bson {
return b.obj();
}

inline void BSONObj::getFields(unsigned n, const char **fieldNames, BSONElement *fields) const {
inline void BSONObj::getFields(unsigned n, const char **fieldNames, BSONElement *fields) const {
BSONObjIterator i(*this);
while ( i.more() ) {
BSONElement e = i.next();
Expand Down Expand Up @@ -589,7 +473,7 @@ namespace bson {
break;
case RegEx: {
const char *p = value();
size_t len1 = ( maxLen == -1 ) ? strlen( p ) :
size_t len1 = ( maxLen == -1 ) ? strlen( p ) :
(size_t)bson::strnlen( p, remain );
//massert( 10318 , "Invalid regex string", len1 != -1 );
// ERH - 4/28/10 - don't think this does anything
Expand Down Expand Up @@ -631,14 +515,14 @@ namespace bson {
case MaxKey:
case MinKey:
break;
case mongo::Bool:
case bson::Bool:
x = 1;
break;
case NumberInt:
x = 4;
break;
case Timestamp:
case mongo::Date:
case bson::Date:
case NumberDouble:
case NumberLong:
x = 8;
Expand All @@ -648,21 +532,21 @@ namespace bson {
break;
case Symbol:
case Code:
case mongo::String:
case bson::String:
x = valuestrsize() + 4;
break;
case DBRef:
x = valuestrsize() + 4 + 12;
break;
case CodeWScope:
case Object:
case mongo::Array:
case bson::Array:
x = objsize();
break;
case BinData:
x = valuestrsize() + 4 + 1/*subtype*/;
break;
case RegEx:
case RegEx:
{
const char *p = value();
size_t len1 = strlen(p);
Expand All @@ -672,7 +556,7 @@ namespace bson {
x = (int) (len1 + 1 + len2 + 1);
}
break;
default:
default:
{
StringBuilder ss;
ss << "BSONElement: bad type " << (int) type();
Expand Down
1 change: 0 additions & 1 deletion src/bsonassert.h
Expand Up @@ -37,7 +37,6 @@
#include <boost/utility.hpp>

#include "stringdata.h"
#include "util/builder.h"

namespace bson {

Expand Down
8 changes: 4 additions & 4 deletions src/bsonelement.h
Expand Up @@ -70,13 +70,13 @@ namespace bson {
int Int() const { return chk(NumberInt)._numberInt(); }
bool Bool() const { return chk(bson::Bool).boolean(); }
vector<BSONElement> Array() const; // see implementation for detailed comments
mongo::OID OID() const { return chk(jstOID).__oid(); }
bson::OID OID() const { return chk(jstOID).__oid(); }
void Null() const { chk(isNull()); } // throw UserException if not null
void OK() const { chk(ok()); } // throw UserException if element DNE

/** @return the embedded object associated with this field.
Note the returned object is a reference to within the parent bson object. If that
object is out of scope, this pointer will no longer be valid. Call getOwned() on the
Note the returned object is a reference to within the parent bson object. If that
object is out of scope, this pointer will no longer be valid. Call getOwned() on the
returned BSONObj if you need your own copy.
throws UserException if the element is not of type object.
*/
Expand Down Expand Up @@ -230,7 +230,7 @@ namespace bson {
}

/** Size (length) of a string element.
You must assure of type String first.
You must assure of type String first.
@return string size including terminating null
*/
int valuestrsize() const {
Expand Down
10 changes: 6 additions & 4 deletions src/bsonobj.cpp
Expand Up @@ -444,6 +444,7 @@ namespace bson {
}

/* wo = "well ordered" */
/* TODO(jbenet): does this belong here or in bson-inl.h?
int BSONElement::woCompare( const BSONElement &e,
bool considerFieldName ) const {
int lt = (int) canonicalType();
Expand All @@ -458,10 +459,9 @@ namespace bson {
}
x = compareElementValues(*this, e);
return x;
}
}*/

/* must be same type when called, unless both sides are #s
*/
/* must be same type when called, unless both sides are #s*/
int compareElementValues(const BSONElement& l, const BSONElement& r) {
int f;
double x;
Expand Down Expand Up @@ -960,6 +960,7 @@ namespace bson {
return BSONElement();
}

/* TOOD(jbenet) evaluate whether these belong there:
int BSONObj::getIntField(const char *name) const {
BSONElement e = getField(name);
return e.isNumber() ? (int) e.number() : INT_MIN;
Expand All @@ -974,6 +975,7 @@ namespace bson {
BSONElement e = getField(name);
return e.type() == String ? e.valuestr() : "";
}
*/

/* grab names of all the fields in this object */
int BSONObj::getFieldNames(set<string>& fields) const {
Expand Down Expand Up @@ -1021,7 +1023,7 @@ namespace bson {

if ( n ) {
int len;
init( b.decouple(len), true );
init( b.decouple(len) );
}

return n;
Expand Down

0 comments on commit c0e8e2a

Please sign in to comment.