summaryrefslogtreecommitdiff
path: root/src/mongo/db/lasterror.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/lasterror.h')
-rw-r--r--src/mongo/db/lasterror.h150
1 files changed, 60 insertions, 90 deletions
diff --git a/src/mongo/db/lasterror.h b/src/mongo/db/lasterror.h
index 5b04b69a57a..55a04c8334d 100644
--- a/src/mongo/db/lasterror.h
+++ b/src/mongo/db/lasterror.h
@@ -29,16 +29,13 @@
#pragma once
-#include <boost/noncopyable.hpp>
-#include <boost/thread/tss.hpp>
#include <string>
+#include "mongo/db/client.h"
#include "mongo/db/jsobj.h"
-#include "mongo/bson/oid.h"
namespace mongo {
class BSONObjBuilder;
- class Message;
static const char kUpsertedFieldName[] = "upserted";
static const char kGLEStatsFieldName[] = "$gleStats";
@@ -46,107 +43,80 @@ namespace mongo {
static const char kGLEStatsLastOpTimeTermFieldName[] = "lastOpTimeTerm";
static const char kGLEStatsElectionIdFieldName[] = "electionId";
- struct LastError {
- int code;
- std::string msg;
- enum UpdatedExistingType { NotUpdate, True, False } updatedExisting;
- // _id field value from inserted doc, returned as kUpsertedFieldName (above)
- BSONObj upsertedId;
- long long nObjects;
- int nPrev;
- bool valid;
- bool disabled;
- void raiseError(int _code , const char *_msg) {
- reset( true );
- code = _code;
- msg = _msg;
- }
- void recordUpdate( bool _updateObjects , long long _nObjects , BSONObj _upsertedId ) {
- reset( true );
- nObjects = _nObjects;
- updatedExisting = _updateObjects ? True : False;
- if ( _upsertedId.valid() && _upsertedId.hasField(kUpsertedFieldName) )
- upsertedId = _upsertedId;
-
- }
- void recordDelete( long long nDeleted ) {
- reset( true );
- nObjects = nDeleted;
- }
- LastError() {
- reset();
- }
- void reset( bool _valid = false ) {
- code = 0;
- msg.clear();
- updatedExisting = NotUpdate;
- nObjects = 0;
- nPrev = 1;
- valid = _valid;
- disabled = false;
- upsertedId = BSONObj();
- }
+ class LastError {
+ public:
+ static const Client::Decoration<LastError> get;
/**
- * @return if there is an err
+ * Resets the object to a newly constructed state. If "valid" is true, marks the last-error
+ * object as "valid".
*/
- bool appendSelf( BSONObjBuilder &b , bool blankErr = true );
-
- struct Disabled : boost::noncopyable {
- Disabled( LastError * le ) {
- _le = le;
- if ( _le ) {
- _prev = _le->disabled;
- _le->disabled = true;
- }
- else {
- _prev = false;
- }
- }
+ void reset(bool valid = false);
- ~Disabled() {
- if ( _le )
- _le->disabled = _prev;
- }
+ /**
+ * when db receives a message/request, call this
+ */
+ void startRequest();
- LastError * _le;
- bool _prev;
- };
+ /**
+ * Disables error recording for the current operation.
+ */
+ void disable();
- static LastError noError;
- };
+ /**
+ * Sets the error information for the current operation, if error recording was not
+ * explicitly disabled via a call to disable() since the call to startRequest.
+ */
+ void setLastError(int code, std::string msg);
- extern class LastErrorHolder {
- public:
- LastErrorHolder(){}
- ~LastErrorHolder();
+ void recordInsert(long long nObjects);
- LastError * get( bool create = false );
- LastError * getSafe();
- LastError * _get( bool create = false ); // may return a disabled LastError
+ void recordUpdate(bool updateObjects, long long nObjects, BSONObj upsertedId);
- void reset( LastError * le );
+ void recordDelete(long long nDeleted);
- /** ok to call more than once. */
- void initThread();
+ /**
+ * Writes the last-error state described by this object to "b".
+ *
+ * If "blankErr" is true, the "err" field will be explicitly set to null in the result
+ * instead of being omitted when the error string is empty.
+ *
+ * Returns true if there is a non-empty error message.
+ */
+ bool appendSelf(BSONObjBuilder &b, bool blankErr) const;
- void release();
+ bool isValid() const { return _valid; }
+ int const getNPrev() const { return _nPrev; }
- /** when db receives a message/request, call this */
- LastError * startRequest( Message& m , LastError * connectionOwned );
+ class Disabled {
+ public:
+ explicit Disabled(LastError* le) : _le(le), _prev(le->_disabled) {
+ _le->_disabled = true;
+ }
- // used to disable lastError reporting while processing a killCursors message
- // disable causes get() to return 0.
- LastError *disableForCommand(); // only call once per command invocation!
- private:
- boost::thread_specific_ptr<LastError> _tl;
+ ~Disabled() {
+ _le->_disabled = _prev;
+ }
- struct Status {
- time_t time;
- LastError *lerr;
+ private:
+ LastError * const _le;
+ const bool _prev;
};
- } lastError;
- void setLastError(int code , const char *msg);
+ static LastError noError;
+
+ private:
+ enum UpdatedExistingType { NotUpdate, True, False };
+
+ int _code = 0;
+ std::string _msg = {};
+ UpdatedExistingType _updatedExisting = NotUpdate;
+ // _id field value from inserted doc, returned as kUpsertedFieldName (above)
+ BSONObj _upsertedId = {};
+ long long _nObjects = 0;
+ int _nPrev = 1;
+ bool _valid = false;
+ bool _disabled = false;
+ };
} // namespace mongo