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);
72 Date_t Date()
const {
return chk(bson::Date).
date(); }
74 double Double()
const {
return chk(NumberDouble).
_numberDouble(); }
75 long long Long()
const {
return chk(NumberLong).
_numberLong(); }
76 bsonDecimal Decimal()
const {
return chk(NumberDecimal).
numberDecimal(); }
77 int Int()
const {
return chk(NumberInt).
_numberInt(); }
78 bool Bool()
const {
return chk(bson::Bool).
boolean(); }
79 vector<BSONElement> Array()
const;
80 bson::OID OID()
const {
return chk(jstOID).
__oid(); }
81 void Null()
const { chk(
isNull()); }
82 void OK()
const { chk(
ok()); }
97 void Val(
long long& v)
const { v = Long(); }
98 void Val(
bool& v)
const { v = Bool(); }
99 void Val(BSONObj& v)
const;
100 void Val(bson::OID& v)
const { v = OID(); }
101 void Val(
int& v)
const { v = Int(); }
102 void Val(
double& v)
const { v = Double(); }
103 void Val(
string& v)
const { v =
String(); }
104 void Val(bsonDecimal& v)
const { v = Decimal(); }
111 string toString(
bool includeFieldName =
true,
bool full=
false)
const;
112 void toString(StringBuilder& s,
bool includeFieldName =
true,
113 bool full=
false)
const;
114 string jsonString( JsonStringFormat format,
115 bool includeFieldNames =
true,
int pretty = 0 )
const;
116 operator string()
const {
return toString(); }
119 BSONType
type()
const {
return (BSONType) (
signed char)*data; }
144 int size(
int maxLen )
const;
158 if (
eoo() )
return "";
164 return (data + fieldNameSize() + 1);
168 return size() - fieldNameSize() - 1;
171 bool isBoolean()
const {
return type() == bson::Bool; }
177 return *
value() ?
true :
false;
180 bool booleanSafe()
const {
return isBoolean() &&
boolean(); }
187 return *
reinterpret_cast< const Date_t*
>(
value() );
204 {
return *
reinterpret_cast< const double*
>(
value() ); }
207 {
return *
reinterpret_cast< const int*
>(
value() ); }
210 {
return *
reinterpret_cast< const long long*
>(
value() ); }
237 {
return *
reinterpret_cast< const bson::OID*
>(
value() ); }
241 return type() == jstNULL;
249 return *
reinterpret_cast< const int*
>(
value() );
253 int objsize()
const {
254 return *
reinterpret_cast< const int*
>(
value() );
271 return type() == bson::String ?
289 BSONObj embeddedObjectUserCheck()
const;
291 BSONObj codeWScopeObject()
const;
297 assert(
type() == BinData );
304 if (binDataType() != ByteArrayDeprecated) {
310 return value() + 5 + 4;
314 BinDataType binDataType()
const {
316 assert(
type() == BinData );
317 unsigned char c = (
value() + 4)[0];
318 return (BinDataType)c;
323 assert(
type() == RegEx);
329 const char *p =
regex();
330 return p + strlen(p) + 1;
335 assert(
type() == Code);
359 const char * rawdata()
const {
return data; }
393 Date_t timestampTime()
const {
394 long long t = ((
int*)(
value() + 4 ))[0];
397 unsigned int timestampInc()
const {
398 return ((
unsigned int*)(
value() ))[0];
401 const char * dbrefNS()
const {
402 uassert( 10063 ,
"not a dbref" ,
type() == DBRef );
406 const bson::OID& dbrefOID()
const {
407 uassert( 10064 ,
"not a dbref" ,
type() == DBRef );
408 const char * start =
value();
409 start += 4 + *
reinterpret_cast< const int*
>( start );
410 return *
reinterpret_cast< const bson::OID*
>( start );
416 if ( x < 0 )
return true;
417 else if ( x > 0 )
return false;
418 return compareElementValues(*
this,other) < 0;
422 explicit BSONElement(
const char *d,
int maxLen) : data(d) {
430 if ( maxLen != -1 ) {
431 int size = (int) strnlen(
fieldName(), maxLen - 1 );
432 massert( 10333 ,
"Invalid field name", size != -1 );
433 fieldNameSize_ = size + 1;
447 string _asCode()
const;
448 OpTime _opTime()
const;
452 mutable int fieldNameSize_;
453 int fieldNameSize()
const {
454 if ( fieldNameSize_ == -1 )
455 fieldNameSize_ = (int)strlen(
fieldName() ) + 1;
456 return fieldNameSize_;
458 mutable int totalSize;
460 friend class BSONObjIterator;
461 friend class BSONObj;
466 ss <<
"field not found, expected type " << t;
468 ss <<
"wrong type for field (" <<
fieldName() <<
") "
469 <<
type() <<
" != " << t;
470 uasserted(13111, ss.str() );
475 uassert(13118,
"unexpected or missing type value in BSON object",
480 inline string _numberDecimalStr()
const ;
485 BSONType t =
type() ;
533 return *
reinterpret_cast< const long long*
>(
value() ) != 0;
537 return *
reinterpret_cast< const double*
>(
value() ) != 0;
539 return *
reinterpret_cast< const int*
>(
value() ) != 0;
586 return *
reinterpret_cast< const int*
>(
value() );
588 return (
double) *
reinterpret_cast< const long long*
>(
value() );
591 bsonDecimal decimal ;
592 double tempValue = 0.0 ;
593 decimal.fromBsonValue(
value() ) ;
594 decimal.toDouble( &tempValue ) ;
614 bsonDecimal decimal ;
616 decimal.fromBsonValue(
value() ) ;
617 decimal.toInt( &tempValue ) ;
637 bsonDecimal decimal ;
638 long long tempValue = 0 ;
639 decimal.fromBsonValue(
value() ) ;
640 decimal.toLong( &tempValue ) ;
649 bsonDecimal decimal ;
661 decimal.fromBsonValue(
value() ) ;