couchbase 4.2.9 → 4.2.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. package/deps/couchbase-cxx-client/CMakeLists.txt +4 -0
  2. package/deps/couchbase-cxx-client/cmake/CompilerWarnings.cmake +7 -3
  3. package/deps/couchbase-cxx-client/cmake/OpenSSL.cmake +1 -0
  4. package/deps/couchbase-cxx-client/core/bucket.cxx +52 -2
  5. package/deps/couchbase-cxx-client/core/bucket.hxx +1 -1
  6. package/deps/couchbase-cxx-client/core/cluster.cxx +38 -0
  7. package/deps/couchbase-cxx-client/core/impl/bucket.cxx +24 -0
  8. package/deps/couchbase-cxx-client/core/impl/cluster.cxx +73 -0
  9. package/deps/couchbase-cxx-client/core/impl/diagnostics.cxx +294 -0
  10. package/deps/couchbase-cxx-client/core/impl/diagnostics.hxx +39 -0
  11. package/deps/couchbase-cxx-client/core/impl/scope.cxx +19 -4
  12. package/deps/couchbase-cxx-client/core/impl/search.cxx +75 -2
  13. package/deps/couchbase-cxx-client/core/impl/search.hxx +8 -0
  14. package/deps/couchbase-cxx-client/core/impl/search_index_manager.cxx +261 -18
  15. package/deps/couchbase-cxx-client/core/impl/search_request.cxx +139 -0
  16. package/deps/couchbase-cxx-client/core/impl/vector_query.cxx +42 -0
  17. package/deps/couchbase-cxx-client/core/impl/vector_search.cxx +40 -0
  18. package/deps/couchbase-cxx-client/core/io/http_session.hxx +6 -3
  19. package/deps/couchbase-cxx-client/core/io/mcbp_session.cxx +24 -32
  20. package/deps/couchbase-cxx-client/core/io/mcbp_session.hxx +1 -0
  21. package/deps/couchbase-cxx-client/core/logger/logger.cxx +1 -1
  22. package/deps/couchbase-cxx-client/core/meta/features.hxx +10 -0
  23. package/deps/couchbase-cxx-client/core/operations/document_query.cxx +6 -1
  24. package/deps/couchbase-cxx-client/core/operations/document_search.cxx +37 -1
  25. package/deps/couchbase-cxx-client/core/operations/document_search.hxx +11 -0
  26. package/deps/couchbase-cxx-client/core/operations/management/collection_create.cxx +7 -6
  27. package/deps/couchbase-cxx-client/core/operations/management/collection_create.hxx +1 -1
  28. package/deps/couchbase-cxx-client/core/operations/management/collection_update.cxx +7 -8
  29. package/deps/couchbase-cxx-client/core/operations/management/collection_update.hxx +1 -1
  30. package/deps/couchbase-cxx-client/core/operations/management/error_utils.cxx +3 -0
  31. package/deps/couchbase-cxx-client/core/operations/management/search_index_analyze_document.cxx +17 -1
  32. package/deps/couchbase-cxx-client/core/operations/management/search_index_analyze_document.hxx +3 -0
  33. package/deps/couchbase-cxx-client/core/operations/management/search_index_control_ingest.cxx +21 -1
  34. package/deps/couchbase-cxx-client/core/operations/management/search_index_control_ingest.hxx +3 -0
  35. package/deps/couchbase-cxx-client/core/operations/management/search_index_control_plan_freeze.cxx +21 -1
  36. package/deps/couchbase-cxx-client/core/operations/management/search_index_control_plan_freeze.hxx +3 -0
  37. package/deps/couchbase-cxx-client/core/operations/management/search_index_control_query.cxx +21 -1
  38. package/deps/couchbase-cxx-client/core/operations/management/search_index_control_query.hxx +3 -0
  39. package/deps/couchbase-cxx-client/core/operations/management/search_index_drop.cxx +17 -1
  40. package/deps/couchbase-cxx-client/core/operations/management/search_index_drop.hxx +3 -0
  41. package/deps/couchbase-cxx-client/core/operations/management/search_index_get.cxx +17 -1
  42. package/deps/couchbase-cxx-client/core/operations/management/search_index_get.hxx +2 -0
  43. package/deps/couchbase-cxx-client/core/operations/management/search_index_get_all.cxx +39 -22
  44. package/deps/couchbase-cxx-client/core/operations/management/search_index_get_all.hxx +3 -0
  45. package/deps/couchbase-cxx-client/core/operations/management/search_index_get_documents_count.cxx +18 -1
  46. package/deps/couchbase-cxx-client/core/operations/management/search_index_get_documents_count.hxx +3 -0
  47. package/deps/couchbase-cxx-client/core/operations/management/search_index_upsert.cxx +17 -1
  48. package/deps/couchbase-cxx-client/core/operations/management/search_index_upsert.hxx +3 -0
  49. package/deps/couchbase-cxx-client/core/topology/collections_manifest.hxx +1 -1
  50. package/deps/couchbase-cxx-client/core/topology/collections_manifest_json.hxx +1 -1
  51. package/deps/couchbase-cxx-client/core/transactions/internal/utils.hxx +4 -0
  52. package/deps/couchbase-cxx-client/core/vector_query_combination.hxx +23 -0
  53. package/deps/couchbase-cxx-client/couchbase/bucket.hxx +29 -0
  54. package/deps/couchbase-cxx-client/couchbase/cluster.hxx +110 -0
  55. package/deps/couchbase-cxx-client/couchbase/codec/json_transcoder.hxx +1 -1
  56. package/deps/couchbase-cxx-client/couchbase/codec/raw_binary_transcoder.hxx +2 -1
  57. package/deps/couchbase-cxx-client/couchbase/codec/raw_json_transcoder.hxx +78 -0
  58. package/deps/couchbase-cxx-client/couchbase/codec/raw_string_transcoder.hxx +72 -0
  59. package/deps/couchbase-cxx-client/couchbase/create_collection_options.hxx +29 -1
  60. package/deps/couchbase-cxx-client/couchbase/diagnostics_options.hxx +75 -0
  61. package/deps/couchbase-cxx-client/couchbase/diagnostics_result.hxx +124 -0
  62. package/deps/couchbase-cxx-client/couchbase/endpoint_diagnostics.hxx +206 -0
  63. package/deps/couchbase-cxx-client/couchbase/endpoint_ping_report.hxx +205 -0
  64. package/deps/couchbase-cxx-client/couchbase/get_options.hxx +1 -6
  65. package/deps/couchbase-cxx-client/couchbase/management/collection_spec.hxx +1 -1
  66. package/deps/couchbase-cxx-client/couchbase/ping_options.hxx +93 -0
  67. package/deps/couchbase-cxx-client/couchbase/ping_result.hxx +118 -0
  68. package/deps/couchbase-cxx-client/couchbase/scope.hxx +24 -8
  69. package/deps/couchbase-cxx-client/couchbase/scope_search_index_manager.hxx +291 -0
  70. package/deps/couchbase-cxx-client/couchbase/search_request.hxx +120 -0
  71. package/deps/couchbase-cxx-client/couchbase/service_type.hxx +58 -0
  72. package/deps/couchbase-cxx-client/couchbase/update_collection_options.hxx +32 -3
  73. package/deps/couchbase-cxx-client/couchbase/vector_query.hxx +99 -0
  74. package/deps/couchbase-cxx-client/couchbase/vector_search.hxx +85 -0
  75. package/deps/couchbase-cxx-client/couchbase/vector_search_options.hxx +76 -0
  76. package/dist/binding.d.ts +9 -0
  77. package/dist/binding.js +4 -1
  78. package/dist/bindingutilities.d.ts +6 -1
  79. package/dist/bindingutilities.js +15 -1
  80. package/dist/cluster.d.ts +12 -1
  81. package/dist/cluster.js +22 -0
  82. package/dist/couchbase.d.ts +1 -0
  83. package/dist/couchbase.js +1 -0
  84. package/dist/searchexecutor.d.ts +2 -2
  85. package/dist/searchexecutor.js +19 -3
  86. package/dist/searchtypes.d.ts +46 -0
  87. package/dist/searchtypes.js +81 -1
  88. package/dist/vectorsearch.d.ts +99 -0
  89. package/dist/vectorsearch.js +132 -0
  90. package/package.json +7 -7
  91. package/src/constants.cpp +11 -0
  92. package/src/jstocbpp_autogen.hpp +24 -7
  93. package/tools/gen-bindings-json.py +2 -0
