17 #define M (mongo_sync_connection*)m_
18 #define PROPERTIES std::string(databaseName_ + ".properties").c_str()
23 dazeus::Database::Database(
const std::string &hostname, uint16_t port,
const std::string &database,
const std::string &username,
const std::string &password )
27 , databaseName_(database)
41 mongo_sync_disconnect(
M);
55 fprintf(stderr,
"Initiating connection to Mongo daemon at %s:%d\n",
56 hostName_.c_str(), port_);
58 m_ = (
void*)mongo_sync_connect(hostName_.c_str(), port_, TRUE);
60 lastError_ = strerror(errno);
62 fprintf(stderr,
"Connection error: %s\n", lastError_.c_str());
66 if(!mongo_sync_conn_set_auto_reconnect(
M, TRUE)) {
67 lastError_ = strerror(errno);
68 mongo_sync_disconnect(
M);
70 fprintf(stderr,
"Cannot set auto-reconnect: %s\n", lastError_.c_str());
76 fprintf(stderr,
"Building index on table %s...\n",
PROPERTIES);
78 bson *index = bson_build(
79 BSON_TYPE_INT32,
"network", 1,
80 BSON_TYPE_INT32,
"receiver", 1,
81 BSON_TYPE_INT32,
"sender", 1,
86 if(!mongo_sync_cmd_index_create(
M,
PROPERTIES, index, 0))
89 fprintf(stderr,
"Index create error: %s\n", strerror(errno));
91 lastError_ = strerror(errno);
112 const std::string &receiverScope,
const std::string &senderScope )
114 std::stringstream regexStr;
115 regexStr <<
"^\\Q" << ns <<
"\\E\\.";
116 std::string regex = regexStr.str();
118 bson *selector = bson_new();
119 bson_append_regex(selector,
"variable", regex.c_str(),
"");
120 if(networkScope.length() > 0) {
121 bson_append_string(selector,
"network", networkScope.c_str(), -1);
122 if(receiverScope.length() > 0) {
123 bson_append_string(selector,
"receiver", receiverScope.c_str(), -1);
124 if(senderScope.length() > 0) {
125 bson_append_string(selector,
"sender", senderScope.c_str(), -1);
127 bson_append_null(selector,
"sender");
130 bson_append_null(selector,
"receiver");
131 bson_append_null(selector,
"sender");
134 bson_append_null(selector,
"network");
135 bson_append_null(selector,
"receiver");
136 bson_append_null(selector,
"sender");
138 bson_finish(selector);
140 mongo_packet *p = mongo_sync_cmd_query(
M,
PROPERTIES, 0, 0, 0, selector, NULL );
145 lastError_ = strerror(errno);
147 fprintf(stderr,
"Database error: %s\n", lastError_.c_str());
149 return std::vector<std::string>();
152 mongo_sync_cursor *cursor = mongo_sync_cursor_new(
M,
PROPERTIES, p);
154 lastError_ = strerror(errno);
156 fprintf(stderr,
"Database error: %s\n", lastError_.c_str());
158 return std::vector<std::string>();
161 std::vector<std::string> res;
162 while(mongo_sync_cursor_next(cursor)) {
163 bson *result = mongo_sync_cursor_get_data(cursor);
165 lastError_ = strerror(errno);
167 fprintf(stderr,
"Database error: %s\n", lastError_.c_str());
169 mongo_sync_cursor_free(cursor);
173 bson_cursor *c = bson_find(result,
"variable");
175 if(!bson_cursor_get_string(c, &value)) {
176 lastError_ = strerror(errno);
178 fprintf(stderr,
"Database error: %s\n", lastError_.c_str());
180 mongo_sync_cursor_free(cursor);
185 value += ns.length() + 1;
186 res.push_back(value);
205 const std::string &networkScope,
const std::string &receiverScope,
206 const std::string &senderScope )
222 bson *query = bson_build_full(
223 BSON_TYPE_DOCUMENT,
"$orderby", TRUE,
224 bson_build(BSON_TYPE_INT32,
"network", -1, BSON_TYPE_INT32,
"receiver", -1, BSON_TYPE_INT32,
"sender", -1, BSON_TYPE_NONE),
236 bson *selector = bson_new();
237 bson *network = 0, *receiver = 0, *sender = 0;
238 bson_append_string(selector,
"variable", variable.c_str(), -1);
239 if(networkScope.length() > 0) {
240 network = bson_build_full(
241 BSON_TYPE_ARRAY,
"$in", TRUE,
242 bson_build(BSON_TYPE_STRING,
"1", networkScope.c_str(), -1,
243 BSON_TYPE_NULL,
"2", BSON_TYPE_NONE),
245 bson_finish(network);
246 bson_append_document(selector,
"network", network);
247 if(receiverScope.length() > 0) {
248 receiver = bson_build_full(
249 BSON_TYPE_ARRAY,
"$in", TRUE,
250 bson_build(BSON_TYPE_STRING,
"1", receiverScope.c_str(), -1,
251 BSON_TYPE_NULL,
"2", BSON_TYPE_NONE),
253 bson_finish(receiver);
254 bson_append_document(selector,
"receiver", receiver);
255 if(senderScope.length() > 0) {
256 sender = bson_build_full(
257 BSON_TYPE_ARRAY,
"$in", TRUE,
258 bson_build(BSON_TYPE_STRING,
"1", senderScope.c_str(), -1,
259 BSON_TYPE_NULL,
"2", BSON_TYPE_NONE),
262 bson_append_document(selector,
"sender", sender);
266 bson_finish(selector);
267 bson_append_document(query,
"$query", selector);
270 mongo_packet *p = mongo_sync_cmd_query(
M,
PROPERTIES, 0, 0, 1, query, NULL );
274 if(network) bson_free(network);
275 if(receiver) bson_free(receiver);
276 if(sender) bson_free(sender);
279 lastError_ = strerror(errno);
280 return std::string();
283 mongo_sync_cursor *cursor = mongo_sync_cursor_new(
M,
PROPERTIES, p);
285 lastError_ = strerror(errno);
286 return std::string();
289 if(!mongo_sync_cursor_next(cursor)) {
291 fprintf(stderr,
"Variable %s not found within given scope.\n", variable.c_str());
295 bson *result = mongo_sync_cursor_get_data(cursor);
297 lastError_ = strerror(errno);
298 mongo_sync_cursor_free(cursor);
299 return std::string();
302 bson_cursor *c = bson_find(result,
"value");
304 if(!bson_cursor_get_string(c, &value)) {
305 lastError_ = strerror(errno);
307 fprintf(stderr,
"Database error: %s\n", lastError_.c_str());
309 mongo_sync_cursor_free(cursor);
311 return std::string();
314 std::string res(value);
320 assert(!mongo_sync_cursor_next(cursor));
322 mongo_sync_cursor_free(cursor);
327 const std::string &value,
const std::string &networkScope,
328 const std::string &receiverScope,
const std::string &senderScope )
334 bson *
object = bson_build_full(
335 BSON_TYPE_DOCUMENT,
"$set", TRUE,
337 BSON_TYPE_STRING,
"value", value.c_str(), -1,
341 bson *selector = bson_build(
342 BSON_TYPE_STRING,
"variable", variable.c_str(), -1,
344 if(networkScope.length() > 0) {
345 bson_append_string(selector,
"network", networkScope.c_str(), -1);
347 if(receiverScope.length() > 0) {
348 bson_append_string(selector,
"receiver", receiverScope.c_str(), -1);
350 if(senderScope.length() > 0) {
351 bson_append_string(selector,
"sender", senderScope.c_str(), -1);
353 bson_append_null(selector,
"sender");
356 bson_append_null(selector,
"receiver");
357 bson_append_null(selector,
"sender");
360 bson_append_null(selector,
"network");
361 bson_append_null(selector,
"receiver");
362 bson_append_null(selector,
"sender");
364 bson_finish(selector);
367 if(value.length() == 0) {
371 lastError_ = strerror(errno);
373 fprintf(stderr,
"Error: %s\n", lastError_.c_str());
377 MONGO_WIRE_FLAG_UPDATE_UPSERT, selector,
object))
379 lastError_ = strerror(errno);
381 fprintf(stderr,
"Error: %s\n", lastError_.c_str());