Couchbase C++ SDK 1.2.0 (rev. c2439a4)
Loading...
Searching...
No Matches
collection.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright 2020-Present Couchbase, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
23#include <couchbase/error.hxx>
25#include <couchbase/expiry.hxx>
47
48#include <future>
49#include <memory>
50
51namespace couchbase
52{
53#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
54namespace core
55{
56class cluster;
57} // namespace core
58class bucket;
59class scope;
60class collection_impl;
61namespace crypto
62{
63class manager;
64} // namespace crypto
65#endif
66
73class collection
74{
75public:
82 static constexpr auto default_name{ "_default" };
83
92 [[nodiscard]] auto bucket_name() const -> const std::string&;
93
102 [[nodiscard]] auto scope_name() const -> const std::string&;
103
112 [[nodiscard]] auto name() const -> const std::string&;
113
122 [[nodiscard]] auto binary() const -> binary_collection;
123
139 void get(std::string document_id, const get_options& options, get_handler&& handler) const;
140
156 [[nodiscard]] auto get(std::string document_id, const get_options& options = {}) const
157 -> std::future<std::pair<error, get_result>>;
158
175 void get_and_touch(std::string document_id,
176 std::chrono::seconds duration,
177 const get_and_touch_options& options,
178 get_and_touch_handler&& handler) const;
179
196 [[nodiscard]] auto get_and_touch(std::string document_id,
197 std::chrono::seconds duration,
198 const get_and_touch_options& options = {}) const
199 -> std::future<std::pair<error, get_result>>;
200
217 void get_and_touch(std::string document_id,
218 std::chrono::system_clock::time_point time_point,
219 const get_and_touch_options& options,
220 get_and_touch_handler&& handler) const;
221
238 [[nodiscard]] auto get_and_touch(std::string document_id,
239 std::chrono::system_clock::time_point time_point,
240 const get_and_touch_options& options = {}) const
241 -> std::future<std::pair<error, get_result>>;
242
259 void touch(std::string document_id,
260 std::chrono::seconds duration,
261 const touch_options& options,
262 touch_handler&& handler) const;
263
280 [[nodiscard]] auto touch(std::string document_id,
281 std::chrono::seconds duration,
282 const touch_options& options = {}) const
283 -> std::future<std::pair<error, result>>;
284
301 void touch(std::string document_id,
302 std::chrono::system_clock::time_point time_point,
303 const touch_options& options,
304 touch_handler&& handler) const;
305
322 [[nodiscard]] auto touch(std::string document_id,
323 std::chrono::system_clock::time_point time_point,
324 const touch_options& options = {}) const
325 -> std::future<std::pair<error, result>>;
326
344 void get_any_replica(std::string document_id,
345 const get_any_replica_options& options,
346 get_any_replica_handler&& handler) const;
347
372 [[nodiscard]] auto get_any_replica(std::string document_id,
373 const get_any_replica_options& options = {}) const
374 -> std::future<std::pair<error, get_replica_result>>;
375
392 void get_all_replicas(std::string document_id,
393 const get_all_replicas_options& options,
394 get_all_replicas_handler&& handler) const;
395
419 [[nodiscard]] auto get_all_replicas(std::string document_id,
420 const get_all_replicas_options& options = {}) const
421 -> std::future<std::pair<error, get_all_replicas_result>>;
422
438 void upsert(std::string document_id,
439 codec::encoded_value document,
440 const upsert_options& options,
441 upsert_handler&& handler) const;
442
460 template<typename Transcoder = codec::default_json_transcoder, typename Document>
461 void upsert(std::string document_id,
462 Document document,
463 const upsert_options& options,
464 upsert_handler&& handler) const
465 {
466 return upsert(
467 std::move(document_id), encode_document<Transcoder>(document), options, std::move(handler));
468 }
469
485 [[nodiscard]] auto upsert(std::string document_id,
486 codec::encoded_value document,
487 const upsert_options& options) const
488 -> std::future<std::pair<error, mutation_result>>;
489
507 template<typename Transcoder = codec::default_json_transcoder, typename Document>
508 [[nodiscard]] auto upsert(std::string document_id,
509 const Document& document,
510 const upsert_options& options = {}) const
511 -> std::future<std::pair<error, mutation_result>>
512 {
513 return upsert(std::move(document_id), encode_document<Transcoder>(document), options);
514 }
515
530 void insert(std::string document_id,
531 codec::encoded_value document,
532 const insert_options& options,
533 insert_handler&& handler) const;
534
554 template<typename Transcoder = codec::default_json_transcoder,
555 typename Document,
556 std::enable_if_t<!std::is_same_v<codec::encoded_value, Document>, bool> = true>
557 void insert(std::string document_id,
558 Document document,
559 const insert_options& options,
560 insert_handler&& handler) const
561 {
562 return insert(
563 std::move(document_id), encode_document<Transcoder>(document), options, std::move(handler));
564 }
565
580 [[nodiscard]] auto insert(std::string document_id,
581 codec::encoded_value document,
582 const insert_options& options) const
583 -> std::future<std::pair<error, mutation_result>>;
584
604 template<typename Transcoder = codec::default_json_transcoder,
605 typename Document,
606 std::enable_if_t<!std::is_same_v<codec::encoded_value, Document>, bool> = true>
607 [[nodiscard]] auto insert(std::string document_id,
608 const Document& document,
609 const insert_options& options = {}) const
610 -> std::future<std::pair<error, mutation_result>>
611 {
612 return insert(std::move(document_id), encode_document<Transcoder>(document), options);
613 }
614
629 void replace(std::string document_id,
630 codec::encoded_value document,
631 const replace_options& options,
632 replace_handler&& handler) const;
633
655 template<typename Transcoder = codec::default_json_transcoder,
656 typename Document,
657 std::enable_if_t<!std::is_same_v<codec::encoded_value, Document>, bool> = true>
658 void replace(std::string document_id,
659 Document document,
660 const replace_options& options,
661 replace_handler&& handler) const
662 {
663 return replace(
664 std::move(document_id), encode_document<Transcoder>(document), options, std::move(handler));
665 }
666
681 [[nodiscard]] auto replace(std::string document_id,
682 codec::encoded_value document,
683 const replace_options& options) const
684 -> std::future<std::pair<error, mutation_result>>;
685
707 template<typename Transcoder = codec::default_json_transcoder,
708 typename Document,
709 std::enable_if_t<!std::is_same_v<codec::encoded_value, Document>, bool> = true>
710 [[nodiscard]] auto replace(std::string document_id,
711 const Document& document,
712 const replace_options& options = {}) const
713 -> std::future<std::pair<error, mutation_result>>
714 {
715 return replace(std::move(document_id), encode_document<Transcoder>(document), options);
716 }
717
735 void remove(std::string document_id,
736 const remove_options& options,
737 remove_handler&& handler) const;
738
756 [[nodiscard]] auto remove(std::string document_id, const remove_options& options = {}) const
757 -> std::future<std::pair<error, mutation_result>>;
758
779 void mutate_in(std::string document_id,
780 const mutate_in_specs& specs,
781 const mutate_in_options& options,
782 mutate_in_handler&& handler) const;
783
804 [[nodiscard]] auto mutate_in(std::string document_id,
805 const mutate_in_specs& specs,
806 const mutate_in_options& options = {}) const
807 -> std::future<std::pair<error, mutate_in_result>>;
808
825 void lookup_in(std::string document_id,
826 const lookup_in_specs& specs,
827 const lookup_in_options& options,
828 lookup_in_handler&& handler) const;
829
846 [[nodiscard]] auto lookup_in(std::string document_id,
847 const lookup_in_specs& specs,
848 const lookup_in_options& options = {}) const
849 -> std::future<std::pair<error, lookup_in_result>>;
850
868 void lookup_in_all_replicas(std::string document_id,
869 const lookup_in_specs& specs,
870 const lookup_in_all_replicas_options& options,
871 lookup_in_all_replicas_handler&& handler) const;
872
890 [[nodiscard]] auto lookup_in_all_replicas(std::string document_id,
891 const lookup_in_specs& specs,
892 const lookup_in_all_replicas_options& options = {})
893 const -> std::future<std::pair<error, lookup_in_all_replicas_result>>;
894
912 void lookup_in_any_replica(std::string document_id,
913 const lookup_in_specs& specs,
914 const lookup_in_any_replica_options& options,
915 lookup_in_any_replica_handler&& handler) const;
916
934 [[nodiscard]] auto lookup_in_any_replica(std::string document_id,
935 const lookup_in_specs& specs,
936 const lookup_in_any_replica_options& options = {}) const
937 -> std::future<std::pair<error, lookup_in_replica_result>>;
938
950 void get_and_lock(std::string document_id,
951 std::chrono::seconds lock_duration,
952 const get_and_lock_options& options,
953 get_and_lock_handler&& handler) const;
954
966 [[nodiscard]] auto get_and_lock(std::string document_id,
967 std::chrono::seconds lock_duration,
968 const get_and_lock_options& options = {}) const
969 -> std::future<std::pair<error, get_result>>;
970
989 void unlock(std::string document_id,
991 const unlock_options& options,
992 unlock_handler&& handler) const;
993
1012 [[nodiscard]] auto unlock(std::string document_id,
1014 const unlock_options& options = {}) const -> std::future<error>;
1015
1029 void exists(std::string document_id,
1030 const exists_options& options,
1031 exists_handler&& handler) const;
1032
1046 [[nodiscard]] auto exists(std::string document_id, const exists_options& options = {}) const
1047 -> std::future<std::pair<error, exists_result>>;
1048
1064 void scan(const scan_type& scan_type, const scan_options& options, scan_handler&& handler) const;
1065
1081 [[nodiscard]] auto scan(const scan_type& scan_type, const scan_options& options = {}) const
1082 -> std::future<std::pair<error, scan_result>>;
1083
1084 [[nodiscard]] auto query_indexes() const -> collection_query_index_manager;
1085
1086private:
1087 friend class bucket;
1088 friend class scope;
1089
1090 [[nodiscard]] auto crypto_manager() const -> const std::shared_ptr<crypto::manager>&;
1091
1092 template<typename Transcoder, typename Document>
1093 [[nodiscard]] auto encode_document(const Document& document) const -> codec::encoded_value
1094 {
1096 return Transcoder::encode(document, crypto_manager());
1097 } else {
1098 return Transcoder::encode(document);
1099 }
1100 }
1101
1102 collection(core::cluster core,
1103 std::string_view bucket_name,
1104 std::string_view scope_name,
1105 std::string_view name,
1106 std::shared_ptr<crypto::manager> crypto_manager);
1107
1108 std::shared_ptr<collection_impl> impl_;
1109};
1110} // namespace couchbase
Allows to perform certain operations on non-JSON documents.
Definition binary_collection.hxx:47
Provides access to Couchbase bucket.
Definition bucket.hxx:49
CAS is a special type that represented in protocol using unsigned 64-bit integer, but only equality c...
Definition cas.hxx:34
The Query Index Manager interface contains the means for managing indexes used for queries.
Definition collection_query_index_manager.hxx:53
The collection provides access to all collection APIs.
Definition collection.hxx:74
auto touch(std::string document_id, std::chrono::system_clock::time_point time_point, const touch_options &options={}) const -> std::future< std::pair< error, result > >
Updates the expiration a document given an id, without modifying or returning its value.
auto replace(std::string document_id, codec::encoded_value document, const replace_options &options) const -> std::future< std::pair< error, mutation_result > >
Replaces a body of the document which already exists with specified encoded body.
friend class scope
Definition collection.hxx:1088
void replace(std::string document_id, Document document, const replace_options &options, replace_handler &&handler) const
Replaces a full document which already exists.
Definition collection.hxx:658
friend class bucket
Definition collection.hxx:1087
auto insert(std::string document_id, const Document &document, const insert_options &options={}) const -> std::future< std::pair< error, mutation_result > >
Inserts a full document which does not exist yet with custom options.
Definition collection.hxx:607
auto scan(const scan_type &scan_type, const scan_options &options={}) const -> std::future< std::pair< error, scan_result > >
Performs a key-value scan operation on the collection.
auto mutate_in(std::string document_id, const mutate_in_specs &specs, const mutate_in_options &options={}) const -> std::future< std::pair< error, mutate_in_result > >
Performs mutations to document fragments.
void remove(std::string document_id, const remove_options &options, remove_handler &&handler) const
Removes a Document from a collection.
auto bucket_name() const -> const std::string &
Returns name of the bucket where the collection is defined.
auto unlock(std::string document_id, couchbase::cas cas, const unlock_options &options={}) const -> std::future< error >
Unlocks a document if it has been locked previously, with default options.
void lookup_in_all_replicas(std::string document_id, const lookup_in_specs &specs, const lookup_in_all_replicas_options &options, lookup_in_all_replicas_handler &&handler) const
Performs lookups to document fragments with default options from all replicas and the active node and...
void get_and_lock(std::string document_id, std::chrono::seconds lock_duration, const get_and_lock_options &options, get_and_lock_handler &&handler) const
Gets a document for a given id and places a pessimistic lock on it for mutations.
static constexpr auto default_name
Constant for the name of the default collection in the bucket.
Definition collection.hxx:82
auto binary() const -> binary_collection
Provides access to the binary APIs, not used for JSON documents.
void scan(const scan_type &scan_type, const scan_options &options, scan_handler &&handler) const
Performs a key-value scan operation on the collection.
void lookup_in_any_replica(std::string document_id, const lookup_in_specs &specs, const lookup_in_any_replica_options &options, lookup_in_any_replica_handler &&handler) const
Performs lookups to document fragments with default options from all replicas and returns the first f...
void touch(std::string document_id, std::chrono::system_clock::time_point time_point, const touch_options &options, touch_handler &&handler) const
Updates the expiration a document given an id, without modifying or returning its value.
void exists(std::string document_id, const exists_options &options, exists_handler &&handler) const
Checks if the document exists on the server.
void get_and_touch(std::string document_id, std::chrono::seconds duration, const get_and_touch_options &options, get_and_touch_handler &&handler) const
Fetches a full document and resets its expiration time to the value provided.
auto lookup_in_all_replicas(std::string document_id, const lookup_in_specs &specs, const lookup_in_all_replicas_options &options={}) const -> std::future< std::pair< error, lookup_in_all_replicas_result > >
Performs lookups to document fragments with default options from all replicas and the active node and...
void get_any_replica(std::string document_id, const get_any_replica_options &options, get_any_replica_handler &&handler) const
Reads all available replicas, and returns the first found.
auto get_and_touch(std::string document_id, std::chrono::seconds duration, const get_and_touch_options &options={}) const -> std::future< std::pair< error, get_result > >
Fetches a full document and resets its expiration time to the value provided.
void lookup_in(std::string document_id, const lookup_in_specs &specs, const lookup_in_options &options, lookup_in_handler &&handler) const
Performs lookups to document fragments with default options.
void replace(std::string document_id, codec::encoded_value document, const replace_options &options, replace_handler &&handler) const
Replaces a body of the document which already exists with specified encoded body.
auto replace(std::string document_id, const Document &document, const replace_options &options={}) const -> std::future< std::pair< error, mutation_result > >
Replaces a full document which already exists.
Definition collection.hxx:710
auto get_and_touch(std::string document_id, std::chrono::system_clock::time_point time_point, const get_and_touch_options &options={}) const -> std::future< std::pair< error, get_result > >
Fetches a full document and resets its expiration time to the absolute value provided.
auto upsert(std::string document_id, const Document &document, const upsert_options &options={}) const -> std::future< std::pair< error, mutation_result > >
Upserts a full document which might or might not exist yet with custom options.
Definition collection.hxx:508
auto touch(std::string document_id, std::chrono::seconds duration, const touch_options &options={}) const -> std::future< std::pair< error, result > >
Updates the expiration a document given an id, without modifying or returning its value.
void upsert(std::string document_id, codec::encoded_value document, const upsert_options &options, upsert_handler &&handler) const
Upserts an encoded body of the document which might or might not exist yet, with custom options.
void upsert(std::string document_id, Document document, const upsert_options &options, upsert_handler &&handler) const
Upserts a full document which might or might not exist yet with custom options.
Definition collection.hxx:461
auto remove(std::string document_id, const remove_options &options={}) const -> std::future< std::pair< error, mutation_result > >
Removes a Document from a collection.
auto name() const -> const std::string &
Returns name of the collection.
auto upsert(std::string document_id, codec::encoded_value document, const upsert_options &options) const -> std::future< std::pair< error, mutation_result > >
Upserts an encoded body of the document which might or might not exist yet, with custom options.
auto lookup_in(std::string document_id, const lookup_in_specs &specs, const lookup_in_options &options={}) const -> std::future< std::pair< error, lookup_in_result > >
Performs lookups to document fragments with default options.
auto exists(std::string document_id, const exists_options &options={}) const -> std::future< std::pair< error, exists_result > >
Checks if the document exists on the server.
void insert(std::string document_id, codec::encoded_value document, const insert_options &options, insert_handler &&handler) const
Inserts an encoded body of the document which does not exist yet with custom options.
void get(std::string document_id, const get_options &options, get_handler &&handler) const
Fetches the full document from this collection.
auto get_all_replicas(std::string document_id, const get_all_replicas_options &options={}) const -> std::future< std::pair< error, get_all_replicas_result > >
Reads from all available replicas and the active node and returns the results as a vector.
auto get_and_lock(std::string document_id, std::chrono::seconds lock_duration, const get_and_lock_options &options={}) const -> std::future< std::pair< error, get_result > >
Gets a document for a given id and places a pessimistic lock on it for mutations.
auto get_any_replica(std::string document_id, const get_any_replica_options &options={}) const -> std::future< std::pair< error, get_replica_result > >
Reads all available replicas, and returns the first found.
auto lookup_in_any_replica(std::string document_id, const lookup_in_specs &specs, const lookup_in_any_replica_options &options={}) const -> std::future< std::pair< error, lookup_in_replica_result > >
Performs lookups to document fragments with default options from all replicas and returns the first f...
void get_all_replicas(std::string document_id, const get_all_replicas_options &options, get_all_replicas_handler &&handler) const
Reads from all available replicas and the active node and returns the results as a vector.
auto query_indexes() const -> collection_query_index_manager
void mutate_in(std::string document_id, const mutate_in_specs &specs, const mutate_in_options &options, mutate_in_handler &&handler) const
Performs mutations to document fragments.
void unlock(std::string document_id, couchbase::cas cas, const unlock_options &options, unlock_handler &&handler) const
Unlocks a document if it has been locked previously, with default options.
auto insert(std::string document_id, codec::encoded_value document, const insert_options &options) const -> std::future< std::pair< error, mutation_result > >
Inserts an encoded body of the document which does not exist yet with custom options.
auto scope_name() const -> const std::string &
Returns name of the scope where the collection is defined.
void touch(std::string document_id, std::chrono::seconds duration, const touch_options &options, touch_handler &&handler) const
Updates the expiration a document given an id, without modifying or returning its value.
void get_and_touch(std::string document_id, std::chrono::system_clock::time_point time_point, const get_and_touch_options &options, get_and_touch_handler &&handler) const
Fetches a full document and resets its expiration time to the absolute value provided.
void insert(std::string document_id, Document document, const insert_options &options, insert_handler &&handler) const
Inserts a full document which does not exist yet with custom options.
Definition collection.hxx:557
Definition lookup_in_specs.hxx:34
Definition mutate_in_specs.hxx:80
The scope identifies a group of collections and allows high application density as a result.
Definition scope.hxx:47
Definition analytics_options.hxx:35
constexpr bool is_crypto_transcoder_v
Definition transcoder_traits.hxx:36
json_transcoder< tao_json_serializer > default_json_transcoder
Definition default_json_transcoder.hxx:28
Definition expiry.hxx:24
Definition internal.hxx:27
Represents a single item from the result of scan()
Definition allow_querying_search_index_options.hxx:28
std::function< void(error, get_replica_result)> get_any_replica_handler
The signature for the handler of the get_any_replica() operation.
Definition get_any_replica_options.hxx:92
std::function< void(error, mutation_result)> insert_handler
The signature for the handler of the insert() operation.
Definition insert_options.hxx:114
@ replace
Replace the document, fail if it does not exist.
Definition store_semantics.hxx:37
@ upsert
Replace the document or create it if it does not exist.
Definition store_semantics.hxx:45
@ insert
Create the document, fail if it exists.
Definition store_semantics.hxx:53
std::function< void(error, lookup_in_result)> lookup_in_handler
The signature for the handler of the lookup_in() operation.
Definition lookup_in_options.hxx:94
std::function< void(error, get_result)> get_handler
The signature for the handler of the get() operation.
Definition get_options.hxx:111
std::function< void(error, get_all_replicas_result)> get_all_replicas_handler
The signature for the handler of the get_all_replicas() operation.
Definition get_all_replicas_options.hxx:101
std::function< void(error, get_result)> get_and_lock_handler
The signature for the handler of the get_and_lock() operation.
Definition get_and_lock_options.hxx:70
std::function< void(error, mutation_result)> remove_handler
The signature for the handler of the remove() operation.
Definition remove_options.hxx:103
std::function< void(error, mutation_result)> upsert_handler
The signature for the handler of the upsert() operation.
Definition upsert_options.hxx:138
std::function< void(error, mutate_in_result)> mutate_in_handler
The signature for the handler of the mutate_in() operation.
Definition mutate_in_options.hxx:219
std::function< void(error, lookup_in_all_replicas_result)> lookup_in_all_replicas_handler
The signature for the handler of the lookup_in_all_replicas() operation.
Definition lookup_in_all_replicas_options.hxx:106
std::function< void(error, scan_result)> scan_handler
The signature for the handler of the scan() operation.
Definition scan_options.hxx:168
std::function< void(error, result)> touch_handler
The signature for the handler of the touch() operation.
Definition touch_options.hxx:70
std::function< void(error, mutation_result)> replace_handler
The signature for the handler of the replace() operation.
Definition replace_options.hxx:167
std::function< void(error, lookup_in_replica_result)> lookup_in_any_replica_handler
The signature for the handler of the lookup_in_any_replica() operation.
Definition lookup_in_any_replica_options.hxx:97
std::function< void(error)> unlock_handler
The signature for the handler of the unlock() operation.
Definition unlock_options.hxx:73
std::function< void(error, get_result)> get_and_touch_handler
The signature for the handler of the get_and_touch() operation.
Definition get_and_touch_options.hxx:70
std::function< void(error, exists_result)> exists_handler
The signature for the handler of the exists() operation.
Definition exists_options.hxx:74
Definition encoded_value.hxx:27
Options for exists().
Definition exists_options.hxx:41
Options for get_all_replicas().
Definition get_all_replicas_options.hxx:36
Options for collection::get_and_lock().
Definition get_and_lock_options.hxx:37
Options for collection::get_and_touch().
Definition get_and_touch_options.hxx:37
Options for collection::get_any_replica().
Definition get_any_replica_options.hxx:35
Options for collection::get().
Definition get_options.hxx:37
Options for insert().
Definition insert_options.hxx:41
Options for lookup_in_all_replicas().
Definition lookup_in_all_replicas_options.hxx:41
Options for lookup_in_any_replica().
Definition lookup_in_any_replica_options.hxx:40
Options for lookup_in().
Definition lookup_in_options.hxx:44
Options for mutate_in().
Definition mutate_in_options.hxx:44
Options for remove().
Definition remove_options.hxx:41
Options for replace().
Definition replace_options.hxx:41
Options for scan().
Definition scan_options.hxx:40
The base class for the different scan types.
Definition scan_type.hxx:96
Options for collection::touch().
Definition touch_options.hxx:37
Options for unlock().
Definition unlock_options.hxx:40
Options for upsert().
Definition upsert_options.hxx:41