summaryrefslogtreecommitdiff
path: root/db/db.h
blob: 48e430001cac2807136706d97fff37ce505abee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
*    Copyright (C) 2008 10gen Inc.
*
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "../stdafx.h"
#include "../util/message.h"

namespace mongo {

    void jniCallback(Message& m, Message& out);

    class MutexInfo {
        unsigned long long start, enter, timeLocked; // all in microseconds
        int locked;

    public:
        MutexInfo() : locked(0) {
            start = curTimeMicros64();
        }
        void entered() {
            enter = curTimeMicros64();
            locked++;
            assert( locked == 1 );
        }
        void leaving() {
            locked--;
            assert( locked == 0 );
            timeLocked += curTimeMicros64() - enter;
        }
        int isLocked() const {
            return locked;
        }
        void timingInfo(unsigned long long &s, unsigned long long &tl) {
            s = start;
            tl = timeLocked;
        }
    };

    extern boost::mutex &dbMutex;
    extern MutexInfo dbMutexInfo;
//extern int dbLocked;

    struct lock {
        boostlock bl_;
        MutexInfo& info_;
        lock( boost::mutex &mutex, MutexInfo &info ) :
                bl_( mutex ),
                info_( info ) {
            info_.entered();
        }
        ~lock() {
            info_.leaving();
        }
    };

    void dbunlocking();

    struct dblock : public lock {
        dblock() :
                lock( dbMutex, dbMutexInfo ) {
        }
        ~dblock() { 
            /* todo: this should be inlined */
            dbunlocking();
        }
    };

} // namespace mongo

#include "boost/version.hpp"

namespace mongo {

    /* a scoped release of a mutex temporarily -- like a scopedlock but reversed.
    */
    struct temprelease {
        boost::mutex& m;
        temprelease(boost::mutex& _m) : m(_m) {
#if BOOST_VERSION >= 103500
            m.unlock();
#else
            boost::detail::thread::lock_ops<boost::mutex>::unlock(m);
#endif
        }
        ~temprelease() {
#if BOOST_VERSION >= 103500
            m.lock();
#else
            boost::detail::thread::lock_ops<boost::mutex>::lock(m);
#endif
        }
    };

} // namespace mongo

#include "pdfile.h"

namespace mongo {

// tempish...move to TLS or pass all the way down as a parm
    extern map<string,Database*> databases;
    extern Database *database;
    extern const char *curNs;
    extern bool master;

    inline string getKey( const char *ns, const char *path ) {
        char cl[256];
        nsToClient(ns, cl);
        return string( cl ) + ":" + path;
    }

    /* returns true if the database ("database") did not exist, and it was created on this call */
    inline bool setClient(const char *ns, const char *path=dbpath) {
        /* we must be in critical section at this point as these are global
           variables.
        */
        assert( dbMutexInfo.isLocked() );

        curNs = ns;
        string key = getKey( ns, path );
        map<string,Database*>::iterator it = databases.find(key);
        if ( it != databases.end() ) {
            database = it->second;
            return false;
        }

        // when master for replication, we advertise all the db's, and that
        // looks like a 'first operation'. so that breaks this log message's
        // meaningfulness.  instead of fixing (which would be better), we just
        // stop showing for now.
        // 2008-12-22 We now open every database on startup, so this log is
        // no longer helpful.  Commenting.
//    if( !master )
//        log() << "first operation for database " << key << endl;

        char cl[256];
        nsToClient(ns, cl);
        bool justCreated;
        Database *c = new Database(cl, justCreated, path);
        databases[key] = c;
        database = c;
        database->finishInit();

        return justCreated;
    }

// shared functionality for removing references to a database from this program instance
// does not delete the files on disk
    void closeClient( const char *cl, const char *path = dbpath );

    inline void eraseDatabase( const char *ns, const char *path=dbpath ) {
        string key = getKey( ns, path );
        databases.erase( key );
    }

    /* We normally keep around a curNs ptr -- if this ns is temporary,
       use this instead so we don't have a bad ptr.  we could have made a copy,
       but trying to be fast as we call setClient this for every single operation.
    */
    inline bool setClientTempNs(const char *ns) {
        bool jc = setClient(ns);
        curNs = "";
        return jc;
    }

    struct dbtemprelease {
        string clientname;
        string clientpath;
        dbtemprelease() {
            if ( database ) {
                clientname = database->name;
                clientpath = database->path;
            }
            dbMutexInfo.leaving();
#if BOOST_VERSION >= 103500
            dbMutex.unlock();
#else
            boost::detail::thread::lock_ops<boost::mutex>::unlock(dbMutex);
#endif
        }
        ~dbtemprelease() {
#if BOOST_VERSION >= 103500
            dbMutex.lock();
#else
            boost::detail::thread::lock_ops<boost::mutex>::lock(dbMutex);
#endif
            dbMutexInfo.entered();
            if ( clientname.empty() )
                database = 0;
            else
                setClient(clientname.c_str(), clientpath.c_str());
        }
    };

} // namespace mongo

#include "dbinfo.h"