userver: userver/storages/mongo/pool.hpp Source File
Loading...
Searching...
No Matches
pool.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/pool.hpp
4/// @brief @copybrief storages::mongo::Pool
5
6#include <memory>
7#include <string>
8#include <vector>
9
10#include <userver/clients/dns/resolver_fwd.hpp>
11#include <userver/dynamic_config/fwd.hpp>
12#include <userver/formats/bson/value.hpp>
13#include <userver/storages/mongo/collection.hpp>
14#include <userver/storages/mongo/cursor.hpp>
15#include <userver/storages/mongo/operations.hpp>
16#include <userver/storages/mongo/pool_config.hpp>
17#include <userver/storages/mongo/transaction.hpp>
18#include <userver/utils/statistics/fwd.hpp>
19#include <userver/utils/zstring_view.hpp>
20
21USERVER_NAMESPACE_BEGIN
22
23namespace storages::mongo {
24
25namespace impl {
26class PoolImpl;
27} // namespace impl
28
29/// @ingroup userver_clients
30///
31/// @brief MongoDB client pool.
32///
33/// Use constructor only for tests, in production the pool should be retrieved
34/// from @ref userver_components "the components" via
35/// components::Mongo::GetPool() or components::MultiMongo::GetPool().
36///
37/// ## Example usage:
38///
39/// @snippet mongo/src/storages/mongo/collection_mongotest.hpp Sample Mongo usage
40class Pool {
41public:
42 Pool(Pool&&) noexcept;
43 Pool& operator=(Pool&&) noexcept;
44 ~Pool();
45
46 /// Checks whether a collection exists
47 bool HasCollection(utils::zstring_view name) const;
48
49 /// Returns a handle for the specified collection
50 Collection GetCollection(std::string name) const;
51
52 /// Drops the associated database if it exists. New modifications of
53 /// collections will attempt to re-create the database automatically.
55
56 /// Get a list of all the collection names in the associated database
57 std::vector<std::string> ListCollectionNames() const;
58
59 /// @throws storages::mongo::MongoException if failed to connect to the mongo server.
60 void Ping();
61
62 /// @brief Begin a new transaction.
63 ///
64 /// @return Transaction handle for executing operations within transaction context
65 /// @throws MongoException if transaction cannot be started
67
68 /// @brief Executes an aggregation pipeline on the database, without a collection
69 /// @param pipeline an array of aggregation operations
70 /// @param options see @ref storages::mongo::options
71 ///
72 /// Corresponds to MongoDB `db.aggregate([...])`. Use this for pipelines that
73 /// cannot run on a collection, for example when the first stage is `$documents`.
74 ///
75 /// On sharded clusters MongoDB may reject `$documents` together with `$lookup`:
76 /// `$documents` must run on mongos, while `$lookup` must run on a shard.
77 /// @see Collection::Aggregate
78 /// @snippet storages/mongo/pool_mongotest.cpp Sample Mongo database aggregate
79 template <typename... Options>
80 Cursor Aggregate(formats::bson::Value pipeline, Options&&... options);
81
82 /// @name Prepared operation executors
83 /// @{
84 Cursor Execute(const operations::Aggregate&);
85 /// @}
86
87 /// @cond
88 // For internal use only
89 Pool(
90 std::string id,
91 const std::string& uri,
92 const PoolConfig& pool_config,
93 clients::dns::Resolver* dns_resolver,
94 dynamic_config::Source config_source
95 );
96
97 // Writes pool statistics
98 friend void DumpMetric(utils::statistics::Writer& writer, const Pool& pool);
99
100 friend std::shared_ptr<impl::PoolImpl> GetPoolImpl(const Pool& pool) { return pool.impl_; }
101
102 // Sets new dynamic pool settings
103 void SetPoolSettings(const PoolSettings& pool_settings);
104
105 void SetConnectionString(const std::string& connection_string);
106 /// @endcond
107
108private:
109 std::shared_ptr<impl::PoolImpl> impl_;
110};
111
112using PoolPtr = std::shared_ptr<Pool>;
113
114template <typename... Options>
115Cursor Pool::Aggregate(formats::bson::Value pipeline, Options&&... options) {
116 operations::Aggregate aggregate(std::move(pipeline));
117 (aggregate.SetOption(std::forward<Options>(options)), ...);
118 return Execute(aggregate);
119}
120
121} // namespace storages::mongo
122
123USERVER_NAMESPACE_END