couchbase 3.4.0 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/ext/couchbase/CMakeLists.txt +8 -3
  4. data/ext/couchbase/cmake/CompilerWarnings.cmake +12 -4
  5. data/ext/couchbase/cmake/Documentation.cmake +4 -3
  6. data/ext/couchbase/cmake/OpenSSL.cmake +52 -7
  7. data/ext/couchbase/cmake/VersionInfo.cmake +39 -3
  8. data/ext/couchbase/cmake/test_openssl.cxx +7 -0
  9. data/ext/couchbase/core/crypto/CMakeLists.txt +5 -1
  10. data/ext/couchbase/core/impl/collection_query_index_manager.cxx +3 -3
  11. data/ext/couchbase/core/impl/get_all_query_indexes.cxx +3 -3
  12. data/ext/couchbase/core/impl/transaction_get_result.cxx +54 -0
  13. data/ext/couchbase/core/meta/CMakeLists.txt +7 -5
  14. data/ext/couchbase/core/meta/version.cxx +19 -0
  15. data/ext/couchbase/core/operations/document_search.cxx +5 -2
  16. data/ext/couchbase/core/operations/document_search.hxx +0 -1
  17. data/ext/couchbase/core/transactions/atr_cleanup_entry.cxx +1 -0
  18. data/ext/couchbase/core/transactions/attempt_context_impl.cxx +62 -31
  19. data/ext/couchbase/core/transactions/attempt_context_impl.hxx +43 -22
  20. data/ext/couchbase/core/transactions/forward_compat.hxx +2 -2
  21. data/ext/couchbase/core/transactions/internal/transaction_context.hxx +1 -1
  22. data/ext/couchbase/core/transactions/internal/transaction_fields.hxx +1 -0
  23. data/ext/couchbase/core/transactions/staged_mutation.cxx +1 -1
  24. data/ext/couchbase/core/transactions/staged_mutation.hxx +12 -2
  25. data/ext/couchbase/core/transactions/transaction_context.cxx +8 -11
  26. data/ext/couchbase/core/transactions/transaction_get_result.cxx +41 -31
  27. data/ext/couchbase/core/transactions/transaction_get_result.hxx +7 -3
  28. data/ext/couchbase/core/transactions/transaction_links.hxx +13 -1
  29. data/ext/couchbase/core/transactions/waitable_op_list.hxx +1 -0
  30. data/ext/couchbase/couchbase/cluster.hxx +2 -2
  31. data/ext/couchbase/couchbase/cluster_options.hxx +10 -10
  32. data/ext/couchbase/couchbase/collection.hxx +22 -17
  33. data/ext/couchbase/couchbase/collection_query_index_manager.hxx +1 -1
  34. data/ext/couchbase/couchbase/common_options.hxx +1 -1
  35. data/ext/couchbase/couchbase/configuration_profile.hxx +1 -1
  36. data/ext/couchbase/couchbase/configuration_profiles_registry.hxx +0 -1
  37. data/ext/couchbase/couchbase/create_primary_query_index_options.hxx +1 -1
  38. data/ext/couchbase/couchbase/drop_primary_query_index_options.hxx +1 -1
  39. data/ext/couchbase/couchbase/drop_query_index_options.hxx +1 -1
  40. data/ext/couchbase/couchbase/fmt/cas.hxx +12 -0
  41. data/ext/couchbase/couchbase/fmt/durability_level.hxx +6 -0
  42. data/ext/couchbase/couchbase/fmt/key_value_extended_error_info.hxx +6 -0
  43. data/ext/couchbase/couchbase/fmt/key_value_status_code.hxx +6 -0
  44. data/ext/couchbase/couchbase/fmt/mutation_token.hxx +6 -0
  45. data/ext/couchbase/couchbase/fmt/query_scan_consistency.hxx +6 -0
  46. data/ext/couchbase/couchbase/fmt/query_status.hxx +6 -0
  47. data/ext/couchbase/couchbase/fmt/retry_reason.hxx +6 -0
  48. data/ext/couchbase/couchbase/fmt/tls_verify_mode.hxx +6 -0
  49. data/ext/couchbase/couchbase/get_all_query_indexes_options.hxx +5 -4
  50. data/ext/couchbase/couchbase/query_index_manager.hxx +4 -2
  51. data/ext/couchbase/couchbase/scope.hxx +1 -1
  52. data/ext/couchbase/couchbase/subdoc/array_add_unique.hxx +2 -0
  53. data/ext/couchbase/couchbase/subdoc/array_append.hxx +2 -0
  54. data/ext/couchbase/couchbase/subdoc/array_insert.hxx +2 -0
  55. data/ext/couchbase/couchbase/subdoc/array_prepend.hxx +2 -0
  56. data/ext/couchbase/couchbase/subdoc/count.hxx +2 -0
  57. data/ext/couchbase/couchbase/subdoc/counter.hxx +2 -0
  58. data/ext/couchbase/couchbase/subdoc/exists.hxx +2 -0
  59. data/ext/couchbase/couchbase/subdoc/get.hxx +2 -0
  60. data/ext/couchbase/couchbase/subdoc/insert.hxx +2 -0
  61. data/ext/couchbase/couchbase/subdoc/remove.hxx +2 -0
  62. data/ext/couchbase/couchbase/subdoc/replace.hxx +3 -1
  63. data/ext/couchbase/couchbase/subdoc/upsert.hxx +2 -0
  64. data/ext/couchbase/couchbase/transaction_op_error_context.hxx +4 -4
  65. data/ext/couchbase/couchbase/transactions/transaction_get_result.hxx +36 -51
  66. data/ext/couchbase/couchbase/transactions/transactions_config.hxx +1 -1
  67. data/ext/couchbase/test/CMakeLists.txt +2 -0
  68. data/ext/couchbase/test/test_integration_examples.cxx +141 -0
  69. data/ext/couchbase/test/test_unit_transaction_utils.cxx +76 -19
  70. data/ext/couchbase.cxx +479 -20
  71. data/ext/extconf.rb +2 -1
  72. data/ext/revisions.rb +3 -2
  73. data/lib/couchbase/binary_collection.rb +4 -4
  74. data/lib/couchbase/collection.rb +5 -0
  75. data/lib/couchbase/errors.rb +10 -0
  76. data/lib/couchbase/management/collection_query_index_manager.rb +183 -0
  77. data/lib/couchbase/management/query_index_manager.rb +35 -3
  78. data/lib/couchbase/management.rb +1 -0
  79. data/lib/couchbase/options.rb +2 -3
  80. data/lib/couchbase/version.rb +1 -1
  81. metadata +9 -5
