[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>(); auto mongoHandler = std::make_unique<mongoStub>();
mongocxx::options::change_stream options; mongocxx::options::change_stream options;
//voiceEvents collection watcher
mongocxx::change_stream colCs = mongoHandler->getCol().watch(options); 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 //Check for new messages in the collection
for (;;){ for (;;){
std::vector<std::string> t = mongoHandler->getNewMessages(&colCs); std::vector<mongoStub::mongoMessage> t = mongoHandler->getNewMessages(&colCs);
for(auto &i : t){ for(auto &i : t){
std::cout << i << std::endl; std::cout << "[" << i.eventName << "] " << std::endl;
} }
} }
std::cout << "Server created" << std::endl;
return 0; return 0;
} }

View File

@ -17,10 +17,28 @@ mongoStub::mongoStub() {
} }
} }
std::vector<std::string>mongoStub::getNewMessages(mongocxx::change_stream* colCs) { //Too slow for my liking
std::vector<std::string> retVec; std::vector<mongoStub::mongoMessage> mongoStub::getNewMessages(mongocxx::change_stream* colCs) {
for (const auto& event : *colCs) { std::vector<mongoStub::mongoMessage> retVec;
retVec.push_back(bsoncxx::to_json(event));
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; return retVec;
} }

View File

@ -7,17 +7,26 @@
#include <vector> #include <vector>
#include <mongocxx/client.hpp> #include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp> #include <mongocxx/instance.hpp>
#include <mongocxx/v_noabi/mongocxx/change_stream.hpp> #include <mongocxx/change_stream.hpp>
#include <bsoncxx/json.hpp> #include <bsoncxx/json.hpp>
#include <bsoncxx/document/element.hpp>
class mongoStub : boost::noncopyable { class mongoStub{
public: public:
mongoStub(); 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; } mongocxx::collection getCol() const { return col; }
private: private:
mongocxx::instance instance; mongocxx::instance instance;
mongocxx::client client{mongocxx::uri{}}; mongocxx::client client{mongocxx::uri{}};