summaryrefslogtreecommitdiff
path: root/src/mongo/db/cc_by_loc.h
blob: b9dfd7d2117adced627d6f66d691f83ea8cc0f7d (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
/*    Copyright (C) 20012 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/>.
 */

/**
 *  Defines CursorId and defines ByLocKey, which pairs up a DiskLoc
 *  and a CursorId and provides a comparison operation.
 */

#pragma once

#include <limits>
#include <map>

#include "mongo/db/diskloc.h"

namespace mongo {

    typedef long long CursorId; /* passed to the client so it can send back on getMore */
    static const CursorId INVALID_CURSOR_ID = -1; // But see SERVER-5726.

    // TODO(acm|mathias): ByLocKey allows constructing a map so we can
    // invalidate or advance all cursors pointing to a DiskLoc when we
    // remove that record. During review of moving this class to this
    // header, Mathias said: "On further thought, I think this need
    // would be better served by an unordered_map<DiskLoc,
    // unordered_map<CursorId, ClientCursor*>>." At which point, this
    // class should be removed.

    struct ByLocKey {

        ByLocKey(const DiskLoc& l, const CursorId& i) : loc(l), id(i) {}

        static ByLocKey min(const DiskLoc& l) {
            return ByLocKey(l, std::numeric_limits<long long>::min());
        }

        static ByLocKey max(const DiskLoc& l) {
            return ByLocKey(l, std::numeric_limits<long long>::max());
        }

        const DiskLoc loc;
        const CursorId id;
    };

    inline bool operator<(const ByLocKey& lhs, const ByLocKey& rhs) {
        int x = lhs.loc.compare(rhs.loc);
        if (x)
            return x < 0;
        return lhs.id < rhs.id;
    }

    class ClientCursor;
    typedef std::map<ByLocKey, ClientCursor*> CCByLoc;

} // namespace mongo