Skip to content
Browse files

ran astyle SERVER-2304

  • Loading branch information...
1 parent 4315a90 commit b828d21630d8715fff5a30c682a51ab79880093d @erh erh committed
Showing with 1,930 additions and 1,886 deletions.
  1. +51 −49 bson/bson-inl.h
  2. +13 −13 bson/bson.h
  3. +10 −10 bson/bson_db.h
  4. +10 −11 bson/bsondemo/bsondemo.cpp
  5. +43 −42 bson/bsonelement.h
  6. +35 −35 bson/bsonmisc.h
  7. +60 −60 bson/bsonobj.h
  8. +95 −93 bson/bsonobjbuilder.h
  9. +29 −29 bson/bsonobjiterator.h
  10. +56 −56 bson/bsontypes.h
  11. +1 −1 bson/inline_decls.h
  12. +13 −13 bson/oid.cpp
  13. +13 −13 bson/oid.h
  14. +5 −5 bson/ordering.h
  15. +6 −6 bson/stringdata.h
  16. +16 −16 bson/util/atomic_int.h
  17. +28 −27 bson/util/builder.h
  18. +2 −2 bson/util/misc.h
  19. +7 −7 client/clientOnly.cpp
  20. +52 −52 client/connpool.cpp
  21. +33 −33 client/connpool.h
  22. +10 −10 client/constants.h
  23. +139 −138 client/dbclient.cpp
  24. +127 −127 client/dbclient.h
  25. +92 −92 client/dbclient_rs.cpp
  26. +35 −35 client/dbclient_rs.h
  27. +32 −31 client/dbclientcursor.cpp
  28. +52 −52 client/dbclientcursor.h
  29. +1 −1 client/dbclientmockcursor.h
  30. +60 −58 client/distlock.cpp
  31. +9 −9 client/distlock.h
  32. +13 −13 client/distlock_test.cpp
  33. +4 −3 client/examples/authTest.cpp
  34. +16 −12 client/examples/clientTest.cpp
  35. +6 −5 client/examples/first.cpp
  36. +3 −3 client/examples/httpClientTest.cpp
  37. +8 −8 client/examples/rs.cpp
  38. +1 −1 client/examples/second.cpp
  39. +20 −20 client/examples/tail.cpp
  40. +32 −32 client/examples/tutorial.cpp
  41. +4 −4 client/examples/whereExample.cpp
  42. +31 −29 client/gridfs.cpp
  43. +11 −11 client/gridfs.h
  44. +22 −22 client/model.cpp
  45. +4 −4 client/model.h
  46. +5 −5 client/mongo_client_lib.cpp
  47. +121 −120 client/parallel.cpp
  48. +42 −42 client/parallel.h
  49. +11 −11 client/simple_client_demo.cpp
  50. +79 −79 client/syncclusterconnection.cpp
  51. +18 −18 client/syncclusterconnection.h
  52. +6 −6 db/background.h
  53. +205 −183 db/btree.cpp
  54. +54 −53 db/btree.h
  55. +48 −48 db/btreecursor.cpp
  56. +31 −28 db/cap.cpp
