1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include <string>
34 
35 #include "mongo/base/status_with.h"
36 #include "mongo/db/logical_session_id_gen.h"
37 #include "mongo/stdx/unordered_set.h"
38 #include "mongo/util/uuid.h"
39 
40 namespace mongo {
41 
42 using TxnNumber = std::int64_t;
43 using StmtId = std::int32_t;
44 
45 // Default value for unassigned statementId.
46 const StmtId kUninitializedStmtId = -1;
47 
48 // Used as a substitute statementId for oplog entries that were truncated and lost.
49 const StmtId kIncompleteHistoryStmtId = -2;
50 
51 const TxnNumber kUninitializedTxnNumber = -1;
52 
53 class BSONObjBuilder;
54 class OperationContext;
55 
56 const Minutes kLogicalSessionDefaultTimeout = Minutes(30);
57 extern int localLogicalSessionTimeoutMinutes;
58 
59 inline bool operator==(const LogicalSessionId& lhs, const LogicalSessionId& rhs) {
60     auto makeEqualityLens = [](const auto& lsid) { return std::tie(lsid.getId(), lsid.getUid()); };
61 
62     return makeEqualityLens(lhs) == makeEqualityLens(rhs);
63 }
64 
65 inline bool operator!=(const LogicalSessionId& lhs, const LogicalSessionId& rhs) {
66     return !(lhs == rhs);
67 }
68 
69 inline bool operator==(const LogicalSessionRecord& lhs, const LogicalSessionRecord& rhs) {
70     return lhs.getId() == rhs.getId();
71 }
72 
73 inline bool operator!=(const LogicalSessionRecord& lhs, const LogicalSessionRecord& rhs) {
74     return !(lhs == rhs);
75 }
76 
77 LogicalSessionId makeLogicalSessionIdForTest();
78 
79 LogicalSessionRecord makeLogicalSessionRecordForTest();
80 
81 struct LogicalSessionIdHash {
operatorLogicalSessionIdHash82     std::size_t operator()(const LogicalSessionId& lsid) const {
83         return _hasher(lsid.getId());
84     }
85 
86 private:
87     UUID::Hash _hasher;
88 };
89 
90 struct LogicalSessionRecordHash {
operatorLogicalSessionRecordHash91     std::size_t operator()(const LogicalSessionRecord& lsid) const {
92         return _hasher(lsid.getId().getId());
93     }
94 
95 private:
96     UUID::Hash _hasher;
97 };
98 
99 
100 inline std::ostream& operator<<(std::ostream& s, const LogicalSessionId& lsid) {
101     return (s << lsid.getId() << " - " << lsid.getUid());
102 }
103 
104 inline StringBuilder& operator<<(StringBuilder& s, const LogicalSessionId& lsid) {
105     return (s << lsid.getId().toString() << " - " << lsid.getUid().toString());
106 }
107 
108 inline std::ostream& operator<<(std::ostream& s, const LogicalSessionFromClient& lsid) {
109     return (s << lsid.getId() << " - " << (lsid.getUid() ? lsid.getUid()->toString() : ""));
110 }
111 
112 inline StringBuilder& operator<<(StringBuilder& s, const LogicalSessionFromClient& lsid) {
113     return (s << lsid.getId() << " - " << (lsid.getUid() ? lsid.getUid()->toString() : ""));
114 }
115 
116 /**
117  * An alias for sets of session ids.
118  */
119 using LogicalSessionIdSet = stdx::unordered_set<LogicalSessionId, LogicalSessionIdHash>;
120 using LogicalSessionRecordSet = stdx::unordered_set<LogicalSessionRecord, LogicalSessionRecordHash>;
121 
122 template <typename T>
123 using LogicalSessionIdMap = stdx::unordered_map<LogicalSessionId, T, LogicalSessionIdHash>;
124 
125 }  // namespace mongo
126