index_view.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // Copyright 2017 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 <string>
  16. #include <vector>
  17. #include <bsoncxx/document/value.hpp>
  18. #include <bsoncxx/stdx/optional.hpp>
  19. #include <mongocxx/client_session.hpp>
  20. #include <mongocxx/cursor.hpp>
  21. #include <mongocxx/index_model.hpp>
  22. #include <mongocxx/options/index_view.hpp>
  23. #include <mongocxx/config/prelude.hpp>
  24. namespace mongocxx {
  25. MONGOCXX_INLINE_NAMESPACE_BEGIN
  26. class MONGOCXX_API index_view {
  27. public:
  28. index_view(index_view&&) noexcept;
  29. index_view& operator=(index_view&&) noexcept;
  30. ~index_view();
  31. ///
  32. /// @{
  33. ///
  34. /// Returns a cursor over all the indexes.
  35. ///
  36. cursor list();
  37. ///
  38. /// Returns a cursor over all the indexes.
  39. ///
  40. /// @param session
  41. /// The mongocxx::client_session with which to perform the list operation.
  42. ///
  43. cursor list(const client_session& session);
  44. ///
  45. /// @}
  46. ///
  47. ///
  48. /// @{
  49. ///
  50. /// Creates an index. A convenience method that calls create_many.
  51. ///
  52. /// @param keys
  53. /// A document containing the index keys and their corresponding index types.
  54. /// @param index_options
  55. /// A document containing set of options that controls the creation of the index. See
  56. /// https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/.
  57. /// @param options
  58. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  59. ///
  60. /// @return
  61. /// An optional containing the name of the created index. If and index with the same keys
  62. /// already exists, an empty optional is returned.
  63. ///
  64. /// @exception
  65. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  66. /// option is present and the operation exceeds the time limit.
  67. ///
  68. /// @see https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/
  69. ///
  70. stdx::optional<std::string> create_one(
  71. const bsoncxx::document::view_or_value& keys,
  72. const bsoncxx::document::view_or_value& index_options = {},
  73. const options::index_view& options = options::index_view{});
  74. ///
  75. /// Creates an index. A convenience method that calls create_many.
  76. ///
  77. /// @param session
  78. /// The mongocxx::client_session with which to perform the operation.
  79. /// @param keys
  80. /// A document containing the index keys and their corresponding index types.
  81. /// @param index_options
  82. /// A document containing set of options that controls the creation of the index. See
  83. /// https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/.
  84. /// @param options
  85. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  86. ///
  87. /// @return
  88. /// An optional containing the name of the created index. If and index with the same keys
  89. /// already exists, an empty optional is returned.
  90. ///
  91. /// @exception
  92. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  93. /// option is present and the operation exceeds the time limit.
  94. ///
  95. /// @see https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/
  96. ///
  97. stdx::optional<std::string> create_one(
  98. const client_session& session,
  99. const bsoncxx::document::view_or_value& keys,
  100. const bsoncxx::document::view_or_value& index_options = {},
  101. const options::index_view& options = options::index_view{});
  102. ///
  103. /// @}
  104. ///
  105. ///
  106. /// @{
  107. ///
  108. /// Creates an index. A convenience method that calls create_many.
  109. ///
  110. /// @param index
  111. /// Index_model describing the index being created.
  112. /// @param options
  113. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  114. ///
  115. /// @return
  116. /// An optional containing the name of the created index. If and index with the same keys
  117. /// already exists, an empty optional is returned.
  118. ///
  119. /// @exception
  120. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  121. /// option is present and the operation exceeds the time limit.
  122. ///
  123. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  124. ///
  125. stdx::optional<std::string> create_one(
  126. const index_model& index, const options::index_view& options = options::index_view{});
  127. ///
  128. /// Creates an index. A convenience method that calls create_many.
  129. ///
  130. /// @param session
  131. /// The mongocxx::client_session with which to perform the operation.
  132. /// @param index
  133. /// Index_model describing the index being created.
  134. /// @param options
  135. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  136. ///
  137. /// @return
  138. /// An optional containing the name of the created index. If and index with the same keys
  139. /// already exists, an empty optional is returned.
  140. ///
  141. /// @exception
  142. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  143. /// option is present and the operation exceeds the time limit.
  144. ///
  145. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  146. ///
  147. stdx::optional<std::string> create_one(
  148. const client_session& session,
  149. const index_model& index,
  150. const options::index_view& options = options::index_view{});
  151. ///
  152. /// @}
  153. ///
  154. ///
  155. /// @{
  156. ///
  157. /// Adds a container of indexes to the collection.
  158. ///
  159. /// @param indexes
  160. /// std::vector containing index models describing the indexes being created.
  161. /// @param options
  162. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  163. ///
  164. /// @return
  165. /// The result document sent back by the server as if the createIndexes command was run from
  166. /// the shell.
  167. ///
  168. /// @exception
  169. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  170. /// option is present and the operation exceeds the time limit.
  171. ///
  172. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  173. ///
  174. bsoncxx::document::value create_many(
  175. const std::vector<index_model>& indexes,
  176. const options::index_view& options = options::index_view{});
  177. ///
  178. /// Adds a container of indexes to the collection.
  179. ///
  180. /// @param session
  181. /// The mongocxx::client_session with which to perform the operation.
  182. /// @param indexes
  183. /// std::vector containing index models describing the indexes being created.
  184. /// @param options
  185. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  186. ///
  187. /// @return
  188. /// The result document sent back by the server as if the createIndexes command was run from
  189. /// the shell.
  190. ///
  191. /// @exception
  192. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  193. /// option is present and the operation exceeds the time limit.
  194. ///
  195. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  196. ///
  197. bsoncxx::document::value create_many(
  198. const client_session& session,
  199. const std::vector<index_model>& indexes,
  200. const options::index_view& options = options::index_view{});
  201. ///
  202. /// @}
  203. ///
  204. ///
  205. /// @{
  206. ///
  207. /// Drops a single index by name.
  208. ///
  209. /// @param name
  210. /// The name of the index being dropped.
  211. /// @param options
  212. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  213. ///
  214. /// @exception
  215. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  216. /// option is present and the operation exceeds the time limit.
  217. /// @exception
  218. /// Throws logic_error if "*" is passed in for the index name.
  219. ///
  220. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  221. ///
  222. void drop_one(stdx::string_view name,
  223. const options::index_view& options = options::index_view{});
  224. ///
  225. /// Drops a single index by name.
  226. ///
  227. /// @param session
  228. /// The mongocxx::client_session with which to perform the drop.
  229. /// @param name
  230. /// The name of the index being dropped.
  231. /// @param options
  232. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  233. ///
  234. /// @exception
  235. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  236. /// option is present and the operation exceeds the time limit.
  237. /// @exception
  238. /// Throws logic_error if "*" is passed in for the index name.
  239. ///
  240. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  241. ///
  242. void drop_one(const client_session& session,
  243. stdx::string_view name,
  244. const options::index_view& options = options::index_view{});
  245. ///
  246. /// @}
  247. ///
  248. ///
  249. /// @{
  250. ///
  251. /// Attempts to drop a single index from the collection given the keys and options.
  252. ///
  253. /// @param keys
  254. /// A document containing the index keys and their corresponding index types. If no name
  255. /// option is present in the options, a name based on the keys will be used.
  256. /// @param index_options (optional)
  257. /// A document containing set of options used to create the index. Only the name field will
  258. /// be used from here, and if it is not included, a name based on they keys will be used.
  259. /// @param options
  260. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  261. ///
  262. /// @exception
  263. /// Throws bsoncxx::exception if "name" key is present in options but is not a string.
  264. /// @exception
  265. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  266. /// option is present and the operation exceeds the time limit.
  267. /// @exception
  268. /// Throws logic_error if "*" is passed in for the index name
  269. ///
  270. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  271. ///
  272. void drop_one(const bsoncxx::document::view_or_value& keys,
  273. const bsoncxx::document::view_or_value& index_options = {},
  274. const options::index_view& options = options::index_view{});
  275. ///
  276. /// Attempts to drop a single index from the collection given the keys and options.
  277. ///
  278. /// @param session
  279. /// The mongocxx::client_session with which to perform the drop.
  280. /// @param keys
  281. /// A document containing the index keys and their corresponding index types. If no name
  282. /// option is present in the options, a name based on the keys will be used.
  283. /// @param index_options (optional)
  284. /// A document containing set of options used to create the index. Only the name field will
  285. /// be used from here, and if it is not included, a name based on they keys will be used.
  286. /// @param options
  287. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  288. ///
  289. /// @exception
  290. /// Throws bsoncxx::exception if "name" key is present in options but is not a string.
  291. /// @exception
  292. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  293. /// option is present and the operation exceeds the time limit.
  294. /// @exception
  295. /// Throws logic_error if "*" is passed in for the index name
  296. ///
  297. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  298. ///
  299. void drop_one(const client_session& session,
  300. const bsoncxx::document::view_or_value& keys,
  301. const bsoncxx::document::view_or_value& index_options = {},
  302. const options::index_view& options = options::index_view{});
  303. ///
  304. /// @}
  305. ///
  306. ///
  307. /// @{
  308. ///
  309. /// Attempts to drop a single index from the collection given an index model.
  310. ///
  311. /// @param index
  312. /// An index model describing the index being dropped.
  313. /// @param options
  314. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  315. ///
  316. /// @exception
  317. /// Throws bsoncxx::exception if "name" key is present in options but is not a string.
  318. /// @exception
  319. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  320. /// option is present and the operation exceeds the time limit.
  321. /// @exception
  322. /// Throws logic_error if "*" is passed in for the index name
  323. ///
  324. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  325. ///
  326. void drop_one(const index_model& index,
  327. const options::index_view& options = options::index_view{});
  328. ///
  329. /// Attempts to drop a single index from the collection given an index model.
  330. ///
  331. /// @param session
  332. /// The mongocxx::client_session with which to perform the drop.
  333. /// @param index
  334. /// An index model describing the index being dropped.
  335. /// @param options
  336. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  337. ///
  338. /// @exception
  339. /// Throws bsoncxx::exception if "name" key is present in options but is not a string.
  340. /// @exception
  341. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  342. /// option is present and the operation exceeds the time limit.
  343. /// @exception
  344. /// Throws logic_error if "*" is passed in for the index name
  345. ///
  346. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  347. ///
  348. void drop_one(const client_session& session,
  349. const index_model& index,
  350. const options::index_view& options = options::index_view{});
  351. ///
  352. /// @}
  353. ///
  354. ///
  355. /// @{
  356. ///
  357. /// Drops all indexes in the collection.
  358. ///
  359. /// @param options
  360. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  361. ///
  362. /// @exception
  363. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  364. /// option is present and the operation exceeds the time limit.
  365. ///
  366. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  367. ///
  368. void drop_all(const options::index_view& options = options::index_view{});
  369. ///
  370. /// Drops all indexes in the collection.
  371. ///
  372. /// @param session
  373. /// The mongocxx::client_session with which to perform the drop.
  374. /// @param options
  375. /// Optional arguments for the overall operation, see mongocxx::options::index_view.
  376. ///
  377. /// @exception
  378. /// Throws operation_exception for any errors encountered by the server or if max_time_ms
  379. /// option is present and the operation exceeds the time limit.
  380. ///
  381. /// @see https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/
  382. ///
  383. void drop_all(const client_session& session,
  384. const options::index_view& options = options::index_view{});
  385. ///
  386. /// @}
  387. ///
  388. private:
  389. friend class collection;
  390. class MONGOCXX_PRIVATE impl;
  391. MONGOCXX_PRIVATE index_view(void* coll, void* client);
  392. MONGOCXX_PRIVATE impl& _get_impl();
  393. private:
  394. std::unique_ptr<impl> _impl;
  395. };
  396. MONGOCXX_INLINE_NAMESPACE_END
  397. } // namespace mongocxx
  398. #include <mongocxx/config/postlude.hpp>