Skip to content

Instantly share code, notes, and snippets.

@ctriolo
Created July 10, 2010 22:44
Show Gist options
  • Select an option

  • Save ctriolo/471093 to your computer and use it in GitHub Desktop.

Select an option

Save ctriolo/471093 to your computer and use it in GitHub Desktop.
/*
* Example of SERVER-1389
* Download the official mongo-c-driver
* Place SERVER-1389.c into mongo-c-driver/
* Compile with gcc:
* $ gcc -Isrc --std=c99 src/*.c SERVER-1389.c -o SERVER-1389
* Run a mongod with default settings (127.0.0.1:27017)
* Run SERVER-1389:
* $ ./SERVER-1389
*/
#include <stdio.h>
#include <string.h>
#include "src/bson.h"
#include "src/mongo.h"
#include "src/mongo_except.h"
#define DATA_LENGTH 1024*1024
int main() {
mongo_connection conn[1];
bson b[1];
bson_buffer buf[1];
bson_buffer query_buf[1];
bson query_bson[1];
bson_buffer command_buf[1];
bson command_bson[1];
char data[DATA_LENGTH];
if (mongo_connect(conn, NULL) == mongo_conn_success) {
printf("Successfully connected to mongod\n");
} else {
printf("Could not connect to mongod\n");
printf("Please start a mongod at 127.0.0.1:27017 and try again\n");
return 0;
}
printf("Inserting a document into test.test {\"name\" : \"SERVER-1389\"} with bin-data of length :%i\n", DATA_LENGTH);
bson_buffer_init(buf);
bson_append_string(buf, "name", "SERVER-1389");
bson_append_binary(buf, "data", 2, data, DATA_LENGTH);
bson_from_buffer(b, buf);
mongo_insert(conn, "test.test", b);
// CREATE QUERY BSON
bson_buffer_init(query_buf);
bson_append_string(query_buf, "name", "SERVER-1389");
bson_from_buffer(query_bson, query_buf);
bson_buffer_init(command_buf);
bson_append_bson(command_buf, "query", query_bson);
bson_from_buffer(command_bson, command_buf);
printf("Entering loop: Sending mongo_find() command to mongo\n");
printf("Please shut down mongo now.\n");
for (;;) {
MONGO_TRY {
mongo_cursor_destroy(mongo_find(conn, "test.test", command_bson, NULL, 1, 0, 0));
} MONGO_CATCH {
printf("Network exception caught, attempting reconnect\n");
if (mongo_reconnect(conn) == mongo_conn_success) {
printf("Successfully connected to mongod (Should NOT happen)\n");
} else {
printf("Could not connect to mongod (Should happen)\n");
}
return 0;
}
}
// SHOULD NEVER REACH HERE.
return 1;
}
/*
* Successfully connected to mongod
* Inserting a document into test.test {"name" : "SERVER-1389"} with bin-data of length :1048576
* Entering loop: Sending mongo_find() command to mongo
* Please shut down mongo now.
* Network exception caught, attempting reconnect
* Successfully connected to mongod (Should NOT happen)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment