MongoDB C++ Driver  mongocxx-3.9.0
All Classes Namespaces Functions Typedefs Enumerations Enumerator Friends Pages
client_session.hpp
1 // Copyright 2017-present MongoDB Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <functional>
18 #include <memory>
19 
20 #include <bsoncxx/document/view.hpp>
21 #include <bsoncxx/stdx/optional.hpp>
22 #include <mongocxx/options/client_session.hpp>
23 
24 #include <mongocxx/config/prelude.hpp>
25 
26 namespace mongocxx {
27 inline namespace v_noabi {
28 class client;
29 
40 class MONGOCXX_API client_session {
41  public:
42  enum class transaction_state {
43  k_transaction_none,
44  k_transaction_starting,
45  k_transaction_in_progress,
46  k_transaction_committed,
47  k_transaction_aborted,
48  };
49 
54 
58  client_session& operator=(client_session&&) noexcept;
59 
60  client_session(const client_session&) = delete;
61  client_session& operator=(const client_session&) = delete;
62 
66  ~client_session() noexcept;
67 
71  const class client& client() const noexcept;
72 
76  const options::client_session& options() const noexcept;
77 
82  bsoncxx::document::view id() const noexcept;
83 
90  bsoncxx::document::view cluster_time() const noexcept;
91 
97  bsoncxx::types::b_timestamp operation_time() const noexcept;
98 
103  std::uint32_t server_id() const noexcept;
104 
108  transaction_state get_transaction_state() const noexcept;
109 
113  bool get_dirty() const noexcept;
114 
123  void advance_cluster_time(const bsoncxx::document::view& cluster_time);
124 
133  void advance_operation_time(const bsoncxx::types::b_timestamp& operation_time);
134 
145  void start_transaction(const stdx::optional<options::transaction>& transaction_opts = {});
146 
155 
163 
185  using with_transaction_cb = std::function<void MONGOCXX_CALL(client_session*)>;
186  void with_transaction(with_transaction_cb cb, options::transaction opts = {});
187 
188  private:
189  friend class bulk_write;
190  friend class client;
191  friend class collection;
192  friend class database;
193  friend class index_view;
194  friend class search_index_view;
195 
196  class MONGOCXX_PRIVATE impl;
197 
198  // "class client" distinguishes from client() method above.
199  MONGOCXX_PRIVATE client_session(const class client* client,
200  const options::client_session& options);
201 
202  MONGOCXX_PRIVATE impl& _get_impl();
203  MONGOCXX_PRIVATE const impl& _get_impl() const;
204 
205  std::unique_ptr<impl> _impl;
206 };
207 
208 } // namespace v_noabi
209 } // namespace mongocxx
210 
211 #include <mongocxx/config/postlude.hpp>
212 
Class representing a batch of write operations that can be sent to the server as a group.
Definition: bulk_write.hpp:42
Use a session for a sequence of operations, optionally with either causal consistency or snapshots.
Definition: client_session.hpp:40
client_session(client_session &&) noexcept
Move constructs a session.
std::function< void(client_session *)> with_transaction_cb
Helper to run a user-provided callback within a transaction.
Definition: client_session.hpp:185
void abort_transaction()
Aborts a transaction on the current client session.
void commit_transaction()
Commits a transaction on the current client session.
Class representing a client connection to MongoDB.
Definition: client.hpp:54
Class representing server side document groupings within a MongoDB database.
Definition: collection.hpp:85
Class representing a MongoDB database.
Definition: database.hpp:44
Class representing a MongoDB index view.
Definition: index_view.hpp:34
Class representing the optional arguments to mongocxx::client::start_session.
Definition: client_session.hpp:32
Class representing the optional arguments for a transaction.
Definition: transaction.hpp:37
Class representing a MongoDB search index view.
Definition: search_index_view.hpp:19
The top-level namespace for bsoncxx library entities.
Definition: element.hpp:24
The top-level namespace for mongocxx library entities.
Definition: bulk_write.hpp:24