diff options
author | Antonin Kral <a.kral@bobek.cz> | 2010-08-11 12:38:57 +0200 |
---|---|---|
committer | Antonin Kral <a.kral@bobek.cz> | 2010-08-11 12:38:57 +0200 |
commit | 7645618fd3914cb8a20561625913c20d49504a49 (patch) | |
tree | 8370f846f58f6d71165b7a0e2eda04648584ec76 /client/model.cpp | |
parent | 68c73c3c7608b4c87f07440dc3232801720b1168 (diff) | |
download | mongodb-7645618fd3914cb8a20561625913c20d49504a49.tar.gz |
Imported Upstream version 1.6.0
Diffstat (limited to 'client/model.cpp')
-rw-r--r-- | client/model.cpp | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/client/model.cpp b/client/model.cpp index 3978105..7861b91 100644 --- a/client/model.cpp +++ b/client/model.cpp @@ -15,7 +15,7 @@ * limitations under the License. */ -#include "stdafx.h" +#include "pch.h" #include "model.h" #include "connpool.h" @@ -57,6 +57,31 @@ namespace mongo { BSONObjBuilder b; serialize( b ); + BSONElement myId; + { + BSONObjIterator i = b.iterator(); + while ( i.more() ){ + BSONElement e = i.next(); + if ( strcmp( e.fieldName() , "_id" ) == 0 ){ + myId = e; + break; + } + } + } + + if ( myId.type() ){ + if ( _id.isEmpty() ){ + _id = myId.wrap(); + } + else if ( myId.woCompare( _id.firstElement() ) ){ + stringstream ss; + ss << "_id from serialize and stored differ: "; + ss << '[' << myId << "] != "; + ss << '[' << _id.firstElement() << ']'; + throw UserException( 13121 , ss.str() ); + } + } + if ( _id.isEmpty() ){ OID oid; oid.init(); @@ -69,18 +94,22 @@ namespace mongo { log(4) << "inserted new model " << getNS() << " " << o << endl; } else { - BSONElement id = _id["_id"]; - b.append( id ); + if ( myId.eoo() ){ + myId = _id["_id"]; + b.append( myId ); + } + + assert( ! myId.eoo() ); BSONObjBuilder qb; - qb.append( id ); + qb.append( myId ); BSONObj q = qb.obj(); BSONObj o = b.obj(); - log(4) << "updated old model" << getNS() << " " << q << " " << o << endl; + log(4) << "updated model" << getNS() << " " << q << " " << o << endl; - conn->update( getNS() , q , o ); + conn->update( getNS() , q , o , true ); } @@ -94,4 +123,16 @@ namespace mongo { throw UserException( 9003 , (string)"error on Model::save: " + errmsg ); } + BSONObj Model::toObject(){ + BSONObjBuilder b; + serialize( b ); + return b.obj(); + } + + void Model::append( const char * name , BSONObjBuilder& b ){ + BSONObjBuilder bb( b.subobjStart( name ) ); + serialize( bb ); + bb.done(); + } + } // namespace mongo |