-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongowriter.cpp
More file actions
89 lines (67 loc) · 2.02 KB
/
mongowriter.cpp
File metadata and controls
89 lines (67 loc) · 2.02 KB
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
/*
* File: mongowriter.cpp
* Author: max
*
* Created on December 30, 2014, 5:03 PM
*/
#include <string>
#include <amqp_ssl_socket.h>
#include <amqp_framing.h>
#include "reader.hpp"
#include <mongo/bson/bson.h>
#include <mongo/client/dbclient.h>
#include "mongowriter.h"
#include <ctime>
mongowriter::mongowriter() : ready(false) {
// do nothing
}
bool mongowriter::init() {
std::string errmsg;
collectionID = (maincol.find_first_of('.') != std::string::npos) ? maincol : (db + "." + maincol);
dumpID = (dumpcol.find_first_of('.') != std::string::npos) ? dumpcol : (db + "." + dumpcol);
if (!c.connect(host, errmsg)) {
std::cerr << errmsg << std::endl;
return false;
} else {
if (!name.empty() && !c.auth(db, name, pass, errmsg, true)) {
std::cerr << errmsg << std::endl;
return false;
} else {
std::cout << "connected to mongo(" << host << ") -> " << db << std::endl;
return true;
}
}
}
void mongowriter::accept(char *message, int len) {
time_t now = time(0);
struct tm tstruct;
char buf[80];
int lenParced = 0;
tstruct = *localtime(&now);
strftime(buf, sizeof (buf), "%Y%m%d%H%M%S", &tstruct);
std::string errJSON;
try {
mongo::BSONObj obj;
obj = mongo::fromjson(message, &lenParced);
if (!obj.hasField("ts") && !obj.hasElement("ts")) {
mongo::BSONObjBuilder b;
b.appendElements(obj);
b.append("ts", buf);
obj = b.obj();
}
c.insert(collectionID, obj);
return;
} catch (mongo::MsgAssertionException mx) {
errJSON = mx.toString();
}
mongo::BSONObjBuilder b;
b << "message" << message << "ts" << buf << "err" << errJSON;
mongo::BSONObj p = b.obj();
// std::cout<< dumpID << std::endl;
c.insert(dumpID, p);
}
mongowriter::mongowriter(const mongoConfig& orig) : mongoConfig(orig), ready(false) {
ready = init();
}
mongowriter::~mongowriter() {
}