couchbase 3.4.3 → 3.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/ext/couchbase/CMakeLists.txt +22 -1
  4. data/ext/couchbase/core/bucket.cxx +183 -152
  5. data/ext/couchbase/core/bucket.hxx +17 -4
  6. data/ext/couchbase/core/cluster.hxx +41 -13
  7. data/ext/couchbase/core/cluster_options.hxx +3 -0
  8. data/ext/couchbase/core/crud_component.cxx +51 -22
  9. data/ext/couchbase/core/error_context/key_value.cxx +2 -1
  10. data/ext/couchbase/core/error_context/key_value.hxx +10 -12
  11. data/ext/couchbase/core/impl/build_deferred_query_indexes.cxx +115 -50
  12. data/ext/couchbase/core/impl/cluster.cxx +6 -0
  13. data/ext/couchbase/core/impl/create_bucket.cxx +158 -0
  14. data/ext/couchbase/core/impl/create_collection.cxx +83 -0
  15. data/ext/couchbase/core/impl/create_query_index.cxx +172 -59
  16. data/ext/couchbase/core/impl/create_scope.cxx +69 -0
  17. data/ext/couchbase/core/impl/dns_srv_tracker.cxx +2 -1
  18. data/ext/couchbase/core/impl/drop_bucket.cxx +66 -0
  19. data/ext/couchbase/core/impl/drop_collection.cxx +76 -0
  20. data/ext/couchbase/core/impl/drop_query_index.cxx +138 -59
  21. data/ext/couchbase/core/impl/drop_scope.cxx +68 -0
  22. data/ext/couchbase/core/impl/flush_bucket.cxx +66 -0
  23. data/ext/couchbase/core/impl/get_all_buckets.cxx +178 -0
  24. data/ext/couchbase/core/impl/get_all_query_indexes.cxx +67 -37
  25. data/ext/couchbase/core/impl/get_all_scopes.cxx +94 -0
  26. data/ext/couchbase/core/impl/get_bucket.cxx +168 -0
  27. data/ext/couchbase/core/impl/internal_manager_error_context.cxx +113 -0
  28. data/ext/couchbase/core/impl/internal_manager_error_context.hxx +60 -0
  29. data/ext/couchbase/core/impl/key_value_error_category.cxx +2 -4
  30. data/ext/couchbase/core/impl/key_value_error_context.cxx +98 -0
  31. data/ext/couchbase/core/impl/lookup_in.cxx +1 -0
  32. data/ext/couchbase/core/impl/lookup_in_all_replicas.cxx +178 -0
  33. data/ext/couchbase/core/impl/lookup_in_all_replicas.hxx +80 -0
  34. data/ext/couchbase/core/impl/lookup_in_any_replica.cxx +169 -0
  35. data/ext/couchbase/core/impl/lookup_in_any_replica.hxx +75 -0
  36. data/ext/couchbase/core/impl/lookup_in_replica.cxx +104 -0
  37. data/ext/couchbase/core/impl/lookup_in_replica.hxx +67 -0
  38. data/ext/couchbase/core/impl/manager_error_context.cxx +100 -0
  39. data/ext/couchbase/core/impl/query.cxx +1 -0
  40. data/ext/couchbase/core/impl/query_error_context.cxx +75 -0
  41. data/ext/couchbase/core/impl/update_bucket.cxx +133 -0
  42. data/ext/couchbase/core/impl/update_collection.cxx +83 -0
  43. data/ext/couchbase/core/impl/watch_query_indexes.cxx +53 -29
  44. data/ext/couchbase/core/io/dns_client.cxx +111 -40
  45. data/ext/couchbase/core/io/dns_config.cxx +5 -4
  46. data/ext/couchbase/core/io/http_session.hxx +24 -1
  47. data/ext/couchbase/core/io/mcbp_command.hxx +9 -2
  48. data/ext/couchbase/core/io/mcbp_session.cxx +80 -43
  49. data/ext/couchbase/core/io/mcbp_session.hxx +4 -3
  50. data/ext/couchbase/core/logger/custom_rotating_file_sink.cxx +1 -1
  51. data/ext/couchbase/core/logger/logger.cxx +80 -20
  52. data/ext/couchbase/core/logger/logger.hxx +31 -0
  53. data/ext/couchbase/core/management/bucket_settings.hxx +8 -5
  54. data/ext/couchbase/core/management/bucket_settings_json.hxx +12 -2
  55. data/ext/couchbase/core/meta/features.hxx +42 -0
  56. data/ext/couchbase/core/operations/document_lookup_in.cxx +8 -1
  57. data/ext/couchbase/core/operations/document_lookup_in_all_replicas.hxx +192 -0
  58. data/ext/couchbase/core/operations/document_lookup_in_any_replica.hxx +188 -0
  59. data/ext/couchbase/core/operations/document_query.cxx +11 -0
  60. data/ext/couchbase/core/operations/document_query.hxx +1 -0
  61. data/ext/couchbase/core/operations/management/CMakeLists.txt +1 -0
  62. data/ext/couchbase/core/operations/management/bucket_create.cxx +30 -9
  63. data/ext/couchbase/core/operations/management/bucket_update.cxx +27 -6
  64. data/ext/couchbase/core/operations/management/collection_create.cxx +5 -1
  65. data/ext/couchbase/core/operations/management/collection_create.hxx +1 -0
  66. data/ext/couchbase/core/operations/management/collection_update.cxx +87 -0
  67. data/ext/couchbase/core/operations/management/collection_update.hxx +54 -0
  68. data/ext/couchbase/core/operations/management/collections.hxx +1 -0
  69. data/ext/couchbase/core/operations.hxx +2 -0
  70. data/ext/couchbase/core/origin.cxx +270 -0
  71. data/ext/couchbase/core/origin.hxx +2 -0
  72. data/ext/couchbase/core/protocol/client_response.hxx +1 -0
  73. data/ext/couchbase/core/protocol/cmd_hello.hxx +1 -0
  74. data/ext/couchbase/core/protocol/cmd_lookup_in_replica.cxx +107 -0
  75. data/ext/couchbase/core/protocol/cmd_lookup_in_replica.hxx +137 -0
  76. data/ext/couchbase/core/protocol/hello_feature.hxx +6 -0
  77. data/ext/couchbase/core/protocol/hello_feature_fmt.hxx +3 -0
  78. data/ext/couchbase/core/protocol/status.cxx +2 -2
  79. data/ext/couchbase/core/range_scan_options.cxx +3 -27
  80. data/ext/couchbase/core/range_scan_options.hxx +13 -17
  81. data/ext/couchbase/core/range_scan_orchestrator.cxx +388 -170
  82. data/ext/couchbase/core/range_scan_orchestrator.hxx +13 -2
  83. data/ext/couchbase/core/range_scan_orchestrator_options.hxx +5 -3
  84. data/ext/couchbase/core/scan_options.hxx +0 -19
  85. data/ext/couchbase/core/scan_result.cxx +19 -5
  86. data/ext/couchbase/core/scan_result.hxx +5 -2
  87. data/ext/couchbase/core/timeout_defaults.hxx +3 -4
  88. data/ext/couchbase/core/topology/capabilities.hxx +4 -0
  89. data/ext/couchbase/core/topology/capabilities_fmt.hxx +11 -0
  90. data/ext/couchbase/core/topology/collections_manifest.hxx +2 -0
  91. data/ext/couchbase/core/topology/collections_manifest_fmt.hxx +1 -1
  92. data/ext/couchbase/core/topology/collections_manifest_json.hxx +3 -0
  93. data/ext/couchbase/core/topology/configuration.hxx +20 -0
  94. data/ext/couchbase/core/topology/configuration_json.hxx +8 -1
  95. data/ext/couchbase/core/utils/connection_string.cxx +62 -47
  96. data/ext/couchbase/core/utils/connection_string.hxx +1 -0
  97. data/ext/couchbase/couchbase/analytics_error_context.hxx +1 -1
  98. data/ext/couchbase/couchbase/behavior_options.hxx +19 -2
  99. data/ext/couchbase/couchbase/bucket.hxx +14 -0
  100. data/ext/couchbase/couchbase/bucket_manager.hxx +135 -0
  101. data/ext/couchbase/couchbase/build_query_index_options.hxx +0 -30
  102. data/ext/couchbase/couchbase/cluster.hxx +14 -0
  103. data/ext/couchbase/couchbase/collection.hxx +111 -0
  104. data/ext/couchbase/couchbase/collection_manager.hxx +160 -0
  105. data/ext/couchbase/couchbase/collection_query_index_manager.hxx +7 -48
  106. data/ext/couchbase/couchbase/create_bucket_options.hxx +41 -0
  107. data/ext/couchbase/couchbase/create_collection_options.hxx +44 -0
  108. data/ext/couchbase/couchbase/create_primary_query_index_options.hxx +0 -29
  109. data/ext/couchbase/couchbase/create_query_index_options.hxx +0 -33
  110. data/ext/couchbase/couchbase/create_scope_options.hxx +41 -0
  111. data/ext/couchbase/couchbase/drop_bucket_options.hxx +41 -0
  112. data/ext/couchbase/couchbase/drop_collection_options.hxx +41 -0
  113. data/ext/couchbase/couchbase/drop_primary_query_index_options.hxx +0 -30
  114. data/ext/couchbase/couchbase/drop_query_index_options.hxx +0 -31
  115. data/ext/couchbase/couchbase/drop_scope_options.hxx +41 -0
  116. data/ext/couchbase/couchbase/error_codes.hxx +1 -2
  117. data/ext/couchbase/couchbase/error_context.hxx +10 -2
  118. data/ext/couchbase/couchbase/flush_bucket_options.hxx +41 -0
  119. data/ext/couchbase/{core/topology/error_map_fmt.hxx → couchbase/fmt/key_value_error_map_attribute.hxx} +21 -21
  120. data/ext/couchbase/couchbase/get_all_buckets_options.hxx +44 -0
  121. data/ext/couchbase/couchbase/get_all_query_indexes_options.hxx +0 -30
  122. data/ext/couchbase/couchbase/get_all_scopes_options.hxx +44 -0
  123. data/ext/couchbase/couchbase/get_and_lock_options.hxx +2 -2
  124. data/ext/couchbase/couchbase/get_and_touch_options.hxx +2 -2
  125. data/ext/couchbase/couchbase/get_bucket_options.hxx +43 -0
  126. data/ext/couchbase/couchbase/get_options.hxx +2 -2
  127. data/ext/couchbase/couchbase/insert_options.hxx +3 -3
  128. data/ext/couchbase/couchbase/key_value_error_context.hxx +7 -2
  129. data/ext/couchbase/couchbase/lookup_in_all_replicas_options.hxx +109 -0
  130. data/ext/couchbase/couchbase/lookup_in_any_replica_options.hxx +101 -0
  131. data/ext/couchbase/couchbase/lookup_in_options.hxx +2 -2
  132. data/ext/couchbase/couchbase/lookup_in_replica_result.hxx +74 -0
  133. data/ext/couchbase/couchbase/lookup_in_result.hxx +26 -0
  134. data/ext/couchbase/couchbase/management/bucket_settings.hxx +119 -0
  135. data/ext/couchbase/couchbase/management/collection_spec.hxx +29 -0
  136. data/ext/couchbase/couchbase/management/scope_spec.hxx +29 -0
  137. data/ext/couchbase/couchbase/manager_error_context.hxx +29 -53
  138. data/ext/couchbase/couchbase/mutate_in_options.hxx +2 -2
  139. data/ext/couchbase/couchbase/query_error_context.hxx +3 -1
  140. data/ext/couchbase/couchbase/query_index_manager.hxx +16 -83
  141. data/ext/couchbase/couchbase/query_options.hxx +18 -0
  142. data/ext/couchbase/couchbase/remove_options.hxx +2 -2
  143. data/ext/couchbase/couchbase/replace_options.hxx +3 -3
  144. data/ext/couchbase/couchbase/security_options.hxx +15 -0
  145. data/ext/couchbase/couchbase/subdocument_error_context.hxx +4 -2
  146. data/ext/couchbase/couchbase/touch_options.hxx +2 -2
  147. data/ext/couchbase/couchbase/unlock_options.hxx +2 -2
  148. data/ext/couchbase/couchbase/update_bucket_options.hxx +41 -0
  149. data/ext/couchbase/couchbase/update_collection_options.hxx +44 -0
  150. data/ext/couchbase/couchbase/upsert_options.hxx +3 -3
  151. data/ext/couchbase/couchbase/watch_query_indexes_options.hxx +0 -31
  152. data/ext/couchbase/test/CMakeLists.txt +1 -0
  153. data/ext/couchbase/test/test_integration_collections.cxx +6 -0
  154. data/ext/couchbase/test/test_integration_crud.cxx +5 -0
  155. data/ext/couchbase/test/test_integration_examples.cxx +137 -1
  156. data/ext/couchbase/test/test_integration_management.cxx +1009 -309
  157. data/ext/couchbase/test/test_integration_query.cxx +19 -7
  158. data/ext/couchbase/test/test_integration_range_scan.cxx +351 -112
  159. data/ext/couchbase/test/test_integration_search.cxx +10 -1
  160. data/ext/couchbase/test/test_integration_subdoc.cxx +721 -7
  161. data/ext/couchbase/test/test_transaction_public_async_api.cxx +13 -12
  162. data/ext/couchbase/test/test_transaction_public_blocking_api.cxx +27 -21
  163. data/ext/couchbase/test/test_unit_connection_string.cxx +29 -0
  164. data/ext/couchbase/test/test_unit_query.cxx +75 -0
  165. data/ext/couchbase.cxx +735 -60
  166. data/ext/revisions.rb +3 -3
  167. data/lib/couchbase/cluster.rb +1 -1
  168. data/lib/couchbase/collection.rb +108 -0
  169. data/lib/couchbase/collection_options.rb +100 -1
  170. data/lib/couchbase/errors.rb +5 -0
  171. data/lib/couchbase/key_value_scan.rb +125 -0
  172. data/lib/couchbase/management/bucket_manager.rb +22 -15
  173. data/lib/couchbase/management/collection_manager.rb +158 -9
  174. data/lib/couchbase/options.rb +151 -0
  175. data/lib/couchbase/scope.rb +1 -1
  176. data/lib/couchbase/utils/time.rb +14 -1
  177. data/lib/couchbase/version.rb +1 -1
  178. metadata +59 -8
  179. data/ext/couchbase/core/impl/collection_query_index_manager.cxx +0 -93
