1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
// Copyright 2009-present MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <chrono>
#include <iostream>
#include <thread>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/collection.hpp>
#include <mongocxx/database.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/options/find.hpp>
#include <mongocxx/uri.hpp>
#include <examples/macros.hh>
using bsoncxx::builder::basic::document;
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
namespace {
//
// Document number counter for sample inserted documents. This just
// makes the tailed document more obviously in sequence.
//
static std::int32_t counter = 0;
//
// Drop and recreate capped collection. Inserts a doc as tailing an empty
// collection returns a closed cursor.
//
// TODO CDRIVER-2093: After CDRIVER-2093 is fixed, we can provide better
// diagnostics as to whether the cursor is alive or closed and won't need
// to prime the collection with a document.
//
void init_capped_collection(mongocxx::client* conn, std::string name) {
auto db = (*conn)["test"];
auto coll = db[name];
coll.drop();
db.create_collection(name, make_document(kvp("capped", true), kvp("size", 1024 * 1024)));
document builder{};
builder.append(kvp("n", counter++));
db[name].insert_one(builder.extract());
}
//
// Insert 5 documents so there are more documents to tail.
//
void insert_docs(mongocxx::collection* coll) {
std::cout << "Inserting batch... " << std::endl;
for (int j = 0; j < 5; j++) {
document builder{};
builder.append(kvp("n", counter++));
coll->insert_one(builder.extract());
}
}
} // namespace
int EXAMPLES_CDECL main() {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
// must remain alive for as long as the driver is in use.
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
std::string name{"capped_coll"};
// Create the capped collection.
init_capped_collection(&conn, name);
// Construct a tailable cursor.
auto coll = conn["test"][name];
mongocxx::options::find opts{};
opts.cursor_type(mongocxx::cursor::type::k_tailable);
auto cursor = coll.find({}, opts);
// Loop "forever", or in this case, until we find >= 25 documents.
std::cout << "Tailing the collection..." << std::endl;
int docs_found = 0;
for (;;) {
// Loop over the cursor until no more documents are available.
// On the next iteration of the outer loop, if more documents
// are available, the tailable cursor will return them.
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
docs_found++;
}
if (docs_found >= 25) {
break;
}
// No documents are available, so sleep a bit before trying again.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// For the sake of this example, add more documents before the next loop.
insert_docs(&coll);
}
return EXIT_SUCCESS;
}
|