Sorry, we could not display the entire diff because too many files (407) changed.
View
100 bson/bson-inl.h
@@ -24,7 +24,7 @@
namespace mongo {
- inline BSONObjIterator BSONObj::begin() {
+ inline BSONObjIterator BSONObj::begin() {
return BSONObjIterator(*this);
}
@@ -48,7 +48,7 @@ namespace mongo {
return BSONObj( value() + 4 + 4 + strSizeWNull );
}
- inline NOINLINE_DECL void BSONObj::_assertInvalid() const {
+ inline NOINLINE_DECL void BSONObj::_assertInvalid() const {
StringBuilder ss;
int os = objsize();
ss << "Invalid BSONObj size: " << os << " (0x" << toHex( &os, 4 ) << ')';
@@ -59,8 +59,8 @@ namespace mongo {
catch ( ... ) { }
massert( 10334 , ss.str() , 0 );
}
-
- /* the idea with NOINLINE_DECL here is to keep this from inlining in the
+
+ /* the idea with NOINLINE_DECL here is to keep this from inlining in the
getOwned() method. the presumption being that is better.
*/
inline NOINLINE_DECL BSONObj BSONObj::copy() const {
@@ -121,14 +121,14 @@ namespace mongo {
return *this;
}
- inline bool BSONObj::isValid(){
+ inline bool BSONObj::isValid() {
int x = objsize();
return x > 0 && x <= BSONObjMaxInternalSize;
}
- inline bool BSONObj::getObjectID(BSONElement& e) const {
+ inline bool BSONObj::getObjectID(BSONElement& e) const {
BSONElement f = getField("_id");
- if( !f.eoo() ) {
+ if( !f.eoo() ) {
e = f;
return true;
}
@@ -139,21 +139,21 @@ namespace mongo {
_fieldName = 0;
_builder = builder;
}
-
- template<class T>
- inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<( T value ) {
+
+ template<class T>
+ inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<( T value ) {
_builder->append(_fieldName, value);
_fieldName = 0;
return *_builder;
}
- inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<( const BSONElement& e ) {
+ inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<( const BSONElement& e ) {
_builder->appendAs( e , _fieldName );
_fieldName = 0;
return *_builder;
}
- inline Labeler BSONObjBuilderValueStream::operator<<( const Labeler::Label &l ) {
+ inline Labeler BSONObjBuilderValueStream::operator<<( const Labeler::Label &l ) {
return Labeler( l, this );
}
@@ -163,29 +163,29 @@ namespace mongo {
}
_subobj.reset();
_fieldName = nextFieldName;
- }
+ }
inline BSONObjBuilder *BSONObjBuilderValueStream::subobj() {
if ( !haveSubobj() )
_subobj.reset( new BSONObjBuilder() );
return _subobj.get();
}
-
+
template<class T> inline
BSONObjBuilder& Labeler::operator<<( T value ) {
s_->subobj()->append( l_.l_, value );
return *s_->_builder;
- }
+ }
inline
BSONObjBuilder& Labeler::operator<<( const BSONElement& e ) {
s_->subobj()->appendAs( e, l_.l_ );
return *s_->_builder;
- }
+ }
// {a: {b:1}} -> {a.b:1}
void nested2dotted(BSONObjBuilder& b, const BSONObj& obj, const string& base="");
- inline BSONObj nested2dotted(const BSONObj& obj){
+ inline BSONObj nested2dotted(const BSONObj& obj) {
BSONObjBuilder b;
nested2dotted(b, obj);
return b.obj();
@@ -193,7 +193,7 @@ namespace mongo {
// {a.b:1} -> {a: {b:1}}
void dotted2nested(BSONObjBuilder& b, const BSONObj& obj);
- inline BSONObj dotted2nested(const BSONObj& obj){
+ inline BSONObj dotted2nested(const BSONObj& obj) {
BSONObjBuilder b;
dotted2nested(b, obj);
return b.obj();
@@ -204,17 +204,17 @@ namespace mongo {
const char * e = _b.buf() + _b.len();
return BSONObjIterator( s , e );
}
-
+
/* WARNING: nested/dotted conversions are not 100% reversible
* nested2dotted(dotted2nested({a.b: {c:1}})) -> {a.b.c: 1}
* also, dotted2nested ignores order
*/
typedef map<string, BSONElement> BSONMap;
- inline BSONMap bson2map(const BSONObj& obj){
+ inline BSONMap bson2map(const BSONObj& obj) {
BSONMap m;
BSONObjIterator it(obj);
- while (it.more()){
+ while (it.more()) {
BSONElement e = it.next();
m[e.fieldName()] = e;
}
@@ -228,7 +228,7 @@ namespace mongo {
};
typedef set<BSONElement, BSONElementFieldNameCmp> BSONSortedElements;
- inline BSONSortedElements bson2set( const BSONObj& obj ){
+ inline BSONSortedElements bson2set( const BSONObj& obj ) {
BSONSortedElements s;
BSONObjIterator it(obj);
while ( it.more() )
@@ -243,7 +243,7 @@ namespace mongo {
return s.str();
}
inline void BSONObj::toString(StringBuilder& s, bool isArray, bool full ) const {
- if ( isEmpty() ){
+ if ( isEmpty() ) {
s << "{}";
return;
}
@@ -258,7 +258,7 @@ namespace mongo {
massert( 10329 , "Element too large", e.size() < ( 1 << 30 ) );
int offset = (int) (e.rawdata() - this->objdata());
massert( 10330 , "Element extends past end of object",
- e.size() + offset <= this->objsize() );
+ e.size() + offset <= this->objsize() );
e.validate();
bool end = ( e.size() + offset == this->objsize() );
if ( e.eoo() ) {
@@ -278,7 +278,7 @@ namespace mongo {
inline void BSONElement::validate() const {
const BSONType t = type();
-
+
switch( t ) {
case DBRef:
case Code:
@@ -290,7 +290,7 @@ namespace mongo {
return;
StringBuilder buf;
buf << "Invalid dbref/code/string/symbol size: " << x;
- if( lenOk )
+ if( lenOk )
buf << " strnlen:" << mongo::strnlen( valuestr() , x );
msgasserted( 10321 , buf.str() );
break;
@@ -368,8 +368,7 @@ namespace mongo {
massert( 10317 , "Insufficient bytes to calculate element size", maxLen == -1 || remain > 3 );
x = valuestrsize() + 4 + 1/*subtype*/;
break;
- case RegEx:
- {
+ case RegEx: {
const char *p = value();
size_t len1 = ( maxLen == -1 ) ? strlen( p ) : mongo::strnlen( p, remain );
//massert( 10318 , "Invalid regex string", len1 != -1 ); // ERH - 4/28/10 - don't think this does anything
@@ -413,13 +412,12 @@ namespace mongo {
case mongo::Date:
s << "new Date(" << date() << ')';
break;
- case RegEx:
- {
- s << "/" << regex() << '/';
- const char *p = regexFlags();
- if ( p ) s << p;
- }
- break;
+ case RegEx: {
+ s << "/" << regex() << '/';
+ const char *p = regexFlags();
+ if ( p ) s << p;
+ }
+ break;
case NumberDouble:
s.appendDoubleNice( number() );
break;
@@ -452,13 +450,14 @@ namespace mongo {
break;
case CodeWScope:
s << "CodeWScope( "
- << codeWScopeCode() << ", " << codeWScopeObject().toString(false, full) << ")";
+ << codeWScopeCode() << ", " << codeWScopeObject().toString(false, full) << ")";
break;
case Code:
if ( !full && valuestrsize() > 80 ) {
s.write(valuestr(), 70);
s << "...";
- } else {
+ }
+ else {
s.write(valuestr(), valuestrsize()-1);
}
break;
@@ -468,7 +467,8 @@ namespace mongo {
if ( !full && valuestrsize() > 80 ) {
s.write(valuestr(), 70);
s << "...\"";
- } else {
+ }
+ else {
s.write(valuestr(), valuestrsize()-1);
s << '"';
}
@@ -486,7 +486,7 @@ namespace mongo {
break;
case BinData:
s << "BinData";
- if (full){
+ if (full) {
int len;
const char* data = binDataClean(len);
s << '(' << binDataType() << ", " << toHex(data, len) << ')';
@@ -537,8 +537,8 @@ namespace mongo {
}
inline BSONObj::BSONObj() {
- /* little endian ordering here, but perhaps that is ok regardless as BSON is spec'd
- to be little endian external to the system. (i.e. the rest of the implementation of bson,
+ /* little endian ordering here, but perhaps that is ok regardless as BSON is spec'd
+ to be little endian external to the system. (i.e. the rest of the implementation of bson,
not this part, fails to support big endian)
*/
static char p[] = { /*size*/5, 0, 0, 0, /*eoo*/0 };
@@ -547,7 +547,7 @@ namespace mongo {
inline BSONObj BSONElement::Obj() const { return embeddedObjectUserCheck(); }
- inline BSONElement BSONElement::operator[] (const string& field) const {
+ inline BSONElement BSONElement::operator[] (const string& field) const {
BSONObj o = Obj();
return o[field];
}
@@ -558,14 +558,14 @@ namespace mongo {
v.push_back(i.next());
}
- inline void BSONObj::elems(list<BSONElement> &v) const {
+ inline void BSONObj::elems(list<BSONElement> &v) const {
BSONObjIterator i(*this);
while( i.more() )
v.push_back(i.next());
}
template <class T>
- void BSONObj::Vals(vector<T>& v) const {
+ void BSONObj::Vals(vector<T>& v) const {
BSONObjIterator i(*this);
while( i.more() ) {
T t;
@@ -574,7 +574,7 @@ namespace mongo {
}
}
template <class T>
- void BSONObj::Vals(list<T>& v) const {
+ void BSONObj::Vals(list<T>& v) const {
BSONObjIterator i(*this);
while( i.more() ) {
T t;
@@ -584,25 +584,27 @@ namespace mongo {
}
template <class T>
- void BSONObj::vals(vector<T>& v) const {
+ void BSONObj::vals(vector<T>& v) const {
BSONObjIterator i(*this);
while( i.more() ) {
try {
T t;
i.next().Val(t);
v.push_back(t);
- } catch(...) { }
+ }
+ catch(...) { }
}
}
template <class T>
- void BSONObj::vals(list<T>& v) const {
+ void BSONObj::vals(list<T>& v) const {
BSONObjIterator i(*this);
while( i.more() ) {
try {
T t;
i.next().Val(t);
v.push_back(t);
- } catch(...) { }
+ }
+ catch(...) { }
}
}
View
26 bson/bson.h
@@ -1,10 +1,10 @@
-/* NOTE: Standalone bson header for when not using MongoDB.
+/* NOTE: Standalone bson header for when not using MongoDB.
See also: bsondemo.
MongoDB includes ../db/jsobj.h instead. This file, however, pulls in much less code / dependencies.
*/
-/** @file bson.h
+/** @file bson.h
BSON classes
*/
@@ -47,15 +47,15 @@
#include <boost/utility.hpp>
#include "util/builder.h"
-namespace bson {
+namespace bson {
using std::string;
using std::stringstream;
- class assertion : public std::exception {
+ class assertion : public std::exception {
public:
assertion( unsigned u , const string& s )
- : id( u ) , msg( s ){
+ : id( u ) , msg( s ) {
mongo::StringBuilder ss;
ss << "BsonAssertion id: " << u << " " << s;
full = ss.str();
@@ -64,7 +64,7 @@ namespace bson {
virtual ~assertion() throw() {}
virtual const char* what() const throw() { return full.c_str(); }
-
+
unsigned id;
string msg;
string full;
@@ -72,9 +72,9 @@ namespace bson {
}
namespace mongo {
-#if !defined(assert)
+#if !defined(assert)
inline void assert(bool expr) {
- if(!expr) {
+ if(!expr) {
throw bson::assertion( 0 , "assertion failure in bson library" );
}
}
@@ -88,12 +88,12 @@ namespace mongo {
if( !expr )
uasserted( msgid , msg );
}
- inline void msgasserted(int msgid, const char *msg) {
+ inline void msgasserted(int msgid, const char *msg) {
throw bson::assertion( msgid , msg );
}
inline void msgasserted(int msgid, const std::string &msg) { msgasserted(msgid, msg.c_str()); }
- inline void massert(unsigned msgid, std::string msg, bool expr) {
- if(!expr) {
+ inline void massert(unsigned msgid, std::string msg, bool expr) {
+ if(!expr) {
std::cout << "assertion failure in bson library: " << msgid << ' ' << msg << std::endl;
throw bson::assertion( msgid , msg );
}
@@ -110,13 +110,13 @@ namespace mongo {
#include "../bson/bsonobjiterator.h"
#include "../bson/bson-inl.h"
-namespace mongo {
+namespace mongo {
inline unsigned getRandomNumber() {
#if defined(_WIN32)
return rand();
#else
- return random();
+ return random();
#endif
}
View
20 bson/bson_db.h
@@ -1,10 +1,10 @@
-/** @file bson_db.h
+/** @file bson_db.h
- This file contains the implementation of BSON-related methods that are required
+ This file contains the implementation of BSON-related methods that are required
by the MongoDB database server.
- Normally, for standalone BSON usage, you do not want this file - it will tend to
- pull in some other files from the MongoDB project. Thus, bson.h (the main file
+ Normally, for standalone BSON usage, you do not want this file - it will tend to
+ pull in some other files from the MongoDB project. Thus, bson.h (the main file
one would use) does not include this file.
*/
@@ -35,10 +35,10 @@ namespace mongo {
Append a timestamp element to the object being ebuilt.
@param time - in millis (but stored in seconds)
*/
- inline BSONObjBuilder& BSONObjBuilder::appendTimestamp( const StringData& fieldName , unsigned long long time , unsigned int inc ){
+ inline BSONObjBuilder& BSONObjBuilder::appendTimestamp( const StringData& fieldName , unsigned long long time , unsigned int inc ) {
OpTime t( (unsigned) (time / 1000) , inc );
appendTimestamp( fieldName , t.asDate() );
- return *this;
+ return *this;
}
inline OpTime BSONElement::_opTime() const {
@@ -48,7 +48,7 @@ namespace mongo {
}
inline string BSONElement::_asCode() const {
- switch( type() ){
+ switch( type() ) {
case mongo::String:
case Code:
return string(valuestr(), valuestrsize()-1);
@@ -61,19 +61,19 @@ namespace mongo {
return "";
}
- inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(DateNowLabeler& id){
+ inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(DateNowLabeler& id) {
_builder->appendDate(_fieldName, jsTime());
_fieldName = 0;
return *_builder;
}
- inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(MinKeyLabeler& id){
+ inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(MinKeyLabeler& id) {
_builder->appendMinKey(_fieldName);
_fieldName = 0;
return *_builder;
}
- inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(MaxKeyLabeler& id){
+ inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(MaxKeyLabeler& id) {
_builder->appendMaxKey(_fieldName);
_fieldName = 0;
return *_builder;
View
21 bson/bsondemo/bsondemo.cpp 100755 → 100644
@@ -1,4 +1,4 @@
-/** @file bsondemo.cpp
+/** @file bsondemo.cpp
Example of use of BSON from C++.
@@ -29,16 +29,15 @@
using namespace std;
using namespace bson;
-void iter(bo o) {
+void iter(bo o) {
/* iterator example */
cout << "\niter()\n";
- for( bo::iterator i(o); i.more(); ) {
+ for( bo::iterator i(o); i.more(); ) {
cout << ' ' << i.next().toString() << '\n';
}
}
-int main()
-{
+int main() {
cout << "build bits: " << 8 * sizeof(char *) << '\n' << endl;
/* a bson object defaults on construction to { } */
@@ -47,7 +46,7 @@ int main()
/* make a simple { name : 'joe', age : 33.7 } object */
{
- bob b;
+ bob b;
b.append("name", "joe");
b.append("age", 33.7);
b.obj();
@@ -73,7 +72,7 @@ int main()
/* reach in and get subobj.z */
cout << "subobj.z: " << y.getFieldDotted("subobj.z").Number() << endl;
-
+
/* alternate syntax: */
cout << "subobj.z: " << y["subobj"]["z"].Number() << endl;
@@ -83,19 +82,19 @@ int main()
cout << v[0] << endl;
/* into an array */
- list<be> L;
+ list<be> L;
y.elems(L);
bo sub = y["subobj"].Obj();
- /* grab all the int's that were in subobj. if it had elements that were not ints, we throw an exception
- (capital V on Vals() means exception if wrong type found
+ /* grab all the int's that were in subobj. if it had elements that were not ints, we throw an exception
+ (capital V on Vals() means exception if wrong type found
*/
vector<int> myints;
sub.Vals(myints);
cout << "my ints: " << myints[0] << ' ' << myints[1] << endl;
- /* grab all the string values from x. if the field isn't of string type, just skip it --
+ /* grab all the string values from x. if the field isn't of string type, just skip it --
lowercase v on vals() indicates skip don't throw.
*/
vector<string> strs;
View
85 bson/bsonelement.h
@@ -38,7 +38,7 @@ namespace mongo {
/** BSONElement represents an "element" in a BSONObj. So for the object { a : 3, b : "abc" },
'a : 3' is the first element (key+value).
-
+
The BSONElement object points into the BSONObj's data. Thus the BSONObj must stay in scope
for the life of the BSONElement.
@@ -51,7 +51,7 @@ namespace mongo {
*/
class BSONElement {
public:
- /** These functions, which start with a capital letter, throw a UserException if the
+ /** These functions, which start with a capital letter, throw a UserException if the
element is not of the required type. Example:
string foo = obj["foo"].String(); // exception if not a string type or DNE
@@ -69,7 +69,7 @@ namespace mongo {
void Null() const { chk(isNull()); }
void OK() const { chk(ok()); }
- /** populate v with the value of the element. If type does not match, throw exception.
+ /** populate v with the value of the element. If type does not match, throw exception.
useful in templates -- see also BSONObj::Vals().
*/
void Val(Date_t& v) const { v = Date(); }
@@ -94,24 +94,24 @@ namespace mongo {
/** Returns the type of the element */
BSONType type() const { return (BSONType) *data; }
- /** retrieve a field within this element
+ /** retrieve a field within this element
throws exception if *this is not an embedded object
*/
BSONElement operator[] (const string& field) const;
-
+
/** returns the tyoe of the element fixed for the main type
the main purpose is numbers. any numeric type will return NumberDouble
Note: if the order changes, indexes have to be re-built or than can be corruption
*/
int canonicalType() const;
- /** Indicates if it is the end-of-object element, which is present at the end of
+ /** Indicates if it is the end-of-object element, which is present at the end of
every BSON object.
*/
bool eoo() const { return type() == EOO; }
/** Size of the element.
- @param maxLen If maxLen is specified, don't scan more than maxLen bytes to calculate size.
+ @param maxLen If maxLen is specified, don't scan more than maxLen bytes to calculate size.
*/
int size( int maxLen = -1 ) const;
@@ -121,7 +121,7 @@ namespace mongo {
/** Wrap this element up as a singleton object with a new name. */
BSONObj wrap( const char* newName) const;
- /** field name of the element. e.g., for
+ /** field name of the element. e.g., for
name : "Joe"
"name" is the fieldname
*/
@@ -141,21 +141,21 @@ namespace mongo {
bool isBoolean() const { return type() == mongo::Bool; }
- /** @return value of a boolean element.
- You must assure element is a boolean before
+ /** @return value of a boolean element.
+ You must assure element is a boolean before
calling. */
bool boolean() const {
return *value() ? true : false;
}
- /** Retrieve a java style date value from the element.
+ /** Retrieve a java style date value from the element.
Ensure element is of type Date before calling.
*/
Date_t date() const {
return *reinterpret_cast< const Date_t* >( value() );
}
- /** Convert the value to boolean, regardless of its type, in a javascript-like fashion
+ /** Convert the value to boolean, regardless of its type, in a javascript-like fashion
(i.e., treat zero and null as false).
*/
bool trueValue() const;
@@ -177,16 +177,16 @@ namespace mongo {
int numberInt() const;
/** Retrieve long value for the element safely. Zero returned if not a number. */
long long numberLong() const;
- /** Retrieve the numeric value of the element. If not of a numeric type, returns 0.
+ /** Retrieve the numeric value of the element. If not of a numeric type, returns 0.
Note: casts to double, data loss may occur with large (>52 bit) NumberLong values.
*/
double numberDouble() const;
- /** Retrieve the numeric value of the element. If not of a numeric type, returns 0.
+ /** Retrieve the numeric value of the element. If not of a numeric type, returns 0.
Note: casts to double, data loss may occur with large (>52 bit) NumberLong values.
*/
double number() const { return numberDouble(); }
- /** Retrieve the object ID stored in the object.
+ /** Retrieve the object ID stored in the object.
You must ensure the element is of type jstOID first. */
const mongo::OID &__oid() const { return *reinterpret_cast< const mongo::OID* >( value() ); }
@@ -194,8 +194,8 @@ namespace mongo {
bool isNull() const {
return type() == jstNULL;
}
-
- /** Size (length) of a string element.
+
+ /** Size (length) of a string element.
You must assure of type String first. */
int valuestrsize() const {
return *reinterpret_cast< const int* >( value() );
@@ -206,7 +206,7 @@ namespace mongo {
return *reinterpret_cast< const int* >( value() );
}
- /** Get a string's value. Also gives you start of the real data for an embedded object.
+ /** Get a string's value. Also gives you start of the real data for an embedded object.
You must assure data is of an appropriate type first -- see also valuestrsafe().
*/
const char * valuestr() const {
@@ -241,24 +241,25 @@ namespace mongo {
BSONObj codeWScopeObject() const;
/** Get raw binary data. Element must be of type BinData. Doesn't handle type 2 specially */
- const char *binData(int& len) const {
+ const char *binData(int& len) const {
// BinData: <int len> <byte subtype> <byte[len] data>
assert( type() == BinData );
len = valuestrsize();
return value() + 5;
}
/** Get binary data. Element must be of type BinData. Handles type 2 */
- const char *binDataClean(int& len) const {
+ const char *binDataClean(int& len) const {
// BinData: <int len> <byte subtype> <byte[len] data>
- if (binDataType() != ByteArrayDeprecated){
+ if (binDataType() != ByteArrayDeprecated) {
return binData(len);
- } else {
+ }
+ else {
// Skip extra size
len = valuestrsize() - 4;
return value() + 5 + 4;
}
}
-
+
BinDataType binDataType() const {
// BinData: <int len> <byte subtype> <byte[len] data>
assert( type() == BinData );
@@ -298,19 +299,19 @@ namespace mongo {
int woCompare( const BSONElement &e, bool considerFieldName = true ) const;
const char * rawdata() const { return data; }
-
+
/** 0 == Equality, just not defined yet */
int getGtLtOp( int def = 0 ) const;
/** Constructs an empty element */
BSONElement();
-
+
/** Check that data is internally consistent. */
void validate() const;
/** True if this element may contain subobjects. */
bool mayEncapsulate() const {
- switch ( type() ){
+ switch ( type() ) {
case Object:
case mongo::Array:
case CodeWScope:
@@ -322,7 +323,7 @@ namespace mongo {
/** True if this element can be a BSONObj */
bool isABSONObj() const {
- switch( type() ){
+ switch( type() ) {
case Object:
case mongo::Array:
return true;
@@ -331,11 +332,11 @@ namespace mongo {
}
}
- Date_t timestampTime() const{
+ Date_t timestampTime() const {
unsigned long long t = ((unsigned int*)(value() + 4 ))[0];
return t * 1000;
}
- unsigned int timestampInc() const{
+ unsigned int timestampInc() const {
return ((unsigned int*)(value() ))[0];
}
@@ -357,7 +358,7 @@ namespace mongo {
else if ( x > 0 ) return false;
return compareElementValues(*this,other) < 0;
}
-
+
// If maxLen is specified, don't scan more than maxLen bytes.
explicit BSONElement(const char *d, int maxLen = -1) : data(d) {
fieldNameSize_ = -1;
@@ -388,15 +389,15 @@ namespace mongo {
friend class BSONObjIterator;
friend class BSONObj;
- const BSONElement& chk(int t) const {
- if ( t != type() ){
+ const BSONElement& chk(int t) const {
+ if ( t != type() ) {
StringBuilder ss;
ss << "wrong type for BSONElement (" << fieldName() << ") " << type() << " != " << t;
uasserted(13111, ss.str() );
}
return *this;
}
- const BSONElement& chk(bool expr) const {
+ const BSONElement& chk(bool expr) const {
uassert(13118, "unexpected or missing type value in BSON object", expr);
return *this;
}
@@ -405,7 +406,7 @@ namespace mongo {
inline int BSONElement::canonicalType() const {
BSONType t = type();
- switch ( t ){
+ switch ( t ) {
case MinKey:
case MaxKey:
return t;
@@ -446,7 +447,7 @@ namespace mongo {
assert(0);
return -1;
}
- }
+ }
inline bool BSONElement::trueValue() const {
switch( type() ) {
@@ -462,7 +463,7 @@ namespace mongo {
case jstNULL:
case Undefined:
return false;
-
+
default:
;
}
@@ -476,13 +477,13 @@ namespace mongo {
case NumberDouble:
case NumberInt:
return true;
- default:
+ default:
return false;
}
}
inline bool BSONElement::isSimpleType() const {
- switch( type() ){
+ switch( type() ) {
case NumberLong:
case NumberDouble:
case NumberInt:
@@ -491,7 +492,7 @@ namespace mongo {
case mongo::Date:
case jstOID:
return true;
- default:
+ default:
return false;
}
}
@@ -510,7 +511,7 @@ namespace mongo {
}
/** Retrieve int value for the element safely. Zero returned if not a number. Converted to int if another numeric type. */
- inline int BSONElement::numberInt() const {
+ inline int BSONElement::numberInt() const {
switch( type() ) {
case NumberDouble:
return (int) _numberDouble();
@@ -524,7 +525,7 @@ namespace mongo {
}
/** Retrieve long value for the element safely. Zero returned if not a number. */
- inline long long BSONElement::numberLong() const {
+ inline long long BSONElement::numberLong() const {
switch( type() ) {
case NumberDouble:
return (long long) _numberDouble();
@@ -535,7 +536,7 @@ namespace mongo {
default:
return 0;
}
- }
+ }
inline BSONElement::BSONElement() {
static char z = 0;
View
70 bson/bsonmisc.h
@@ -26,7 +26,7 @@ namespace mongo {
return l.woCompare( r, false ) < 0;
}
};
-
+
class BSONObjCmp {
public:
BSONObjCmp( const BSONObj &_order = BSONObj() ) : order( _order ) {}
@@ -54,26 +54,26 @@ namespace mongo {
FieldCompareResult compareDottedFieldNames( const string& l , const string& r );
-/** Use BSON macro to build a BSONObj from a stream
+ /** Use BSON macro to build a BSONObj from a stream
+
+ e.g.,
+ BSON( "name" << "joe" << "age" << 33 )
- e.g.,
- BSON( "name" << "joe" << "age" << 33 )
+ with auto-generated object id:
+ BSON( GENOID << "name" << "joe" << "age" << 33 )
- with auto-generated object id:
- BSON( GENOID << "name" << "joe" << "age" << 33 )
-
- The labels GT, GTE, LT, LTE, NE can be helpful for stream-oriented construction
- of a BSONObj, particularly when assembling a Query. For example,
- BSON( "a" << GT << 23.4 << NE << 30 << "b" << 2 ) produces the object
- { a: { \$gt: 23.4, \$ne: 30 }, b: 2 }.
-*/
+ The labels GT, GTE, LT, LTE, NE can be helpful for stream-oriented construction
+ of a BSONObj, particularly when assembling a Query. For example,
+ BSON( "a" << GT << 23.4 << NE << 30 << "b" << 2 ) produces the object
+ { a: { \$gt: 23.4, \$ne: 30 }, b: 2 }.
+ */
#define BSON(x) (( mongo::BSONObjBuilder(64) << x ).obj())
-/** Use BSON_ARRAY macro like BSON macro, but without keys
+ /** Use BSON_ARRAY macro like BSON macro, but without keys
- BSONArray arr = BSON_ARRAY( "hello" << 1 << BSON( "foo" << BSON_ARRAY( "bar" << "baz" << "qux" ) ) );
+ BSONArray arr = BSON_ARRAY( "hello" << 1 << BSON( "foo" << BSON_ARRAY( "bar" << "baz" << "qux" ) ) );
- */
+ */
#define BSON_ARRAY(x) (( mongo::BSONArrayBuilder() << x ).arr())
/* Utility class to auto assign object IDs.
@@ -83,14 +83,14 @@ namespace mongo {
extern struct GENOIDLabeler { } GENOID;
/* Utility class to add a Date element with the current time
- Example:
+ Example:
cout << BSON( "created" << DATENOW ); // { created : "2009-10-09 11:41:42" }
*/
extern struct DateNowLabeler { } DATENOW;
/* Utility class to add the minKey (minus infinity) to a given attribute
Example:
- cout << BSON( "a" << MINKEY ); // { "a" : { "$minKey" : 1 } }
+ cout << BSON( "a" << MINKEY ); // { "a" : { "$minKey" : 1 } }
*/
extern struct MinKeyLabeler { } MINKEY;
extern struct MaxKeyLabeler { } MAXKEY;
@@ -106,17 +106,17 @@ namespace mongo {
template<class T>
BSONObjBuilder& operator<<( T value );
- /* the value of the element e is appended i.e. for
+ /* the value of the element e is appended i.e. for
"age" << GT << someElement
- one gets
- { age : { $gt : someElement's value } }
+ one gets
+ { age : { $gt : someElement's value } }
*/
BSONObjBuilder& operator<<( const BSONElement& e );
private:
const Label &l_;
BSONObjBuilderValueStream *s_;
};
-
+
extern Labeler::Label GT;
extern Labeler::Label GTE;
extern Labeler::Label LT;
@@ -133,7 +133,7 @@ namespace mongo {
inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e);
inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e, const BSONObj& f);
// definitions in bsonobjbuilder.h b/c of incomplete types
-
+
// Utility class to implement BSON( key << val ) as described above.
class BSONObjBuilderValueStream : public boost::noncopyable {
public:
@@ -141,20 +141,20 @@ namespace mongo {
BSONObjBuilderValueStream( BSONObjBuilder * builder );
BSONObjBuilder& operator<<( const BSONElement& e );
-
- template<class T>
+
+ template<class T>
BSONObjBuilder& operator<<( T value );
BSONObjBuilder& operator<<(DateNowLabeler& id);
BSONObjBuilder& operator<<(MinKeyLabeler& id);
BSONObjBuilder& operator<<(MaxKeyLabeler& id);
-
+
Labeler operator<<( const Labeler::Label &l );
void endField( const char *nextFieldName = 0 );
bool subobjStarted() const { return _fieldName != 0; }
-
+
private:
const char * _fieldName;
BSONObjBuilder * _builder;
@@ -163,39 +163,39 @@ namespace mongo {
BSONObjBuilder *subobj();
auto_ptr< BSONObjBuilder > _subobj;
};
-
+
/**
used in conjuction with BSONObjBuilder, allows for proper buffer size to prevent crazy memory usage
*/
class BSONSizeTracker {
public:
- BSONSizeTracker(){
+ BSONSizeTracker() {
_pos = 0;
for ( int i=0; i<SIZE; i++ )
_sizes[i] = 512; // this is the default, so just be consistent
}
-
- ~BSONSizeTracker(){
+
+ ~BSONSizeTracker() {
}
-
- void got( int size ){
+
+ void got( int size ) {
_sizes[_pos++] = size;
if ( _pos >= SIZE )
_pos = 0;
}
-
+
/**
* right now choosing largest size
*/
int getSize() const {
int x = 16; // sane min
- for ( int i=0; i<SIZE; i++ ){
+ for ( int i=0; i<SIZE; i++ ) {
if ( _sizes[i] > x )
x = _sizes[i];
}
return x;
}
-
+
private:
enum { SIZE = 10 };
int _pos;
View
120 bson/bsonobj.h
@@ -28,23 +28,23 @@ namespace mongo {
typedef set< BSONElement, BSONElementCmpWithoutField > BSONElementSet;
/**
- C++ representation of a "BSON" object -- that is, an extended JSON-style
+ C++ representation of a "BSON" object -- that is, an extended JSON-style
object in a binary representation.
See bsonspec.org.
- Note that BSONObj's have a smart pointer capability built in -- so you can
+ Note that BSONObj's have a smart pointer capability built in -- so you can
pass them around by value. The reference counts used to implement this
do not use locking, so copying and destroying BSONObj's are not thread-safe
operations.
BSON object format:
-
+
code
<unsigned totalSize> {<byte BSONType><cstring FieldName><Data>}* EOO
-
+
totalSize includes itself.
-
+
Data:
Bool: <byte>
EOO: nothing follows
@@ -67,11 +67,11 @@ namespace mongo {
*/
class BSONObj {
public:
-
- /** Construct a BSONObj from data in the proper format.
- @param ifree true if the BSONObj should free() the msgdata when
- it destructs.
- */
+
+ /** Construct a BSONObj from data in the proper format.
+ @param ifree true if the BSONObj should free() the msgdata when
+ it destructs.
+ */
explicit BSONObj(const char *msgdata, bool ifree = false) {
init(msgdata, ifree);
}
@@ -84,24 +84,24 @@ namespace mongo {
~BSONObj() { /*defensive:*/ _objdata = 0; }
/**
- A BSONObj can use a buffer it "owns" or one it does not.
-
+ A BSONObj can use a buffer it "owns" or one it does not.
+
OWNED CASE
- If the BSONObj owns the buffer, the buffer can be shared among several BSONObj's (by assignment).
+ If the BSONObj owns the buffer, the buffer can be shared among several BSONObj's (by assignment).
In this case the buffer is basically implemented as a shared_ptr.
Since BSONObj's are typically immutable, this works well.
UNOWNED CASE
A BSONObj can also point to BSON data in some other data structure it does not "own" or free later.
- For example, in a memory mapped file. In this case, it is important the original data stays in
- scope for as long as the BSONObj is in use. If you think the original data may go out of scope,
- call BSONObj::getOwned() to promote your BSONObj to having its own copy.
+ For example, in a memory mapped file. In this case, it is important the original data stays in
+ scope for as long as the BSONObj is in use. If you think the original data may go out of scope,
+ call BSONObj::getOwned() to promote your BSONObj to having its own copy.
- On a BSONObj assignment, if the source is unowned, both the source and dest will have unowned
+ On a BSONObj assignment, if the source is unowned, both the source and dest will have unowned
pointers to the original buffer after the assignment.
-
- If you are not sure about ownership but need the buffer to last as long as the BSONObj, call
- getOwned(). getOwned() is a no-op if the buffer is already owned. If not already owned, a malloc
+
+ If you are not sure about ownership but need the buffer to last as long as the BSONObj, call
+ getOwned(). getOwned() is a no-op if the buffer is already owned. If not already owned, a malloc
and memcpy will result.
Most ways to create BSONObj's create 'owned' variants. Unowned versions can be created with:
@@ -119,13 +119,13 @@ namespace mongo {
/** @return a new full (and owned) copy of the object. */
BSONObj copy() const;
- /** Readable representation of a BSON object in an extended JSON-style notation.
+ /** Readable representation of a BSON object in an extended JSON-style notation.
This is an abbreviated representation which might be used for logging.
*/
string toString( bool isArray = false, bool full=false ) const;
void toString(StringBuilder& s, bool isArray = false, bool full=false ) const;
-
- /** Properly formatted JSON string.
+
+ /** Properly formatted JSON string.
@param pretty if true we try to add some lf's and indentation
*/
string jsonString( JsonStringFormat format = Strict, int pretty = 0 ) const;
@@ -160,36 +160,36 @@ namespace mongo {
names with respect to the returned element. */
BSONElement getFieldDottedOrArray(const char *&name) const;
- /** Get the field of the specified name. eoo() is true on the returned
- element if not found.
+ /** Get the field of the specified name. eoo() is true on the returned
+ element if not found.
*/
BSONElement getField(const StringData& name) const;
- /** Get the field of the specified name. eoo() is true on the returned
- element if not found.
+ /** Get the field of the specified name. eoo() is true on the returned
+ element if not found.
*/
- BSONElement operator[] (const char *field) const {
+ BSONElement operator[] (const char *field) const {
return getField(field);
}
- BSONElement operator[] (const string& field) const {
+ BSONElement operator[] (const string& field) const {
return getField(field);
}
- BSONElement operator[] (int field) const {
+ BSONElement operator[] (int field) const {
StringBuilder ss;
ss << field;
string s = ss.str();
return getField(s.c_str());
}
- /** @return true if field exists */
+ /** @return true if field exists */
bool hasField( const char * name ) const { return ! getField( name ).eoo(); }
/** @return "" if DNE or wrong type */
const char * getStringField(const char *name) const;
- /** @return subobject of the given name */
+ /** @return subobject of the given name */
BSONObj getObjectField(const char *name) const;
/** @return INT_MIN if not present - does some type conversions */
@@ -204,18 +204,18 @@ namespace mongo {
object.
*/
BSONObj extractFieldsUnDotted(BSONObj pattern) const;
-
+
/** extract items from object which match a pattern object.
- e.g., if pattern is { x : 1, y : 1 }, builds an object with
- x and y elements of this object, if they are present.
+ e.g., if pattern is { x : 1, y : 1 }, builds an object with
+ x and y elements of this object, if they are present.
returns elements with original field names
*/
BSONObj extractFields(const BSONObj &pattern , bool fillWithNull=false) const;
-
+
BSONObj filterFieldsUndotted(const BSONObj &filter, bool inFilter) const;
BSONElement getFieldUsingIndexNames(const char *fieldName, const BSONObj &indexKey) const;
-
+
/** @return the raw data of the object */
const char *objdata() const {
return _objdata;
@@ -231,30 +231,30 @@ namespace mongo {
*/
bool okForStorage() const;
- /** @return true if object is empty -- i.e., {} */
+ /** @return true if object is empty -- i.e., {} */
bool isEmpty() const { return objsize() <= 5; }
void dump() const;
/** Alternative output format */
string hexDump() const;
-
+
/**wo='well ordered'. fields must be in same order in each object.
- Ordering is with respect to the signs of the elements
+ Ordering is with respect to the signs of the elements
and allows ascending / descending key mixing.
- @return <0 if l<r. 0 if l==r. >0 if l>r
+ @return <0 if l<r. 0 if l==r. >0 if l>r
*/
int woCompare(const BSONObj& r, const Ordering &o,
bool considerFieldName=true) const;
/**wo='well ordered'. fields must be in same order in each object.
- Ordering is with respect to the signs of the elements
+ Ordering is with respect to the signs of the elements
and allows ascending / descending key mixing.
- @return <0 if l<r. 0 if l==r. >0 if l>r
+ @return <0 if l<r. 0 if l==r. >0 if l>r
*/
int woCompare(const BSONObj& r, const BSONObj &ordering = BSONObj(),
bool considerFieldName=true) const;
-
+
bool operator<( const BSONObj& other ) const { return woCompare( other ) < 0; }
bool operator<=( const BSONObj& other ) const { return woCompare( other ) <= 0; }
@@ -277,18 +277,18 @@ namespace mongo {
return false;
}
- /** @return first field of the object */
+ /** @return first field of the object */
BSONElement firstElement() const { return BSONElement(objdata() + 4); }
- /** @return true if field exists in the object */
+ /** @return true if field exists in the object */
bool hasElement(const char *name) const;
- /** Get the _id field from the object. For good performance drivers should
- assure that _id is the first element of the object; however, correct operation
+ /** Get the _id field from the object. For good performance drivers should
+ assure that _id is the first element of the object; however, correct operation
is assured regardless.
@return true if found
- */
- bool getObjectID(BSONElement& e) const;
+ */
+ bool getObjectID(BSONElement& e) const;
/** @return A hash code for the object */
int hash() const {
@@ -304,18 +304,18 @@ namespace mongo {
// string identifier equivalents.
// TODO Support conversion of element types other than min and max.
BSONObj clientReadable() const;
-
+
/** Return new object with the field names replaced by those in the
passed object. */
BSONObj replaceFieldNames( const BSONObj &obj ) const;
-
+
/** true unless corrupt */
bool valid() const;
-
+
/** @return an md5 value for this object. */
string md5() const;
-
- bool operator==( const BSONObj& other ) const{
+
+ bool operator==( const BSONObj& other ) const {
return woCompare( other ) == 0;
}
@@ -339,14 +339,14 @@ namespace mongo {
opNEAR = 0x13,
opWITHIN = 0x14,
opMAX_DISTANCE=0x15
- };
+ };
/** add all elements of the object to the specified vector */
void elems(vector<BSONElement> &) const;
/** add all elements of the object to the specified list */
void elems(list<BSONElement> &) const;
- /** add all values of the object to the specified vector. If type mismatches, exception.
+ /** add all values of the object to the specified vector. If type mismatches, exception.
this is most useful when the BSONObj is an array, but can be used with non-arrays too in theory.
example:
@@ -371,7 +371,7 @@ namespace mongo {
typedef BSONObjIterator iterator;
/** use something like this:
- for( BSONObj::iterator i = myObj.begin(); i.more(); ) {
+ for( BSONObj::iterator i = myObj.begin(); i.more(); ) {
BSONElement e = i.next();
...
}
@@ -383,11 +383,11 @@ namespace mongo {
b.appendBuf(reinterpret_cast<const void *>( objdata() ), objsize());
}
-private:
+ private:
class Holder {
public:
Holder( const char *objdata ) :
- _objdata( objdata ) {
+ _objdata( objdata ) {
}
~Holder() {
free((void *)_objdata);
View
188 bson/bsonobjbuilder.h
@@ -36,7 +36,7 @@ namespace mongo {
template<typename T>
class BSONFieldValue {
public:
- BSONFieldValue( const string& name , const T& t ){
+ BSONFieldValue( const string& name , const T& t ) {
_name = name;
_t = t;
}
@@ -52,8 +52,8 @@ namespace mongo {
template<typename T>
class BSONField {
public:
- BSONField( const string& name , const string& longName="" )
- : _name(name), _longName(longName){}
+ BSONField( const string& name , const string& longName="" )
+ : _name(name), _longName(longName) {}
const string& name() const { return _name; }
operator string() const { return _name; }
@@ -65,11 +65,11 @@ namespace mongo {
BSONFieldValue<BSONObj> lt( const T& t ) const { return query( "$lt" , t ); }
BSONFieldValue<BSONObj> query( const char * q , const T& t ) const;
-
+
BSONFieldValue<T> operator()( const T& t ) const {
return BSONFieldValue<T>( _name , t );
}
-
+
private:
string _name;
string _longName;
@@ -90,13 +90,13 @@ namespace mongo {
BSONObjBuilder( BufBuilder &baseBuilder ) : _b( baseBuilder ), _buf( 0 ), _offset( baseBuilder.len() ), _s( this ) , _tracker(0) , _doneCalled(false) {
_b.skip( 4 );
}
-
+
BSONObjBuilder( const BSONSizeTracker & tracker ) : _b(_buf) , _buf(tracker.getSize() ), _offset(0), _s( this ) , _tracker( (BSONSizeTracker*)(&tracker) ) , _doneCalled(false) {
_b.skip( 4 );
}
- ~BSONObjBuilder(){
- if ( !_doneCalled && _b.buf() && _buf.getSize() == 0 ){
+ ~BSONObjBuilder() {
+ if ( !_doneCalled && _b.buf() && _buf.getSize() == 0 ) {
_done();
}
}
@@ -129,14 +129,14 @@ namespace mongo {
}
/** add a subobject as a member */
- BSONObjBuilder& appendObject(const StringData& fieldName, const char * objdata , int size = 0 ){
+ BSONObjBuilder& appendObject(const StringData& fieldName, const char * objdata , int size = 0 ) {
assert( objdata );
- if ( size == 0 ){
+ if ( size == 0 ) {
size = *((int*)objdata);
}
-
+
assert( size > 4 && size < 100000000 );
-
+
_b.appendNum((char) Object);
_b.appendStr(fieldName);
_b.appendBuf((void*)objdata, size );
@@ -151,7 +151,7 @@ namespace mongo {
_b.appendStr(fieldName);
return _b;
}
-
+
/** add a subobject as a member with type Array. Thus arr object should have "0", "1", ...
style fields in it.
*/
@@ -161,9 +161,9 @@ namespace mongo {
_b.appendBuf((void *) subObj.objdata(), subObj.objsize());
return *this;
}
- BSONObjBuilder& append(const StringData& fieldName, BSONArray arr) {
- return appendArray(fieldName, arr);
- }
+ BSONObjBuilder& append(const StringData& fieldName, BSONArray arr) {
+ return appendArray(fieldName, arr);
+ }
/** add header for a new subarray and return bufbuilder for writing to
the subarray's body */
@@ -172,7 +172,7 @@ namespace mongo {
_b.appendStr(fieldName);
return _b;
}
-
+
/** Append a boolean element */
BSONObjBuilder& appendBool(const StringData& fieldName, int val) {
_b.appendNum((char) Bool);
@@ -185,10 +185,10 @@ namespace mongo {
BSONObjBuilder& append(const StringData& fieldName, bool val) {
_b.appendNum((char) Bool);
_b.appendStr(fieldName);
- _b.appendNum((char) (val?1:0));
+ _b.appendNum((char) (val?1:0));
return *this;
}
-
+
/** Append a 32 bit integer element */
BSONObjBuilder& append(const StringData& fieldName, int n) {
_b.appendNum((char) NumberInt);
@@ -198,20 +198,20 @@ namespace mongo {
}
/** Append a 32 bit unsigned element - cast to a signed int. */
- BSONObjBuilder& append(const StringData& fieldName, unsigned n) {
- return append(fieldName, (int) n);
+ BSONObjBuilder& append(const StringData& fieldName, unsigned n) {
+ return append(fieldName, (int) n);
}
/** Append a NumberLong */
- BSONObjBuilder& append(const StringData& fieldName, long long n) {
+ BSONObjBuilder& append(const StringData& fieldName, long long n) {
_b.appendNum((char) NumberLong);
_b.appendStr(fieldName);
_b.appendNum(n);
- return *this;
+ return *this;
}
/** appends a number. if n < max(int)/2 then uses int, otherwise long long */
- BSONObjBuilder& appendIntOrLL( const StringData& fieldName , long long n ){
+ BSONObjBuilder& appendIntOrLL( const StringData& fieldName , long long n ) {
long long x = n;
if ( x < 0 )
x = x * -1;
@@ -226,15 +226,15 @@ namespace mongo {
* appendNumber is a series of method for appending the smallest sensible type
* mostly for JS
*/
- BSONObjBuilder& appendNumber( const StringData& fieldName , int n ){
+ BSONObjBuilder& appendNumber( const StringData& fieldName , int n ) {
return append( fieldName , n );
}
- BSONObjBuilder& appendNumber( const StringData& fieldName , double d ){
+ BSONObjBuilder& appendNumber( const StringData& fieldName , double d ) {
return append( fieldName , d );
}
- BSONObjBuilder& appendNumber( const StringData& fieldName , size_t n ){
+ BSONObjBuilder& appendNumber( const StringData& fieldName , size_t n ) {
static size_t maxInt = (size_t)pow( 2.0 , 30.0 );
if ( n < maxInt )
@@ -245,7 +245,7 @@ namespace mongo {
}
- BSONObjBuilder& appendNumber( const StringData& fieldName , long long l ){
+ BSONObjBuilder& appendNumber( const StringData& fieldName , long long l ) {
static long long maxInt = (int)pow( 2.0 , 30.0 );
static long long maxDouble = (long long)pow( 2.0 , 40.0 );
@@ -257,7 +257,7 @@ namespace mongo {
append( fieldName , l );
return *this;
}
-
+
/** Append a double element */
BSONObjBuilder& append(const StringData& fieldName, double n) {
_b.appendNum((char) NumberDouble);
@@ -271,8 +271,8 @@ namespace mongo {
*/
bool appendAsNumber( const StringData& fieldName , const string& data );
- /** Append a BSON Object ID (OID type).
- @deprecated Generally, it is preferred to use the append append(name, oid)
+ /** Append a BSON Object ID (OID type).
+ @deprecated Generally, it is preferred to use the append append(name, oid)
method for this.
*/
BSONObjBuilder& appendOID(const StringData& fieldName, OID *oid = 0 , bool generateIfBlank = false ) {
@@ -291,8 +291,8 @@ namespace mongo {
return *this;
}
- /**
- Append a BSON Object ID.
+ /**
+ Append a BSON Object ID.
@param fieldName Field name, e.g., "_id".
@returns the builder object
*/
@@ -321,14 +321,14 @@ namespace mongo {
_b.appendNum(static_cast<unsigned long long>(dt) * 1000);
return *this;
}
- /** Append a date.
- @param dt a Java-style 64 bit date value, that is
+ /** Append a date.
+ @param dt a Java-style 64 bit date value, that is
the number of milliseconds since January 1, 1970, 00:00:00 GMT
*/
BSONObjBuilder& appendDate(const StringData& fieldName, Date_t dt) {
/* easy to pass a time_t to this and get a bad result. thus this warning. */
#if defined(_DEBUG) && defined(MONGO_EXPOSE_MACROS)
- if( dt > 0 && dt <= 0xffffffff ) {
+ if( dt > 0 && dt <= 0xffffffff ) {
static int n;
if( n++ == 0 )
log() << "DEV WARNING appendDate() called with a tiny (but nonzero) date" << endl;
@@ -373,7 +373,7 @@ namespace mongo {
}
/** Append a string element */
BSONObjBuilder& append(const StringData& fieldName, const char *str) {
- return append(fieldName, str, (int) strlen(str)+1);
+ return append(fieldName, str, (int) strlen(str)+1);
}
/** Append a string element */
BSONObjBuilder& append(const StringData& fieldName, const string& str) {
@@ -385,40 +385,42 @@ namespace mongo {
_b.appendStr(fieldName);
_b.appendNum((int) symbol.size()+1);
_b.appendStr(symbol);
- return *this; }
+ return *this;
+ }
/** Append a Null element to the object */
BSONObjBuilder& appendNull( const StringData& fieldName ) {
_b.appendNum( (char) jstNULL );
_b.appendStr( fieldName );
- return *this; }
+ return *this;
+ }
// Append an element that is less than all other keys.
BSONObjBuilder& appendMinKey( const StringData& fieldName ) {
_b.appendNum( (char) MinKey );
_b.appendStr( fieldName );
- return *this;
+ return *this;
}
// Append an element that is greater than all other keys.
BSONObjBuilder& appendMaxKey( const StringData& fieldName ) {
_b.appendNum( (char) MaxKey );
_b.appendStr( fieldName );
- return *this;
+ return *this;
}
-
+
// Append a Timestamp field -- will be updated to next OpTime on db insert.
BSONObjBuilder& appendTimestamp( const StringData& fieldName ) {
_b.appendNum( (char) Timestamp );
_b.appendStr( fieldName );
_b.appendNum( (unsigned long long) 0 );
- return *this;
+ return *this;
}
BSONObjBuilder& appendTimestamp( const StringData& fieldName , unsigned long long val ) {
_b.appendNum( (char) Timestamp );
_b.appendStr( fieldName );
_b.appendNum( val );
- return *this;
+ return *this;
}
/**
@@ -427,10 +429,10 @@ namespace mongo {
@param time - in millis (but stored in seconds)
*/
BSONObjBuilder& appendTimestamp( const StringData& fieldName , unsigned long long time , unsigned int inc );
-
+
/*
Append an element of the deprecated DBRef type.
- @deprecated
+ @deprecated
*/
BSONObjBuilder& appendDBRef( const StringData& fieldName, const StringData& ns, const OID &oid ) {
_b.appendNum( (char) DBRef );
@@ -438,13 +440,13 @@ namespace mongo {
_b.appendNum( (int) ns.size() + 1 );
_b.appendStr( ns );
_b.appendBuf( (void *) &oid, 12 );
- return *this;
+ return *this;
}
- /** Append a binary data element
+ /** Append a binary data element
@param fieldName name of the field
@param len length of the binary data in bytes
- @param subtype subtype information for the data. @see enum BinDataType in bsontypes.h.
+ @param subtype subtype information for the data. @see enum BinDataType in bsontypes.h.
Use BinDataGeneral if you don't care about the type.
@param data the byte array
*/
@@ -454,29 +456,29 @@ namespace mongo {
_b.appendNum( len );
_b.appendNum( (char) type );
_b.appendBuf( (void *) data, len );
- return *this;
+ return *this;
}
BSONObjBuilder& appendBinData( const StringData& fieldName, int len, BinDataType type, const unsigned char *data ) {
return appendBinData(fieldName, len, type, (const char *) data);
}
-
+
/**
Subtype 2 is deprecated.
Append a BSON bindata bytearray element.
@param data a byte array
@param len the length of data
*/
- BSONObjBuilder& appendBinDataArrayDeprecated( const char * fieldName , const char * data , int len ){
+ BSONObjBuilder& appendBinDataArrayDeprecated( const char * fieldName , const char * data , int len ) {
_b.appendNum( (char) BinData );
_b.appendStr( fieldName );
_b.appendNum( len + 4 );
_b.appendNum( (char)0x2 );
_b.appendNum( len );
- _b.appendBuf( (void *) data, len );
- return *this;
+ _b.appendBuf( (void *) data, len );
+ return *this;
}
- /** Append to the BSON object a field of type CodeWScope. This is a javascript code
+ /** Append to the BSON object a field of type CodeWScope. This is a javascript code
fragment accompanied by some scope that goes with it.
*/
BSONObjBuilder& appendCodeWScope( const StringData& fieldName, const StringData& code, const BSONObj &scope ) {
@@ -493,12 +495,12 @@ namespace mongo {
_b.appendNum( (char) Undefined );
_b.appendStr( fieldName );
}
-
+
/* helper function -- see Query::where() for primary way to do this. */
- void appendWhere( const StringData& code, const BSONObj &scope ){
+ void appendWhere( const StringData& code, const BSONObj &scope ) {
appendCodeWScope( "$where" , code , scope );
}
-
+
/**
these are the min/max when comparing, not strict min/max elements for a given type
*/
@@ -512,9 +514,9 @@ namespace mongo {
template < class T >
BSONObjBuilder& append( const StringData& fieldName, const list< T >& vals );
- /**
+ /**
* destructive
- * The returned BSONObj will free the buffer when it is finished.
+ * The returned BSONObj will free the buffer when it is finished.
* @return owned BSONObj
*/
BSONObj obj() {
@@ -525,9 +527,9 @@ namespace mongo {
}
/** Fetch the object we have built.
- BSONObjBuilder still frees the object when the builder goes out of
- scope -- very important to keep in mind. Use obj() if you
- would like the BSONObj to last longer than the builder.
+ BSONObjBuilder still frees the object when the builder goes out of
+ scope -- very important to keep in mind. Use obj() if you
+ would like the BSONObj to last longer than the builder.
*/
BSONObj done() {
return BSONObj(_done(), /*ifree*/false);
@@ -600,14 +602,14 @@ namespace mongo {
BSONObjBuilderValueStream& operator<<( const BSONField<T>& f ) {
_s.endField( f.name().c_str() );
return _s;
- }
+ }
template<typename T>
BSONObjBuilder& operator<<( const BSONFieldValue<T>& v ) {
append( v.name().c_str() , v.value() );
return *this;
- }
-
+ }
+
/** @return true if we are using our own bufbuilder, and not an alternate that was given to us in our constructor */
bool owned() const { return &_b == &_buf; }
@@ -615,12 +617,12 @@ namespace mongo {
BSONObjIterator iterator() const ;
int len() const { return _b.len(); }
-
+
private:
char* _done() {
if ( _doneCalled )
return _b.buf() + _offset;
-
+
_doneCalled = true;
_s.endField();
_b.appendNum((char) EOO);
@@ -649,42 +651,42 @@ namespace mongo {
BSONArrayBuilder( int initialSize ) : _i(0), _b(initialSize) {}
template <typename T>
- BSONArrayBuilder& append(const T& x){
+ BSONArrayBuilder& append(const T& x) {
_b.append(num(), x);
return *this;
}
- BSONArrayBuilder& append(const BSONElement& e){
+ BSONArrayBuilder& append(const BSONElement& e) {
_b.appendAs(e, num());
return *this;
}
-
+
template <typename T>
- BSONArrayBuilder& operator<<(const T& x){
+ BSONArrayBuilder& operator<<(const T& x) {
return append(x);
}
-
+
void appendNull() {
_b.appendNull(num());
}
-
+
/**
* destructive - ownership moves to returned BSONArray
* @return owned BSONArray
*/
- BSONArray arr(){ return BSONArray(_b.obj()); }
-
+ BSONArray arr() { return BSONArray(_b.obj()); }
+
BSONObj done() { return _b.done(); }
-
+
void doneFast() { _b.doneFast(); }
-
+
template <typename T>
- BSONArrayBuilder& append(const StringData& name, const T& x){
+ BSONArrayBuilder& append(const StringData& name, const T& x) {
fill( name );
append( x );
return *this;
}
-
+
BufBuilder &subobjStart( const StringData& name = "0" ) {
fill( name );
return _b.subobjStart( num() );
@@ -694,17 +696,17 @@ namespace mongo {
fill( name );
return _b.subarrayStart( num() );
}
-
+
void appendArray( const StringData& name, BSONObj subObj ) {
fill( name );
_b.appendArray( num(), subObj );
}
-
+
void appendAs( const BSONElement &e, const char *name) {
fill( name );
append( e );
}
-
+
int len() const { return _b.len(); }
private:
@@ -716,19 +718,19 @@ namespace mongo {
while( _i < n )
append( nullElt() );
}
-
+
static BSONElement nullElt() {
static BSONObj n = nullObj();
return n.firstElement();
}
-
+
static BSONObj nullObj() {
BSONObjBuilder _b;
_b.appendNull( "" );
return _b.obj();
}
-
- string num(){ return _b.numStr(_i++); }
+
+ string num() { return _b.numStr(_i++); }
int _i;
BSONObjBuilder _b;
};
@@ -754,14 +756,14 @@ namespace mongo {
// $or helper: OR(BSON("x" << GT << 7), BSON("y" << LT 6));
inline BSONObj OR(const BSONObj& a, const BSONObj& b)
- { return BSON( "$or" << BSON_ARRAY(a << b) ); }
+ { return BSON( "$or" << BSON_ARRAY(a << b) ); }
inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c)
- { return BSON( "$or" << BSON_ARRAY(a << b << c) ); }
+ { return BSON( "$or" << BSON_ARRAY(a << b << c) ); }
inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d)
- { return BSON( "$or" << BSON_ARRAY(a << b << c << d) ); }
+ { return BSON( "$or" << BSON_ARRAY(a << b << c << d) ); }
inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e)
- { return BSON( "$or" << BSON_ARRAY(a << b << c << d << e) ); }
+ { return BSON( "$or" << BSON_ARRAY(a << b << c << d << e) ); }
inline BSONObj OR(const BSONObj& a, const BSONObj& b, const BSONObj& c, const BSONObj& d, const BSONObj& e, const BSONObj& f)
- { return BSON( "$or" << BSON_ARRAY(a << b << c << d << e << f) ); }
-
+ { return BSON( "$or" << BSON_ARRAY(a << b << c << d << e << f) ); }
+
}
View
58 bson/bsonobjiterator.h
@@ -31,7 +31,7 @@ namespace mongo {
*/
class BSONObjIterator {
public:
- /** Create an iterator for a BSON object.
+ /** Create an iterator for a BSON object.
*/
BSONObjIterator(const BSONObj& jso) {
int sz = jso.objsize();
@@ -43,11 +43,11 @@ namespace mongo {
_theend = jso.objdata() + sz;
}
- BSONObjIterator( const char * start , const char * end ){
+ BSONObjIterator( const char * start , const char * end ) {
_pos = start + 4;
_theend = end;
}
-
+
/** @return true if more elements exist to be enumerated. */
bool more() { return _pos < _theend && _pos[0]; }