RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/logger/kvp_logger_mongodb.cc
00001 #include "kvp_logger_mongodb.h"
00002 #include "mongo/bson/bson.h"
00003 #include "mongo/util/net/hostandport.h"
00004 
00005 namespace Datacratic{
00006 
00007 using namespace std;
00008 
00009 KvpLoggerMongoDb::KvpLoggerMongoDb(const KvpLoggerParams& params) :
00010     params(params)
00011 {
00012     function<void()> init = [&] (){
00013         mongo::HostAndPort hostAndPort(params.hostAndPort);
00014         conn.connect(hostAndPort);
00015         string err;
00016         if(!conn.auth(params.db, params.user, params.pwd, err)){
00017             throw ML::Exception("MongoDB connection failed with msg [" 
00018                 + err + "]");
00019         }
00020     };
00021     doIt(init);
00022 }
00023 
00024 void KvpLoggerMongoDb
00025 ::log(const map<string, string>& data, const string& coll){
00026     function<void()>  _log = [&](){
00027         map<string, string>::const_iterator it;
00028         mongo::BSONObjBuilder b;
00029         for(it = data.begin(); it != data.end(); it ++){
00030             b.append((*it).first, (*it).second);
00031         }
00032         mongo::BSONObj p = b.obj();
00033         conn.insert(params.db + "." + coll, p);
00034     };
00035     doIt(_log);
00036 }
00037 
00038 void KvpLoggerMongoDb::doIt(function<void()>& fct){
00039     if(params.failSafe){
00040         try{
00041             fct();
00042         }catch(const exception& e){
00043             cerr << e.what() << endl;
00044         }
00045     }else{
00046         fct();
00047     }
00048 }
00049 
00050 }//namespace Datacratic
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator