[edit] Retrieve the event name from mongoDB event payload

This commit is contained in:
Newe 2021-05-25 12:41:28 +02:00
parent 7ddc2aaf39
commit 63407d96e3
3 changed files with 40 additions and 11 deletions

View File

@ -30,16 +30,18 @@ int main(int argc, char **argv){
auto mongoHandler = std::make_unique<mongoStub>();
mongocxx::options::change_stream options;
//voiceEvents collection watcher
mongocxx::change_stream colCs = mongoHandler->getCol().watch(options);
std::cout << "Server created and listening for events" << std::endl;
//Check for new messages in the collection
for (;;){
std::vector<std::string> t = mongoHandler->getNewMessages(&colCs);
std::vector<mongoStub::mongoMessage> t = mongoHandler->getNewMessages(&colCs);
for(auto &i : t){
std::cout << i << std::endl;
std::cout << "[" << i.eventName << "] " << std::endl;
}
}
std::cout << "Server created" << std::endl;
return 0;
}

View File

@ -17,10 +17,28 @@ mongoStub::mongoStub() {
}
}
std::vector<std::string>mongoStub::getNewMessages(mongocxx::change_stream* colCs) {
std::vector<std::string> retVec;
for (const auto& event : *colCs) {
retVec.push_back(bsoncxx::to_json(event));
//Too slow for my liking
std::vector<mongoStub::mongoMessage> mongoStub::getNewMessages(mongocxx::change_stream* colCs) {
std::vector<mongoStub::mongoMessage> retVec;
for (auto&& event : *colCs) {
mongoStub::mongoMessage returnValue;
/*if(event["fullDocument"]["data"]){
returnValue.data.push_back(event["fullDocument"]["data"].get_utf8().value.to_string());
}*/
std::cout << bsoncxx::to_json(event) << std::endl;
//Oly listen to insert events (to avoid "precondition failed: data" exception)
if(event["operationType"].get_utf8().value.to_string()!="insert"){
continue;
}
returnValue.eventName = event["fullDocument"]["event"].get_utf8().value.to_string();
retVec.push_back(returnValue);
}
return retVec;
}

View File

@ -7,23 +7,32 @@
#include <vector>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/v_noabi/mongocxx/change_stream.hpp>
#include <mongocxx/change_stream.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/document/element.hpp>
class mongoStub : boost::noncopyable {
class mongoStub{
public:
mongoStub();
std::vector<std::string> getNewMessages(mongocxx::change_stream* colCs);
struct mongoMessage{
std::string eventName;
std::vector<std::string> data;
};
std::vector<mongoMessage> getNewMessages(mongocxx::change_stream* colCs);
mongocxx::collection getCol() const { return col; }
private:
mongocxx::instance instance;
mongocxx::client client{mongocxx::uri{}};
mongocxx::database db;
mongocxx::collection col;
mongocxx::change_stream* colCs = nullptr;
mongocxx::change_stream* colCs = nullptr;
};
#endif