@@ -21,6 +21,12 @@
21
21
 
22
22
  #include <fmt/core.h>
23
23
 
24
+ /**
25
+ * Helper for fmtlib to format @ref couchbase::query_scan_consistency objects.
26
+ *
27
+ * @since 1.0.0
28
+ * @committed
29
+ */
24
30
  template<>
25
31
  struct fmt::formatter<couchbase::query_scan_consistency> {
26
32
  template<typename ParseContext>
@@ -21,6 +21,12 @@
21
21
 
22
22
  #include <fmt/core.h>
23
23
 
24
+ /**
25
+ * Helper for fmtlib to format @ref couchbase::query_status objects.
26
+ *
27
+ * @since 1.0.0
28
+ * @committed
29
+ */
24
30
  template<>
25
31
  struct fmt::formatter<couchbase::query_status> {
26
32
  template<typename ParseContext>
@@ -21,6 +21,12 @@
21
21
 
22
22
  #include <fmt/core.h>
23
23
 
24
+ /**
25
+ * Helper for fmtlib to format @ref couchbase::retry_reason objects.
26
+ *
27
+ * @since 1.0.0
28
+ * @committed
29
+ */
24
30
  template<>
25
31
  struct fmt::formatter<couchbase::retry_reason> {
26
32
  template<typename ParseContext>
@@ -21,6 +21,12 @@
21
21
 
22
22
  #include <fmt/core.h>
23
23
 
24
+ /**
25
+ * Helper for fmtlib to format @ref couchbase::tls_verify_mode objects.
26
+ *
27
+ * @since 1.0.0
28
+ * @committed
29
+ */
24
30
  template<>
25
31
  struct fmt::formatter<couchbase::tls_verify_mode> {
26
32
  template<typename ParseContext>
@@ -66,7 +66,8 @@ class get_all_query_indexes_options : public common_options<get_all_query_indexe
66
66
  * @uncommitted
67
67
  */
68
68
 
69
- using get_all_indexes_handler = std::function<void(couchbase::manager_error_context, std::vector<couchbase::management::query::index>)>;
69
+ using get_all_query_indexes_handler =
70
+ std::function<void(couchbase::manager_error_context, std::vector<couchbase::management::query::index>)>;
70
71
 
71
72
  #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
72
73
  namespace core
@@ -84,7 +85,7 @@ void
84
85
  initiate_get_all_query_indexes(std::shared_ptr<couchbase::core::cluster> core,
85
86
  std::string bucket_name,
86
87
  couchbase::get_all_query_indexes_options::built options,
87
- get_all_indexes_handler&& handler);
88
+ get_all_query_indexes_handler&& handler);
88
89
 
89
90
  void
90
91
  initiate_get_all_query_indexes(std::shared_ptr<couchbase::core::cluster> core,
@@ -92,9 +93,9 @@ initiate_get_all_query_indexes(std::shared_ptr<couchbase::core::cluster> core,
92
93
  couchbase::get_all_query_indexes_options::built options,
93
94
  query_context query_ctx,
94
95
  std::string collection_name,
95
- get_all_indexes_handler&& handler);
96
+ get_all_query_indexes_handler&& handler);
96
97
 
97
98
  #endif
98
99
  } // namespace impl
99
100
  } // namespace core
100
- } // namespace couchbase
101
+ } // namespace couchbase
@@ -59,10 +59,12 @@ class query_index_manager
59
59
  * @since 1.0.0
