index.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // Copyright 2015 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 <chrono>
  16. #include <memory>
  17. #include <bsoncxx/document/view.hpp>
  18. #include <bsoncxx/stdx/optional.hpp>
  19. #include <bsoncxx/string/view_or_value.hpp>
  20. #include <mongocxx/stdx.hpp>
  21. #include <bsoncxx/document/value.hpp>
  22. #include <bsoncxx/document/view_or_value.hpp>
  23. #include <mongocxx/config/prelude.hpp>
  24. namespace mongocxx {
  25. MONGOCXX_INLINE_NAMESPACE_BEGIN
  26. class collection;
  27. namespace options {
  28. ///
  29. /// Class representing the optional arguments to a MongoDB create index operation.
  30. ///
  31. /// @see https://docs.mongodb.com/master/reference/command/createIndexes
  32. ///
  33. class MONGOCXX_API index {
  34. public:
  35. ///
  36. /// Base class representing the optional storage engine options for indexes.
  37. ///
  38. class MONGOCXX_API base_storage_options {
  39. public:
  40. virtual ~base_storage_options();
  41. private:
  42. friend class options::index;
  43. MONGOCXX_PRIVATE virtual int type() const = 0;
  44. };
  45. ///
  46. /// Class representing the optional WiredTiger storage engine options for indexes.
  47. ///
  48. class MONGOCXX_API wiredtiger_storage_options final : public base_storage_options {
  49. public:
  50. ~wiredtiger_storage_options() override;
  51. ///
  52. /// Set the WiredTiger configuration string.
  53. ///
  54. /// @param config_string
  55. /// The WiredTiger configuration string.
  56. ///
  57. void config_string(bsoncxx::string::view_or_value config_string);
  58. ///
  59. /// The current config_string setting.
  60. ///
  61. /// @return The current config_string.
  62. ///
  63. const stdx::optional<bsoncxx::string::view_or_value>& config_string() const;
  64. private:
  65. friend collection;
  66. MONGOCXX_PRIVATE int type() const override;
  67. stdx::optional<bsoncxx::string::view_or_value> _config_string;
  68. };
  69. index();
  70. ///
  71. /// Whether or not to build the index in the background so that building the index does not
  72. /// block other database activities. The default is to build indexes in the foreground
  73. ///
  74. /// @param background
  75. /// Whether or not to build the index in the background.
  76. ///
  77. /// @return
  78. /// A reference to the object on which this member function is being called. This facilitates
  79. /// method chaining.
  80. ///
  81. /// @see https://docs.mongodb.com/master/tutorial/build-indexes-in-the-background/
  82. ///
  83. index& background(bool background);
  84. ///
  85. /// The current background setting.
  86. ///
  87. /// @return The current background.
  88. ///
  89. const stdx::optional<bool>& background() const;
  90. ///
  91. /// Whether or not to create a unique index so that the collection will not accept insertion of
  92. /// documents where the index key or keys match an existing value in the index.
  93. ///
  94. /// @param unique
  95. /// Whether or not to create a unique index.
  96. ///
  97. /// @return
  98. /// A reference to the object on which this member function is being called. This facilitates
  99. /// method chaining.
  100. ///
  101. /// @see https://docs.mongodb.com/master/core/index-unique/
  102. ///
  103. index& unique(bool unique);
  104. ///
  105. /// The current unique setting.
  106. ///
  107. /// @return The current unique.
  108. ///
  109. const stdx::optional<bool>& unique() const;
  110. ///
  111. /// Whether or not the index is hidden from the query planner. A hidden index is not evaluated
  112. /// as part of query plan selection.
  113. ///
  114. /// @param hidden
  115. /// Whether or not to create a hidden index.
  116. ///
  117. /// @return
  118. /// A reference to the object on which this member function is being called. This facilitates
  119. /// method chaining.
  120. ///
  121. /// @see https://docs.mongodb.com/master/core/index-hidden/
  122. ///
  123. index& hidden(bool hidden);
  124. ///
  125. /// The current hidden setting.
  126. ///
  127. /// @return The current hidden.
  128. ///
  129. const stdx::optional<bool>& hidden() const;
  130. ///
  131. /// The name of the index.
  132. ///
  133. /// @param name
  134. /// The name of the index.
  135. ///
  136. /// @return
  137. /// A reference to the object on which this member function is being called. This facilitates
  138. /// method chaining.
  139. ///
  140. index& name(bsoncxx::string::view_or_value name);
  141. ///
  142. /// The current name setting.
  143. ///
  144. /// @return The current name.
  145. ///
  146. const stdx::optional<bsoncxx::string::view_or_value>& name() const;
  147. ///
  148. /// Sets the collation for this index.
  149. ///
  150. /// @param collation
  151. /// The new collation.
  152. ///
  153. /// @return
  154. /// A reference to the object on which this member function is being called. This facilitates
  155. /// method chaining.
  156. ///
  157. /// @see
  158. /// https://docs.mongodb.com/master/reference/collation/
  159. ///
  160. index& collation(bsoncxx::document::view collation);
  161. ///
  162. /// Retrieves the current collation for this index.
  163. ///
  164. /// @return
  165. /// The current collation.
  166. ///
  167. /// @see
  168. /// https://docs.mongodb.com/master/reference/collation/
  169. ///
  170. const stdx::optional<bsoncxx::document::view>& collation() const;
  171. ///
  172. /// Whether or not to create a sparse index. Sparse indexes only reference documents with the
  173. /// indexed fields.
  174. ///
  175. /// @param sparse
  176. /// Whether or not to create a sparse index.
  177. ///
  178. /// @return
  179. /// A reference to the object on which this member function is being called. This facilitates
  180. /// method chaining.
  181. ///
  182. /// @see https://docs.mongodb.com/master/core/index-sparse/
  183. ///
  184. index& sparse(bool sparse);
  185. ///
  186. /// The current sparse setting.
  187. ///
  188. /// @return The current sparse setting.
  189. ///
  190. const stdx::optional<bool>& sparse() const;
  191. ///
  192. /// Optionally used only in MongoDB 3.0.0 and higher. Specifies the storage engine options for
  193. /// the index.
  194. ///
  195. /// @param storage_options
  196. /// The storage engine options for the index.
  197. ///
  198. /// @return
  199. /// A reference to the object on which this member function is being called. This facilitates
  200. /// method chaining.
  201. ///
  202. index& storage_options(std::unique_ptr<base_storage_options> storage_options);
  203. ///
  204. /// Optionally used only in MongoDB 3.0.0 and higher. Specifies the WiredTiger-specific storage
  205. /// engine options for the index.
  206. ///
  207. /// @param storage_options
  208. /// The storage engine options for the index.
  209. ///
  210. index& storage_options(std::unique_ptr<wiredtiger_storage_options> storage_options);
  211. ///
  212. /// Set a value, in seconds, as a TTL to control how long MongoDB retains documents in this
  213. /// collection.
  214. ///
  215. /// @param seconds
  216. /// The amount of time, in seconds, to retain documents.
  217. ///
  218. /// @return
  219. /// A reference to the object on which this member function is being called. This facilitates
  220. /// method chaining.
  221. ///
  222. /// @see https://docs.mongodb.com/master/core/index-ttl/
  223. ///
  224. index& expire_after(std::chrono::seconds seconds);
  225. ///
  226. /// The current expire_after setting.
  227. ///
  228. /// @return The current expire_after value.
  229. ///
  230. const stdx::optional<std::chrono::seconds>& expire_after() const;
  231. ///
  232. /// Sets the index version.
  233. ///
  234. /// @param v
  235. /// The index version.
  236. ///
  237. /// @return
  238. /// A reference to the object on which this member function is being called. This facilitates
  239. /// method chaining.
  240. ///
  241. index& version(std::int32_t v);
  242. ///
  243. /// The current index version.
  244. ///
  245. /// @return The current index version.
  246. ///
  247. const stdx::optional<std::int32_t>& version() const;
  248. ///
  249. /// For text indexes, sets the weight document. The weight document contains field and weight
  250. /// pairs.
  251. ///
  252. /// @param weights
  253. /// The weight document for text indexes.
  254. ///
  255. /// @return
  256. /// A reference to the object on which this member function is being called. This facilitates
  257. /// method chaining.
  258. ///
  259. index& weights(bsoncxx::document::view weights);
  260. ///
  261. /// The current weights setting.
  262. ///
  263. /// @return The current weights.
  264. ///
  265. const stdx::optional<bsoncxx::document::view>& weights() const;
  266. ///
  267. /// For text indexes, the language that determines the list of stop words and the rules for the
  268. /// stemmer and tokenizer.
  269. ///
  270. /// @param default_language
  271. /// The default language used when creating text indexes.
  272. ///
  273. /// @return
  274. /// A reference to the object on which this member function is being called. This facilitates
  275. /// method chaining.
  276. ///
  277. index& default_language(bsoncxx::string::view_or_value default_language);
  278. ///
  279. /// The current default_language setting.
  280. ///
  281. /// @return The current default_language.
  282. ///
  283. const stdx::optional<bsoncxx::string::view_or_value>& default_language() const;
  284. ///
  285. /// For text indexes, the name of the field, in the collection’s documents, that contains the
  286. /// override language for the document.
  287. ///
  288. /// @param language_override
  289. /// The name of the field that contains the override language for text indexes.
  290. ///
  291. /// @return
  292. /// A reference to the object on which this member function is being called. This facilitates
  293. /// method chaining.
  294. ///
  295. index& language_override(bsoncxx::string::view_or_value language_override);
  296. ///
  297. /// The current name of the field that contains the override language for text indexes.
  298. ///
  299. /// @return The name of the field that contains the override language for text indexes.
  300. ///
  301. const stdx::optional<bsoncxx::string::view_or_value>& language_override() const;
  302. ///
  303. /// Sets the document for the partial filter expression for partial indexes.
  304. ///
  305. /// @param partial_filter_expression
  306. /// The partial filter expression document.
  307. ///
  308. /// @return
  309. /// A reference to the object on which this member function is being called. This facilitates
  310. /// method chaining.
  311. ///
  312. index& partial_filter_expression(bsoncxx::document::view partial_filter_expression);
  313. ///
  314. /// The current partial_filter_expression setting.
  315. ///
  316. /// @return The current partial_filter_expression.
  317. ///
  318. const stdx::optional<bsoncxx::document::view>& partial_filter_expression() const;
  319. ///
  320. /// For 2dsphere indexes, the 2dsphere index version number. Version can be either 1 or 2.
  321. ///
  322. /// @param twod_sphere_version
  323. /// The 2dsphere index version number.
  324. ///
  325. /// @return
  326. /// A reference to the object on which this member function is being called. This facilitates
  327. /// method chaining.
  328. ///
  329. index& twod_sphere_version(std::uint8_t twod_sphere_version);
  330. ///
  331. /// The current twod_sphere_version setting.
  332. ///
  333. /// @return The current twod_sphere_version.
  334. ///
  335. const stdx::optional<std::uint8_t>& twod_sphere_version() const;
  336. ///
  337. /// For 2d indexes, the precision of the stored geohash value of the location data.
  338. ///
  339. /// @param twod_bits_precision
  340. /// The precision of the stored geohash value.
  341. ///
  342. /// @return
  343. /// A reference to the object on which this member function is being called. This facilitates
  344. /// method chaining.
  345. ///
  346. index& twod_bits_precision(std::uint8_t twod_bits_precision);
  347. ///
  348. /// The current precision of the stored geohash value of the location data.
  349. ///
  350. /// @return The precision of the stored geohash value of the location data.
  351. ///
  352. const stdx::optional<std::uint8_t>& twod_bits_precision() const;
  353. ///
  354. /// For 2d indexes, the lower inclusive boundary for the longitude and latitude values.
  355. ///
  356. /// @param twod_location_min
  357. /// The lower inclusive boundary.
  358. ///
  359. /// @return
  360. /// A reference to the object on which this member function is being called. This facilitates
  361. /// method chaining.
  362. ///
  363. index& twod_location_min(double twod_location_min);
  364. ///
  365. /// The current lower inclusive boundary for the longitude and latitude values.
  366. ///
  367. /// @return The lower inclusive boundary for the longitude and latitude values.
  368. ///
  369. const stdx::optional<double>& twod_location_min() const;
  370. ///
  371. /// For 2d indexes, the upper inclusive boundary for the longitude and latitude values.
  372. ///
  373. /// @param twod_location_max
  374. /// The upper inclusive boundary.
  375. ///
  376. /// @return
  377. /// A reference to the object on which this member function is being called. This facilitates
  378. /// method chaining.
  379. ///
  380. index& twod_location_max(double twod_location_max);
  381. ///
  382. /// The current upper inclusive boundary for the longitude and latitude values.
  383. ///
  384. /// @return The upper inclusive boundary for the longitude and latitude values.
  385. ///
  386. const stdx::optional<double>& twod_location_max() const;
  387. ///
  388. /// For geoHaystack indexes, specify the number of units within which to group the location
  389. /// values; i.e. group in the same bucket those location values that are within the specified
  390. /// number of units to each other.
  391. ///
  392. /// @see https://docs.mongodb.com/master/core/geohaystack/
  393. ///
  394. /// @param haystack_bucket_size
  395. /// The geoHaystack bucket size.
  396. ///
  397. /// @deprecated
  398. /// This option is deprecated.
  399. ///
  400. /// @return
  401. /// A reference to the object on which this member function is being called. This facilitates
  402. /// method chaining.
  403. ///
  404. MONGOCXX_DEPRECATED index& haystack_bucket_size(double haystack_bucket_size);
  405. index& haystack_bucket_size_deprecated(double haystack_bucket_size);
  406. ///
  407. /// The current haystack_bucket_size setting.
  408. ///
  409. /// @return The current haystack_bucket_size.
  410. ///
  411. /// @deprecated
  412. /// This method is deprecated.
  413. ///
  414. MONGOCXX_DEPRECATED const stdx::optional<double>& haystack_bucket_size() const;
  415. const stdx::optional<double>& haystack_bucket_size_deprecated() const;
  416. ///
  417. /// Conversion operator that provides a view of the options in document form.
  418. ///
  419. /// @exception mongocxx::logic_error if an invalid expireAfterSeconds field is provided.
  420. ///
  421. /// @return A view of the current builder contents.
  422. ///
  423. operator bsoncxx::document::view_or_value();
  424. private:
  425. friend collection;
  426. stdx::optional<bool> _background;
  427. stdx::optional<bool> _unique;
  428. stdx::optional<bool> _hidden;
  429. stdx::optional<bsoncxx::string::view_or_value> _name;
  430. stdx::optional<bsoncxx::document::view> _collation;
  431. stdx::optional<bool> _sparse;
  432. std::unique_ptr<base_storage_options> _storage_options;
  433. stdx::optional<std::chrono::seconds> _expire_after;
  434. stdx::optional<std::int32_t> _version;
  435. stdx::optional<bsoncxx::document::view> _weights;
  436. stdx::optional<bsoncxx::string::view_or_value> _default_language;
  437. stdx::optional<bsoncxx::string::view_or_value> _language_override;
  438. stdx::optional<bsoncxx::document::view> _partial_filter_expression;
  439. stdx::optional<std::uint8_t> _twod_sphere_version;
  440. stdx::optional<std::uint8_t> _twod_bits_precision;
  441. stdx::optional<double> _twod_location_min;
  442. stdx::optional<double> _twod_location_max;
  443. stdx::optional<double> _haystack_bucket_size;
  444. //
  445. // Return the current storage_options setting.
  446. //
  447. const std::unique_ptr<base_storage_options>& storage_options() const;
  448. };
  449. } // namespace options
  450. MONGOCXX_INLINE_NAMESPACE_END
  451. } // namespace mongocxx
  452. #include <mongocxx/config/postlude.hpp>