@@ -121,6 +121,7 @@ set(couchbase_cxx_client_FILES
121
121
  core/impl/date_range_facet.cxx
122
122
  core/impl/date_range_facet_result.cxx
123
123
  core/impl/date_range_query.cxx
124
+ core/impl/diagnostics.cxx
124
125
  core/impl/disjunction_query.cxx
125
126
  core/impl/dns_srv_tracker.cxx
126
127
  core/impl/doc_id_query.cxx
@@ -173,6 +174,7 @@ set(couchbase_cxx_client_FILES
173
174
  core/impl/search_index_manager.cxx
174
175
  core/impl/search_meta_data.cxx
175
176
  core/impl/search_result.cxx
177
+ core/impl/search_request.cxx
176
178
  core/impl/search_row.cxx
177
179
  core/impl/search_row_location.cxx
178
180
  core/impl/search_row_locations.cxx
@@ -205,6 +207,8 @@ set(couchbase_cxx_client_FILES
205
207
  core/impl/transaction_error_category.cxx
206
208
  core/impl/transaction_get_result.cxx
207
209
  core/impl/transaction_op_error_category.cxx
210
+ core/impl/vector_query.cxx
211
+ core/impl/vector_search.cxx
208
212
  core/impl/view_error_category.cxx
209
213
  core/impl/wildcard_query.cxx
210
214
  core/io/dns_client.cxx
@@ -67,9 +67,13 @@ function(set_project_warnings project_name)
67
67
  -Wuseless-cast # warn if you perform a cast to the same type
68
68
  -Wdeprecated-declarations # warn if [[deprecated]] elements being used
69
69
  )
70
- set(CLANG_WARNINGS ${COMMON_WARNINGS} -Wshadow # warn the user if a variable declaration shadows one from a parent
71
- # context
72
- )
70
+
71
+ set(CLANG_WARNINGS ${COMMON_WARNINGS})
72
+ if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS_EQUAL "17.0.0")
73
+ set(CLANG_WARNINGS ${CLANG_WARNINGS} -Wshadow # warn the user if a variable declaration shadows one from a parent
74
+ # context
75
+ )
76
+ endif()
73
77
 
74
78
  if(MSVC)
75
79
  set(PROJECT_WARNINGS ${MSVC_WARNINGS})
@@ -54,6 +54,7 @@ elseif(COUCHBASE_CXX_CLIENT_STATIC_BORINGSSL)
54
54
  )
55
55
  endif()
56
56
  endif()
57
+ declare_system_library(ssl)
57
58
  add_library(OpenSSL::SSL ALIAS ssl)
58
59
  add_library(OpenSSL::Crypto ALIAS ssl)
59
60
  else()
@@ -28,6 +28,7 @@
28
28
  #include "mcbp/queue_response.hxx"
29
29
  #include "origin.hxx"
30
30
  #include "ping_collector.hxx"
31
+ #include "protocol/cmd_get_cluster_config.hxx"
31
32
  #include "retry_orchestrator.hxx"
32
33
 
33
34
  #include <couchbase/metrics/meter.hxx>
@@ -67,6 +68,10 @@ class bucket_impl
67
68
  , codec_{ { known_features_.begin(), known_features_.end() } }
68
69
  , ctx_{ ctx }
69
70
  , tls_{ tls }
71
+ , heartbeat_timer_(ctx_)
72
+ , heartbeat_interval_{ origin_.options().config_poll_floor > origin_.options().config_poll_interval
73
+ ? origin_.options().config_poll_floor
74
+ : origin_.options().config_poll_interval }
70
75
  {
71
76
  }
72
77
 
@@ -421,6 +426,7 @@ class bucket_impl
421
426
  }
422
427
  self->update_config(cfg);
423
428
  self->drain_deferred_queue();
429
+ self->fetch_config({});
424
430
  }
425
431
  asio::post(asio::bind_executor(self->ctx_, [h = std::move(h), ec, cfg = std::move(cfg)]() mutable { h(ec, cfg); }));
426
432
  });
@@ -476,12 +482,52 @@ class bucket_impl
476
482
  }
477
483
  }
478
484
 
