~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to db/lasterror.h

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// lasterror.h
 
2
 
 
3
/*    Copyright 2009 10gen Inc.
 
4
 *
 
5
 *    Licensed under the Apache License, Version 2.0 (the "License");
 
6
 *    you may not use this file except in compliance with the License.
 
7
 *    You may obtain a copy of the License at
 
8
 *
 
9
 *    http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *    Unless required by applicable law or agreed to in writing, software
 
12
 *    distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *    See the License for the specific language governing permissions and
 
15
 *    limitations under the License.
 
16
 */
 
17
 
 
18
#pragma once
 
19
 
 
20
#include <boost/thread/tss.hpp>
 
21
#undef assert
 
22
#define assert xassert
 
23
 
 
24
namespace mongo {
 
25
    class BSONObjBuilder;
 
26
    class Message;
 
27
 
 
28
    struct LastError {
 
29
        int code;
 
30
        string msg;
 
31
        enum UpdatedExistingType { NotUpdate, True, False } updatedExisting;
 
32
        /* todo: nObjects should be 64 bit */
 
33
        int nObjects;
 
34
        int nPrev;
 
35
        bool valid;
 
36
        bool overridenById;
 
37
        bool disabled;
 
38
        void raiseError(int _code , const char *_msg) {
 
39
            reset( true );
 
40
            code = _code;
 
41
            msg = _msg;
 
42
        }
 
43
        void recordUpdate( bool _updatedExisting, int nChanged ) {
 
44
            reset( true );
 
45
            nObjects = nChanged;
 
46
            updatedExisting = _updatedExisting ? True : False;
 
47
        }
 
48
        void recordDelete( int nDeleted ) {
 
49
            reset( true );
 
50
            nObjects = nDeleted;
 
51
        }
 
52
        LastError() {
 
53
            overridenById = false;
 
54
            reset();
 
55
        }
 
56
        void reset( bool _valid = false ) {
 
57
            code = 0;
 
58
            msg.clear();
 
59
            updatedExisting = NotUpdate;
 
60
            nObjects = 0;
 
61
            nPrev = 1;
 
62
            valid = _valid;
 
63
            disabled = false;
 
64
        }
 
65
        void appendSelf( BSONObjBuilder &b );
 
66
        static LastError noError;
 
67
    };
 
68
 
 
69
    extern class LastErrorHolder {
 
70
    public:
 
71
        LastErrorHolder() : _id( 0 ) {}
 
72
 
 
73
        LastError * get( bool create = false );
 
74
 
 
75
        LastError * _get( bool create = false ); // may return a disabled LastError
 
76
 
 
77
        void reset( LastError * le );
 
78
        
 
79
        /**
 
80
         * id of 0 means should use thread local management
 
81
         */
 
82
        void setID( int id );
 
83
        int getID();
 
84
 
 
85
        void remove( int id );
 
86
        void release();
 
87
        
 
88
        /** when db receives a message/request, call this */
 
89
        void startRequest( Message& m , LastError * connectionOwned );
 
90
        void startRequest( Message& m );
 
91
        
 
92
        // used to disable lastError reporting while processing a killCursors message
 
93
        // disable causes get() to return 0.
 
94
        LastError *disableForCommand(); // only call once per command invocation!
 
95
    private:
 
96
        ThreadLocalValue<int> _id;
 
97
        boost::thread_specific_ptr<LastError> _tl;
 
98
        
 
99
        struct Status {
 
100
            time_t time;
 
101
            LastError *lerr;
 
102
        };
 
103
        static boost::mutex _idsmutex;
 
104
        map<int,Status> _ids;    
 
105
    } lastError;
 
106
    
 
107
    inline void raiseError(int code , const char *msg) {
 
108
        LastError *le = lastError.get();
 
109
        if ( le == 0 ) {
 
110
            DEV log() << "warning: lastError==0 can't report:" << msg << '\n';
 
111
        } else if ( le->disabled ) {
 
112
            log() << "lastError disabled, can't report: " << msg << endl;
 
113
        } else {
 
114
            le->raiseError(code, msg);
 
115
        }
 
116
    }
 
117
    
 
118
    inline void recordUpdate( bool updatedExisting, int nChanged ) {
 
119
        LastError *le = lastError.get();
 
120
        if ( le )
 
121
            le->recordUpdate( updatedExisting, nChanged );        
 
122
    }
 
123
 
 
124
    inline void recordDelete( int nDeleted ) {
 
125
        LastError *le = lastError.get();
 
126
        if ( le )
 
127
            le->recordDelete( nDeleted );        
 
128
    }
 
129
 
 
130
} // namespace mongo