Couchbase C++ SDK 1.0.1 (rev. 58d46d7)
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
21
22#include <stdexcept>
23
24namespace couchbase
25{
26class collection;
27class scope;
28
29namespace transactions
30{
40{
41public:
54 virtual auto get(const couchbase::collection& coll,
55 const std::string& id) -> std::pair<error, transaction_get_result> = 0;
56
74 const std::string& id)
75 -> std::pair<error, transaction_get_result> = 0;
76
92 template<typename Transcoder = codec::default_json_transcoder,
93 typename Document,
94 std::enable_if_t<!std::is_same_v<codec::encoded_value, Document>, bool> = true>
95 auto insert(const couchbase::collection& coll,
96 const std::string& id,
97 const Document& content) -> std::pair<error, transaction_get_result>
98 {
100 try {
101 data = Transcoder::encode(content);
102 } catch (std::system_error& e) {
103 return { error(e.code(), e.what()), {} };
104 } catch (std::runtime_error& e) {
105 return { error(errc::common::encoding_failure, e.what()), {} };
106 }
107 return insert_raw(coll, id, data);
108 }
109
125 template<typename Transcoder = codec::default_json_transcoder,
126 typename Document,
127 std::enable_if_t<!std::is_same_v<codec::encoded_value, Document>, bool> = true>
129 const Document& content) -> std::pair<error, transaction_get_result>
130 {
132 try {
133 data = Transcoder::encode(content);
134 } catch (std::system_error& e) {
135 return { error(e.code(), e.what()), {} };
136 } catch (std::runtime_error& e) {
137 return { error(errc::common::encoding_failure, e.what()), {} };
138 }
139 return replace_raw(doc, data);
140 }
141
152 virtual auto remove(const transaction_get_result& doc) -> error = 0;
153
162 auto query(const std::string& statement, const transaction_query_options& options = {})
163 -> std::pair<error, transaction_query_result>;
164
174 auto query(const scope& scope,
175 const std::string& statement,
176 const transaction_query_options& opts = {})
177 -> std::pair<error, transaction_query_result>;
178
179 virtual ~attempt_context() = default;
180
181protected:
183 virtual auto replace_raw(const transaction_get_result& doc, codec::encoded_value content)
184 -> std::pair<error, transaction_get_result> = 0;
186 virtual auto insert_raw(const couchbase::collection& coll,
187 const std::string& id,
188 codec::encoded_value document)
189 -> std::pair<error, transaction_get_result> = 0;
191 virtual auto do_public_query(const std::string& statement,
192 const transaction_query_options& options,
193 std::optional<std::string> query_context)
194 -> std::pair<error, transaction_query_result> = 0;
195};
196} // namespace transactions
197} // 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:40
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:95
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:128
Definition transaction_get_result.hxx:39
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.
Represents a single item from the result of collection::scan()
Definition allow_querying_search_index_options.hxx:28
@ error
Indicates that the ping operation failed.
Definition encoded_value.hxx:27