485
+ void fetch_config(std::error_code ec)
486
+ {
487
+ if (ec == asio::error::operation_aborted || closed_) {
488
+ return;
489
+ }
490
+ if (heartbeat_timer_.expiry() > std::chrono::steady_clock::now()) {
491
+ return;
492
+ }
493
+
494
+ std::optional<io::mcbp_session> session{};
495
+ {
496
+ std::scoped_lock lock(sessions_mutex_);
497
+
498
+ std::size_t start = heartbeat_next_index_.fetch_add(1);
499
+ std::size_t i = start;
500
+ do {
501
+ auto ptr = sessions_.find(i % sessions_.size());
502
+ if (ptr != sessions_.end() && ptr->second.supports_gcccp()) {
503
+ session = ptr->second;
504
+ }
505
+ i = heartbeat_next_index_.fetch_add(1);
506
+ } while (start % sessions_.size() != i % sessions_.size());
507
+ }
508
+ if (session) {
509
+ protocol::client_request<protocol::get_cluster_config_request_body> req;
510
+ req.opaque(session->next_opaque());
511
+ session->write_and_flush(req.data());
512
+ } else {
513
+ CB_LOG_WARNING(R"({} unable to find session with GCCCP support, retry in {})", log_prefix_, heartbeat_interval_);
514
+ }
515
+ heartbeat_timer_.expires_after(heartbeat_interval_);
516
+ return heartbeat_timer_.async_wait([self = shared_from_this()](std::error_code e) {
517
+ if (e == asio::error::operation_aborted) {
518
+ return;
519
+ }
520
+ self->fetch_config(e);
521
+ });
522
+ }
523
+
479
524
  void close()
480
525
  {
481
- if (closed_) {
526
+ if (bool expected_state{ false }; !closed_.compare_exchange_strong(expected_state, true)) {
482
527
  return;
483
528
  }
484
- closed_ = true;
529
+
530
+ heartbeat_timer_.cancel();
485
531
 
486
532
  drain_deferred_queue();
487
533
 
@@ -773,6 +819,10 @@ class bucket_impl
773
819
  asio::io_context& ctx_;
774
820
  asio::ssl::context& tls_;
775
821
 
822
+ asio::steady_timer heartbeat_timer_;
823
+ std::chrono::milliseconds heartbeat_interval_;
824
+ std::atomic_size_t heartbeat_next_index_{ 0 };
825
+
776
826
  std::atomic_bool closed_{ false };
777
827
  std::atomic_bool configured_{ false };
778
828
 
@@ -151,7 +151,7 @@ class bucket
151
151
  }
152
152
  cmd->last_dispatched_from_ = session->local_address();
153
153
  cmd->last_dispatched_to_ = session->bootstrap_address();
154
- CB_LOG_TRACE(R"([{}] send operation id="{}", key="{}", partition={}, index={}, address="{}", rev={})",
154
+ CB_LOG_TRACE(R"({} send operation id="{}", key="{}", partition={}, index={}, address="{}", rev={})",
155
155
  session->log_prefix(),
156
156
  cmd->id_,
157
157
  cmd->request.id,
@@ -333,6 +333,38 @@ class cluster_impl : public std::enable_shared_from_this<cluster_impl>
333
333
  }
334
334
  }
335
335
 
336
+ template<class Request,
337
+ class Handler,
338
+ typename std::enable_if_t<std::is_same_v<typename Request::encoded_request_type, io::http_request>, int> = 0>
339
+ void execute_with_bucket_capability_check(Request request, bucket_capability cap, Handler&& handler)
340
+ {
341
+ auto bucket_name = request.bucket_name;
342
+ return open_bucket(
343
+ bucket_name,
344
+ [self = shared_from_this(), bucket_name, cap, request = std::move(request), handler = std::forward<Handler>(handler)](
345
+ std::error_code ec) mutable {
346
+ if (ec) {
347
+ handler(request.make_response({ ec }, {}));
348
+ return;
349
+ }
350
+ return self->with_bucket_configuration(
351
+ bucket_name,
352
+ [self = std::move(self), cap, request = std::move(request), handler = std::forward<Handler>(handler)](
353
+ std::error_code ec, topology::configuration config) mutable {
354
+ if (ec) {
355
+ handler(request.make_response({ ec }, {}));
356
+ return;
357
+ }
358
+ auto bucket_caps = config.bucket_capabilities;
359
+ if (bucket_caps.find(cap) == bucket_caps.end()) {
360
+ handler(request.make_response({ errc::common::feature_not_available }, {}));
361
+ return;
362
+ }
363
+ return self->execute(std::move(request), std::forward<Handler>(handler));
364
+ });
365
+ });
366
+ }
367
+
336
368
  std::shared_ptr<bucket> find_bucket_by_name(const std::string& name)
337
369
  {
338
370
  std::scoped_lock lock(buckets_mutex_);
@@ -1074,6 +1106,9 @@ void
1074
1106
  cluster::execute(operations::management::collection_create_request request,
1075
1107
  utils::movable_function<void(operations::management::collection_create_response)>&& handler) const
1076
1108
  {
1109
+ if (request.history.has_value()) {
1110
+ return impl_->execute_with_bucket_capability_check(std::move(request), bucket_capability::non_deduped_history, std::move(handler));
1111
+ }
1077
1112
  return impl_->execute(std::move(request), std::move(handler));
1078
1113
  }
1079
1114
 
@@ -1081,6 +1116,9 @@ void
1081
1116
  cluster::execute(operations::management::collection_update_request request,
1082
1117
  utils::movable_function<void(operations::management::collection_update_response)>&& handler) const
1083
1118
  {
1119
+ if (request.history.has_value()) {
1120
+ return impl_->execute_with_bucket_capability_check(std::move(request), bucket_capability::non_deduped_history, std::move(handler));
1121
+ }
1084
1122
  return impl_->execute(std::move(request), std::move(handler));
1085
1123
  }
1086
1124
 
@@ -16,6 +16,7 @@
16
16
  */
17
17
 
18
18
  #include "core/cluster.hxx"
19
+ #include "diagnostics.hxx"
19
20
 
20
21
  #include <couchbase/bucket.hxx>
21
22
 
@@ -42,6 +43,15 @@ class bucket_impl : public std::enable_shared_from_this<bucket_impl>
42
43
  return core_;
43
44
  }
44
45
 
46
+ void ping(const ping_options::built& options, ping_handler&& handler) const
47
+ {
48
+ return core_.ping(options.report_id,
49
+ name_,
50
+ core::impl::to_core_service_types(options.service_types),
51
+ options.timeout,
52
+ [handler = std::move(handler)](auto resp) mutable { return handler(core::impl::build_result(resp)); });
53
+ }
54
+
45
55
  private:
46
56
  core::cluster core_;
47
57
  std::string name_;
@@ -70,6 +80,20 @@ bucket::scope(std::string_view scope_name) const -> couchbase::scope
70
80
  return { impl_->core(), impl_->name(), scope_name };
71
81
  }
