Couchbase Transactions C++ Client  1.0.0
Transactions client for couchbase
transactions.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2021 Couchbase, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cmath>
20 #include <functional>
21 #include <thread>
22 
23 #include <couchbase/client/cluster.hxx>
24 #include <couchbase/transactions/attempt_context.hxx>
25 #include <couchbase/transactions/exceptions.hxx>
26 #include <couchbase/transactions/transaction_config.hxx>
27 #include <couchbase/transactions/transaction_result.hxx>
33 namespace couchbase
34 {
35 namespace transactions
36 {
39  class transactions_cleanup;
40 
42  typedef std::function<void(attempt_context&)> logic;
43 
106  class transactions
107  {
108  public:
118 
122  ~transactions();
123 
136 
141  void commit(attempt_context& ctx)
142  {
143  ctx.commit();
144  }
145 
150  void rollback(attempt_context& ctx)
151  {
152  ctx.rollback();
153  }
154 
161  void close();
162 
168  CB_NODISCARD transaction_config& config()
169  {
170  return config_;
171  }
172 
177  CB_NODISCARD const transactions_cleanup& cleanup() const
178  {
179  return *cleanup_;
180  }
186  CB_NODISCARD transactions_cleanup& cleanup()
187  {
188  return *cleanup_;
189  }
190 
196  CB_NODISCARD couchbase::cluster& cluster_ref()
197  {
198  return cluster_;
199  }
200 
201  private:
202  couchbase::cluster& cluster_;
203  transaction_config config_;
204  std::unique_ptr<transactions_cleanup> cleanup_;
205  const int max_attempts_{ 1000 };
206  const std::chrono::milliseconds min_retry_delay_{ 1 };
207  };
208 } // namespace transactions
209 } // namespace couchbase
Definition: bucket.hxx:33
void close()
Shut down the transactions object.
transaction_result run(const logic &logic)
Run a transaction.
Main class for creating a transaction.
Definition: transactions.hxx:107
CB_NODISCARD transaction_config & config()
Return reference to transaction_config.
Definition: transactions.hxx:171
transactions(couchbase::cluster &cluster, const transaction_config &config)
Create a transactions object.
Results of a transaction.
Definition: transaction_result.hxx:32
void commit(attempt_context &ctx)
Definition: transactions.hxx:143
Configuration parameters for transactions.
Definition: transaction_config.hxx:38
void rollback(attempt_context &ctx)
Definition: transactions.hxx:153
Connects to a couchbase cluster, exposes cluster operations and bucket accessors. ...
Definition: cluster.hxx:172
std::function< void(attempt_context &)> logic
Transaction logic should be contained in a lambda of this form.
Definition: transactions.hxx:40
CB_NODISCARD const transactions_cleanup & cleanup() const
Definition: transactions.hxx:181
Provides methods to perform transactional operations.
Definition: attempt_context.hxx:35
CB_NODISCARD couchbase::cluster & cluster_ref()
Return a reference to the cluster.
Definition: transactions.hxx:201