60
60
  * @committed
61
61
  */
62
- void get_all_indexes(std::string bucket_name, const get_all_query_indexes_options& options, get_all_indexes_handler&& handler) const
62
+ void get_all_indexes(std::string bucket_name,
63
+ const get_all_query_indexes_options& options,
64
+ get_all_query_indexes_handler&& handler) const
63
65
  {
64
66
  return core::impl::initiate_get_all_query_indexes(
65
- core_, std::move(bucket_name), options.build(), std::forward<get_all_indexes_handler>(handler));
67
+ core_, std::move(bucket_name), options.build(), std::forward<get_all_query_indexes_handler>(handler));
66
68
  }
67
69
  [[nodiscard]] auto get_all_indexes(std::string bucket_name, const get_all_query_indexes_options& options) const
68
70
  -> std::future<std::pair<manager_error_context, std::vector<couchbase::management::query::index>>>
@@ -121,7 +121,7 @@ class scope
121
121
  * @since 1.0.0
122
122
  * @committed
123
123
  */
124
- [[nodiscard]] auto query(std::string statement, const query_options& options) const
124
+ [[nodiscard]] auto query(std::string statement, const query_options& options = {}) const
125
125
  -> std::future<std::pair<query_error_context, query_result>>
126
126
  {
127
127
  auto barrier = std::make_shared<std::promise<std::pair<query_error_context, query_result>>>();
@@ -71,7 +71,9 @@ class array_add_unique
71
71
  }
72
72
 
73
73
  private:
74
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
74
75
  friend couchbase::mutate_in_specs;
76
+ #endif
75
77
 
76
78
  array_add_unique(std::string path, std::vector<std::byte> value)
77
79
  : path_(std::move(path))
@@ -70,7 +70,9 @@ class array_append
70
70
  }
71
71
 
72
72
  private:
73
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
73
74
  friend couchbase::mutate_in_specs;
75
+ #endif
74
76
 
75
77
  array_append(std::string path, std::vector<std::vector<std::byte>> values)
76
78
  : path_(std::move(path))
@@ -71,7 +71,9 @@ class array_insert
71
71
  }
72
72
 
73
73
  private:
74
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
74
75
  friend couchbase::mutate_in_specs;
76
+ #endif
75
77
 
76
78
  array_insert(std::string path, std::vector<std::vector<std::byte>> values)
77
79
  : path_(std::move(path))
@@ -71,7 +71,9 @@ class array_prepend
71
71
  }
72
72
 
73
73
  private:
74
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
74
75
  friend couchbase::mutate_in_specs;
76
+ #endif
75
77
 
76
78
  array_prepend(std::string path, std::vector<std::vector<std::byte>> values)
77
79
  : path_(std::move(path))
@@ -54,7 +54,9 @@ class count
54
54
  }
55
55
 
56
56
  private:
57
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
57
58
  friend couchbase::lookup_in_specs;
59
+ #endif
58
60
 
59
61
  explicit count(std::string path)
60
62
  : path_(std::move(path))
@@ -69,7 +69,9 @@ class counter
69
69
  }
70
70
 
71
71
  private:
72
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
72
73
  friend couchbase::mutate_in_specs;
74
+ #endif
73
75
 
74
76
  counter(std::string path, std::int64_t value)
75
77
  : path_(std::move(path))
@@ -54,7 +54,9 @@ class exists
54
54
  }