72
82
 
83
+ void
84
+ bucket::ping(const couchbase::ping_options& options, couchbase::ping_handler&& handler) const
85
+ {
86
+ return impl_->ping(options.build(), std::move(handler));
87
+ }
88
+
89
+ auto
90
+ bucket::ping(const couchbase::ping_options& options) const -> std::future<ping_result>
91
+ {
92
+ auto barrier = std::make_shared<std::promise<ping_result>>();
93
+ ping(options, [barrier](auto result) mutable { barrier->set_value(std::move(result)); });
94
+ return barrier->get_future();
95
+ }
96
+
73
97
  auto
74
98
  bucket::collections() const -> collection_manager
75
99
  {
@@ -21,6 +21,7 @@
21
21
  #include "core/agent_group.hxx"
22
22
  #include "core/transactions.hxx"
23
23
  #include "core/utils/connection_string.hxx"
24
+ #include "diagnostics.hxx"
24
25
  #include "internal_search_error_context.hxx"
25
26
  #include "internal_search_meta_data.hxx"
26
27
  #include "internal_search_result.hxx"
@@ -186,6 +187,33 @@ class cluster_impl : public std::enable_shared_from_this<cluster_impl>
186
187
  });
187
188
  }
188
189
 
190
+ void ping(const ping_options::built& options, ping_handler&& handler) const
191
+ {
192
+ return core_.ping(options.report_id,
193
+ {},
194
+ core::impl::to_core_service_types(options.service_types),
195
+ options.timeout,
196
+ [handler = std::move(handler)](auto resp) mutable { return handler(core::impl::build_result(resp)); });
197
+ };
198
+
199
+ void diagnostics(const diagnostics_options::built& options, diagnostics_handler&& handler) const
200
+ {
201
+ return core_.diagnostics(options.report_id,
202
+ [handler = std::move(handler)](auto resp) mutable { return handler(core::impl::build_result(resp)); });
203
+ }
204
+
205
+ void search(std::string index_name,
206
+ couchbase::search_request request,
207
+ const search_options::built& options,
208
+ search_handler&& handler) const
209
+ {
210
+ return core_.execute(core::impl::build_search_request(std::move(index_name), std::move(request), options, {}, {}),
211
+ [handler = std::move(handler)](auto resp) mutable {
212
+ return handler(search_error_context{ internal_search_error_context{ resp } },
213
+ search_result{ internal_search_result{ resp } });
214
+ });
215
+ }
216
+
189
217
  void close(core::utils::movable_function<void()> handler)
