|
| 1 | +// Copyright 2016 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 | + |
| 15 | +#include <cstdlib> |
| 16 | +#include <memory> |
| 17 | + |
| 18 | +#include <bsoncxx/stdx/make_unique.hpp> |
| 19 | +#include <bsoncxx/stdx/optional.hpp> |
| 20 | +#include <bsoncxx/stdx/string_view.hpp> |
| 21 | + |
| 22 | +#include <mongocxx/instance.hpp> |
| 23 | +#include <mongocxx/logger.hpp> |
| 24 | +#include <mongocxx/pool.hpp> |
| 25 | +#include <mongocxx/stdx.hpp> |
| 26 | +#include <mongocxx/uri.hpp> |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +// This example demonstrates how one might keep a heap allocated mongocxx::instance and |
| 31 | +// mongocxx::pool object associated in a way that allows dynamic configuration. Most of the examples |
| 32 | +// simply create a stack allocated mongocxx::instance object, which is fine for small examples, but |
| 33 | +// real programs will typically require shared access to a commonly configured instance and |
| 34 | +// connection pool across different translation units and components. By providing a singleton which |
| 35 | +// owns the instance and pool objects, we can defer configuration until we are ready to use MongoDB, |
| 36 | +// and share the instance and pool objects between multiple functions. |
| 37 | + |
| 38 | +class mongo_access { |
| 39 | +public: |
| 40 | + |
| 41 | + static mongo_access& instance() { |
| 42 | + static mongo_access instance; |
| 43 | + return instance; |
| 44 | + } |
| 45 | + |
| 46 | + void configure(std::unique_ptr<mongocxx::instance> instance, |
| 47 | + std::unique_ptr<mongocxx::pool> pool) { |
| 48 | + _instance = std::move(instance); |
| 49 | + _pool = std::move(pool); |
| 50 | + } |
| 51 | + |
| 52 | + using connection = mongocxx::pool::entry; |
| 53 | + |
| 54 | + connection get_connection() { |
| 55 | + return _pool->acquire(); |
| 56 | + } |
| 57 | + |
| 58 | + mongocxx::stdx::optional<connection> try_get_connection() { |
| 59 | + return _pool->try_acquire(); |
| 60 | + } |
| 61 | + |
| 62 | +private: |
| 63 | + mongo_access() = default; |
| 64 | + |
| 65 | + std::unique_ptr<mongocxx::instance> _instance = nullptr; |
| 66 | + std::unique_ptr<mongocxx::pool> _pool = nullptr; |
| 67 | +}; |
| 68 | + |
| 69 | +} // namespace |
| 70 | + |
| 71 | +// The 'configure' and 'do_work' functions use the same mongocxx::instance and mongocxx::pool |
| 72 | +// objects by way of the mongo_access singleton. |
| 73 | + |
| 74 | +void configure(mongocxx::uri uri) { |
| 75 | + class noop_logger : public mongocxx::logger { |
| 76 | + public: |
| 77 | + virtual void operator()(mongocxx::log_level, mongocxx::stdx::string_view, |
| 78 | + mongocxx::stdx::string_view message) noexcept {} |
| 79 | + }; |
| 80 | + |
| 81 | + mongo_access::instance().configure( |
| 82 | + mongocxx::stdx::make_unique<mongocxx::instance>(mongocxx::stdx::make_unique<noop_logger>()), |
| 83 | + mongocxx::stdx::make_unique<mongocxx::pool>(std::move(uri)) |
| 84 | + ); |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +bool do_work() { |
| 89 | + auto connection = mongo_access::instance().get_connection(); |
| 90 | + if (!connection) |
| 91 | + return false; |
| 92 | + return true; |
| 93 | +} |
| 94 | + |
| 95 | +int main(int argc, char* argv[]) { |
| 96 | + auto uri = mongocxx::uri{(argc >= 2) ? argv[1] : mongocxx::uri::k_default_uri}; |
| 97 | + configure(std::move(uri)); |
| 98 | + return do_work() ? EXIT_SUCCESS : EXIT_FAILURE; |
| 99 | +} |
0 commit comments