0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/db/jsobj.cpp

859 lines
22 KiB
C++
Raw Normal View History

2008-06-06 15:43:15 +02:00
// jsobj.cpp
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
* 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.
2008-12-29 02:28:49 +01:00
*
* 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.
2008-12-29 02:28:49 +01:00
*
* 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/>.
*/
2008-06-06 15:43:15 +02:00
#include "stdafx.h"
#include "jsobj.h"
#include "../util/goodies.h"
#include <limits>
#include "../util/unittest.h"
2008-06-06 15:43:15 +02:00
2008-10-21 22:13:48 +02:00
BSONElement nullElement;
2008-06-06 15:43:15 +02:00
2008-12-30 23:05:15 +01:00
ostream& operator<<( ostream &s, const OID &o ) {
s << o.str();
return s;
}
2008-12-29 02:28:49 +01:00
string BSONElement::toString() const {
stringstream s;
switch ( type() ) {
2008-06-06 15:43:15 +02:00
case EOO:
2008-12-29 02:28:49 +01:00
return "EOO";
2008-06-06 15:43:15 +02:00
case Date:
2008-12-29 02:28:49 +01:00
s << fieldName() << ": Date(" << hex << date() << ')';
break;
2008-11-14 17:20:23 +01:00
case RegEx:
2008-12-29 02:28:49 +01:00
{
s << fieldName() << ": /" << regex() << '/';
const char *p = regexFlags();
if ( p ) s << p;
}
break;
case NumberDouble:
case NumberInt:
2009-01-06 19:02:09 +01:00
s.precision( 16 );
2008-12-29 02:28:49 +01:00
s << fieldName() << ": " << number();
break;
case Bool:
s << fieldName() << ": " << ( boolean() ? "true" : "false" );
break;
case Object:
case Array:
s << fieldName() << ": " << embeddedObject().toString();
break;
case Undefined:
s << fieldName() << ": undefined";
break;
case jstNULL:
s << fieldName() << ": null";
break;
case MaxKey:
s << fieldName() << ": MaxKey";
break;
case MinKey:
s << fieldName() << ": MinKey";
2008-11-14 17:20:23 +01:00
break;
2008-12-01 16:59:53 +01:00
case CodeWScope:
2008-12-29 02:28:49 +01:00
s << fieldName() << ": codewscope";
break;
case Code:
s << fieldName() << ": ";
if ( valuestrsize() > 80 )
s << string(valuestr()).substr(0, 70) << "...";
else {
s << valuestr();
}
break;
2008-12-01 16:59:53 +01:00
case Symbol:
2008-12-29 02:28:49 +01:00
case String:
s << fieldName() << ": ";
if ( valuestrsize() > 80 )
s << '"' << string(valuestr()).substr(0, 70) << "...\"";
else {
s << '"' << valuestr() << '"';
}
break;
case DBRef:
s << fieldName();
s << " : DBRef('" << valuestr() << "',";
{
OID *x = (OID *) (valuestr() + valuestrsize());
2008-12-30 23:05:15 +01:00
s << *x << ')';
2008-12-29 02:28:49 +01:00
}
break;
case jstOID:
s << fieldName() << " : ObjId(";
2008-12-30 23:05:15 +01:00
s << oid() << ')';
2008-12-29 02:28:49 +01:00
break;
2009-01-06 19:02:09 +01:00
case BinData:
s << fieldName() << " : BinData";
break;
2008-06-06 15:43:15 +02:00
default:
2008-12-29 02:28:49 +01:00
s << fieldName() << ": ?type=" << type();
break;
}
return s.str();
2008-06-06 15:43:15 +02:00
}
2008-12-30 17:16:14 +01:00
string escape( string s ) {
stringstream ret;
2009-01-02 16:57:33 +01:00
for( string::iterator i = s.begin(); i != s.end(); ++i ) {
2008-12-30 17:16:14 +01:00
switch( *i ) {
case '"':
ret << "\\\"";
break;
case '\\':
ret << "\\\\";
break;
case '/':
ret << "\\/";
break;
case '\b':
ret << "\\b";
break;
case '\f':
ret << "\\f";
break;
case '\n':
ret << "\\n";
break;
case '\r':
ret << "\\r";
break;
case '\t':
ret << "\\t";
break;
default:
if ( *i >= 0 && *i <= 0x1f ) {
2008-12-30 17:16:14 +01:00
ret << "\\u";
ret << hex;
ret.width( 4 );
ret.fill( '0' );
ret << int( *i );
} else {
ret << *i;
}
}
}
return ret.str();
}
typedef boost::archive::iterators::base64_from_binary
< boost::archive::iterators::transform_width
2009-01-06 19:02:09 +01:00
< string::const_iterator, 6, 8 >
> base64_t;
2008-12-31 01:04:40 +01:00
string BSONElement::jsonString( JsonStringFormat format, bool includeFieldNames ) const {
2008-12-30 17:16:14 +01:00
stringstream s;
if ( includeFieldNames )
2009-01-06 19:02:09 +01:00
s << '"' << escape( fieldName() ) << "\" : ";
2008-12-30 17:16:14 +01:00
switch ( type() ) {
case String:
case Symbol:
2008-12-30 17:16:14 +01:00
s << '"' << escape( valuestr() ) << '"';
break;
case NumberInt:
case NumberDouble:
2009-01-06 19:02:09 +01:00
if ( number() >= -numeric_limits< double >::max() &&
2008-12-30 17:16:14 +01:00
number() <= numeric_limits< double >::max() ) {
2009-01-06 19:02:09 +01:00
s.precision( 16 );
2008-12-30 17:16:14 +01:00
s << number();
} else {
stringstream ss;
ss << "Number " << number() << " cannot be represented in JSON" << endl;
string message = ss.str();
massert( message.c_str(), false );
2008-12-30 17:16:14 +01:00
}
break;
case Bool:
s << ( boolean() ? "true" : "false" );
break;
case jstNULL:
s << "null";
break;
case Object:
2008-12-31 01:04:40 +01:00
s << embeddedObject().jsonString( format );
2008-12-30 17:16:14 +01:00
break;
case Array: {
if ( embeddedObject().isEmpty() ) {
s << "[]";
break;
}
s << "[ ";
BSONObjIterator i( embeddedObject() );
BSONElement e = i.next();
if ( !e.eoo() )
while ( 1 ) {
2008-12-31 01:04:40 +01:00
s << e.jsonString( format, false );
2008-12-30 17:16:14 +01:00
e = i.next();
if ( e.eoo() )
break;
s << ", ";
}
s << " ]";
break;
}
case DBRef: {
OID *x = (OID *) (valuestr() + valuestrsize());
if ( format == Strict )
s << "{ \"$ns\" : ";
else
s << "Dbref( ";
s << '"' << valuestr() << "\", ";
if ( format == Strict )
s << "\"$id\" : ";
s << '"' << *x << "\" ";
if ( format == Strict )
s << '}';
else
s << ')';
2008-12-30 17:16:14 +01:00
break;
}
case jstOID:
2008-12-31 01:04:40 +01:00
if ( format == TenGen )
s << "ObjectId( ";
2008-12-30 23:05:15 +01:00
s << '"' << oid() << '"';
2008-12-31 01:04:40 +01:00
if ( format == TenGen )
s << " )";
2008-12-30 17:16:14 +01:00
break;
case BinData: {
int len = *(int *)( value() );
BinDataType type = BinDataType( *(char *)( (int *)( value() ) + 1 ) );
s << "{ \"$binary\" : \"";
char *start = ( char * )( value() ) + sizeof( int ) + 1;
2009-01-02 16:39:59 +01:00
string temp(start, len);
string base64 = string( base64_t( temp.begin() ), base64_t( temp.end() ) );
s << base64;
int padding = ( 4 - ( base64.length() % 4 ) ) % 4;
for( int i = 0; i < padding; ++i )
s << '=';
s << "\", \"$type\" : \"" << hex;
2008-12-30 17:16:14 +01:00
s.width( 2 );
s.fill( '0' );
s << type << dec;
s << "\" }";
break;
}
2008-12-30 23:47:36 +01:00
case Date:
2008-12-31 01:04:40 +01:00
if ( format == Strict )
s << "{ \"$date\" : ";
else
s << "Date( ";
s << date();
if ( format == Strict )
s << " }";
else
s << " )";
2008-12-30 23:47:36 +01:00
break;
case RegEx:
2008-12-31 01:04:40 +01:00
if ( format == Strict )
s << "{ \"$regex\" : \"";
else
s << "/";
2009-01-06 19:54:19 +01:00
s << escape( regex() );
2008-12-31 01:04:40 +01:00
if ( format == Strict )
s << "\", \"$options\" : \"";
else
s << "/";
s << regexFlags();
if ( format == Strict )
s << "\" }";
2008-12-30 23:47:36 +01:00
break;
2008-12-30 17:16:14 +01:00
default:
stringstream ss;
ss << "Cannot create a properly formatted JSON string with "
<< "element: " << toString() << " of type: " << type()
<< endl;
string message = ss.str();
massert( message.c_str(), false );
2008-12-30 17:16:14 +01:00
}
return s.str();
}
2008-10-21 22:13:48 +02:00
int BSONElement::size() const {
2008-12-29 02:28:49 +01:00
if ( totalSize >= 0 )
return totalSize;
int x = 1;
switch ( type() ) {
case EOO:
case Undefined:
case jstNULL:
case MaxKey:
case MinKey:
break;
case Bool:
x = 2;
break;
case NumberInt:
x = 5;
break;
case Date:
case NumberDouble:
x = 9;
break;
case jstOID:
x = 13;
break;
case Symbol:
case Code:
case String:
x = valuestrsize() + 4 + 1;
break;
case CodeWScope:
x = objsize() + 1;
break;
case DBRef:
x = valuestrsize() + 4 + 12 + 1;
break;
case Object:
case Array:
x = objsize() + 1;
break;
case BinData:
x = valuestrsize() + 4 + 1 + 1/*subtype*/;
break;
case RegEx:
{
const char *p = value();
int len1 = strlen(p);
p = p + len1 + 1;
x = 1 + len1 + strlen(p) + 2;
}
break;
default:
cout << "BSONElement: bad type " << (int) type() << endl;
assert(false);
}
((BSONElement *) this)->totalSize = x + fieldNameSize;
if ( !eoo() ) {
const char *next = data + totalSize;
if ( *next < MinKey || ( *next > JSTypeMax && *next != MaxKey ) ) {
// bad type.
cout << "***\n";
cout << "Bad data or size in BSONElement::size()\n";
cout << "bad type:" << (int) *next << '\n';
cout << "totalsize:" << totalSize << " fieldnamesize:" << fieldNameSize << '\n';
cout << "lastrec:" << endl;
//dumpmemory(data, totalSize + 15);
assert(false);
}
}
return totalSize;
2008-06-06 15:43:15 +02:00
}
int BSONElement::getGtLtOp() const {
2008-12-29 02:28:49 +01:00
const char *fn = fieldName();
if ( fn[0] == '$' && fn[1] ) {
if ( fn[2] == 't' ) {
if ( fn[1] == 'g' ) {
if ( fn[3] == 0 ) return JSMatcher::GT;
else if ( fn[3] == 'e' && fn[4] == 0 ) return JSMatcher::GTE;
}
else if ( fn[1] == 'l' ) {
if ( fn[3] == 0 ) return JSMatcher::LT;
else if ( fn[3] == 'e' && fn[4] == 0 ) return JSMatcher::LTE;
}
}
else if ( fn[2] == 'e' ) {
if ( fn[1] == 'n' && fn[3] == 0 )
return JSMatcher::NE;
}
2008-12-29 02:28:49 +01:00
else if ( fn[1] == 'i' && fn[2] == 'n' && fn[3] == 0 )
return JSMatcher::opIN;
}
return JSMatcher::Equality;
}
int BSONElement::woCompare( const BSONElement &e,
2008-12-29 02:28:49 +01:00
bool considerFieldName ) const {
int lt = (int) type();
if ( lt == NumberInt ) lt = NumberDouble;
int rt = (int) e.type();
if ( rt == NumberInt ) rt = NumberDouble;
int x = lt - rt;
if ( x != 0 )
return x;
if ( considerFieldName ) {
x = strcmp(fieldName(), e.fieldName());
if ( x != 0 )
return x;
}
x = compareElementValues(*this, e);
return x;
}
2008-06-06 15:43:15 +02:00
/* must be same type! */
2008-10-21 22:13:48 +02:00
int compareElementValues(const BSONElement& l, const BSONElement& r) {
2008-12-29 02:28:49 +01:00
int f;
double x;
switch ( l.type() ) {
case EOO:
case Undefined:
case jstNULL:
case MaxKey:
case MinKey:
f = l.type() - r.type();
if ( f<0 ) return -1;
return f==0 ? 0 : 1;
case Bool:
return *l.value() - *r.value();
case Date:
if ( l.date() < r.date() )
return -1;
return l.date() == r.date() ? 0 : 1;
case NumberInt:
case NumberDouble:
x = l.number() - r.number();
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: utf version */
return strcmp(l.valuestr(), r.valuestr());
case Object:
case Array:
return l.embeddedObject().woCompare( r.embeddedObject() );
case DBRef:
2009-01-06 19:02:09 +01:00
case BinData: {
2008-12-29 02:28:49 +01:00
int lsz = l.valuesize();
int rsz = r.valuesize();
if ( lsz - rsz != 0 ) return lsz - rsz;
return memcmp(l.value(), r.value(), lsz);
}
case RegEx:
{
int c = strcmp(l.regex(), r.regex());
if ( c )
return c;
return strcmp(l.regexFlags(), r.regexFlags());
}
default:
cout << "compareElementValues: bad type " << (int) l.type() << endl;
assert(false);
}
return -1;
2008-06-06 15:43:15 +02:00
}
/* JSMatcher --------------------------------------*/
// If the element is something like:
// a : { $gt : 3 }
// we append
// a : 3
// else we just append the element.
//
2008-12-29 02:28:49 +01:00
void appendElementHandlingGtLt(BSONObjBuilder& b, BSONElement& e) {
if ( e.type() == Object ) {
BSONElement fe = e.embeddedObject().firstElement();
const char *fn = fe.fieldName();
if ( fn[0] == '$' && fn[1] && fn[2] == 't' ) {
b.appendAs(fe, e.fieldName());
return;
}
}
b.append(e);
2008-06-06 15:43:15 +02:00
}
2008-12-29 02:28:49 +01:00
int getGtLtOp(BSONElement& e) {
if ( e.type() != Object )
return JSMatcher::Equality;
2008-06-06 15:43:15 +02:00
2008-12-29 02:28:49 +01:00
BSONElement fe = e.embeddedObject().firstElement();
return fe.getGtLtOp();
2008-06-06 15:43:15 +02:00
}
2008-10-21 22:13:48 +02:00
/* BSONObj ------------------------------------------------------------*/
2008-06-06 15:43:15 +02:00
2008-10-21 22:13:48 +02:00
string BSONObj::toString() const {
2008-12-29 02:28:49 +01:00
if ( isEmpty() ) return "{}";
stringstream s;
s << "{ ";
BSONObjIterator i(*this);
BSONElement e = i.next();
if ( !e.eoo() )
while ( 1 ) {
s << e.toString();
e = i.next();
2008-12-30 17:16:14 +01:00
if ( e.eoo() )
break;
s << ", ";
}
s << " }";
return s.str();
}
2008-12-31 01:04:40 +01:00
string BSONObj::jsonString( JsonStringFormat format ) const {
2008-12-30 17:16:14 +01:00
if ( isEmpty() ) return "{}";
stringstream s;
s << "{ ";
BSONObjIterator i(*this);
BSONElement e = i.next();
if ( !e.eoo() )
while ( 1 ) {
2008-12-31 01:04:40 +01:00
s << e.jsonString( format );
2008-12-30 17:16:14 +01:00
e = i.next();
2008-12-29 02:28:49 +01:00
if ( e.eoo() )
break;
s << ", ";
}
s << " }";
return s.str();
2008-06-06 15:43:15 +02:00
}
2008-10-09 01:04:44 +02:00
// todo: can be a little faster if we don't use toString() here.
2008-12-29 02:28:49 +01:00
bool BSONObj::valid() const {
try {
2008-10-09 01:04:44 +02:00
toString();
}
2008-12-29 02:28:49 +01:00
catch (...) {
2008-10-09 01:04:44 +02:00
return false;
}
return true;
}
2008-06-06 15:43:15 +02:00
/* well ordered compare */
2008-12-29 02:28:49 +01:00
int BSONObj::woCompare(const BSONObj& r, bool considerFieldName) const {
if ( isEmpty() )
return r.isEmpty() ? 0 : -1;
if ( r.isEmpty() )
return 1;
BSONObjIterator i(*this);
BSONObjIterator j(r);
while ( 1 ) {
// so far, equal...
BSONElement l = i.next();
BSONElement r = j.next();
if ( l.eoo() )
return 0;
int x = l.woCompare( r, considerFieldName );
if ( x != 0 )
return x;
}
return -1;
}
2008-06-06 15:43:15 +02:00
BSONElement BSONObj::getField(const char *name) const {
2008-12-29 02:28:49 +01:00
if ( details ) {
2008-10-21 22:13:48 +02:00
BSONObjIterator i(*this);
2008-12-29 02:28:49 +01:00
while ( i.more() ) {
2008-10-21 22:13:48 +02:00
BSONElement e = i.next();
2008-12-29 02:28:49 +01:00
if ( e.eoo() )
break;
2008-12-29 02:28:49 +01:00
if ( strcmp(e.fieldName(), name) == 0 )
return e;
}
}
2008-12-29 02:28:49 +01:00
return nullElement;
2008-10-09 23:06:56 +02:00
}
2008-12-29 02:28:49 +01:00
/* return has eoo() true if no match
2008-06-06 15:43:15 +02:00
supports "." notation to reach into embedded objects
*/
BSONElement BSONObj::getFieldDotted(const char *name) const {
2008-12-29 02:28:49 +01:00
BSONElement e = getField( name );
if ( e.eoo() ) {
const char *p = strchr(name, '.');
if ( p ) {
string left(name, p-name);
BSONObj sub = getObjectField(left.c_str());
return sub.isEmpty() ? nullElement : sub.getFieldDotted(p+1);
}
}
2008-06-06 15:43:15 +02:00
return e;
2008-12-29 02:28:49 +01:00
/*
BSONObjIterator i(*this);
while( i.more() ) {
BSONElement e = i.next();
if( e.eoo() )
break;
if( strcmp(e.fieldName(), name) == 0 )
return e;
}
return nullElement;
*/
2008-06-06 15:43:15 +02:00
}
2008-12-24 19:57:30 +01:00
BSONElement BSONObj::getFieldDottedOrArray(const char *&name) const {
2008-12-29 02:28:49 +01:00
const char *p = strchr(name, '.');
string left;
if ( p ) {
left = string(name, p-name);
name = p + 1;
} else {
left = string(name);
name = name + strlen(name);
}
BSONElement sub = getField(left.c_str());
if ( sub.eoo() )
return nullElement;
else if ( sub.type() == Array || strlen( name ) == 0 )
return sub;
else
return sub.embeddedObject().getFieldDottedOrArray( name );
2008-12-24 19:57:30 +01:00
}
2008-10-21 22:13:48 +02:00
/* makes a new BSONObj with the fields specified in pattern.
2008-06-06 15:43:15 +02:00
fields returned in the order they appear in pattern.
if any field missing, you get back an empty object overall.
2008-12-29 02:28:49 +01:00
n^2 implementation bad if pattern and object have lots
2008-06-06 15:43:15 +02:00
of fields - normally pattern doesn't so should be fine.
*/
2008-12-29 02:28:49 +01:00
BSONObj BSONObj::extractFieldsDotted(BSONObj pattern, BSONObjBuilder& b, const char *&nameWithinArray) const {
nameWithinArray = "";
BSONObjIterator i(pattern);
while ( i.more() ) {
BSONElement e = i.next();
if ( e.eoo() )
break;
const char *name = e.fieldName();
BSONElement x = getFieldDottedOrArray( name );
if ( x.eoo() ) {
nameWithinArray = "";
return BSONObj();
} else if ( x.type() == Array ) {
// NOTE: Currently set based on last array discovered.
nameWithinArray = name;
}
b.appendAs(x, "");
}
return b.done();
2008-08-22 00:05:13 +02:00
}
2008-12-29 02:28:49 +01:00
BSONObj BSONObj::extractFieldsUnDotted(BSONObj pattern) {
2008-11-17 21:39:03 +01:00
BSONObjBuilder b;
2008-12-29 02:28:49 +01:00
BSONObjIterator i(pattern);
while ( i.more() ) {
BSONElement e = i.next();
if ( e.eoo() )
break;
BSONElement x = getField(e.fieldName());
if ( x.eoo() )
return BSONObj();
b.appendAs(x, "");
}
return b.doneAndDecouple();
2008-11-04 21:05:01 +01:00
}
2008-08-22 00:05:13 +02:00
2008-12-29 02:28:49 +01:00
BSONObj BSONObj::extractFields(BSONObj& pattern) {
BSONObjBuilder b(32); // scanandorder.h can make a zillion of these, so we start the allocation very small
BSONObjIterator i(pattern);
while ( i.more() ) {
BSONElement e = i.next();
if ( e.eoo() )
break;
BSONElement x = getFieldDotted(e.fieldName());
if ( x.eoo() )
return BSONObj();
b.append(x);
}
return b.doneAndDecouple();
2008-06-06 15:43:15 +02:00
}
2009-01-11 00:17:23 +01:00
int BSONObj::getIntField(const char *name) const {
2008-12-29 02:28:49 +01:00
BSONElement e = getField(name);
return e.isNumber() ? (int) e.number() : INT_MIN;
}
2008-12-29 02:28:49 +01:00
bool BSONObj::getBoolField(const char *name) {
BSONElement e = getField(name);
return e.type() == Bool ? e.boolean() : false;
2008-07-29 23:54:54 +02:00
}
2008-12-29 02:28:49 +01:00
const char * BSONObj::getStringField(const char *name) {
BSONElement e = getField(name);
return e.type() == String ? e.valuestr() : "";
2008-06-06 15:43:15 +02:00
}
2008-12-29 02:28:49 +01:00
BSONObj BSONObj::getObjectField(const char *name) const {
BSONElement e = getField(name);
BSONType t = e.type();
return t == Object || t == Array ? e.embeddedObject() : BSONObj();
2008-06-06 15:43:15 +02:00
}
2008-11-17 21:39:03 +01:00
int BSONObj::nFields() {
int n = 0;
2008-12-29 02:28:49 +01:00
BSONObjIterator i(*this);
while ( i.more() ) {
BSONElement e = i.next();
if ( e.eoo() )
break;
n++;
}
return n;
2008-11-17 21:39:03 +01:00
}
2008-10-21 22:13:48 +02:00
/* grab names of all the fields in this object */
int BSONObj::getFieldNames(set<string>& fields) {
2008-12-29 02:28:49 +01:00
int n = 0;
BSONObjIterator i(*this);
while ( i.more() ) {
BSONElement e = i.next();
if ( e.eoo() )
break;
fields.insert(e.fieldName());
n++;
}
return n;
2008-06-06 15:43:15 +02:00
}
2008-12-29 02:28:49 +01:00
/* note: addFields always adds _id even if not specified
2008-06-06 15:43:15 +02:00
returns n added not counting _id unless requested.
*/
2008-10-21 22:13:48 +02:00
int BSONObj::addFields(BSONObj& from, set<string>& fields) {
2008-12-29 02:28:49 +01:00
assert( details == 0 ); /* partial implementation for now... */
BSONObjBuilder b;
int N = fields.size();
int n = 0;
BSONObjIterator i(from);
bool gotId = false;
while ( i.more() ) {
BSONElement e = i.next();
const char *fname = e.fieldName();
if ( fields.count(fname) ) {
b.append(e);
++n;
gotId = gotId || strcmp(fname, "_id")==0;
if ( n == N && gotId )
break;
} else if ( strcmp(fname, "_id")==0 ) {
b.append(e);
gotId = true;
if ( n == N && gotId )
break;
}
}
if ( n ) {
int len;
init( b.decouple(len), true );
}
return n;
2008-06-06 15:43:15 +02:00
}
ostream& operator<<( ostream &s, const BSONObj &o ) {
2008-12-29 02:28:49 +01:00
return s << o.toString();
}
2008-06-06 15:43:15 +02:00
/*-- test things ----------------------------------------------------*/
2008-08-11 20:53:08 +02:00
#pragma pack(push,1)
2008-12-29 02:28:49 +01:00
struct MaxKeyData {
MaxKeyData() {
totsize=7;
maxkey=MaxKey;
name=0;
eoo=EOO;
}
int totsize;
char maxkey;
char name;
char eoo;
2008-06-06 15:43:15 +02:00
} maxkeydata;
2008-10-21 22:13:48 +02:00
BSONObj maxKey((const char *) &maxkeydata);
2008-06-06 15:43:15 +02:00
2008-12-29 02:28:49 +01:00
struct MinKeyData {
MinKeyData() {
totsize=7;
minkey=MinKey;
name=0;
eoo=EOO;
}
int totsize;
char minkey;
char name;
char eoo;
} minkeydata;
BSONObj minKey((const char *) &minkeydata);
2008-06-06 15:43:15 +02:00
struct JSObj0 {
2008-12-29 02:28:49 +01:00
JSObj0() {
totsize = 5;
eoo = EOO;
}
int totsize;
char eoo;
2008-06-06 15:43:15 +02:00
} js0;
2008-09-09 16:19:31 +02:00
#pragma pack(pop)
2008-06-06 15:43:15 +02:00
2008-12-29 02:28:49 +01:00
BSONElement::BSONElement() {
data = &js0.eoo;
fieldNameSize = 0;
totalSize = -1;
2008-06-06 15:43:15 +02:00
}
2008-09-09 16:19:31 +02:00
#pragma pack(push,1)
struct EmptyObject {
2008-12-29 02:28:49 +01:00
EmptyObject() {
len = 5;
jstype = EOO;
}
int len;
char jstype;
2008-09-09 16:19:31 +02:00
} emptyObject;
2008-06-06 15:43:15 +02:00
#pragma pack(pop)
2008-10-21 22:13:48 +02:00
BSONObj emptyObj((char *) &emptyObject);
2008-09-09 16:19:31 +02:00
2008-12-29 02:28:49 +01:00
struct BsonUnitTest : public UnitTest {
void testRegex() {
2008-12-26 02:51:32 +01:00
BSONObjBuilder b;
b.appendRegex("x", "foo");
BSONObj o = b.done();
BSONObjBuilder c;
c.appendRegex("x", "goo");
BSONObj p = c.done();
assert( o != p );
assert( o < p );
}
2008-12-29 02:28:49 +01:00
void run() {
2008-12-26 02:51:32 +01:00
testRegex();
2008-11-21 20:14:40 +01:00
BSONObjBuilder A,B,C;
A.appendInt("x", 2);
B.append("x", 2.0);
2008-11-21 20:14:40 +01:00
C.append("x", 2.1);
BSONObj a = A.done();
BSONObj b = B.done();
2008-11-21 20:14:40 +01:00
BSONObj c = C.done();
assert( !(a==b) ); // comments on operator==
2008-11-21 20:14:40 +01:00
int cmp = a.woCompare(b);
assert( cmp == 0 );
cmp = a.woCompare(c);
assert( cmp < 0 );
}
2008-12-26 02:51:32 +01:00
} bson_unittest;