couchbase 3.0.0.alpha.1-universal-darwin-19 → 3.0.0.alpha.2-universal-darwin-19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/tests-6.0.3.yml +49 -0
  3. data/.github/workflows/tests.yml +47 -0
  4. data/.gitmodules +3 -0
  5. data/.idea/dictionaries/gem_terms.xml +5 -0
  6. data/.idea/inspectionProfiles/Project_Default.xml +1 -0
  7. data/.idea/vcs.xml +1 -0
  8. data/Gemfile +1 -0
  9. data/README.md +55 -2
  10. data/Rakefile +18 -0
  11. data/bin/init-cluster +62 -0
  12. data/bin/setup +1 -0
  13. data/couchbase.gemspec +3 -2
  14. data/examples/crud.rb +1 -2
  15. data/examples/managing_buckets.rb +47 -0
  16. data/examples/managing_collections.rb +58 -0
  17. data/examples/managing_query_indexes.rb +63 -0
  18. data/examples/query.rb +3 -2
  19. data/examples/query_with_consistency.rb +76 -0
  20. data/examples/subdocument.rb +23 -1
  21. data/ext/.clang-format +1 -1
  22. data/ext/.idea/dictionaries/couchbase_terms.xml +2 -0
  23. data/ext/.idea/vcs.xml +1 -0
  24. data/ext/CMakeLists.txt +30 -12
  25. data/ext/build_version.hxx.in +26 -0
  26. data/ext/couchbase/bucket.hxx +69 -8
  27. data/ext/couchbase/cluster.hxx +70 -54
  28. data/ext/couchbase/collections_manifest.hxx +3 -3
  29. data/ext/couchbase/configuration.hxx +14 -0
  30. data/ext/couchbase/couchbase.cxx +2044 -383
  31. data/ext/couchbase/{operations/document_id.hxx → document_id.hxx} +5 -4
  32. data/ext/couchbase/io/http_message.hxx +5 -1
  33. data/ext/couchbase/io/http_parser.hxx +2 -1
  34. data/ext/couchbase/io/http_session.hxx +6 -3
  35. data/ext/couchbase/io/{binary_message.hxx → mcbp_message.hxx} +15 -12
  36. data/ext/couchbase/io/mcbp_parser.hxx +99 -0
  37. data/ext/couchbase/io/{key_value_session.hxx → mcbp_session.hxx} +200 -95
  38. data/ext/couchbase/io/session_manager.hxx +37 -22
  39. data/ext/couchbase/mutation_token.hxx +2 -1
  40. data/ext/couchbase/operations.hxx +38 -8
  41. data/ext/couchbase/operations/bucket_create.hxx +138 -0
  42. data/ext/couchbase/operations/bucket_drop.hxx +65 -0
  43. data/ext/couchbase/operations/bucket_flush.hxx +65 -0
  44. data/ext/couchbase/operations/bucket_get.hxx +69 -0
  45. data/ext/couchbase/operations/bucket_get_all.hxx +62 -0
  46. data/ext/couchbase/operations/bucket_settings.hxx +111 -0
  47. data/ext/couchbase/operations/bucket_update.hxx +115 -0
  48. data/ext/couchbase/operations/cluster_developer_preview_enable.hxx +60 -0
  49. data/ext/couchbase/operations/collection_create.hxx +86 -0
  50. data/ext/couchbase/operations/collection_drop.hxx +82 -0
  51. data/ext/couchbase/operations/command.hxx +10 -10
  52. data/ext/couchbase/operations/document_decrement.hxx +80 -0
  53. data/ext/couchbase/operations/document_exists.hxx +80 -0
  54. data/ext/couchbase/operations/{get.hxx → document_get.hxx} +4 -2
  55. data/ext/couchbase/operations/document_get_and_lock.hxx +64 -0
  56. data/ext/couchbase/operations/document_get_and_touch.hxx +64 -0
  57. data/ext/couchbase/operations/document_increment.hxx +80 -0
  58. data/ext/couchbase/operations/document_insert.hxx +74 -0
  59. data/ext/couchbase/operations/{lookup_in.hxx → document_lookup_in.hxx} +2 -2
  60. data/ext/couchbase/operations/{mutate_in.hxx → document_mutate_in.hxx} +11 -2
  61. data/ext/couchbase/operations/{query.hxx → document_query.hxx} +101 -6
  62. data/ext/couchbase/operations/document_remove.hxx +67 -0
  63. data/ext/couchbase/operations/document_replace.hxx +76 -0
  64. data/ext/couchbase/operations/{upsert.hxx → document_touch.hxx} +14 -14
  65. data/ext/couchbase/operations/{remove.hxx → document_unlock.hxx} +12 -10
  66. data/ext/couchbase/operations/document_upsert.hxx +74 -0
  67. data/ext/couchbase/operations/query_index_build_deferred.hxx +85 -0
  68. data/ext/couchbase/operations/query_index_create.hxx +134 -0
  69. data/ext/couchbase/operations/query_index_drop.hxx +108 -0
  70. data/ext/couchbase/operations/query_index_get_all.hxx +106 -0
  71. data/ext/couchbase/operations/scope_create.hxx +81 -0
  72. data/ext/couchbase/operations/scope_drop.hxx +79 -0
  73. data/ext/couchbase/operations/scope_get_all.hxx +72 -0
  74. data/ext/couchbase/protocol/client_opcode.hxx +35 -0
  75. data/ext/couchbase/protocol/client_request.hxx +56 -9
  76. data/ext/couchbase/protocol/client_response.hxx +52 -15
  77. data/ext/couchbase/protocol/cmd_cluster_map_change_notification.hxx +81 -0
  78. data/ext/couchbase/protocol/cmd_decrement.hxx +187 -0
  79. data/ext/couchbase/protocol/cmd_exists.hxx +171 -0
  80. data/ext/couchbase/protocol/cmd_get.hxx +31 -8
  81. data/ext/couchbase/protocol/cmd_get_and_lock.hxx +142 -0
  82. data/ext/couchbase/protocol/cmd_get_and_touch.hxx +142 -0
  83. data/ext/couchbase/protocol/cmd_get_cluster_config.hxx +16 -3
  84. data/ext/couchbase/protocol/cmd_get_collections_manifest.hxx +16 -3
  85. data/ext/couchbase/protocol/cmd_get_error_map.hxx +16 -3
  86. data/ext/couchbase/protocol/cmd_hello.hxx +24 -8
  87. data/ext/couchbase/protocol/cmd_increment.hxx +187 -0
  88. data/ext/couchbase/protocol/cmd_info.hxx +1 -0
  89. data/ext/couchbase/protocol/cmd_insert.hxx +172 -0
  90. data/ext/couchbase/protocol/cmd_lookup_in.hxx +28 -13
  91. data/ext/couchbase/protocol/cmd_mutate_in.hxx +65 -13
  92. data/ext/couchbase/protocol/cmd_remove.hxx +59 -4
  93. data/ext/couchbase/protocol/cmd_replace.hxx +172 -0
  94. data/ext/couchbase/protocol/cmd_sasl_auth.hxx +15 -3
  95. data/ext/couchbase/protocol/cmd_sasl_list_mechs.hxx +15 -3
  96. data/ext/couchbase/protocol/cmd_sasl_step.hxx +15 -3
  97. data/ext/couchbase/protocol/cmd_select_bucket.hxx +14 -2
  98. data/ext/couchbase/protocol/cmd_touch.hxx +102 -0
  99. data/ext/couchbase/protocol/cmd_unlock.hxx +95 -0
  100. data/ext/couchbase/protocol/cmd_upsert.hxx +50 -14
  101. data/ext/couchbase/protocol/durability_level.hxx +67 -0
  102. data/ext/couchbase/protocol/frame_info_id.hxx +187 -0
  103. data/ext/couchbase/protocol/hello_feature.hxx +137 -0
  104. data/ext/couchbase/protocol/server_opcode.hxx +57 -0
  105. data/ext/couchbase/protocol/server_request.hxx +122 -0
  106. data/ext/couchbase/protocol/unsigned_leb128.h +15 -15
  107. data/ext/couchbase/utils/byteswap.hxx +1 -2
  108. data/ext/couchbase/utils/url_codec.hxx +225 -0
  109. data/ext/couchbase/version.hxx +3 -1
  110. data/ext/extconf.rb +4 -1
  111. data/ext/test/main.cxx +37 -113
  112. data/ext/third_party/snappy/.appveyor.yml +36 -0
  113. data/ext/third_party/snappy/.gitignore +8 -0
  114. data/ext/third_party/snappy/.travis.yml +98 -0
  115. data/ext/third_party/snappy/AUTHORS +1 -0
  116. data/ext/third_party/snappy/CMakeLists.txt +345 -0
  117. data/ext/third_party/snappy/CONTRIBUTING.md +26 -0
  118. data/ext/third_party/snappy/COPYING +54 -0
  119. data/ext/third_party/snappy/NEWS +188 -0
  120. data/ext/third_party/snappy/README.md +148 -0
  121. data/ext/third_party/snappy/cmake/SnappyConfig.cmake.in +33 -0
  122. data/ext/third_party/snappy/cmake/config.h.in +59 -0
  123. data/ext/third_party/snappy/docs/README.md +72 -0
  124. data/ext/third_party/snappy/format_description.txt +110 -0
  125. data/ext/third_party/snappy/framing_format.txt +135 -0
  126. data/ext/third_party/snappy/snappy-c.cc +90 -0
  127. data/ext/third_party/snappy/snappy-c.h +138 -0
  128. data/ext/third_party/snappy/snappy-internal.h +315 -0
  129. data/ext/third_party/snappy/snappy-sinksource.cc +121 -0
  130. data/ext/third_party/snappy/snappy-sinksource.h +182 -0
  131. data/ext/third_party/snappy/snappy-stubs-internal.cc +42 -0
  132. data/ext/third_party/snappy/snappy-stubs-internal.h +493 -0
  133. data/ext/third_party/snappy/snappy-stubs-public.h.in +63 -0
  134. data/ext/third_party/snappy/snappy-test.cc +613 -0
  135. data/ext/third_party/snappy/snappy-test.h +526 -0
  136. data/ext/third_party/snappy/snappy.cc +1770 -0
  137. data/ext/third_party/snappy/snappy.h +209 -0
  138. data/ext/third_party/snappy/snappy_compress_fuzzer.cc +60 -0
  139. data/ext/third_party/snappy/snappy_uncompress_fuzzer.cc +58 -0
  140. data/ext/third_party/snappy/snappy_unittest.cc +1512 -0
  141. data/ext/third_party/snappy/testdata/alice29.txt +3609 -0
  142. data/ext/third_party/snappy/testdata/asyoulik.txt +4122 -0
  143. data/ext/third_party/snappy/testdata/baddata1.snappy +0 -0
  144. data/ext/third_party/snappy/testdata/baddata2.snappy +0 -0
  145. data/ext/third_party/snappy/testdata/baddata3.snappy +0 -0
  146. data/ext/third_party/snappy/testdata/fireworks.jpeg +0 -0
  147. data/ext/third_party/snappy/testdata/geo.protodata +0 -0
  148. data/ext/third_party/snappy/testdata/html +1 -0
  149. data/ext/third_party/snappy/testdata/html_x_4 +1 -0
  150. data/ext/third_party/snappy/testdata/kppkn.gtb +0 -0
  151. data/ext/third_party/snappy/testdata/lcet10.txt +7519 -0
  152. data/ext/third_party/snappy/testdata/paper-100k.pdf +600 -2
  153. data/ext/third_party/snappy/testdata/plrabn12.txt +10699 -0
  154. data/ext/third_party/snappy/testdata/urls.10K +10000 -0
  155. data/lib/couchbase/binary_collection.rb +33 -76
  156. data/lib/couchbase/binary_collection_options.rb +94 -0
  157. data/lib/couchbase/bucket.rb +9 -3
  158. data/lib/couchbase/cluster.rb +161 -23
  159. data/lib/couchbase/collection.rb +108 -191
  160. data/lib/couchbase/collection_options.rb +430 -0
  161. data/lib/couchbase/errors.rb +136 -134
  162. data/lib/couchbase/json_transcoder.rb +32 -0
  163. data/lib/couchbase/management/analytics_index_manager.rb +185 -9
  164. data/lib/couchbase/management/bucket_manager.rb +84 -33
  165. data/lib/couchbase/management/collection_manager.rb +166 -1
  166. data/lib/couchbase/management/query_index_manager.rb +261 -0
  167. data/lib/couchbase/management/search_index_manager.rb +291 -0
  168. data/lib/couchbase/management/user_manager.rb +12 -10
  169. data/lib/couchbase/management/view_index_manager.rb +151 -1
  170. data/lib/couchbase/mutation_state.rb +11 -1
  171. data/lib/couchbase/scope.rb +4 -4
  172. data/lib/couchbase/version.rb +1 -1
  173. metadata +113 -18
  174. data/.travis.yml +0 -7
  175. data/ext/couchbase/io/binary_parser.hxx +0 -64
  176. data/lib/couchbase/results.rb +0 -307