55
55
 
56
56
  private:
57
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
57
58
  friend couchbase::lookup_in_specs;
59
+ #endif
58
60
 
59
61
  explicit exists(std::string path)
60
62
  : path_(std::move(path))
@@ -56,7 +56,9 @@ class get
56
56
  }
57
57
 
58
58
  private:
59
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
59
60
  friend couchbase::lookup_in_specs;
61
+ #endif
60
62
 
61
63
  explicit get(std::string path)
62
64
  : path_(std::move(path))
@@ -71,7 +71,9 @@ class insert
71
71
  }
72
72
 
73
73
  private:
74
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
74
75
  friend couchbase::mutate_in_specs;
76
+ #endif
75
77
 
76
78
  insert(std::string path, std::vector<std::byte> value)
77
79
  : path_(std::move(path))
@@ -54,7 +54,9 @@ class remove
54
54
  }
55
55
 
56
56
  private:
57
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
57
58
  friend couchbase::mutate_in_specs;
59
+ #endif
58
60
 
59
61
  explicit remove(std::string path)
60
62
  : path_(std::move(path))
@@ -56,7 +56,9 @@ class replace
56
56
  }
57
57
 
58
58
  private:
59
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
59
60
  friend couchbase::mutate_in_specs;
61
+ #endif
60
62
 
61
63
  replace(std::string path, std::vector<std::byte> value)
62
64
  : path_(std::move(path))
@@ -86,4 +88,4 @@ class replace
86
88
  bool expand_macro_{ false };
87
89
  };
88
90
  } // namespace subdoc
89
- } // namespace couchbase
91
+ } // namespace couchbase
@@ -71,7 +71,9 @@ class upsert
71
71
  }
72
72
 
73
73
  private:
74
+ #ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
74
75
  friend couchbase::mutate_in_specs;
76
+ #endif
75
77
 
76
78
  upsert(std::string path, std::vector<std::byte> value)
77
79
  : path_(std::move(path))
@@ -46,13 +46,13 @@ class transaction_op_error_context
46
46
  }
47
47
 
48
48
  /**
49
- * The error_code associated with this error context. This will always be a @ref transaction_op_error_code
49
+ * The error_code associated with this error context.
50
50
  *
51
51
  * Note that some query errors are not _transaction_ errors, so this error code will be 0, but there will be
52
- * a @ref transaction_op_error_code#cause() with a query_error_context in it. These errors do not rollback a
52
+ * a @ref cause() with a @ref query_error_context in it. These errors do not rollback a
53
53
  * transaction. If you want to roll it back, raise an exception.
54
54
  *
55
- * @return a transaction_op_error_code, if any.
55
+ * @return a error code, if any.
56
56
  */
57
57
  [[nodiscard]] std::error_code ec() const
58
58
  {
@@ -60,7 +60,7 @@ class transaction_op_error_context
60
60
  }
61
61
 
62
62
  /**
63
- * The underlying cause of this error. This can be either a @ref key_value_error_code or a @ref query_error_context.
63
+ * The underlying cause of this error. This can be either a @ref key_value_error_context or a @ref query_error_context.
64
64
  *
65
65
  * @return the error_context associated with the underlying cause of this error.
66
66
  */
@@ -24,36 +24,31 @@
24
24
  #include <couchbase/result.hxx>
25
25
  #include <couchbase/transaction_op_error_context.hxx>
26
26
 
27
+ // forward declarations...
28
+ namespace couchbase::core::transactions
29
+ {
30
+ class transaction_get_result;
31
+ class transaction_links;
32
+ class document_metadata;
33
+ } // namespace couchbase::core::transactions
34
+
27
35
  namespace couchbase::transactions