@@ -60,6 +60,7 @@ initiate_lookup_in_operation(std::shared_ptr<couchbase::core::cluster> core,
60
60
  std::move(entry.value),
61
61
  entry.original_index,
62
62
  entry.exists,
63
+ entry.ec,
63
64
  });
64
65
  }
65
66
  return handler(std::move(resp.ctx), lookup_in_result{ resp.cas, std::move(entries), resp.deleted });
@@ -0,0 +1,178 @@
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
+ #include "lookup_in_all_replicas.hxx"
19
+ #include "lookup_in_replica.hxx"
20
+
21
+ #include "core/cluster.hxx"
22
+ #include "core/error_context/key_value.hxx"
23
+ #include "core/operations/document_lookup_in.hxx"
24
+ #include "core/topology/configuration.hxx"
25
+
26
+ namespace couchbase::core::impl
27
+ {
28
+
29
+ void
30
+ initiate_lookup_in_all_replicas_operation(std::shared_ptr<cluster> core,
31
+ const std::string& bucket_name,
32
+ const std::string& scope_name,
33
+ const std::string& collection_name,
34
+ std::string document_key,
35
+ const std::vector<subdoc::command>& specs,
36
+ lookup_in_all_replicas_options::built options,
37
+ lookup_in_all_replicas_handler&& handler)
38
+ {
39
+ return initiate_lookup_in_all_replicas_operation(std::move(core),
40
+ bucket_name,
41
+ scope_name,
42
+ collection_name,
43
+ std::move(document_key),
44
+ specs,
45
+ options.timeout,
46
+ movable_lookup_in_all_replicas_handler{ std::move(handler) });
47
+ }
48
+
49
+ void
50
+ initiate_lookup_in_all_replicas_operation(std::shared_ptr<cluster> core,
51
+ const std::string& bucket_name,
52
+ const std::string& scope_name,
53
+ const std::string& collection_name,
54
+ std::string document_key,
55
+ const std::vector<subdoc::command>& specs,
56
+ std::optional<std::chrono::milliseconds> timeout,
57
+ movable_lookup_in_all_replicas_handler&& handler)
58
+ {
59
+ auto request = std::make_shared<couchbase::core::impl::lookup_in_all_replicas_request>(
60
+ bucket_name, scope_name, collection_name, std::move(document_key), specs, timeout);
61
+ core->with_bucket_configuration(
62
+ bucket_name,
63
+ [core, r = std::move(request), h = std::move(handler)](std::error_code ec, const core::topology::configuration& config) mutable {
64
+ if (!config.supports_subdoc_read_replica()) {
65
+ ec = errc::common::feature_not_available;
66
+ }
67
+
68
+ if (ec) {
69
+ std::optional<std::string> first_error_path{};
70
+ std::optional<std::size_t> first_error_index{};
71
+ return h(
72
+ make_subdocument_error_context(make_key_value_error_context(ec, r->id()), ec, first_error_path, first_error_index, false),
73
+ lookup_in_all_replicas_result{});
74
+ }
75
+ struct replica_context {
76
+ replica_context(movable_lookup_in_all_replicas_handler handler, std::uint32_t expected_responses)
77
+ : handler_(std::move(handler))
78
+ , expected_responses_(expected_responses)
79
+ {
80
+ }
81
+
82
+ movable_lookup_in_all_replicas_handler handler_;
83
+ std::uint32_t expected_responses_;
84
+ bool done_{ false };
85
+ std::mutex mutex_{};
86
+ lookup_in_all_replicas_result result_{};
87
+ };
88
+ auto ctx = std::make_shared<replica_context>(std::move(h), config.num_replicas.value_or(0U) + 1U);
89
+
90
+ for (std::size_t idx = 1U; idx <= config.num_replicas.value_or(0U); ++idx) {
91
+ document_id replica_id{ r->id() };
92
+ replica_id.node_index(idx);
93
+ core->execute(
94
+ impl::lookup_in_replica_request{ std::move(replica_id), r->specs(), r->timeout() },
95
+ [ctx](impl::lookup_in_replica_response&& resp) {
96
+ movable_lookup_in_all_replicas_handler local_handler{};
97
+ {
98
+ std::scoped_lock lock(ctx->mutex_);
99
+ if (ctx->done_) {
100
+ return;
101
+ }
102
+ --ctx->expected_responses_;
103
+ if (resp.ctx.ec()) {
104
+ if (ctx->expected_responses_ > 0) {
105
+ // just ignore the response
106
+ return;
107
+ }
108
+ } else {
109
+ std::vector<lookup_in_replica_result::entry> entries{};
110
+ for (auto& field : resp.fields) {
111
+ lookup_in_replica_result::entry lookup_in_entry{};
112
+ lookup_in_entry.path = field.path;
113
+ lookup_in_entry.value = field.value;
114
+ lookup_in_entry.exists = field.exists;
115
+ lookup_in_entry.original_index = field.original_index;
116
+ lookup_in_entry.ec = field.ec;
117
+ entries.emplace_back(lookup_in_entry);
118
+ }
119
+ ctx->result_.emplace_back(lookup_in_replica_result{ resp.cas, entries, resp.deleted, true /* replica */ });
120
+ }
121
+ if (ctx->expected_responses_ == 0) {
122
+ ctx->done_ = true;
123
+ std::swap(local_handler, ctx->handler_);
124
+ }
125
+ }
126
+ if (local_handler) {
127
+ if (!ctx->result_.empty()) {
128
+ resp.ctx.override_ec({});
129
+ }
130
+ return local_handler(std::move(resp.ctx), std::move(ctx->result_));
131
+ }
132
+ });
133
+ }
134
+
135
+ core::operations::lookup_in_request active{ document_id{ r->id() } };
136
+ active.specs = r->specs();
137
+ active.timeout = r->timeout();
138
+ core->execute(active, [ctx](core::operations::lookup_in_response&& resp) {
139
+ movable_lookup_in_all_replicas_handler local_handler{};
140
+ {
141
+ std::scoped_lock lock(ctx->mutex_);
142
+ if (ctx->done_) {
143
+ return;
144
+ }
145
+ --ctx->expected_responses_;
146
+ if (resp.ctx.ec()) {
147
+ if (ctx->expected_responses_ > 0) {
148
+ // just ignore the response
149
+ return;
150
+ }
151
+ } else {
152
+ std::vector<lookup_in_replica_result::entry> entries{};
153
+ for (auto& field : resp.fields) {
154
+ lookup_in_replica_result::entry lookup_in_entry{};
155
+ lookup_in_entry.path = field.path;
156
+ lookup_in_entry.value = field.value;
157
+ lookup_in_entry.exists = field.exists;
158
+ lookup_in_entry.original_index = field.original_index;
159
+ lookup_in_entry.ec = field.ec;
160
+ entries.emplace_back(lookup_in_entry);
161
+ }
162
+ ctx->result_.emplace_back(lookup_in_replica_result{ resp.cas, entries, resp.deleted, false /* active */ });
163
+ }
164
+ if (ctx->expected_responses_ == 0) {
165
+ ctx->done_ = true;
166
+ std::swap(local_handler, ctx->handler_);
167
+ }
168
+ }
169
+ if (local_handler) {
170
+ if (!ctx->result_.empty()) {
171
+ resp.ctx.override_ec({});
172
+ }
173
+ return local_handler(std::move(resp.ctx), std::move(ctx->result_));
174
+ }
175
+ });
176
+ });
177
+ }
178
+ } // namespace couchbase::core::impl
@@ -0,0 +1,80 @@
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
+
20
+ #include <couchbase/lookup_in_all_replicas_options.hxx>
21
+ #include <couchbase/lookup_in_replica_result.hxx>
22
+
23
+ #include "core/document_id.hxx"
24
+ #include "core/error_context/key_value.hxx"
25
+ #include "core/utils/movable_function.hxx"
26
+
27
+ #include <vector>
28
+
29
+ namespace couchbase::core::impl
30
+ {
31
+
32
+ class lookup_in_all_replicas_request
33
+ {
34
+ public:
35
+ explicit lookup_in_all_replicas_request(std::string bucket_name,
36
+ std::string scope_name,
37
+ std::string collection_name,
38
+ std::string document_key,
39
+ std::vector<couchbase::core::impl::subdoc::command> specs,
40
+ std::optional<std::chrono::milliseconds> timeout)
41
+ : id_{ std::move(bucket_name), std::move(scope_name), std::move(collection_name), std::move(document_key) }
42
+ , specs_{ std::move(specs) }
43
+ , timeout_{ timeout }
44
+ {
45
+ }
46
+
47
+ [[nodiscard]] const auto& id() const
48
+ {
49
+ return id_;
50
+ }
51
+
52
+ [[nodiscard]] const auto& specs() const
53
+ {
54
+ return specs_;
55
+ }
56
+
57
+ [[nodiscard]] const auto& timeout() const
58
+ {
59
+ return timeout_;
60
+ }
61
+
62
+ private:
63
+ core::document_id id_;
64
+ std::vector<couchbase::core::impl::subdoc::command> specs_;
65
+ std::optional<std::chrono::milliseconds> timeout_{};
66
+ };
67
+
68
+ using movable_lookup_in_all_replicas_handler =
69
+ utils::movable_function<void(couchbase::subdocument_error_context, lookup_in_all_replicas_result)>;
70
+
71
+ void
72
+ initiate_lookup_in_all_replicas_operation(std::shared_ptr<cluster> core,
73
+ const std::string& bucket_name,
74
+ const std::string& scope_name,
75
+ const std::string& collection_name,
76
+ std::string document_key,
77
+ const std::vector<subdoc::command>& specs,
78
+ std::optional<std::chrono::milliseconds> timeout,
79
+ movable_lookup_in_all_replicas_handler&& handler);
80
+ } // namespace couchbase::core::impl
@@ -0,0 +1,169 @@
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
+ #include "lookup_in_any_replica.hxx"
19
+ #include "lookup_in_replica.hxx"
20
+
21
+ #include "core/cluster.hxx"
22
+ #include "core/error_context/key_value.hxx"
23
+ #include "core/operations/document_lookup_in.hxx"
24
+ #include "core/topology/configuration.hxx"
25
+
26
+ namespace couchbase::core::impl
27
+ {
28
+ void
29
+ initiate_lookup_in_any_replica_operation(std::shared_ptr<cluster> core,
30
+ const std::string& bucket_name,
31
+ const std::string& scope_name,
32
+ const std::string& collection_name,
33
+ std::string document_key,
34
+ const std::vector<subdoc::command>& specs,
35
+ lookup_in_any_replica_options::built options,
36
+ lookup_in_any_replica_handler&& handler)
37
+ {
38
+ return initiate_lookup_in_any_replica_operation(std::move(core),
39
+ bucket_name,
40
+ scope_name,
41
+ collection_name,
42
+ std::move(document_key),
43
+ specs,
44
+ options.timeout,
45
+ movable_lookup_in_any_replica_handler{ std::move(handler) });
46
+ }
47
+
48
+ void
49
+ initiate_lookup_in_any_replica_operation(std::shared_ptr<cluster> core,
50
+ const std::string& bucket_name,
51
+ const std::string& scope_name,
52
+ const std::string& collection_name,
53
+ std::string document_key,
54
+ const std::vector<subdoc::command>& specs,
55
+ std::optional<std::chrono::milliseconds> timeout,
56
+ movable_lookup_in_any_replica_handler&& handler)
57
+ {
58
+ auto request = std::make_shared<couchbase::core::impl::lookup_in_any_replica_request>(
59
+ bucket_name, scope_name, collection_name, std::move(document_key), specs, timeout);
60
+ core->with_bucket_configuration(
61
+ bucket_name,
62
+ [core, r = std::move(request), h = std::move(handler)](std::error_code ec, const core::topology::configuration& config) mutable {
63
+ if (!config.supports_subdoc_read_replica()) {
64
+ ec = errc::common::feature_not_available;
65
+ }
66
+ if (r->specs().size() > 16) {
67
+ ec = errc::common::invalid_argument;
68
+ }
69
+ if (ec) {
70
+ std::optional<std::string> first_error_path{};
71
+ std::optional<std::size_t> first_error_index{};
72
+ return h(
73
+ make_subdocument_error_context(make_key_value_error_context(ec, r->id()), ec, first_error_path, first_error_index, false),
74
+ lookup_in_replica_result{});
75
+ }
76
+ struct replica_context {
77
+ replica_context(movable_lookup_in_any_replica_handler handler, std::uint32_t expected_responses)
78
+ : handler_(std::move(handler))
79
+ , expected_responses_(expected_responses)
80
+ {
81
+ }
82
+
83
+ movable_lookup_in_any_replica_handler handler_;
84
+ std::uint32_t expected_responses_;
85
+ bool done_{ false };
86
+ std::mutex mutex_{};
87
+ };
88
+ auto ctx = std::make_shared<replica_context>(std::move(h), config.num_replicas.value_or(0U) + 1U);
89
+
90
+ for (std::size_t idx = 1U; idx <= config.num_replicas.value_or(0U); ++idx) {
91
+ document_id replica_id{ r->id() };
92
+ replica_id.node_index(idx);
93
+ core->execute(impl::lookup_in_replica_request{ std::move(replica_id), r->specs(), r->timeout() },
94
+ [ctx](impl::lookup_in_replica_response&& resp) {
95
+ movable_lookup_in_any_replica_handler local_handler;
96
+ {
97
+ std::scoped_lock lock(ctx->mutex_);
98
+ if (ctx->done_) {
99
+ return;
100
+ }
101
+ --ctx->expected_responses_;
102
+ if (resp.ctx.ec()) {
103
+ if (ctx->expected_responses_ > 0) {
104
+ // just ignore the response
105
+ return;
106
+ }
107
+ // consider document irretrievable and give up
108
+ resp.ctx.override_ec(errc::key_value::document_irretrievable);
109
+ }
110
+ ctx->done_ = true;
111
+ std::swap(local_handler, ctx->handler_);
112
+ }
113
+ if (local_handler) {
114
+ std::vector<lookup_in_replica_result::entry> entries;
115
+ for (auto& field : resp.fields) {
116
+ lookup_in_replica_result::entry entry{};
117
+ entry.path = field.path;
118
+ entry.original_index = field.original_index;
119
+ entry.exists = field.exists;
120
+ entry.value = field.value;
121
+ entry.ec = field.ec;
122
+ entries.emplace_back(entry);
123
+ }
124
+ return local_handler(std::move(resp.ctx),
125
+ lookup_in_replica_result{ resp.cas, entries, resp.deleted, true /* replica */ });
126
+ }
127
+ });
128
+ }
129
+
130
+ core::operations::lookup_in_request active{ document_id{ r->id() } };
131
+ active.specs = r->specs();
132
+ active.timeout = r->timeout();
133
+ core->execute(active, [ctx](core::operations::lookup_in_response&& resp) {
134
+ movable_lookup_in_any_replica_handler local_handler{};
135
+ {
136
+ std::scoped_lock lock(ctx->mutex_);
137
+ if (ctx->done_) {
138
+ return;
139
+ }
140
+ --ctx->expected_responses_;
141
+ if (resp.ctx.ec()) {
142
+ if (ctx->expected_responses_ > 0) {
143
+ // just ignore the response
144
+ return;
145
+ }
146
+ // consider document irretrievable and give up
147
+ resp.ctx.override_ec(errc::key_value::document_irretrievable);
148
+ }
149
+ ctx->done_ = true;
150
+ std::swap(local_handler, ctx->handler_);
151
+ }
152
+ if (local_handler) {
153
+ std::vector<lookup_in_replica_result::entry> entries;
154
+ for (auto& field : resp.fields) {
155
+ lookup_in_replica_result::entry entry{};
156
+ entry.path = field.path;
157
+ entry.original_index = field.original_index;
158
+ entry.exists = field.exists;
159
+ entry.value = field.value;
160
+ entry.ec = field.ec;
161
+ entries.emplace_back(entry);
162
+ }
163
+ return local_handler(std::move(resp.ctx),
164
+ lookup_in_replica_result{ resp.cas, entries, resp.deleted, false /* active */ });
165
+ }
166
+ });
167
+ });
168
+ }
169
+ } // namespace couchbase::core::impl
@@ -0,0 +1,75 @@
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
+
20
+ #include <couchbase/lookup_in_any_replica_options.hxx>
21
+ #include <couchbase/lookup_in_replica_result.hxx>
22
+
23
+ #include "core/document_id.hxx"
24
+ #include "core/utils/movable_function.hxx"
25
+
26
+ namespace couchbase::core::impl
27
+ {
28
+ class lookup_in_any_replica_request
29
+ {
30
+ public:
31
+ explicit lookup_in_any_replica_request(std::string bucket_name,
32
+ std::string scope_name,
33
+ std::string collection_name,
34
+ std::string document_key,
35
+ std::vector<couchbase::core::impl::subdoc::command> specs,
36
+ std::optional<std::chrono::milliseconds> timeout)
37
+ : id_{ std::move(bucket_name), std::move(scope_name), std::move(collection_name), std::move(document_key) }
38
+ , specs_{ std::move(specs) }
39
+ , timeout_{ timeout }
40
+ {
41
+ }
42
+
43
+ [[nodiscard]] const auto& id() const
44
+ {
45
+ return id_;
46
+ }
47
+
48
+ [[nodiscard]] const auto& specs() const
49
+ {
50
+ return specs_;
51
+ }
52
+
53
+ [[nodiscard]] const auto& timeout() const
54
+ {
55
+ return timeout_;
56
+ }
57
+
58
+ private:
59
+ core::document_id id_;
60
+ std::vector<couchbase::core::impl::subdoc::command> specs_;
61
+ std::optional<std::chrono::milliseconds> timeout_{};
62
+ };
63
+
64
+ using movable_lookup_in_any_replica_handler = utils::movable_function<void(couchbase::subdocument_error_context, lookup_in_replica_result)>;
65
+
66
+ void
67
+ initiate_lookup_in_any_replica_operation(std::shared_ptr<cluster> core,
68
+ const std::string& bucket_name,
69
+ const std::string& scope_name,
70
+ const std::string& collection_name,
71
+ std::string document_key,
72
+ const std::vector<subdoc::command>& specs,
73
+ std::optional<std::chrono::milliseconds> timeout,
74
+ movable_lookup_in_any_replica_handler&& handler);
75
+ } // namespace couchbase::core::impl
@@ -0,0 +1,104 @@
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
+ #include "lookup_in_replica.hxx"
19
+ #include "core/impl/subdoc/path_flags.hxx"
20
+
21
+ #include <couchbase/error_codes.hxx>
22
+
23
+ namespace couchbase::core::impl
24
+ {
25
+ std::error_code
26
+ lookup_in_replica_request::encode_to(lookup_in_replica_request::encoded_request_type& encoded, mcbp_context&& /* context */)
27
+ {
28
+ for (std::size_t i = 0; i < specs.size(); ++i) {
29
+ specs[i].original_index_ = i;
30
+ }
31
+ std::stable_sort(specs.begin(), specs.end(), [](const auto& lhs, const auto& rhs) {
32
+ /* move XATTRs to the beginning of the vector */
33
+ return core::impl::subdoc::has_xattr_path_flag(lhs.flags_) && !core::impl::subdoc::has_xattr_path_flag(rhs.flags_);
34
+ });
35
+
36
+ encoded.opaque(opaque);
37
+ encoded.partition(partition);
38
+ encoded.body().id(id);
39
+ encoded.body().read_replica(true);
40
+ encoded.body().specs(specs);
41
+ return {};
42
+ }
43
+
44
+ lookup_in_replica_response
45
+ lookup_in_replica_request::make_response(key_value_error_context&& ctx, const encoded_response_type& encoded) const
46
+ {
47
+
48
+ bool deleted = false;
49
+ couchbase::cas cas{};
50
+ std::vector<lookup_in_replica_response::entry> fields{};
51
+ std::error_code ec = ctx.ec();
52
+ std::optional<std::size_t> first_error_index{};
53
+ std::optional<std::string> first_error_path{};
54
+
55
+ if (encoded.status() == key_value_status_code::subdoc_success_deleted ||
56
+ encoded.status() == key_value_status_code::subdoc_multi_path_failure_deleted) {
57
+ deleted = true;
58
+ }
59
+ if (!ctx.ec()) {
60
+ fields.resize(specs.size());
61
+ for (size_t i = 0; i < specs.size(); ++i) {
62
+ const auto& req_entry = specs[i];
63
+ fields[i].original_index = req_entry.original_index_;
64
+ fields[i].path = req_entry.path_;
65
+ fields[i].opcode = static_cast<protocol::subdoc_opcode>(req_entry.opcode_);
66
+ fields[i].status = key_value_status_code::success;
67
+ }
68
+ for (size_t i = 0; i < encoded.body().fields().size(); ++i) {
69
+ const auto& res_entry = encoded.body().fields()[i];
70
+ fields[i].status = res_entry.status;
71
+ fields[i].ec =
72
+ protocol::map_status_code(protocol::client_opcode::subdoc_multi_mutation, static_cast<std::uint16_t>(res_entry.status));
73
+ if (fields[i].opcode == protocol::subdoc_opcode::exists && fields[i].ec == errc::key_value::path_not_found) {
74
+ fields[i].ec.clear();
75
+ }
76
+ if (!fields[i].ec && !ctx.ec()) {
77
+ ec = fields[i].ec;
78
+ }
79
+ if (!first_error_index && !fields[i].ec) {
80
+ first_error_index = i;
81
+ first_error_path = fields[i].path;
82
+ }
83
+ fields[i].exists =
84
+ res_entry.status == key_value_status_code::success || res_entry.status == key_value_status_code::subdoc_success_deleted;
85
+ if (fields[i].opcode == protocol::subdoc_opcode::exists && !fields[i].ec) {
86
+ fields[i].value = utils::json::generate_binary(fields[i].exists);
87
+ } else {
88
+ fields[i].value = utils::to_binary(res_entry.value);
89
+ }
90
+ }
91
+ if (!ec) {
92
+ cas = encoded.cas();
93
+ }
94
+ std::sort(fields.begin(), fields.end(), [](const auto& lhs, const auto& rhs) { return lhs.original_index < rhs.original_index; });
95
+ }
96
+
97
+ return lookup_in_replica_response{
98
+ make_subdocument_error_context(ctx, ec, first_error_path, first_error_index, deleted),
99
+ cas,
100
+ std::move(fields),
101
+ deleted,
102
+ };
103
+ }
104
+ } // namespace couchbase::core::impl