topology_description.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2018-present MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <vector>
  16. #include <bsoncxx/stdx/string_view.hpp>
  17. #include <mongocxx/events/server_description.hpp>
  18. #include <mongocxx/read_preference.hpp>
  19. #include <mongocxx/config/prelude.hpp>
  20. namespace mongocxx {
  21. MONGOCXX_INLINE_NAMESPACE_BEGIN
  22. namespace events {
  23. using mongocxx::read_preference;
  24. ///
  25. /// Class representing what the driver knows about a topology of MongoDB servers: either a
  26. /// standalone, a replica set, or a sharded cluster.
  27. ///
  28. class MONGOCXX_API topology_description {
  29. public:
  30. ///
  31. /// An array of server_description instances.
  32. ///
  33. class MONGOCXX_API server_descriptions {
  34. private:
  35. using container = std::vector<server_description>;
  36. public:
  37. ///
  38. /// Move constructs a server_descriptions array.
  39. ///
  40. server_descriptions(server_descriptions&&) noexcept;
  41. ///
  42. /// Move assigns a server_descriptions array.
  43. ///
  44. server_descriptions& operator=(server_descriptions&&) noexcept;
  45. server_descriptions(const server_descriptions&) = delete;
  46. server_descriptions& operator=(const server_descriptions&) = delete;
  47. ///
  48. /// Destroys a server_descriptions array.
  49. ///
  50. ~server_descriptions();
  51. ///
  52. /// The array's iterator type.
  53. ///
  54. using iterator = container::iterator;
  55. ///
  56. /// The array's const iterator type.
  57. ///
  58. using const_iterator = container::const_iterator;
  59. ///
  60. /// @{
  61. ///
  62. /// Returns an iterator to the beginning.
  63. ///
  64. iterator begin() noexcept;
  65. const_iterator begin() const noexcept;
  66. ///
  67. /// @}
  68. ///
  69. ///
  70. /// @{
  71. ///
  72. /// Returns an iterator to the end.
  73. ///
  74. iterator end() noexcept;
  75. const_iterator end() const noexcept;
  76. ///
  77. /// @}
  78. ///
  79. ///
  80. /// The number of server_description instances in the array.
  81. ///
  82. std::size_t size() const noexcept;
  83. private:
  84. friend topology_description;
  85. MONGOCXX_PRIVATE explicit server_descriptions(void* sds, std::size_t size);
  86. MONGOCXX_PRIVATE void swap(server_descriptions& other) noexcept;
  87. container _container;
  88. void* _sds;
  89. std::size_t _size;
  90. };
  91. MONGOCXX_PRIVATE explicit topology_description(void* event);
  92. ///
  93. /// Destroys a topology_description.
  94. ///
  95. ~topology_description();
  96. ///
  97. /// The topology type: "Unknown", "Sharded", "ReplicaSetNoPrimary", "ReplicaSetWithPrimary", or
  98. /// "Single".
  99. ///
  100. /// @return The type as a short-lived string view.
  101. ///
  102. bsoncxx::stdx::string_view type() const;
  103. ///
  104. /// Determines if the topology has a readable server available. Servers are
  105. /// filtered by the given read preferences only if the driver is connected
  106. /// to a replica set, otherwise the read preferences are ignored. This
  107. /// function uses the driver's current knowledge of the state of the
  108. /// MongoDB server or servers it is connected to; it does no I/O.
  109. ///
  110. /// @return Whether there is a readable server available.
  111. ///
  112. bool has_readable_server(const mongocxx::read_preference& pref) const;
  113. ///
  114. /// Determines if the topology has a writable server available, such as a
  115. /// primary, mongos, or standalone. This function uses the driver's current
  116. /// knowledge of the state of the MongoDB server or servers it is connected
  117. /// to; it does no I/O.
  118. ///
  119. /// @return Whether there is a writable server available.
  120. ///
  121. bool has_writable_server() const;
  122. ///
  123. /// Fetches descriptions for all known servers in the topology.
  124. ///
  125. /// @return An array of server_description objects.
  126. ///
  127. server_descriptions servers() const;
  128. private:
  129. // Non-const since mongoc_topology_description_has_readable_server/writable_server take
  130. // non-const. They do server selection, which modifies the mongoc_topology_description_t.
  131. void* _td;
  132. };
  133. } // namespace events
  134. MONGOCXX_INLINE_NAMESPACE_END
  135. } // namespace mongocxx
  136. #include <mongocxx/config/postlude.hpp>