28
36
  {
29
37
 
30
- class transaction_get_result : public result
38
+ class transaction_get_result
31
39
  {
32
- protected:
33
- std::string bucket_{};
34
- std::string scope_{};
35
- std::string collection_{};
36
- std::string key_{};
37
- std::vector<std::byte> content_{};
40
+ friend class couchbase::core::transactions::transaction_get_result;
38
41
 
39
- public:
40
- transaction_get_result() = default;
41
-
42
- transaction_get_result(std::string bucket,
43
- std::string scope,
44
- std::string collection,
45
- std::string key,
46
- couchbase::cas cas,
47
- std::vector<std::byte> content)
48
- : result(cas)
49
- , bucket_(std::move(bucket))
50
- , scope_(std::move(scope))
51
- , collection_(std::move(collection))
52
- , key_(std::move(key))
53
- , content_(std::move(content))
42
+ private:
43
+ std::shared_ptr<couchbase::core::transactions::transaction_get_result> base_{};
44
+
45
+ transaction_get_result(std::shared_ptr<couchbase::core::transactions::transaction_get_result> base)
46
+ : base_(base)
54
47
  {
55
48
  }
56
49
 
50
+ public:
51
+ transaction_get_result();
57
52
  /**
58
53
  * Content of the document.
59
54
  *
@@ -62,7 +57,7 @@ class transaction_get_result : public result
62
57
  template<typename Content>
63
58
  [[nodiscard]] Content content() const
64
59
  {
65
- return codec::tao_json_serializer::deserialize<Content>(content_);
60
+ return codec::tao_json_serializer::deserialize<Content>(content());
66
61
  }
67
62
 
68
63
  /**
@@ -70,64 +65,54 @@ class transaction_get_result : public result
70
65
  *
71
66
  * @return content
72
67
  */
73
- [[nodiscard]] const std::vector<std::byte>& content() const
74
- {
75
- return content_;
76
- }
68
+ [[nodiscard]] const std::vector<std::byte>& content() const;
69
+
77
70
  /**
78
71
  * Copy content into document
79
72
  * @param content
80
73
  */
81
- void content(std::vector<std::byte> content)
82
- {
83
- content_ = std::move(content);
84
- }
74
+ void content(std::vector<std::byte> content);
75
+
85
76
  /**
86
77
  * Move content into document
87
78
  *
88
79
  * @param content
89
80
  */
90
- void content(std::vector<std::byte>&& content)
91
- {
92
- content_ = std::move(content);
93
- }
81
+ void content(std::vector<std::byte>&& content);
94
82
 
95
83
  /**
96
84
  * Get document id.
97
85
  *
98
86
  * @return the id of this document.
99
87
  */
100
- [[nodiscard]] const std::string key() const
101
- {
102
- return key_;
103
- }
88
+ [[nodiscard]] const std::string key() const;
104
89
 
105
90
  /**
106
91
  * Get the name of the bucket this document is in.
107
92
  *
108
93
  * @return name of the bucket which contains the document.
109
94
  */
110
- [[nodiscard]] const std::string bucket() const
111
- {
112
- return bucket_;
113
- }
95
+ [[nodiscard]] const std::string bucket() const;
96
+
114
97
  /**
115
98
  * Get the name of the scope this document is in.
116
99
  *
117
100
  * @return name of the scope which contains the document.
118
101
  */
119
- [[nodiscard]] const std::string scope() const
120
- {
121
- return scope_;
122
- }
102
+ [[nodiscard]] const std::string scope() const;
103
+
123
104
  /**
124
105
  * Get the name of the collection this document is in.
125
106
  *
126
107
  * @return name of the collection which contains the document.
127
108
  */
128
- [[nodiscard]] const std::string collection() const
129
- {
130
- return collection_;
131
- }
109
+ [[nodiscard]] const std::string collection() const;
110
+
111
+ /**
112
+ * Get the CAS fot this document
113
+ *
114
+ * @return the CAS of the document.
115
+ */
116
+ [[nodiscard]] const couchbase::cas cas() const;
132
117
  };
133
118
  } // namespace couchbase::transactions
@@ -115,7 +115,7 @@ class transactions_config
115
115
  /**
116
116
  * @brief Set the expiration time for transactions.
117
117
  *
118
- * @param duration desired expiration for transactions. see @expiration_time().
118
+ * @param duration desired expiration for transactions. see @ref expiration_time().
119
119
  * @return reference to this, so calls can be chained.
120
120
  */
121
121
  template<typename T>
@@ -39,3 +39,5 @@ transaction_test(transaction_public_async_api)
39
39
  unit_test(transaction_logging)
40
40
  unit_test(transaction_utils)
41
41
  unit_test(waitable_op_list)