190
218
  {
191
219
  if (transactions_) {
@@ -283,6 +311,51 @@ cluster::search_query(std::string index_name, const class search_query& query, c
283
311
  return barrier->get_future();
284
312
  }
285
313
 
314
+ void
315
+ cluster::ping(const couchbase::ping_options& options, couchbase::ping_handler&& handler) const
316
+ {
317
+ return impl_->ping(options.build(), std::move(handler));
318
+ }
319
+
320
+ auto
321
+ cluster::ping(const couchbase::ping_options& options) const -> std::future<ping_result>
322
+ {
323
+ auto barrier = std::make_shared<std::promise<ping_result>>();
324
+ ping(options, [barrier](auto result) mutable { barrier->set_value(std::move(result)); });
325
+ return barrier->get_future();
326
+ }
327
+
328
+ void
329
+ cluster::diagnostics(const couchbase::diagnostics_options& options, couchbase::diagnostics_handler&& handler) const
330
+ {
331
+ return impl_->diagnostics(options.build(), std::move(handler));
332
+ }
333
+
334
+ auto
335
+ cluster::diagnostics(const couchbase::diagnostics_options& options) const -> std::future<diagnostics_result>
336
+ {
337
+ auto barrier = std::make_shared<std::promise<diagnostics_result>>();
338
+ diagnostics(options, [barrier](auto result) mutable { barrier->set_value(std::move(result)); });
339
+ return barrier->get_future();
340
+ }
341
+
342
+ void
343
+ cluster::search(std::string index_name, search_request request, const search_options& options, search_handler&& handler) const
344
+ {
345
+ return impl_->search(std::move(index_name), std::move(request), options.build(), std::move(handler));
346
+ }
347
+
348
+ auto
349
+ cluster::search(std::string index_name, search_request request, const search_options& options) const
350
+ -> std::future<std::pair<search_error_context, search_result>>
351
+ {
352
+ auto barrier = std::make_shared<std::promise<std::pair<search_error_context, search_result>>>();
353
+ search(std::move(index_name), std::move(request), options, [barrier](auto ctx, auto result) mutable {
354
+ barrier->set_value(std::make_pair(std::move(ctx), std::move(result)));
355
+ });
356
+ return barrier->get_future();
357
+ }
358
+
286
359
  auto
287
360
  cluster::connect(asio::io_context& io, const std::string& connection_string, const cluster_options& options)
288
361
  -> std::future<std::pair<cluster, std::error_code>>
@@ -0,0 +1,294 @@
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 "diagnostics.hxx"
19
+
20
+ #include "core/diagnostics.hxx"
21
+ #include "core/service_type.hxx"
22
+
23
+ #include <couchbase/codec/tao_json_serializer.hxx>
24
+ #include <couchbase/diagnostics_result.hxx>
25
+ #include <couchbase/endpoint_ping_report.hxx>
26
+ #include <couchbase/ping_result.hxx>
27
+ #include <couchbase/service_type.hxx>
28
+
29
+ #include <cstdint>
30
+ #include <map>
31
+ #include <set>
32
+ #include <string>
33
+ #include <vector>
34
+
35
+ namespace couchbase
36
+ {
37
+ namespace
38
+ {
39
+ std::string
40
+ service_type_as_string(service_type service_type)
41
+ {
42
+ switch (service_type) {
43
+ case service_type::key_value:
44
+ return "kv";
45
+ case service_type::query:
46
+ return "query";
47
+ case service_type::analytics:
48
+ return "analytics";
49
+ case service_type::search:
50
+ return "search";
51
+ case service_type::view:
52
+ return "views";
53
+ case service_type::management:
54
+ return "mgmt";
55
+ case service_type::eventing:
56
+ return "eventing";
57
+ }
58
+ return "";
59
+ }
60
+
61
+ std::string
62
+ ping_state_as_string(ping_state state)
63
+ {
64
+ switch (state) {
65
+ case ping_state::ok:
66
+ return "ok";
67
+ case ping_state::timeout:
68
+ return "timeout";
69
+ case ping_state::error:
70
+ return "error";
71
+ }
72
+ return "";
73
+ }
74
+
75
+ std::string
76
+ endpoint_state_as_string(endpoint_state state)
77
+ {
78
+ switch (state) {
79
+ case endpoint_state::connected:
80
+ return "connected";
81
+ case endpoint_state::connecting:
82
+ return "connecting";
83
+ case endpoint_state::disconnected:
84
+ return "disconnected";
85
+ case endpoint_state::disconnecting:
86
+ return "disconnecting";
87
+ }
88
+ return "";
89
+ }
90
+
91
+ codec::tao_json_serializer::document_type
92
+ endpoint_ping_report_as_json(const endpoint_ping_report& report)
93
+ {
94
+ codec::tao_json_serializer::document_type res{
95
+ { "id", report.id() }, { "latency_us", report.latency().count() }, { "remote", report.remote() },
96
+ { "local", report.local() }, { "state", ping_state_as_string(report.state()) },
97
+ };
98
+ if (report.error()) {
99
+ res["error"] = report.error().value();
100
+ }
101
+ if (report.endpoint_namespace()) {
102
+ res["namespace"] = report.endpoint_namespace().value();
103
+ }
104
+ return res;
105
+ }
106
+
107
+ codec::tao_json_serializer::document_type
108
+ endpoint_diagnostics_as_json(const endpoint_diagnostics& report)
109
+ {
110
+ codec::tao_json_serializer::document_type res{
111
+ { "id", report.id() },
112
+ { "local", report.local() },
113
+ { "remote", report.remote() },
114
+ { "state", endpoint_state_as_string(report.state()) },
115
+ };
116
+ if (report.last_activity()) {
117
+ res["last_activity_us"] = report.last_activity().value().count();
118
+ }
119
+ if (report.endpoint_namespace()) {
120
+ res["namespace"] = report.endpoint_namespace().value();
121
+ }
122
+ if (report.details()) {
123
+ res["details"] = report.details();
124
+ }
125
+ return res;
126
+ }
127
+ } // namespace
128
+
129
+ auto
130
+ ping_result::as_json() const -> codec::tao_json_serializer::document_type
131
+ {
132
+ codec::tao_json_serializer::document_type endpoints{};
133
+ for (const auto& [service_type, reports] : endpoints_) {
134
+ std::vector<codec::tao_json_serializer::document_type> json_reports{};
135
+ for (const auto& report : reports) {
136
+ json_reports.emplace_back(endpoint_ping_report_as_json(report));
137
+ }
138
+ endpoints[service_type_as_string(service_type)] = json_reports;
139
+ }
140
+ return {
141
+ { "version", version_ },
142
+ { "id", id_ },
143
+ { "sdk", sdk_ },
144
+ { "services", endpoints },
145
+ };
146
+ }
147
+
148
+ auto
149
+ diagnostics_result::as_json() const -> codec::tao_json_serializer::document_type
150
+ {
151
+ codec::tao_json_serializer::document_type endpoints{};
152
+ for (const auto& [service_type, reports] : endpoints_) {
153
+ std::vector<codec::tao_json_serializer::document_type> json_reports{};
154
+ for (const auto& report : reports) {
155
+ json_reports.emplace_back(endpoint_diagnostics_as_json(report));
156
+ }
157
+ endpoints[service_type_as_string(service_type)] = json_reports;
158
+ }
159
+ return {
160
+ { "id", id_ },
161
+ { "sdk", sdk_ },
162
+ { "version", version_ },
163
+ { "services", endpoints },
164
+ };
165
+ }
166
+ } // namespace couchbase
167
+
168
+ namespace couchbase::core::impl
169
+ {
170
+ namespace
171
+ {
172
+ couchbase::service_type
173
+ to_public_service_type(core::service_type service_type)
174
+ {
175
+ switch (service_type) {
176
+ case core::service_type::key_value:
177
+ return couchbase::service_type::key_value;
178
+ case core::service_type::query:
179
+ return couchbase::service_type::query;
180
+ case core::service_type::analytics:
181
+ return couchbase::service_type::analytics;
182
+ case core::service_type::search:
183
+ return couchbase::service_type::search;
184
+ case core::service_type::view:
185
+ return couchbase::service_type::view;
186
+ case core::service_type::management:
187
+ return couchbase::service_type::management;
188
+ case core::service_type::eventing:
189
+ return couchbase::service_type::eventing;
190
+ }
191
+ return {};
192
+ }
193
+
194
+ couchbase::ping_state
195
+ to_public_ping_state(core::diag::ping_state ping_state)
196
+ {
197
+ switch (ping_state) {
198
+ case core::diag::ping_state::timeout:
199
+ return couchbase::ping_state::timeout;
200
+ case core::diag::ping_state::error:
201
+ return couchbase::ping_state::error;
202
+ case core::diag::ping_state::ok:
203
+ return couchbase::ping_state::ok;
204
+ }
205
+ return {};
206
+ }
207
+
208
+ couchbase::endpoint_state
209
+ to_public_endpoint_state(core::diag::endpoint_state endpoint_state)
210
+ {
211
+ switch (endpoint_state) {
212
+ case core::diag::endpoint_state::connected:
213
+ return couchbase::endpoint_state::connected;
214
+ case diag::endpoint_state::disconnected:
215
+ return couchbase::endpoint_state::disconnected;
216
+ case diag::endpoint_state::connecting:
217
+ return couchbase::endpoint_state::connecting;
218
+ case diag::endpoint_state::disconnecting:
219
+ return couchbase::endpoint_state::disconnecting;
220
+ }
221
+ return {};
222
+ }
223
+ } // namespace
224
+
225
+ std::set<core::service_type>
226
+ to_core_service_types(const std::set<couchbase::service_type>& service_types)
227
+ {
228
+ std::set<core::service_type> res{};
229
+ for (auto s : service_types) {
230
+ switch (s) {
231
+ case couchbase::service_type::key_value:
232
+ res.emplace(core::service_type::key_value);
233
+ break;
234
+ case couchbase::service_type::query:
235
+ res.emplace(core::service_type::query);
236
+ break;
237
+ case couchbase::service_type::analytics:
238
+ res.emplace(core::service_type::analytics);
239
+ break;
240
+ case couchbase::service_type::search:
241
+ res.emplace(core::service_type::search);
242
+ break;
243
+ case couchbase::service_type::view:
244
+ res.emplace(core::service_type::view);
245
+ break;
246
+ case couchbase::service_type::management:
247
+ res.emplace(core::service_type::management);
248
+ break;
249
+ case couchbase::service_type::eventing:
250
+ res.emplace(core::service_type::eventing);
251
+ break;
252
+ }
253
+ }
254
+ return res;
255
+ }
256
+
257
+ couchbase::ping_result
258
+ build_result(const core::diag::ping_result& result)
259
+ {
260
+ std::map<couchbase::service_type, std::vector<couchbase::endpoint_ping_report>> endpoints{};
261
+ for (const auto& [core_service_type, core_endpoints] : result.services) {
262
+ auto service_type = to_public_service_type(core_service_type);
263
+ endpoints[service_type] = std::vector<couchbase::endpoint_ping_report>{};
264
+ for (const auto& info : core_endpoints) {
265
+ endpoints[service_type].emplace_back(
266
+ service_type, info.id, info.local, info.remote, to_public_ping_state(info.state), info.error, info.bucket, info.latency);
267
+ }
268
+ }
269
+
270
+ return { result.id, static_cast<std::uint16_t>(result.version), result.sdk, endpoints };
271
+ }
272
+
273
+ couchbase::diagnostics_result
274
+ build_result(const core::diag::diagnostics_result& result)
275
+ {
276
+ std::map<couchbase::service_type, std::vector<couchbase::endpoint_diagnostics>> endpoints{};
277
+ for (const auto& [core_service_type, core_endpoints] : result.services) {
278
+ auto service_type = to_public_service_type(core_service_type);
279
+ endpoints[service_type] = std::vector<couchbase::endpoint_diagnostics>{};
280
+ for (const auto& info : core_endpoints) {
281
+ endpoints[service_type].emplace_back(service_type,
282
+ info.id,
283
+ info.last_activity,
284
+ info.local,
285
+ info.remote,
286
+ info.bucket,
287
+ to_public_endpoint_state(info.state),
288
+ info.details);
289
+ }
290
+ }
291
+
292
+ return { result.id, static_cast<std::uint16_t>(result.version), result.sdk, endpoints };
293
+ }
294
+ } // namespace couchbase::core::impl
@@ -0,0 +1,39 @@
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
+ #pragma once
19
+
20
+ #include <couchbase/diagnostics_result.hxx>
21
+ #include <couchbase/ping_result.hxx>
22
+ #include <couchbase/service_type.hxx>
23
+
24
+ #include "core/diagnostics.hxx"
25
+ #include "core/service_type.hxx"
26
+
27
+ #include <set>
28
+
29
+ namespace couchbase::core::impl
30
+ {
31
+ std::set<core::service_type>
32
+ to_core_service_types(const std::set<couchbase::service_type>& service_types);
33
+
34
+ couchbase::ping_result
35
+ build_result(const core::diag::ping_result& result);
36
+
37
+ couchbase::diagnostics_result
38
+ build_result(const core::diag::diagnostics_result& result);
39
+ } // namespace couchbase::core::impl