|
| 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 | + |
| 15 | +#include <chrono> |
| 16 | +#include <iostream> |
| 17 | +#include <thread> |
| 18 | + |
| 19 | +#include <bsoncxx/builder/basic/document.hpp> |
| 20 | +#include <bsoncxx/json.hpp> |
| 21 | + |
| 22 | +#include <mongocxx/client.hpp> |
| 23 | +#include <mongocxx/collection.hpp> |
| 24 | +#include <mongocxx/database.hpp> |
| 25 | +#include <mongocxx/instance.hpp> |
| 26 | +#include <mongocxx/options/create_collection.hpp> |
| 27 | +#include <mongocxx/options/find.hpp> |
| 28 | +#include <mongocxx/uri.hpp> |
| 29 | + |
| 30 | +using bsoncxx::builder::basic::document; |
| 31 | +using bsoncxx::builder::basic::kvp; |
| 32 | + |
| 33 | +// |
| 34 | +// Document number counter for sample inserted documents. This just |
| 35 | +// makes the tailed document more obviously in sequence. |
| 36 | +// |
| 37 | + |
| 38 | +static std::int32_t counter = 0; |
| 39 | + |
| 40 | +// |
| 41 | +// Drop and recreate capped collection. Inserts a doc as tailing an empty |
| 42 | +// collection returns a closed cursor. |
| 43 | +// |
| 44 | +// TODO CDRIVER-2093: After CDRIVER-2093 is fixed, we can provide better |
| 45 | +// diagnostics as to whether the cursor is alive or closed and won't need |
| 46 | +// to prime the collection with a document. |
| 47 | +// |
| 48 | +void init_capped_collection(mongocxx::client* conn, std::string name) { |
| 49 | + auto db = (*conn)["test"]; |
| 50 | + auto coll = db[name]; |
| 51 | + |
| 52 | + coll.drop(); |
| 53 | + auto create_opts = mongocxx::options::create_collection{}.capped(true).size(1024 * 1024); |
| 54 | + db.create_collection(name, create_opts); |
| 55 | + |
| 56 | + document builder{}; |
| 57 | + builder.append(kvp("n", counter++)); |
| 58 | + db[name].insert_one(builder.extract()); |
| 59 | +} |
| 60 | + |
| 61 | +// |
| 62 | +// Insert 5 documents so there are more documents to tail. |
| 63 | +// |
| 64 | +void insert_docs(mongocxx::collection* coll) { |
| 65 | + std::cout << "Inserting batch... " << std::endl; |
| 66 | + for (int j = 0; j < 5; j++) { |
| 67 | + document builder{}; |
| 68 | + builder.append(kvp("n", counter++)); |
| 69 | + coll->insert_one(builder.extract()); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +int main(int, char**) { |
| 74 | + // The mongocxx::instance constructor and destructor initialize and shut down the driver, |
| 75 | + // respectively. Therefore, a mongocxx::instance must be created before using the driver and |
| 76 | + // must remain alive for as long as the driver is in use. |
| 77 | + mongocxx::instance inst{}; |
| 78 | + mongocxx::client conn{mongocxx::uri{}}; |
| 79 | + std::string name{"capped_coll"}; |
| 80 | + |
| 81 | + // Create the capped collection. |
| 82 | + init_capped_collection(&conn, name); |
| 83 | + |
| 84 | + // Construct a tailable cursor. |
| 85 | + auto coll = conn["test"][name]; |
| 86 | + mongocxx::options::find opts{}; |
| 87 | + opts.cursor_type(mongocxx::cursor::type::k_tailable); |
| 88 | + auto cursor = coll.find({}, opts); |
| 89 | + |
| 90 | + // Loop "forever", or in this case, until we find >= 25 documents. |
| 91 | + std::cout << "Tailing the collection..." << std::endl; |
| 92 | + int docs_found = 0; |
| 93 | + for (;;) { |
| 94 | + // Loop over the cursor until no more documents are available. |
| 95 | + // On the next iteration of the outer loop, if more documents |
| 96 | + // are available, the tailable cursor will return them. |
| 97 | + for (auto&& doc : cursor) { |
| 98 | + std::cout << bsoncxx::to_json(doc) << std::endl; |
| 99 | + docs_found++; |
| 100 | + } |
| 101 | + |
| 102 | + if (docs_found >= 25) { |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + // No documents are available, so sleep a bit before trying again. |
| 107 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 108 | + |
| 109 | + // For the sake of this example, add more documents before the next loop. |
| 110 | + insert_docs(&coll); |
| 111 | + } |
| 112 | + |
| 113 | + return EXIT_SUCCESS; |
| 114 | +} |
0 commit comments