42
+
43
+ integration_test(examples)
@@ -0,0 +1,141 @@
1
+ /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright 2023-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
+ #include "test_helper_integration.hxx"
19
+
20
+ #include <couchbase/cluster.hxx>
21
+ #include <couchbase/fmt/cas.hxx>
22
+ #include <couchbase/fmt/mutation_token.hxx>
23
+
24
+ #include <tao/json.hpp>
25
+
26
+ namespace start_using
27
+ {
28
+ //! [start-using]
29
+ #include <couchbase/cluster.hxx>
30
+ #include <couchbase/fmt/cas.hxx>
31
+ #include <couchbase/fmt/mutation_token.hxx>
32
+
33
+ #include <tao/json.hpp>
34
+
35
+ int
36
+ main(int argc, const char* argv[])
37
+ {
38
+ if (argc != 4) {
39
+ fmt::print("USAGE: ./start_using couchbase://127.0.0.1 Administrator password\n");
40
+ return 1;
41
+ }
42
+
43
+ std::string connection_string{ argv[1] };
44
+ std::string username{ argv[2] };
45
+ std::string password{ argv[3] };
46
+ std::string bucket_name{ "travel-sample" };
47
+
48
+ // run IO context on separate thread
49
+ asio::io_context io;
50
+ auto guard = asio::make_work_guard(io);
51
+ std::thread io_thread([&io]() { io.run(); });
52
+
53
+ auto options = couchbase::cluster_options(username, password);
54
+ // customize through the 'options'.
55
+ // For example, optimize timeouts for WAN
56
+ options.apply_profile("wan_development");
57
+
58
+ auto [cluster, ec] = couchbase::cluster::connect(io, connection_string, options).get();
59
+ if (ec) {
60
+ fmt::print("unable to connect to the cluster: {}\n", ec.message());
61
+ return 1;
62
+ }
63
+
64
+ // get a bucket reference
65
+ auto bucket = cluster.bucket(bucket_name);
66
+
67
+ // get a user-defined collection reference
68
+ auto scope = bucket.scope("tenant_agent_00");
69
+ auto collection = scope.collection("users");
70
+
71
+ {
72
+ // upsert document
73
+ auto [ctx, upsert_result] = collection.upsert("my-document", tao::json::value{ { "name", "mike" } }).get();
74
+ if (ctx.ec()) {
75
+ fmt::print("unable to upsert the document \"{}\": {}\n", ctx.id(), ctx.ec().message());
76
+ return 1;
77
+ }
78
+ fmt::print("saved document \"{}\", cas={}, token={}\n", ctx.id(), upsert_result.cas(), upsert_result.mutation_token().value());
79
+ }
80
+
81
+ {
82
+ // get document
83
+ auto [ctx, get_result] = collection.get("my-document").get();
84
+ if (ctx.ec()) {
85
+ fmt::print("unable to get the document \"{}\": {}\n", ctx.id(), ctx.ec().message());
86
+ return 1;
87
+ }
88
+ auto name = get_result.content_as<tao::json::value>()["name"].get_string();
89
+ fmt::print("retrieved document \"{}\", name=\"{}\"\n", ctx.id(), name);
90
+ }
91
+
92
+ {
93
+ // N1QL query
94
+ auto inventory_scope = bucket.scope("inventory");
95
+ auto [ctx, query_result] = inventory_scope.query("SELECT * FROM airline WHERE id = 10").get();
96
+ if (ctx.ec()) {
97
+ fmt::print("unable to perform query: {}, ({}, {})\n", ctx.ec().message(), ctx.first_error_code(), ctx.first_error_message());
98
+ return 1;
99
+ }
100
+ for (const auto& row : query_result.rows_as_json()) {
101
+ fmt::print("row: {}\n", tao::json::to_string(row));
102
+ }
103
+ }
104
+
105
+ // close cluster connection
106
+ cluster.close();
107
+ guard.reset();
108
+
109
+ io_thread.join();
110
+ return 0;
111
+ }
112
+
113
+ /*
114
+
115
+ $ ./start_using couchbase://127.0.0.1 Administrator password
116
+ saved document "my-document", cas=17486a1722b20000
117
+ retrieved document "my-document", name="mike"
118
+ row: {"airline":{"callsign":"MILE-AIR","country":"United States","iata":"Q5","icao":"MLA","id":10,"name":"40-Mile Air","type":"airline"}}
119
+
120
+ */
121
+ //! [start-using]
122
+
123
+ } // namespace start_using
124
+
125
+ TEST_CASE("example: start using", "[integration]")
126
+ {
127
+ test::utils::integration_test_guard integration;
128
+ if (!integration.cluster_version().supports_collections()) {
129
+ return;
130
+ }
131
+
132
+ const auto env = test::utils::test_context::load_from_environment();
133
+ const char* argv[] = {
134
+ "start_using", // name of the "executable"
135
+ env.connection_string.c_str(),
136
+ env.username.c_str(),
137
+ env.password.c_str(),
138
+ };
139
+
140
+ REQUIRE(start_using::main(4, argv) == 0);
141
+ }