Couchbase C++ SDK 1.1.0 (rev. effbd6e)
Loading...
Searching...
No Matches
attempt_context.hxx
Go to the documentation of this file.
1/*
2 * Copyright 2021-Present 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#pragma once
17
27
28#include <stdexcept>
29#include <vector>
30
31namespace couchbase
32{
33class collection;
34class scope;
35
36namespace transactions
37{
45
47{
48public:
72 virtual auto get(const couchbase::collection& coll, const std::string& id)
73 -> std::pair<error, transaction_get_result> = 0;
74
98 const std::string& id)
99 -> std::pair<error, transaction_get_result> = 0;
100
101 virtual auto get_multi(const std::vector<transaction_get_multi_spec>& specs,
102 const transaction_get_multi_options& options)
103 -> std::pair<error, std::optional<transaction_get_multi_result>> = 0;
104
106 const std::vector<transaction_get_multi_replicas_from_preferred_server_group_spec>& specs,
108 -> std::pair<
109 error,
110 std::optional<transaction_get_multi_replicas_from_preferred_server_group_result>> = 0;
111
127 template<typename Transcoder = codec::default_json_transcoder,
128 typename Document,
129 std::enable_if_t<!std::is_same_v<codec::encoded_value, Document>, bool> = true>
130 auto insert(const couchbase::collection& coll, const std::string& id, const Document& content)
131 -> std::pair<error, transaction_get_result>
132 {
134 try {
135 data = Transcoder::encode(content);
136 } catch (std::system_error& e) {
137 return { error(e.code(), e.what()), {} };
138 } catch (std::runtime_error& e) {
139 return { error(errc::common::encoding_failure, e.what()), {} };
140 }
141 return insert_raw(coll, id, data);
142 }
143
168 template<typename Transcoder = codec::default_json_transcoder,
169 typename Document,
170 std::enable_if_t<!std::is_same_v<codec::encoded_value, Document>, bool> = true>
171 auto replace(const transaction_get_result& doc, const Document& content)
172 -> std::pair<error, transaction_get_result>
173 {
175 try {
176 data = Transcoder::encode(content);
177 } catch (std::system_error& e) {
178 return { error(e.code(), e.what()), {} };
179 } catch (std::runtime_error& e) {
180 return { error(errc::common::encoding_failure, e.what()), {} };
181 }
182 return replace_raw(doc, data);
183 }
184
195 virtual auto remove(const transaction_get_result& doc) -> error = 0;
196
205 auto query(const std::string& statement, const transaction_query_options& options = {})
206 -> std::pair<error, transaction_query_result>;
207
217 auto query(const scope& scope,
218 const std::string& statement,
219 const transaction_query_options& opts = {})
220 -> std::pair<error, transaction_query_result>;
221
222 virtual ~attempt_context() = default;
223
224protected:
226 virtual auto replace_raw(const transaction_get_result& doc, codec::encoded_value content)
227 -> std::pair<error, transaction_get_result> = 0;
229 virtual auto insert_raw(const couchbase::collection& coll,
230 const std::string& id,
231 codec::encoded_value document)
232 -> std::pair<error, transaction_get_result> = 0;
234 virtual auto do_public_query(const std::string& statement,
235 const transaction_query_options& options,
236 std::optional<std::string> query_context)
237 -> std::pair<error, transaction_query_result> = 0;
238};
239} // namespace transactions
240} // namespace couchbase
The collection provides access to all collection APIs.
Definition collection.hxx:70
Definition error.hxx:30
The scope identifies a group of collections and allows high application density as a result.
Definition scope.hxx:47
The attempt_context is used for all synchronous transaction operations.
Definition attempt_context.hxx:47
virtual auto get_multi(const std::vector< transaction_get_multi_spec > &specs, const transaction_get_multi_options &options) -> std::pair< error, std::optional< transaction_get_multi_result > >=0
virtual auto get_multi_replicas_from_preferred_server_group(const std::vector< transaction_get_multi_replicas_from_preferred_server_group_spec > &specs, const transaction_get_multi_replicas_from_preferred_server_group_options &options) -> std::pair< error, std::optional< transaction_get_multi_replicas_from_preferred_server_group_result > >=0
auto query(const scope &scope, const std::string &statement, const transaction_query_options &opts={}) -> std::pair< error, transaction_query_result >
Perform a scoped query.
virtual auto get_replica_from_preferred_server_group(const couchbase::collection &coll, const std::string &id) -> std::pair< error, transaction_get_result >=0
Get a document copy from the selected server group.
auto insert(const couchbase::collection &coll, const std::string &id, const Document &content) -> std::pair< error, transaction_get_result >
Insert a document into a collection.
Definition attempt_context.hxx:130
auto query(const std::string &statement, const transaction_query_options &options={}) -> std::pair< error, transaction_query_result >
Perform an unscoped query.
virtual auto remove(const transaction_get_result &doc) -> error=0
Remove a document.
virtual auto get(const couchbase::collection &coll, const std::string &id) -> std::pair< error, transaction_get_result >=0
Get a document from a collection.
auto replace(const transaction_get_result &doc, const Document &content) -> std::pair< error, transaction_get_result >
Replace the contents of a document in a collection.
Definition attempt_context.hxx:171
Definition transaction_get_multi_options.hxx:30
Definition transaction_get_multi_replicas_from_preferred_server_group_options.hxx:30
The representation of the document in context of distributed transaction.
Definition transaction_get_result.hxx:57
The transaction_query_options are options specific to a query.
Definition transaction_query_options.hxx:35
json_transcoder< tao_json_serializer > default_json_transcoder
Definition default_json_transcoder.hxx:28
@ encoding_failure
Returned when encoding of a user object failed while trying to write it to the cluster.
Definition error_codes.hxx:267
Definition transactions.hxx:28
Represents a single item from the result of scan()
Definition allow_querying_search_index_options.hxx:28
@ error
Indicates that the ping operation failed.
Definition endpoint_ping_report.hxx:52
Definition encoded_value.hxx:27