2009-03-26 18:45:29 +01:00
|
|
|
// lasterror.cpp
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
2009-09-11 17:56:57 +02:00
|
|
|
#include "../util/unittest.h"
|
2009-09-11 18:52:17 +02:00
|
|
|
#include "../util/message.h"
|
|
|
|
|
2009-03-26 18:45:29 +01:00
|
|
|
|
2009-09-11 17:56:57 +02:00
|
|
|
#include "lasterror.h"
|
2009-03-26 18:45:29 +01:00
|
|
|
#include "jsobj.h"
|
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
|
|
|
LastError LastError::noError;
|
2009-09-11 17:56:57 +02:00
|
|
|
LastErrorHolder lastError;
|
2009-03-26 18:45:29 +01:00
|
|
|
|
|
|
|
void LastError::appendSelf( BSONObjBuilder &b ) {
|
|
|
|
if ( !valid ) {
|
|
|
|
b.appendNull( "err" );
|
|
|
|
b.append( "n", 0 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( msg.empty() )
|
|
|
|
b.appendNull( "err" );
|
|
|
|
else
|
|
|
|
b.append( "err", msg );
|
|
|
|
if ( updatedExisting != NotUpdate )
|
|
|
|
b.appendBool( "updatedExisting", updatedExisting == True );
|
|
|
|
b.append( "n", nObjects );
|
|
|
|
}
|
2009-09-11 17:56:57 +02:00
|
|
|
|
|
|
|
void LastErrorHolder::setID( int id ){
|
|
|
|
_id.reset( id );
|
|
|
|
}
|
|
|
|
|
2009-09-11 18:52:17 +02:00
|
|
|
int LastErrorHolder::getID(){
|
|
|
|
return _id.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
LastError * LastErrorHolder::get( bool create ){
|
2009-09-11 17:56:57 +02:00
|
|
|
int id = _id.get();
|
|
|
|
if ( id == 0 )
|
|
|
|
return _tl.get();
|
|
|
|
|
|
|
|
LastErrorIDMap::iterator i = _ids.find( id );
|
2009-09-11 18:52:17 +02:00
|
|
|
if ( i == _ids.end() ){
|
|
|
|
if ( ! create )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LastError * le = new LastError();
|
2009-09-29 21:48:58 +02:00
|
|
|
_ids[id] = make_pair( (int) time(0) , le );
|
2009-09-11 18:52:17 +02:00
|
|
|
return le;
|
|
|
|
}
|
2009-09-11 17:56:57 +02:00
|
|
|
|
|
|
|
LastErrorStatus & status = i->second;
|
2009-09-29 21:48:58 +02:00
|
|
|
status.first = (int) time(0);
|
2009-09-11 17:56:57 +02:00
|
|
|
return status.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LastErrorHolder::remove( int id ){
|
|
|
|
LastErrorIDMap::iterator i = _ids.find( id );
|
|
|
|
if ( i == _ids.end() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
delete i->second.second;
|
|
|
|
_ids.erase( i );
|
|
|
|
}
|
|
|
|
|
|
|
|
void LastErrorHolder::release(){
|
|
|
|
int id = _id.get();
|
|
|
|
if ( id == 0 ){
|
|
|
|
_tl.release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
remove( id );
|
|
|
|
}
|
2009-03-26 18:45:29 +01:00
|
|
|
|
2009-09-11 17:56:57 +02:00
|
|
|
void LastErrorHolder::reset( LastError * le ){
|
|
|
|
int id = _id.get();
|
|
|
|
if ( id == 0 ){
|
|
|
|
_tl.reset( le );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LastErrorStatus & status = _ids[id];
|
2009-09-29 21:48:58 +02:00
|
|
|
status.first = (int) time(0);
|
2009-09-11 17:56:57 +02:00
|
|
|
status.second = le;
|
|
|
|
}
|
2009-09-11 18:52:17 +02:00
|
|
|
|
|
|
|
void LastErrorHolder::startRequest( Message& m , LastError * connectionOwned ){
|
|
|
|
if ( connectionOwned && ! connectionOwned->overridenById ){
|
|
|
|
connectionOwned->nPrev++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int id = m.data->id & 0xFFFF0000;
|
|
|
|
setID( id );
|
|
|
|
LastError * le = get( true);
|
|
|
|
le->nPrev++;
|
|
|
|
}
|
|
|
|
|
2009-09-11 17:56:57 +02:00
|
|
|
|
|
|
|
struct LastErrorHolderTest : public UnitTest {
|
|
|
|
public:
|
|
|
|
|
|
|
|
void test( int i ){
|
|
|
|
_tl.reset( i );
|
|
|
|
assert( _tl.get() == i );
|
|
|
|
}
|
|
|
|
|
|
|
|
void tlmaptest(){
|
|
|
|
test( 1 );
|
|
|
|
test( 12123123 );
|
|
|
|
test( -123123 );
|
|
|
|
test( numeric_limits<int>::min() );
|
|
|
|
test( numeric_limits<int>::max() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(){
|
|
|
|
tlmaptest();
|
|
|
|
|
|
|
|
LastError * a = new LastError();
|
|
|
|
LastError * b = new LastError();
|
|
|
|
|
|
|
|
LastErrorHolder holder;
|
|
|
|
holder.reset( a );
|
|
|
|
assert( a == holder.get() );
|
|
|
|
holder.setID( 1 );
|
|
|
|
assert( 0 == holder.get() );
|
|
|
|
holder.reset( b );
|
|
|
|
assert( b == holder.get() );
|
|
|
|
holder.setID( 0 );
|
|
|
|
assert( a == holder.get() );
|
|
|
|
|
|
|
|
holder.remove( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadLocalInt _tl;
|
|
|
|
} lastErrorHolderTest;
|
|
|
|
|
2009-03-26 18:45:29 +01:00
|
|
|
} // namespace mongo
|