26 #include "util/builder.h"
27 #include "bsontypes.h"
28 #include "bsonassert.h"
29 #include "util/optime.h"
30 #include "bsonDecimal.h"
45 int compareElementValues(
const BSONElement& l,
const BSONElement& r);
47 inline int getBSONCanonicalType( BSONType t )
117 Date_t Date()
const {
return chk(bson::Date).
date(); }
119 double Double()
const {
return chk(NumberDouble).
_numberDouble(); }
120 long long Long()
const {
return chk(NumberLong).
_numberLong(); }
121 bsonDecimal Decimal()
const {
return chk(NumberDecimal).
numberDecimal(); }
122 int Int()
const {
return chk(NumberInt).
_numberInt(); }
123 bool Bool()
const {
return chk(bson::Bool).
boolean(); }
124 vector<BSONElement> Array()
const;
125 bson::OID OID()
const {
return chk(jstOID).
__oid(); }
126 void Null()
const { chk(
isNull()); }
127 void OK()
const { chk(
ok()); }
142 void Val(
long long& v)
const { v = Long(); }
143 void Val(
bool& v)
const { v = Bool(); }
144 void Val(BSONObj& v)
const;
145 void Val(bson::OID& v)
const { v = OID(); }
146 void Val(
int& v)
const { v = Int(); }
147 void Val(
double& v)
const { v = Double(); }
148 void Val(
string& v)
const { v =
String(); }
149 void Val(bsonDecimal& v)
const { v = Decimal(); }
156 string toString(
bool includeFieldName =
true,
157 bool full=
false)
const;
158 void toString( StringBuilder& s,
bool
159 includeFieldName =
true,
160 bool full=
false)
const;
161 string jsonString( JsonStringFormat format,
162 bool includeFieldNames =
true,
163 int pretty = 0 )
const;
164 operator string()
const {
return toString(); }
166 #if defined ( SDB_ENGINE ) || defined ( SDB_FMP ) || defined ( SDB_TOOL )
167 ossPoolString poolString()
const {
return chk(bson::String).
valuestr(); }
168 void Val(ossPoolString& v)
const { v = poolString(); }
170 ossPoolString toPoolString(
bool includeFieldName =
true,
171 bool full=
false)
const;
172 operator ossPoolString()
const {
return toPoolString() ; }
175 ossPoolString poolStr()
const {
176 return type() == bson::String ?
179 #endif //SDB_ENGINE || SDB_FMP || SDB_TOOL
182 BSONType
type()
const {
return (BSONType) (
signed char)*data; }
207 int size(
int maxLen )
const;
221 if (
eoo() )
return "";
227 return (data + fieldNameSize() + 1);
231 return size() - fieldNameSize() - 1;
234 bool isBoolean()
const {
return type() == bson::Bool; }
240 return *
value() ?
true :
false;
243 bool booleanSafe()
const {
return isBoolean() &&
boolean(); }
250 return *
reinterpret_cast< const Date_t*
>(
value() );
267 {
return *
reinterpret_cast< const double*
>(
value() ); }
270 {
return *
reinterpret_cast< const int*
>(
value() ); }
273 {
return *
reinterpret_cast< const long long*
>(
value() ); }
300 {
return *
reinterpret_cast< const bson::OID*
>(
value() ); }
304 return type() == jstNULL;
312 return *
reinterpret_cast< const int*
>(
value() );
316 int objsize()
const {
317 return *
reinterpret_cast< const int*
>(
value() );
334 return type() == bson::String ?
352 BSONObj embeddedObjectUserCheck()
const;
354 BSONObj codeWScopeObject()
const;
360 assert(
type() == BinData );
367 if (binDataType() != ByteArrayDeprecated) {
373 return value() + 5 + 4;
377 BinDataType binDataType()
const {
379 assert(
type() == BinData );
380 unsigned char c = (
value() + 4)[0];
381 return (BinDataType)c;
386 assert(
type() == RegEx);
392 const char *p =
regex();
393 return p + strlen(p) + 1;
398 assert(
type() == Code);
422 const char * rawdata()
const {
return data; }
456 Date_t timestampTime()
const {
457 long long t = ((
int*)(
value() + 4 ))[0];
460 unsigned int timestampInc()
const {
461 return ((
unsigned int*)(
value() ))[0];
464 const char * dbrefNS()
const {
465 uassert( 10063 ,
"not a dbref" ,
type() == DBRef );
469 const bson::OID& dbrefOID()
const {
470 uassert( 10064 ,
"not a dbref" ,
type() == DBRef );
471 const char * start =
value();
472 start += 4 + *
reinterpret_cast< const int*
>( start );
473 return *
reinterpret_cast< const bson::OID*
>( start );
479 if ( x < 0 )
return true;
480 else if ( x > 0 )
return false;
481 return compareElementValues(*
this,other) < 0;
485 explicit BSONElement(
const char *d,
int maxLen) : data(d) {
493 if ( maxLen != -1 ) {
494 int size = (int) strnlen(
fieldName(), maxLen - 1 );
495 massert( 10333 ,
"Invalid field name", size != -1 );
496 fieldNameSize_ = size + 1;
510 string _asCode()
const;
511 OpTime _opTime()
const;
515 mutable int fieldNameSize_;
516 int fieldNameSize()
const {
517 if ( fieldNameSize_ == -1 )
518 fieldNameSize_ = (int)strlen(
fieldName() ) + 1;
519 return fieldNameSize_;
521 mutable int totalSize;
523 friend class BSONObjIterator;
524 friend class BSONObj;
529 ss <<
"field not found, expected type " << t;
531 ss <<
"wrong type for field (" <<
fieldName() <<
") "
532 <<
type() <<
" != " << t;
533 uasserted(13111, ss.str() );
538 uassert(13118,
"unexpected or missing type value in BSON object",
543 inline string _numberDecimalStr()
const ;
545 #if defined ( SDB_ENGINE ) || defined ( SDB_FMP ) || defined ( SDB_TOOL )
546 inline ossPoolString _numberDecimalPoolStr()
const ;
547 #endif //SDB_ENGINE || SDB_FMP || SDB_TOOL
552 return getBSONCanonicalType(
type() ) ;
558 return *
reinterpret_cast< const long long*
>(
value() ) != 0;
562 return *
reinterpret_cast< const double*
>(
value() ) != 0;
564 return *
reinterpret_cast< const int*
>(
value() ) != 0;
611 return *
reinterpret_cast< const int*
>(
value() );
613 return (
double) *
reinterpret_cast< const long long*
>(
value() );
617 bsonDecimal decimal ;
618 double tempValue = 0.0 ;
619 rc = decimal.fromBsonValue(
value() ) ;
620 uassert( rc,
"Failed to parse decimal from bson value",
622 rc = decimal.toDouble( &tempValue ) ;
623 uassert( rc,
"Failed to parse decimal to double", 0 == rc ) ;
644 bsonDecimal decimal ;
646 rc = decimal.fromBsonValue(
value() ) ;
647 uassert( rc,
"Failed to parse decimal from bson value",
649 rc = decimal.toInt( &tempValue ) ;
650 uassert( rc,
"Failed to parse decimal to int",
672 bsonDecimal decimal ;
673 long long tempValue = 0 ;
674 rc = decimal.fromBsonValue(
value() ) ;
675 uassert( rc,
"Failed to parse decimal from bson value",
677 rc = decimal.toLong( &tempValue ) ;
678 uassert( rc,
"Failed to parse decimal to long",
689 bsonDecimal decimal ;
693 uassert( rc,
"Failed to parse decimal from double",
698 uassert( rc,
"Failed to parse decimal from int",
703 uassert( rc,
"Failed to parse decimal from long",
707 rc = decimal.fromBsonValue(
value() ) ;
708 uassert( rc,
"Failed to parse decimal from bson value",