@@ -0,0 +1,74 @@
1
+ /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright 2020 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 <document_id.hxx>
21
+ #include <protocol/cmd_upsert.hxx>
22
+ #include <protocol/durability_level.hxx>
23
+
24
+ namespace couchbase::operations
25
+ {
26
+
27
+ struct upsert_response {
28
+ document_id id;
29
+ std::error_code ec{};
30
+ std::uint64_t cas{};
31
+ mutation_token token{};
32
+ };
33
+
34
+ struct upsert_request {
35
+ using encoded_request_type = protocol::client_request<protocol::upsert_request_body>;
36
+ using encoded_response_type = protocol::client_response<protocol::upsert_response_body>;
37
+
38
+ document_id id;
39
+ std::string value;
40
+ uint16_t partition{};
41
+ uint32_t opaque{};
42
+ uint32_t flags{ 0 };
43
+ uint32_t expiration{ 0 };
44
+ protocol::durability_level durability_level{ protocol::durability_level::none };
45
+ std::optional<std::uint16_t> durability_timeout{};
46
+
47
+ void encode_to(encoded_request_type& encoded)
48
+ {
49
+ encoded.opaque(opaque);
50
+ encoded.partition(partition);
51
+ encoded.body().id(id);
52
+ encoded.body().expiration(expiration);
53
+ encoded.body().flags(flags);
54
+ encoded.body().content(value);
55
+ if (durability_level != protocol::durability_level::none) {
56
+ encoded.body().durability(durability_level, durability_timeout);
57
+ }
58
+ }
59
+ };
60
+
61
+ upsert_response
62
+ make_response(std::error_code ec, upsert_request& request, upsert_request::encoded_response_type encoded)
63
+ {
64
+ upsert_response response{ request.id, ec };
65
+ if (!ec) {
66
+ response.cas = encoded.cas();
67
+ response.token = encoded.body().token();
68
+ response.token.partition_id = request.partition;
69
+ response.token.bucket_name = response.id.bucket;
70
+ }
71
+ return response;
72
+ }
73
+
74
+ } // namespace couchbase::operations
@@ -0,0 +1,85 @@
1
+ /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright 2020 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 <tao/json.hpp>
21
+
22
+ #include <version.hxx>
23
+
24
+ namespace couchbase::operations
25
+ {
26
+ struct query_index_build_deferred_response {
27
+ struct query_problem {
28
+ std::uint64_t code;
29
+ std::string message;
30
+ };
31
+ uuid::uuid_t client_context_id;
32
+ std::error_code ec;
33
+ std::string status{};
34
+ std::vector<query_problem> errors{};
35
+ };
36
+
37
+ struct query_index_build_deferred_request {
38
+ using response_type = query_index_build_deferred_response;
39
+ using encoded_request_type = io::http_request;
40
+ using encoded_response_type = io::http_response;
41
+
42
+ static const inline service_type type = service_type::query;
43
+
44
+ uuid::uuid_t client_context_id{ uuid::random() };
45
+ std::string bucket_name;
46
+
47
+ void encode_to(encoded_request_type& encoded)
48
+ {
49
+ encoded.headers["content-type"] = "application/json";
50
+ tao::json::value body{
51
+ { "statement",
52
+ fmt::format(R"(BUILD INDEX ON `{}` ((SELECT RAW name FROM system:indexes WHERE keyspace_id = "{}" AND state = "deferred")))",
53
+ bucket_name,
54
+ bucket_name) },
55
+ { "client_context_id", uuid::to_string(client_context_id) }
56
+ };
57
+ encoded.method = "POST";
58
+ encoded.path = "/query/service";
59
+ encoded.body = tao::json::to_string(body);
60
+ }
61
+ };
62
+
63
+ query_index_build_deferred_response
64
+ make_response(std::error_code ec,
65
+ query_index_build_deferred_request& request,
66
+ query_index_build_deferred_request::encoded_response_type encoded)
67
+ {
68
+ query_index_build_deferred_response response{ request.client_context_id, ec };
69
+ if (!ec) {
70
+ auto payload = tao::json::from_string(encoded.body);
71
+ response.status = payload.at("status").get_string();
72
+ if (response.status != "success") {
73
+ for (const auto& entry : payload.at("errors").get_array()) {
74
+ query_index_build_deferred_response::query_problem error;
75
+ error.code = entry.at("code").get_unsigned();
76
+ error.message = entry.at("msg").get_string();
77
+ response.errors.emplace_back(error);
78
+ }
79
+ response.ec = std::make_error_code(error::common_errc::internal_server_failure);
80
+ }
81
+ }
82
+ return response;
83
+ }
84
+
85
+ } // namespace couchbase::operations
@@ -0,0 +1,134 @@
1
+ /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright 2020 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 <tao/json.hpp>
21
+
22
+ #include <version.hxx>
23
+
24
+ namespace couchbase::operations
25
+ {
26
+ struct query_index_create_response {
27
+ struct query_problem {
28
+ std::uint64_t code;
29
+ std::string message;
30
+ };
31
+ uuid::uuid_t client_context_id;
32
+ std::error_code ec;
33
+ std::string status{};
34
+ std::vector<query_problem> errors{};
35
+ };
36
+
37
+ struct query_index_create_request {
38
+ using response_type = query_index_create_response;
39
+ using encoded_request_type = io::http_request;
40
+ using encoded_response_type = io::http_response;
41
+
42
+ static const inline service_type type = service_type::query;
43
+
44
+ uuid::uuid_t client_context_id{ uuid::random() };
45
+ std::string bucket_name;
46
+ std::string index_name{};
47
+ std::vector<std::string> fields;
48
+ bool is_primary{ false };
49
+ bool ignore_if_exists{ false };
50
+ std::optional<std::string> condition{};
51
+ std::optional<bool> deferred{};
52
+ std::optional<int> num_replicas{};
53
+
54
+ void encode_to(encoded_request_type& encoded)
55
+ {
56
+ encoded.headers["content-type"] = "application/json";
57
+ tao::json::value with{};
58
+ if (deferred) {
59
+ with["defer_build"] = *deferred;
60
+ }
61
+ if (num_replicas) {
62
+ with["num_replica"] = *num_replicas; /* no 's' in key name */
63
+ }
64
+ std::string where_clause{};
65
+ if (condition) {
66
+ where_clause = fmt::format("WHERE {}", *condition);
67
+ }
68
+ std::string with_clause{};
69
+ if (with) {
70
+ with_clause = fmt::format("WITH {}", tao::json::to_string(with));
71
+ }
72
+ tao::json::value body{ { "statement",
73
+ is_primary ? fmt::format(R"(CREATE PRIMARY INDEX {} ON `{}` USING GSI {})",
74
+ index_name.empty() ? "" : fmt::format("`{}`", index_name),
75
+ bucket_name,
76
+ with_clause)
77
+ : fmt::format(R"(CREATE INDEX `{}` ON `{}`({}) {} USING GSI {})",
78
+ index_name,
79
+ bucket_name,
80
+ fmt::join(fields, ", "),
81
+ where_clause,
82
+ with_clause) },
83
+ { "client_context_id", uuid::to_string(client_context_id) } };
84
+ encoded.method = "POST";
85
+ encoded.path = "/query/service";
86
+ encoded.body = tao::json::to_string(body);
87
+ }
88
+ };
89
+
90
+ query_index_create_response
91
+ make_response(std::error_code ec, query_index_create_request& request, query_index_create_request::encoded_response_type encoded)
92
+ {
93
+ query_index_create_response response{ request.client_context_id, ec };
94
+ if (!ec) {
95
+ auto payload = tao::json::from_string(encoded.body);
96
+ response.status = payload.at("status").get_string();
97
+
98
+ if (response.status != "success") {
99
+ bool index_already_exists = false;
100
+ bool bucket_not_found = false;
101
+ for (const auto& entry : payload.at("errors").get_array()) {
102
+ query_index_create_response::query_problem error;
103
+ error.code = entry.at("code").get_unsigned();
104
+ error.message = entry.at("msg").get_string();
105
+ switch (error.code) {
106
+ case 5000: /* IKey: "Internal Error" */
107
+ if (error.message.find(" already exists") != std::string::npos) {
108
+ index_already_exists = true;
109
+ }
110
+ break;
111
+ case 12003: /* IKey: "datastore.couchbase.keyspace_not_found" */
112
+ bucket_not_found = true;
113
+ break;
114
+ case 4300: /* IKey: "plan.new_index_already_exists" */
115
+ index_already_exists = true;
116
+ break;
117
+ }
118
+ response.errors.emplace_back(error);
119
+ }
120
+ if (index_already_exists) {
121
+ if (!request.ignore_if_exists) {
122
+ response.ec = std::make_error_code(error::common_errc::index_exists);
123
+ }
124
+ } else if (bucket_not_found) {
125
+ response.ec = std::make_error_code(error::common_errc::bucket_not_found);
126
+ } else if (!response.errors.empty()) {
127
+ response.ec = std::make_error_code(error::common_errc::internal_server_failure);
128
+ }
129
+ }
130
+ }
131
+ return response;
132
+ }
133
+
134
+ } // namespace couchbase::operations
@@ -0,0 +1,108 @@
1
+ /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright 2020 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 <tao/json.hpp>
21
+
22
+ #include <version.hxx>
23
+
24
+ namespace couchbase::operations
25
+ {
26
+ struct query_index_drop_response {
27
+ struct query_problem {
28
+ std::uint64_t code;
29
+ std::string message;
30
+ };
31
+ uuid::uuid_t client_context_id;
32
+ std::error_code ec;
33
+ std::string status{};
34
+ std::vector<query_problem> errors{};
35
+ };
36
+
37
+ struct query_index_drop_request {
38
+ using response_type = query_index_drop_response;
39
+ using encoded_request_type = io::http_request;
40
+ using encoded_response_type = io::http_response;
41
+
42
+ static const inline service_type type = service_type::query;
43
+
44
+ uuid::uuid_t client_context_id{ uuid::random() };
45
+ std::string bucket_name;
46
+ std::string index_name;
47
+ bool is_primary{ false };
48
+ bool ignore_if_does_not_exist{ false };
49
+
50
+ void encode_to(encoded_request_type& encoded)
51
+ {
52
+ encoded.headers["content-type"] = "application/json";
53
+ tao::json::value body{ { "statement",
54
+ is_primary ? fmt::format(R"(DROP PRIMARY INDEX ON `{}` USING GSI)", bucket_name)
55
+ : fmt::format(R"(DROP INDEX `{}`.`{}` USING GSI)", bucket_name, index_name) },
56
+ { "client_context_id", uuid::to_string(client_context_id) } };
57
+ encoded.method = "POST";
58
+ encoded.path = "/query/service";
59
+ encoded.body = tao::json::to_string(body);
60
+ }
61
+ };
62
+
63
+ query_index_drop_response
64
+ make_response(std::error_code ec, query_index_drop_request& request, query_index_drop_request::encoded_response_type encoded)
65
+ {
66
+ query_index_drop_response response{ request.client_context_id, ec };
67
+ if (!ec) {
68
+ auto payload = tao::json::from_string(encoded.body);
69
+ response.status = payload.at("status").get_string();
70
+
71
+ if (response.status != "success") {
72
+ bool bucket_not_found = false;
73
+ bool index_not_found = false;
74
+ for (const auto& entry : payload.at("errors").get_array()) {
75
+ query_index_drop_response::query_problem error;
76
+ error.code = entry.at("code").get_unsigned();
77
+ error.message = entry.at("msg").get_string();
78
+ switch (error.code) {
79
+ case 5000: /* IKey: "Internal Error" */
80
+ if (error.message.find("not found.") != std::string::npos) {
81
+ index_not_found = true;
82
+ }
83
+ break;
84
+ case 12003: /* IKey: "datastore.couchbase.keyspace_not_found" */
85
+ bucket_not_found = true;
86
+ break;
87
+ case 12004: /* IKey: "datastore.couchbase.primary_idx_not_found" */
88
+ case 12006: /* IKey: "datastore.couchbase.keyspace_count_error" */
89
+ index_not_found = true;
90
+ break;
91
+ }
92
+ response.errors.emplace_back(error);
93
+ }
94
+ if (index_not_found) {
95
+ if (!request.ignore_if_does_not_exist) {
96
+ response.ec = std::make_error_code(error::common_errc::index_not_found);
97
+ }
98
+ } else if (bucket_not_found) {
99
+ response.ec = std::make_error_code(error::common_errc::bucket_not_found);
100
+ } else if (!response.errors.empty()) {
101
+ response.ec = std::make_error_code(error::common_errc::internal_server_failure);
102
+ }
103
+ }
104
+ }
105
+ return response;
106
+ }
107
+
108
+ } // namespace couchbase::operations
@@ -0,0 +1,106 @@
1
+ /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
+ /*
3
+ * Copyright 2020 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 <tao/json.hpp>
21
+
22
+ #include <version.hxx>
23
+
24
+ namespace couchbase::operations
25
+ {
26
+ struct query_index_get_all_response {
27
+ struct query_index {
28
+ bool is_primary{ false };
29
+ std::string id;
30
+ std::string name;
31
+ std::string state;
32
+ std::string datastore_id;
33
+ std::string keyspace_id;
34
+ std::string namespace_id;
35
+ std::string type;
36
+ std::vector<std::string> index_key{};
37
+ std::optional<std::string> condition{};
38
+ };
39
+ uuid::uuid_t client_context_id;
40
+ std::error_code ec;
41
+ std::string status{};
42
+ std::vector<query_index> indexes{};
43
+ };
44
+
45
+ struct query_index_get_all_request {
46
+ using response_type = query_index_get_all_response;
47
+ using encoded_request_type = io::http_request;
48
+ using encoded_response_type = io::http_response;
49
+
50
+ static const inline service_type type = service_type::query;
51
+
52
+ uuid::uuid_t client_context_id{ uuid::random() };
53
+ std::string bucket_name;
54
+
55
+ void encode_to(encoded_request_type& encoded)
56
+ {
57
+ encoded.headers["content-type"] = "application/json";
58
+ tao::json::value body{
59
+ { "statement",
60
+ fmt::format(
61
+ R"(SELECT idx.* FROM system:indexes AS idx WHERE keyspace_id = "{}" AND `using`="gsi" ORDER BY is_primary DESC, name ASC)",
62
+ bucket_name) },
63
+ { "client_context_id", uuid::to_string(client_context_id) }
64
+ };
65
+ encoded.method = "POST";
66
+ encoded.path = "/query/service";
67
+ encoded.body = tao::json::to_string(body);
68
+ }
69
+ };
70
+
71
+ query_index_get_all_response
72
+ make_response(std::error_code ec, query_index_get_all_request& request, query_index_get_all_request::encoded_response_type encoded)
73
+ {
74
+ query_index_get_all_response response{ request.client_context_id, ec };
75
+ if (!ec) {
76
+ if (encoded.status_code == 200) {
77
+ auto payload = tao::json::from_string(encoded.body);
78
+ response.status = payload.at("status").get_string();
79
+ if (response.status == "success") {
80
+ for (const auto& entry : payload.at("results").get_array()) {
81
+ query_index_get_all_response::query_index index;
82
+ index.id = entry.at("id").get_string();
83
+ index.datastore_id = entry.at("datastore_id").get_string();
84
+ index.namespace_id = entry.at("namespace_id").get_string();
85
+ index.keyspace_id = entry.at("keyspace_id").get_string();
86
+ index.type = entry.at("using").get_string();
87
+ index.name = entry.at("name").get_string();
88
+ index.state = entry.at("state").get_string();
89
+ if (const auto* prop = entry.find("is_primary")) {
90
+ index.is_primary = prop->get_boolean();
91
+ }
92
+ if (const auto* prop = entry.find("condition")) {
93
+ index.condition = prop->get_string();
94
+ }
95
+ for (const auto& key : entry.at("index_key").get_array()) {
96
+ index.index_key.emplace_back(key.get_string());
97
+ }
98
+ response.indexes.emplace_back(index);
99
+ }
100
+ }
101
+ }
102
+ }
103
+ return response;
104
+ }
105
+
106
+ } // namespace couchbase::operations