Skip to content

Commit

Permalink
Modified for new version c-driver v0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwanami committed Sep 7, 2011
1 parent b5f8113 commit 652bb77
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 82 deletions.
2 changes: 1 addition & 1 deletion mongo-c-driver
Submodule mongo-c-driver updated 54 files
+4 −0 .astylerc
+6 −0 .gitignore
+118 −0 HISTORY.md
+29 −51 README.md
+67 −14 SConstruct
+0 −0 docs/buildscripts/__init__.py
+56 −0 docs/buildscripts/docs.py
+130 −0 docs/source/sphinx/Makefile
+155 −0 docs/source/sphinx/make.bat
+218 −0 docs/source/sphinx/source/bson.rst
+119 −0 docs/source/sphinx/source/building.rst
+216 −0 docs/source/sphinx/source/conf.py
+149 −0 docs/source/sphinx/source/connections.rst
+39 −0 docs/source/sphinx/source/index.rst
+402 −0 docs/source/sphinx/source/tutorial.rst
+316 −0 doxygenConfig
+666 −387 src/bson.c
+875 −129 src/bson.h
+148 −0 src/encoding.c
+54 −0 src/encoding.h
+571 −685 src/gridfs.c
+172 −126 src/gridfs.h
+806 −608 src/mongo.c
+524 −136 src/mongo.h
+0 −143 src/mongo_except.h
+98 −0 src/net.c
+57 −0 src/net.h
+1 −1 src/numbers.c
+9 −8 src/platform.h
+183 −0 src/platform/linux/net.c
+50 −0 src/platform/linux/net.h
+0 −223 test/all_types.c
+13 −12 test/auth.c
+253 −227 test/benchmark.c
+240 −0 test/bson.c
+50 −0 test/bson_subobject.c
+32 −32 test/count_delete.c
+14 −30 test/cpptest.cpp
+155 −0 test/cursors.c
+13 −13 test/endian_swap.c
+45 −36 test/errors.c
+21 −25 test/examples.c
+107 −0 test/functions.c
+125 −97 test/gridfs.c
+53 −0 test/helpers.c
+38 −38 test/json.c
+36 −0 test/oid.c
+37 −0 test/platform/linux/timeouts.c
+38 −41 test/replica_set.c
+20 −21 test/resize.c
+115 −67 test/simple.c
+8 −8 test/sizes.c
+65 −64 test/update.c
+124 −0 test/validate.c
133 changes: 52 additions & 81 deletions ngx_http_gridfs_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ typedef struct {

typedef struct {
ngx_str_t name;
mongo_connection conn;
mongo conn;
ngx_array_t *auths; /* ngx_http_mongo_auth_t */
} ngx_http_mongo_connection_t;

Expand Down Expand Up @@ -261,11 +261,11 @@ static char* ngx_http_gridfs(ngx_conf_t* cf, ngx_command_t* command, void* void_
if (type.len == 0) {
gridfs_loc_conf->type = NGX_CONF_UNSET_UINT;
} else if (ngx_strcasecmp(type.data, (u_char *)"objectid") == 0) {
gridfs_loc_conf->type = bson_oid;
gridfs_loc_conf->type = BSON_OID;
} else if (ngx_strcasecmp(type.data, (u_char *)"string") == 0) {
gridfs_loc_conf->type = bson_string;
gridfs_loc_conf->type = BSON_STRING;
} else if (ngx_strcasecmp(type.data, (u_char *)"int") == 0) {
gridfs_loc_conf->type = bson_int;
gridfs_loc_conf->type = BSON_INT;
} else {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"Unsupported Type: %s", (char *)value[i].data);
Expand Down Expand Up @@ -294,7 +294,7 @@ static char* ngx_http_gridfs(ngx_conf_t* cf, ngx_command_t* command, void* void_

if (gridfs_loc_conf->field.data != NULL
&& ngx_strcmp(gridfs_loc_conf->field.data, "filename") == 0
&& gridfs_loc_conf->type != bson_string) {
&& gridfs_loc_conf->type != BSON_STRING) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"Field: filename, must be of Type: string");
return NGX_CONF_ERROR;
Expand Down Expand Up @@ -372,7 +372,7 @@ static char* ngx_http_gridfs_merge_loc_conf(ngx_conf_t* cf, void* void_parent, v
ngx_conf_merge_str_value(child->db, parent->db, NULL);
ngx_conf_merge_str_value(child->root_collection, parent->root_collection, "fs");
ngx_conf_merge_str_value(child->field, parent->field, "_id");
ngx_conf_merge_uint_value(child->type, parent->type, bson_oid);
ngx_conf_merge_uint_value(child->type, parent->type, BSON_OID);
ngx_conf_merge_str_value(child->user, parent->user, NULL);
ngx_conf_merge_str_value(child->pass, parent->pass, NULL);
ngx_conf_merge_str_value(child->mongo, parent->mongo, "127.0.0.1:27017");
Expand Down Expand Up @@ -465,7 +465,7 @@ static ngx_int_t ngx_http_mongo_authenticate(ngx_log_t *log, ngx_http_gridfs_loc

static ngx_int_t ngx_http_mongo_add_connection(ngx_cycle_t* cycle, ngx_http_gridfs_loc_conf_t* gridfs_loc_conf) {
ngx_http_mongo_connection_t* mongo_conn;
mongo_conn_return status;
int status;
ngx_http_mongod_server_t *mongods;
volatile ngx_uint_t i;
u_char host[255];
Expand All @@ -491,7 +491,7 @@ static ngx_int_t ngx_http_mongo_add_connection(ngx_cycle_t* cycle, ngx_http_grid
} else if ( gridfs_loc_conf->mongods->nelts >= 2 && gridfs_loc_conf->mongods->nelts < 9 ) {

/* Initiate replica set connection. */
mongo_replset_init_conn( &mongo_conn->conn, (const char *)gridfs_loc_conf->replset.data );
mongo_replset_init( &mongo_conn->conn, (const char *)gridfs_loc_conf->replset.data );

/* Add replica set seeds. */
for( i=0; i<gridfs_loc_conf->mongods->nelts; ++i ) {
Expand All @@ -506,29 +506,29 @@ static ngx_int_t ngx_http_mongo_add_connection(ngx_cycle_t* cycle, ngx_http_grid
}

switch (status) {
case mongo_conn_success:
case MONGO_CONN_SUCCESS:
break;
case mongo_conn_bad_arg:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: Bad Arguments");
return NGX_ERROR;
case mongo_conn_no_socket:
case MONGO_CONN_NO_SOCKET:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: No Socket");
return NGX_ERROR;
case mongo_conn_fail:
case MONGO_CONN_FAIL:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: Connection Failure.");
return NGX_ERROR;
case mongo_conn_not_master:
case MONGO_CONN_ADDR_FAIL:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: getaddrinfo Failure.");
return NGX_ERROR;
case MONGO_CONN_NOT_MASTER:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: Not Master");
return NGX_ERROR;
case mongo_conn_bad_set_name:
case MONGO_CONN_BAD_SET_NAME:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: Replica set name %s does not match.", gridfs_loc_conf->replset.data);
return NGX_ERROR;
case mongo_conn_cannot_find_primary:
case MONGO_CONN_NO_PRIMARY:
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Exception: Cannot connect to primary node.");
return NGX_ERROR;
Expand Down Expand Up @@ -565,34 +565,34 @@ static ngx_int_t ngx_http_gridfs_init_worker(ngx_cycle_t* cycle) {
}

static ngx_int_t ngx_http_mongo_reconnect(ngx_log_t *log, ngx_http_mongo_connection_t *mongo_conn) {
volatile mongo_conn_return status = mongo_conn_fail;
volatile int status = MONGO_CONN_FAIL;

MONGO_TRY_GENERIC(&mongo_conn->conn) {
if(&mongo_conn->conn.connected) { mongo_disconnect(&mongo_conn->conn); }
if (&mongo_conn->conn.connected) {
mongo_disconnect(&mongo_conn->conn);
ngx_msleep(MONGO_RECONNECT_WAITTIME);
status = mongo_reconnect(&mongo_conn->conn);
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
status = mongo_conn_fail;
} else {
status = MONGO_CONN_FAIL;
}

switch (status) {
case mongo_conn_success:
case MONGO_CONN_SUCCESS:
break;
case mongo_conn_bad_arg:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: Bad Arguments");
return NGX_ERROR;
case mongo_conn_no_socket:
case MONGO_CONN_NO_SOCKET:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: No Socket");
return NGX_ERROR;
case mongo_conn_fail:
case MONGO_CONN_FAIL:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: Connection Failure %s:%i;",
mongo_conn->conn.primary->host,
mongo_conn->conn.primary->port);
return NGX_ERROR;
case mongo_conn_not_master:
case MONGO_CONN_ADDR_FAIL:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: getaddrinfo Failure");
return NGX_ERROR;
case MONGO_CONN_NOT_MASTER:
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo Exception: Not Master");
return NGX_ERROR;
Expand All @@ -611,16 +611,10 @@ static ngx_int_t ngx_http_mongo_reauth(ngx_log_t *log, ngx_http_mongo_connection
auths = mongo_conn->auths->elts;

for (i = 0; i < mongo_conn->auths->nelts; i++) {
MONGO_TRY_GENERIC(&mongo_conn->conn) {
success = mongo_cmd_authenticate( &mongo_conn->conn,
(const char*)auths[i].db.data,
(const char*)auths[i].user.data,
(const char*)auths[i].pass.data );
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"Mongo connection error, during reauth");
return NGX_ERROR;
}
success = mongo_cmd_authenticate( &mongo_conn->conn,
(const char*)auths[i].db.data,
(const char*)auths[i].user.data,
(const char*)auths[i].pass.data );
if (!success) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"Invalid mongo user/pass: %s/%s, during reauth",
Expand Down Expand Up @@ -685,10 +679,8 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) {
ngx_uint_t numchunks;
char* contenttype;
volatile ngx_uint_t i;
volatile ngx_int_t found = 0;
ngx_int_t rc = NGX_OK;
bson query;
bson_buffer buf;
bson_oid_t oid;
mongo_cursor ** cursors;
gridfs_offset chunk_len;
Expand All @@ -697,6 +689,7 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) {
bson chunk;
ngx_pool_cleanup_t* gridfs_cln;
ngx_http_gridfs_cleanup_t* gridfs_clndata;
int status;
volatile ngx_uint_t e = FALSE;
volatile ngx_uint_t ecounter = 0;

Expand All @@ -721,7 +714,6 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) {
return NGX_HTTP_SERVICE_UNAVAILABLE;
}


// ---------- RETRIEVE KEY ---------- //

location_name = core_conf->name;
Expand Down Expand Up @@ -752,13 +744,11 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) {
// ---------- RETRIEVE GRIDFILE ---------- //

do {
MONGO_TRY_GENERIC(&mongo_conn->conn){
e = FALSE;
gridfs_init(&mongo_conn->conn,
e = FALSE;
if (gridfs_init(&mongo_conn->conn,
(const char*)gridfs_conf->db.data,
(const char*)gridfs_conf->root_collection.data,
&gfs);
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
&gfs) != MONGO_OK) {
e = TRUE; ecounter++;
if (ecounter > MONGO_MAX_RETRIES_PER_REQUEST
|| ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR
Expand All @@ -771,44 +761,27 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) {
}
} while (e);

bson_buffer_init(&buf);
bson_init(&query);
switch (gridfs_conf->type) {
case bson_oid:
case BSON_OID:
bson_oid_from_string(&oid, value);
bson_append_oid(&buf, (char*)gridfs_conf->field.data, &oid);
bson_append_oid(&query, (char*)gridfs_conf->field.data, &oid);
break;
case bson_int:
bson_append_int(&buf, (char*)gridfs_conf->field.data, ngx_atoi((u_char*)value, strlen(value)));
case BSON_INT:
bson_append_int(&query, (char*)gridfs_conf->field.data, ngx_atoi((u_char*)value, strlen(value)));
break;
case bson_string:
bson_append_string(&buf, (char*)gridfs_conf->field.data, value);
case BSON_STRING:
bson_append_string(&query, (char*)gridfs_conf->field.data, value);
break;
}
bson_from_buffer(&query, &buf);
bson_finish(&query);

do {
MONGO_TRY_GENERIC(&mongo_conn->conn){
e = FALSE;
found = gridfs_find_query(&gfs, &query, &gfile);
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
e = TRUE; ecounter++;
if (ecounter > MONGO_MAX_RETRIES_PER_REQUEST
|| ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR
|| ngx_http_mongo_reauth(request->connection->log, mongo_conn) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
"Mongo connection dropped, could not reconnect");
if(&mongo_conn->conn.connected) { mongo_disconnect(&mongo_conn->conn); }
bson_destroy(&query);
free(value);
return NGX_HTTP_SERVICE_UNAVAILABLE;
}
}
} while (e);
status = gridfs_find_query(&gfs, &query, &gfile);

bson_destroy(&query);
free(value);

if(!found){
if(status == MONGO_ERROR) {
return NGX_HTTP_NOT_FOUND;
}

Expand Down Expand Up @@ -895,11 +868,9 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) {

/* Fetch the chunk from mongo */
do {
MONGO_TRY_GENERIC(&mongo_conn->conn){
e = FALSE;
cursors[i] = gridfile_get_chunks(&gfile, i, 1);
mongo_cursor_next(cursors[i]);
} MONGO_CATCH_GENERIC(&mongo_conn->conn) {
e = FALSE;
cursors[i] = gridfile_get_chunks(&gfile, i, 1);
if (!(cursors[i] && mongo_cursor_next(cursors[i]) == MONGO_OK)) {
e = TRUE; ecounter++;
if (ecounter > MONGO_MAX_RETRIES_PER_REQUEST
|| ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR
Expand Down

0 comments on commit 652bb77

Please sign in to comment.