Skip to content

Commit

Permalink
Added support for embedded arrays within MongoDB documents
Browse files Browse the repository at this point in the history
(cherry picked from commit 61e21a0)
  • Loading branch information
vladpaiu committed Mar 12, 2014
1 parent 26bdb57 commit 0ae9a46
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 26 deletions.
6 changes: 5 additions & 1 deletion modules/cachedb_mongodb/cachedb_mongodb_dbase.c
Expand Up @@ -584,7 +584,11 @@ int mongo_raw_find(cachedb_con *connection,bson *raw_query,cdb_raw_entry ***repl
continue;
}
LM_ERR("Failed to run query. Err = %d, %d , %d \n",conn->err,conn->errcode,conn->lasterrcode);
mongo_cmd_get_last_error(conn,MONGO_DATABASE(connection),&err_b);
ret = mongo_cmd_get_last_error(conn,MONGO_DATABASE(connection),&err_b);
if (ret == MONGO_OK) {
LM_ERR("We had error - can't tell what it was - we're really bogus - probably mongos down\n");
return -1;
}
bson_iterator_init(&i,&err_b);
while( bson_iterator_next(&i)) {
LM_ERR("Fetched ERR key [%s]. Val = ",bson_iterator_key(&i));
Expand Down
105 changes: 80 additions & 25 deletions modules/cachedb_mongodb/cachedb_mongodb_json.c
Expand Up @@ -191,12 +191,86 @@ int json_to_bson(char *json,bson *bb)
return -1;
}

void bson_to_json_generic(struct json_object *obj,bson_iterator *it,int type)
{
const char *curr_key;
char *s;
int len;
struct json_object *obj2=NULL;
bson_iterator it2;

while (bson_iterator_next(it)) {
curr_key=bson_iterator_key(it);

switch( bson_iterator_type(it) ) {
case BSON_INT:
LM_DBG("Found key %s with type int\n",curr_key);
if (type == BSON_OBJECT)
json_object_object_add(obj,curr_key,
json_object_new_int(bson_iterator_int(it)));
else if (type == BSON_ARRAY)
json_object_array_add(obj,json_object_new_int(bson_iterator_int(it)));
break;
case BSON_LONG:
LM_DBG("Found key %s with type long\n",curr_key);
/* no intrinsic support in OpenSIPS for 64bit integers -
* converting to string */
s = int2str(bson_iterator_long(it),&len);
s[len]=0;
if (type == BSON_OBJECT)
json_object_object_add(obj,curr_key,json_object_new_string(s));
else if (type == BSON_ARRAY)
json_object_array_add(obj,json_object_new_string(s));
break;
case BSON_DOUBLE:
/* no intrinsic support in OpenSIPS for floating point numbers
* converting to int */
LM_DBG("Found key %s with type double\n",curr_key);
if (type == BSON_OBJECT)
json_object_object_add(obj,curr_key,
json_object_new_int((int)bson_iterator_double(it)));
else if (type == BSON_ARRAY)
json_object_array_add(obj,json_object_new_int((int)bson_iterator_double(it)));
break;
case BSON_STRING:
LM_DBG("Found key %s with type string\n",curr_key);
if (type == BSON_OBJECT)
json_object_object_add(obj,curr_key,
json_object_new_string(bson_iterator_string(it)));
else if (type == BSON_ARRAY)
json_object_array_add(obj,json_object_new_string(bson_iterator_string(it)));
break;
case BSON_BOOL:
LM_DBG("Found key %s with type bool\n",curr_key);
if (type == BSON_OBJECT)
json_object_object_add(obj,curr_key,
json_object_new_int((int)bson_iterator_bool(it)));
else if (type == BSON_ARRAY)
json_object_array_add(obj,json_object_new_int((int)bson_iterator_bool(it)));
break;
case BSON_ARRAY:
LM_DBG("Found key %s with type array\n",curr_key);
obj2 = json_object_new_array();
bson_iterator_subiterator(it, &it2 );
bson_to_json_generic(obj2,&it2,BSON_ARRAY);
json_object_object_add(obj,curr_key,obj2);
//json_object_put(obj2);
break;
default:
/* TODO - support embedded documents */
LM_DBG("Unsupported type %d for key %s - skipping\n",
bson_iterator_type(it),curr_key);
break;
}
}
}

int mongo_cursor_to_json(mongo_cursor *m_cursor,
cdb_raw_entry ***reply,int expected_kv_no,int *reply_no)
{
struct json_object *obj=NULL;
bson_iterator it;
const char *curr_key,*p;
const char *p;
int current_size=0,len;

/* start with a single returned document */
Expand Down Expand Up @@ -229,30 +303,8 @@ int mongo_cursor_to_json(mongo_cursor *m_cursor,
}

obj = json_object_new_object();

bson_iterator_init(&it,mongo_cursor_bson(m_cursor));
while (bson_iterator_next(&it)) {
curr_key=bson_iterator_key(&it);

switch( bson_iterator_type( &it ) ) {
case BSON_INT:
json_object_object_add(obj,curr_key,
json_object_new_int(bson_iterator_int(&it)));
break;
case BSON_DOUBLE:
json_object_object_add(obj,curr_key,
json_object_new_int((int)bson_iterator_double(&it)));
break;
case BSON_STRING:
json_object_object_add(obj,curr_key,
json_object_new_string(bson_iterator_string(&it)));
break;
default:
/* TODO - support embedded documents */
LM_WARN("Unsupported type %d - skipping\n",bson_iterator_type(&it));
break;
}
}
bson_to_json_generic(obj,&it,BSON_OBJECT);

p = json_object_to_json_string(obj);
if (!p) {
Expand All @@ -279,7 +331,10 @@ int mongo_cursor_to_json(mongo_cursor *m_cursor,

*reply_no = current_size;
LM_DBG("Fetched %d results\n",current_size);
return 0;
if (current_size == 0)
return -2;

return 1;

error_cleanup:
if (obj)
Expand Down

0 comments on commit 0ae9a46

Please sign in to comment.