/* Test getaddrinfo() by successfully connecting to 'localhost'. */ int test_getaddrinfo( void ) { mongo conn[1]; bson b[1]; const char *ns = "test.foo"; const char *errmsg = "getaddrinfo failed"; if( mongo_client( conn, "badhost.example.com", 27017 ) == MONGO_OK ) { printf( "connected to bad host!\n" ); exit( 1 ); } else { ASSERT( strncmp( errmsg, conn->errstr, strlen( errmsg ) ) == 0 ); } if( mongo_client( conn, "localhost", 27017 ) != MONGO_OK ) { printf( "failed to connect\n" ); exit( 1 ); } mongo_cmd_drop_collection( conn, "test", "foo", NULL ); bson_init( b ); bson_append_int( b, "foo", 17 ); bson_finish( b ); mongo_insert( conn , ns , b, NULL ); ASSERT( mongo_count( conn, "test", "foo", NULL ) == 1 ); bson_destroy( b ); mongo_destroy( conn ); return 0; }
int main() { mongo conn[1]; bson b; const char *sock_path = "/tmp/mongodb-27017.sock"; const char *ns = "test.c.unix_socket"; const char *col = "c.unix_socket"; ASSERT( mongo_client( conn, sock_path, -1 ) == MONGO_OK ); mongo_cmd_drop_collection( conn, "test", col, NULL ); bson_init( &b ); bson_append_new_oid( &b, "_id" ); bson_append_string( &b, "foo", "bar" ); bson_append_int( &b, "x", 1); bson_finish( &b ); ASSERT( mongo_insert( conn, ns, &b, NULL ) == MONGO_OK ); mongo_cmd_drop_collection( conn, "test", col, NULL ); bson_destroy( &b ); mongo_destroy( conn ); return 0; }
int main() { mongo conn[1]; bson b; int res; if( mongo_client( conn, TEST_SERVER, 27017 ) != MONGO_OK ) { printf("Failed to connect"); exit(1); } res = mongo_simple_str_command( conn, "test", "$eval", "for(i=0; i<100000; i++) { db.foo.find() }", &b ); ASSERT( res == MONGO_OK ); /* 50ms timeout */ mongo_set_op_timeout( conn, 50 ); ASSERT( conn->err == 0 ); res = mongo_simple_str_command( conn, "test", "$eval", "for(i=0; i<100000; i++) { db.foo.find() }", &b ); ASSERT( res == MONGO_ERROR ); ASSERT( conn->err == MONGO_IO_ERROR ); return 0; }
int read_config() { mongo conn[1]; int status = mongo_client(conn, "127.0.0.1", 27017); if (status != MONGO_OK) { switch (conn->err) { case MONGO_CONN_NO_SOCKET: printf("no socket\n"); exit(EXIT_FAILURE); case MONGO_CONN_FAIL: printf("connection failed\n"); exit(EXIT_FAILURE); case MONGO_CONN_NOT_MASTER: printf("not master\n"); exit(EXIT_FAILURE); } } scale_t *scales = get_signatures(&conn); printf("%d signatures found\n", sizeof(scales) / sizeof(scales[0])); mongo_destroy(conn); return (0); }
int main() { mongo conn[1]; char version[10]; INIT_SOCKETS_FOR_WINDOWS; test_write_concern_finish( ); if( mongo_client( conn, TEST_SERVER, 27017 ) != MONGO_OK ) { printf( "failed to connect\n" ); exit( 1 ); } ASSERT( conn->write_concern != (void*)0 ); test_insert( conn ); if( mongo_get_server_version( version ) != -1 && version[0] != '1' ) { test_write_concern_input( conn ); test_update_and_remove( conn ); test_batch_insert_with_continue( conn ); } mongo_destroy( conn ); return 0; }
void test_streaming( void ) { mongo conn[1]; gridfs gfs[1]; gridfile gfile[1]; char *medium = (char*)bson_malloc( 2*MEDIUM ); char *small = (char*)bson_malloc( LOWER ); char *buf = (char*)bson_malloc( LARGE ); int n; if( buf == NULL || small == NULL ) { printf( "Failed to allocate" ); exit( 1 ); } srand( (unsigned int)time( NULL ) ); INIT_SOCKETS_FOR_WINDOWS; if ( mongo_client( conn , TEST_SERVER, 27017 ) ) { printf( "failed to connect 3\n" ); exit( 1 ); } fill_buffer_randomly( medium, ( int64_t )2 * MEDIUM ); fill_buffer_randomly( small, ( int64_t )LOWER ); fill_buffer_randomly( buf, ( int64_t )LARGE ); gridfs_init( conn, "test", "fs", gfs ); gridfile_writer_init( gfile, gfs, "medium", "text/html", GRIDFILE_DEFAULT ); gridfile_write_buffer( gfile, medium, MEDIUM ); gridfile_write_buffer( gfile, medium + MEDIUM, MEDIUM ); gridfile_writer_done( gfile ); test_gridfile( gfs, medium, 2 * MEDIUM, "medium", "text/html" ); gridfs_destroy( gfs ); gridfs_init( conn, "test", "fs", gfs ); gridfs_store_buffer( gfs, small, LOWER, "small", "text/html", GRIDFILE_DEFAULT ); test_gridfile( gfs, small, LOWER, "small", "text/html" ); gridfs_destroy( gfs ); gridfs_init( conn, "test", "fs", gfs ); gridfs_remove_filename( gfs, "large" ); gridfile_writer_init( gfile, gfs, "large", "text/html", GRIDFILE_DEFAULT ); for( n=0; n < ( LARGE / 1024 ); n++ ) { gridfile_write_buffer( gfile, buf + ( n * 1024 ), 1024 ); } gridfile_writer_done( gfile ); test_gridfile( gfs, buf, LARGE, "large", "text/html" ); gridfs_destroy( gfs ); mongo_destroy( conn ); free( buf ); free( small ); free( medium ); }
int main() { mongo conn[1]; int status = mongo_client(conn, "127.0.0.1", 27017); if (status != MONGO_OK) { switch(conn->err) { case MONGO_CONN_NO_SOCKET: printf("no socket\n"); return 1; case MONGO_CONN_FAIL: printf("connection failed\n"); return 1; case MONGO_CONN_NOT_MASTER: printf("not master\n"); return 1; } } char target1[] = "Pitt, Brad"; char target2[] = "Foxx, Jamie"; if (strcmp(target1, target2) == 0) { printf("same guy, dipshit\n"); exit(1); } /* The program is given a movie and it finds the cast of that movie in the form of a linked list. */ printf("actor for inglorious basterds\n"); struct node *root; root = getActors("Inglourious Basterds (2009", conn); struct node *conductor; conductor = root; if (conductor != 0) { while (conductor->next != 0) { printf("%s\n", conductor->x); conductor = conductor->next; } printf("%s\n", conductor->x); } /* The program is given an actor and it finds all the movies that person has starred in, in the form of a linked list. */ printf("movies for brad pitt\n"); struct node *root2; root2 = getMovies(target1, conn); struct node *conductor2; conductor2 = root2; if (conductor2 != 0) { while(conductor2->next != 0) { printf("%s\n", conductor2->x); conductor2 = conductor2->next; } printf("%s\n", conductor->x); } mongo_destroy(conn); return 0; }
//------------------------------------------------------------------------------ int main( int argc, char * argv[] ){ mongo conn; if( mongo_client( &conn , TEST_SERVER, TEST_PORT ) != MONGO_OK ) { std::cout << "failed to connect\n"; return EXIT_FAILURE; } mongo_cursor cursor; mongo_cursor_init( &cursor, &conn, "test.test" ); char hex_oid[25]; while( mongo_cursor_next( &cursor ) == MONGO_OK ) { std::cout << "row:\n"; bson_iterator it; bson_iterator_init( &it, mongo_cursor_bson( &cursor ) ); while( bson_iterator_next( &it ) ) { std::cout << " " << bson_iterator_key( &it ) << " = "; switch( bson_iterator_type( &it ) ) { case BSON_DOUBLE: std::cout << "(double) " << bson_iterator_double( &it ) << std::endl; break; case BSON_INT: std::cout << "(int) " << bson_iterator_int( &it ) << std::endl; break; case BSON_STRING: std::cout << "(string) \"" << bson_iterator_string( &it ) << "\"\n"; break; case BSON_OID: bson_oid_to_string( bson_iterator_oid( &it ), hex_oid ); std::cout << "(oid) \"" << hex_oid << "\"\n"; break; case BSON_OBJECT: std::cout << "(subobject) {...}\n"; break; case BSON_ARRAY: std::cout << "(array) [...]\n"; break; case BSON_TIMESTAMP: std::cout << "(timestamp) [...]\n"; break; default: std::cout << "(type " << bson_iterator_type( &it ) << std::endl; break; } } std::cout << std::endl; } mongo_disconnect( &conn ); return EXIT_SUCCESS; }
SEXP rmongo_connect(SEXP mongo_conn) { mongo* conn = _checkMongo(mongo_conn); mongo_host_port hp; SEXP host = getAttrib(mongo_conn, sym_host); int len = LENGTH(host); int i; if (len == 0) error("No hosts defined\n"); const char* name = CHAR(STRING_ELT(getAttrib(mongo_conn, sym_name), 0)); if (name[0] == '\0') { for (i = 0; i < len; i++) { mongo_parse_host(CHAR(STRING_ELT(host, i)), &hp); if (mongo_client(conn, hp.host, hp.port) == MONGO_OK) break; } if (i == len) { if (len == 1) Rprintf("Unable to connect to %s:%d, error code = %d\n", hp.host, hp.port, conn->err); else Rprintf("Unable to connect to any of the given hosts, error code = %d\n", conn->err); return mongo_conn; } } else { mongo_replica_set_init(conn, name); for (i = 0; i < len; i++) { mongo_parse_host(CHAR(STRING_ELT(host, i)), &hp); mongo_replica_set_add_seed(conn, hp.host, hp.port); } if (mongo_replica_set_client(conn) != MONGO_OK) Rprintf("Unable to connect to replset\n"); } int timeout = asInteger(getAttrib(mongo_conn, sym_timeout)); if (timeout > 0) mongo_set_op_timeout(conn, timeout); SEXP username = getAttrib(mongo_conn, sym_username); if (CHAR(STRING_ELT(username, 0))[0] != '\0') { SEXP password = getAttrib(mongo_conn, install("password")); SEXP db = getAttrib(mongo_conn, install("db")); SEXP ret = mongo_authenticate(mongo_conn, username, password, db); if (!LOGICAL(ret)[0]) { mongo_disconnect(conn); Rprintf("Authentication failed.\n"); } } return mongo_conn; }
int MongoLink::connect(const std::string& ip, short port) { LOG(TAG_ROUTER, "mongo connect ip=%s, port=%d.", ip.c_str(), port); int status = mongo_client( &m_mongo, ip.c_str(), port ); if( status != MONGO_OK ) { LOG(TAG_ROUTER, "mongo connect failed with code=%d", status); } else { LOG(TAG_ROUTER, "mongo connected."); } //test code here: //query("yunzed"); return status; }
void test_basic( void ) { mongo conn[1]; gridfs gfs[1]; char *data_before = (char*)bson_malloc( UPPER ); int64_t i; FILE *fd; srand((unsigned int) time( NULL ) ); INIT_SOCKETS_FOR_WINDOWS; if ( mongo_client( conn, TEST_SERVER, 27017 ) ) { printf( "failed to connect 2\n" ); exit( 1 ); } gridfs_init( conn, "test", "fs", gfs ); fill_buffer_randomly( data_before, UPPER ); for ( i = LOWER; i <= UPPER; i += DELTA ) { /* Input from buffer */ gridfs_store_buffer( gfs, data_before, i, "input-buffer", "text/html", GRIDFILE_COMPRESS ); test_gridfile( gfs, data_before, i, "input-buffer", "text/html" ); /* Input from file */ fd = fopen( "input-file", "w" ); fwrite( data_before, sizeof( char ), (size_t)i, fd ); fclose( fd ); gridfs_store_file( gfs, "input-file", "input-file", "text/html", GRIDFILE_DEFAULT ); test_gridfile( gfs, data_before, i, "input-file", "text/html" ); gfs->caseInsensitive = 1; gridfs_store_file( gfs, "input-file", "input-file", "text/html", GRIDFILE_DEFAULT ); test_gridfile( gfs, data_before, i, "inPut-file", "text/html" ); } gridfs_destroy( gfs ); mongo_disconnect( conn ); mongo_destroy( conn ); free( data_before ); /* Clean up files. */ _unlink( "input-file" ); _unlink( "output" ); }
bool TMongoDriver::open(const QString &db, const QString &user, const QString &password, const QString &host, quint16 port, const QString &) { if (host.isEmpty()) { return false; } if (!port) port = MONGO_DEFAULT_PORT; mongo_clear_errors(mongoConnection); mongo_set_op_timeout(mongoConnection, 1000); int status = mongo_client(mongoConnection, qPrintable(host), port); if (status != MONGO_OK) { switch (mongoConnection->err) { case MONGO_CONN_NO_SOCKET: tSystemError("MongoDB socket error: %s", mongoConnection->lasterrstr); break; case MONGO_CONN_FAIL: tSystemError("MongoDB connection failed: %s", mongoConnection->lasterrstr); break; case MONGO_CONN_NOT_MASTER: tSystemDebug("MongoDB not master: %s", mongoConnection->lasterrstr); break; default: tSystemError("MongoDB error: %s", mongoConnection->lasterrstr); break; } return false; } if (!user.isEmpty()) { status = mongo_cmd_authenticate(mongoConnection, qPrintable(db), qPrintable(user), qPrintable(password)); if (status != MONGO_OK) { tSystemDebug("MongoDB authentication error: %s", mongoConnection->lasterrstr); return false; } } return (status == MONGO_OK); }
/*esb:mongodb connection string config*/ static char * ngx_http_mongodb_set_conn(ngx_conf_t *cf,ngx_command_t *cmd, void *conf) { ngx_http_vesb_aio_log_main_conf_t *valcf = conf; mongo conn[1]; ngx_str_t *value; char *conn_str; int port,status; value = cf->args->elts; valcf->mongodb_conn_str = value[1]; valcf->mongodb_conn_port = ngx_atoi(value[2].data, value[2].len); if (valcf->mongodb_conn_port == (ngx_int_t) NGX_ERROR) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid number \"%V\"", &value[2]); return NGX_CONF_ERROR; } conn_str = (char *)valcf->mongodb_conn_str.data; port = (int)valcf->mongodb_conn_port; status = mongo_client( conn, conn_str, port ); if( status != MONGO_OK ) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "can't connect server %V %V", &value[1],&value[2]); return NGX_CONF_ERROR; } mongo_destroy( conn ); valcf->mongodb_enable = 1; return NGX_CONF_OK; }
u32 c_mongo_connection::prv_connect(const std::string& set_name, const init_type::socket_info_set_type& set) { typedef init_type::socket_info_set_citer_type iter_type; assert(!_init.empty()); if(_init.size() == 1) { iter_type i = set.begin(); return mongo_client(this, i->host().c_str(), i->port()); } mongo_replica_set_init(this, set_name.c_str()); for(iter_type i = set.begin(), isize = set.end(); i != isize; ++i) { mongo_replica_set_add_seed(this, i->host().c_str(), i->port()); } return mongo_replica_set_client(this); }
int main() { mongo conn[1]; INIT_SOCKETS_FOR_WINDOWS; if( mongo_client( conn, TEST_SERVER, 27017 ) != MONGO_OK ) { printf( "Failed to connect" ); exit( 1 ); } test_multiple_getmore( conn ); test_tailable( conn ); test_builder_api( conn ); test_bad_query( conn ); test_copy_cursor_data( conn ); mongo_destroy( conn ); return 0; }
int main() { /* * We assume objects in the form of {_id:<any_id>, list:[{a:<int>,b:<int>}, ...]} */ mongo conn[1]; mongo_init(conn); if(MONGO_OK != mongo_client(conn, "127.0.0.1", 27017)) return 1; bson b[1], b_result[1]; /*create the aggregation command in bson*/ bson_init(b); bson_append_string(b, "aggregate", "agg"); bson_append_start_array(b, "pipeline"); bson_append_start_object(b,"0"); bson_append_string(b, "$unwind", "$list"); bson_append_finish_object(b); bson_append_start_object(b,"1"); bson_append_start_object(b,"$group"); bson_append_string(b,"_id", "$list"); bson_append_start_object(b, "distinct_count"); bson_append_int(b, "$sum", 1); bson_append_finish_object(b); bson_append_finish_object(b); bson_append_finish_object(b); bson_append_finish_array(b); bson_finish(b); /*So you can see your command*/ bson_print(b); /*run the command*/ mongo_run_command(conn, "test", b, b_result); /*command results*/ bson_print(b_result); bson_destroy(b_result); bson_destroy(b); mongo_destroy(conn); return 0; }
/* Queries the test database for actors and movies and prints out the results from the query */ void testQuery() { mongo conn[1]; int status = mongo_client( conn, "127.0.0.1", 27017 ); Node *actor_node = new_Node(); Node *movie_node = new_Node(); actorNode(actor_node, "Shaq", conn); movieNode(movie_node, "Star Wars", conn); printf("actor name: %s actor type: %s number children:%d\n", actor_node->name, actor_node->type, actor_node->numberChildren); for (int i=0; i<actor_node->numberChildren; i++) { printf("actor child: %s\n", actor_node->children[i]); } printf("movie name: %s movie type: %s movie children:%d\n", movie_node->name, movie_node->type, movie_node->numberChildren); for (int i=0; i<movie_node->numberChildren; i++) { printf("movie child: %s\n", movie_node->children[i]); } }
int main() { mongo conn[1]; int status = mongo_client( conn, "127.0.0.1", 27017 ); if( status != MONGO_OK ) { switch ( conn->err ) { case MONGO_CONN_NO_SOCKET: printf( "no socket\n" ); return 1; case MONGO_CONN_FAIL: printf( "connection failed\n" ); return 1; case MONGO_CONN_NOT_MASTER: printf( "not master\n" ); return 1; } } bson b[1]; bson_init( b ); bson_append_new_oid( b, "_id" ); bson_append_string( b, "name", "Joe" ); bson_append_int( b, "age", 33 ); bson_finish( b ); mongo_insert( conn, "tutorial.persons", b, 0 ); bson_destroy( b ); tutorial_insert_batch(conn); tutorial_empty_query(conn); tutorial_simple_query(conn); tutorial_update(conn); tutorial_index(conn); mongo_destroy( conn ); return 0; }
result_t MongoDB::open(exlib::string connString) { assert(!Isolate::check()); obj_ptr<Url> u = new Url(); int32_t result; int32_t nPort; result_t hr = u->parse(connString); if (hr < 0) return hr; if (qstrchr(u->m_host.c_str(), ',')) { const char *host = u->m_host.c_str(); mongo_replset_init(&m_conn, ""); while (true) { exlib::string hostname; exlib::string port; Url::parseHost(host, hostname, port); nPort = 27017; if (!port.empty()) nPort = atoi(port.c_str()); mongo_replset_add_seed(&m_conn, hostname.c_str(), nPort); if (*host != ',') break; host++; } result = mongo_replset_connect(&m_conn); } else { nPort = 27017; if (!u->m_port.empty()) nPort = atoi(u->m_port.c_str()); mongo_init(&m_conn); result = mongo_client(&m_conn, u->m_hostname.c_str(), nPort); } if (result != MONGO_OK) return CHECK_ERROR(error()); if (!u->m_pathname.empty()) m_ns = u->m_pathname.substr(1); if (!u->m_username.empty()) if (mongo_cmd_authenticate(&m_conn, m_ns.c_str(), u->m_username.c_str(), u->m_password.c_str()) != MONGO_OK) return CHECK_ERROR(error()); return 0; }
void test_large( void ) { mongo conn[1]; gridfs gfs[1]; gridfile gfile[1]; FILE *fd; size_t i, n; char *buffer = (char*)bson_malloc( LARGE ); char *read_buf = (char*)bson_malloc( LARGE ); int64_t filesize = ( int64_t )1024 * ( int64_t )LARGE; mongo_write_concern wc; bson lastError; bson lastErrorCmd; srand( (unsigned int) time( NULL ) ); INIT_SOCKETS_FOR_WINDOWS; if ( mongo_client( conn, TEST_SERVER, 27017 ) ) { printf( "failed to connect 1\n" ); exit( 1 ); } mongo_write_concern_init(&wc); wc.j = 1; mongo_write_concern_finish(&wc); mongo_set_write_concern(conn, &wc); gridfs_init( conn, "test", "fs", gfs ); fd = fopen( "bigfile", "r" ); if( fd ) { fclose( fd ); } else { /* Create a very large file */ fill_buffer_randomly( buffer, ( int64_t )LARGE ); fd = fopen( "bigfile", "w" ); for( i=0; i<1024; i++ ) { fwrite( buffer, 1, LARGE, fd ); } fclose( fd ); } /* Now read the file into GridFS */ gridfs_remove_filename( gfs, "bigfile" ); gridfs_store_file( gfs, "bigfile", "bigfile", "text/html", GRIDFILE_NOMD5 | GRIDFILE_COMPRESS); gridfs_find_filename( gfs, "bigfile", gfile ); ASSERT( strcmp( gridfile_get_filename( gfile ), "bigfile" ) == 0 ); ASSERT( gridfile_get_contentlength( gfile ) == filesize ); fd = fopen( "bigfile", "r" ); while( ( n = fread( buffer, 1, MEDIUM, fd ) ) != 0 ) { ASSERT( gridfile_read( gfile, MEDIUM, read_buf ) == n ); ASSERT( memcmp( buffer, read_buf, n ) == 0 ); } fclose( fd ); gridfile_destroy( gfile ); /* Read the file using the streaming interface */ gridfs_remove_filename( gfs, "bigfile" ); gridfs_remove_filename( gfs, "bigfile-stream" ); gridfile_writer_init( gfile, gfs, "bigfile-stream", "text/html", GRIDFILE_NOMD5 | GRIDFILE_COMPRESS ); mongo_write_concern_destroy( &wc ); mongo_write_concern_init(&wc); wc.j = 0; /* Let's reset write concern j field to zero, we will manually call getLastError with j = 1 */ mongo_write_concern_finish(&wc); mongo_set_write_concern(conn, &wc); fd = fopen( "bigfile", "r" ); i = 0; while( ( n = fread( buffer, 1, READ_WRITE_BUF_SIZE, fd ) ) != 0 ) { gridfile_write_buffer( gfile, buffer, n ); if(i++ % 10 == 0) { bson_init( &lastErrorCmd ); bson_append_int( &lastErrorCmd, "getLastError", 1); bson_append_int( &lastErrorCmd, "j", 1); bson_finish( &lastErrorCmd ); bson_init( &lastError ); mongo_run_command( conn, "test", &lastErrorCmd, &lastError ); bson_destroy( &lastError ); bson_destroy( &lastErrorCmd ); } } mongo_write_concern_destroy( &wc ); mongo_write_concern_init(&wc); wc.j = 1; /* Let's reset write concern j field to 1 */ mongo_write_concern_finish(&wc); mongo_set_write_concern(conn, &wc); fclose( fd ); gridfile_writer_done( gfile ); gridfs_find_filename( gfs, "bigfile-stream", gfile ); ASSERT( strcmp( gridfile_get_filename( gfile ), "bigfile-stream" ) == 0 ); ASSERT( gridfile_get_contentlength( gfile ) == filesize ); gridfs_remove_filename( gfs, "bigfile-stream" ); gridfs_destroy( gfs ); mongo_disconnect( conn ); mongo_destroy( conn ); bson_free( buffer ); bson_free( read_buf ); mongo_write_concern_destroy( &wc ); }
int main() { bson b, sub, out; bson_iterator it; mongo conn; mongo_cursor cursor; int result; /* Create a rich document like this one: * * { _id: ObjectId("4d95ea712b752328eb2fc2cc"), * user_id: ObjectId("4d95ea712b752328eb2fc2cd"), * * items: [ * { sku: "col-123", * name: "John Coltrane: Impressions", * price: 1099, * }, * * { sku: "young-456", * name: "Larry Young: Unity", * price: 1199 * } * ], * * address: { * street: "59 18th St.", * zip: 10010 * }, * * total: 2298 * } */ bson_init( &b ); bson_append_new_oid( &b, "_id" ); bson_append_new_oid( &b, "user_id" ); bson_append_start_array( &b, "items" ); bson_append_start_object( &b, "0" ); bson_append_string( &b, "name", "John Coltrane: Impressions" ); bson_append_int( &b, "price", 1099 ); bson_append_finish_object( &b ); bson_append_start_object( &b, "1" ); bson_append_string( &b, "name", "Larry Young: Unity" ); bson_append_int( &b, "price", 1199 ); bson_append_finish_object( &b ); bson_append_finish_object( &b ); bson_append_start_object( &b, "address" ); bson_append_string( &b, "street", "59 18th St." ); bson_append_int( &b, "zip", 10010 ); bson_append_finish_object( &b ); bson_append_int( &b, "total", 2298 ); /* Finish the BSON obj. */ bson_finish( &b ); printf("Here's the whole BSON object:\n"); bson_print( &b ); /* Advance to the 'items' array */ bson_find( &it, &b, "items" ); /* Get the subobject representing items */ bson_iterator_subobject_init( &it, &sub, 0 ); /* Now iterate that object */ printf("And here's the inner sub-object by itself.\n"); bson_print( &sub ); bson_destroy( &sub ); /* Now make a connection to MongoDB. */ if( mongo_client( &conn, "127.0.0.1", 27017 ) != MONGO_OK ) { switch( conn.err ) { case MONGO_CONN_SUCCESS: break; case MONGO_CONN_NO_SOCKET: printf( "FAIL: Could not create a socket!\n" ); break; case MONGO_CONN_FAIL: printf( "FAIL: Could not connect to mongod. Make sure it's listening at 127.0.0.1:27017.\n" ); break; default: printf( "MongoDB connection error number %d.\n", conn.err ); break; } exit( 1 ); } /* Insert the sample BSON document. */ if( mongo_insert( &conn, "test.records", &b, NULL ) != MONGO_OK ) { printf( "FAIL: Failed to insert document with error %d\n", conn.err ); exit( 1 ); } /* Query for the sample document. */ mongo_cursor_init( &cursor, &conn, "test.records" ); mongo_cursor_set_query( &cursor, bson_shared_empty() ); if( mongo_cursor_next( &cursor ) != MONGO_OK ) { printf( "FAIL: Failed to find inserted document." ); exit( 1 ); } printf( "Found saved BSON object:\n" ); bson_print( (bson *)mongo_cursor_bson( &cursor ) ); mongo_cmd_drop_collection( &conn, "test", "records", NULL ); mongo_cursor_destroy( &cursor ); bson_destroy( &b ); mongo_destroy( &conn ); return 0; }
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; int status; ngx_http_mongod_server_t *mongods; volatile ngx_uint_t i; u_char host[255]; mongods = gridfs_loc_conf->mongods->elts; mongo_conn = ngx_http_get_mongo_connection( gridfs_loc_conf->mongo ); if (mongo_conn != NULL) { return NGX_OK; } mongo_conn = ngx_array_push(&ngx_http_mongo_connections); if (mongo_conn == NULL) { return NGX_ERROR; } mongo_conn->name = gridfs_loc_conf->mongo; mongo_conn->auths = ngx_array_create(cycle->pool, 4, sizeof(ngx_http_mongo_auth_t)); if ( gridfs_loc_conf->mongods->nelts == 1 ) { ngx_cpystrn( host, mongods[0].host.data, mongods[0].host.len + 1 ); status = mongo_client( &mongo_conn->conn, (const char*)host, mongods[0].port ); } else if ( gridfs_loc_conf->mongods->nelts >= 2 && gridfs_loc_conf->mongods->nelts < 9 ) { /* Initiate replica set connection. */ mongo_replica_set_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 ) { ngx_cpystrn( host, mongods[i].host.data, mongods[i].host.len + 1 ); mongo_replica_set_add_seed( &mongo_conn->conn, (const char *)host, mongods[i].port ); } status = mongo_replica_set_client( &mongo_conn->conn ); } else { ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "Mongo Nginx Exception: Too many strings provided in 'mongo' directive."); return NGX_ERROR; } switch (status) { case MONGO_CONN_SUCCESS: break; 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: ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "Mongo Exception: Connection Failure."); return NGX_ERROR; 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: 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_NO_PRIMARY: ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "Mongo Exception: Cannot connect to primary node."); return NGX_ERROR; default: ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "Mongo Exception: Unknown Error"); return NGX_ERROR; } return NGX_OK; }
static void ngx_wirte_tracking_into_mongodb(ngx_http_request_t *r, ngx_http_vesb_aio_log_main_conf_t *valcf,ngx_http_vesb_aio_log_loc_conf_t *vallcf,int timeuse) { mongo conn[1]; char *conn_str; int port,status,insertstatus; time_t timenow; ngx_list_part_t *part = &r->headers_in.headers.part; ngx_table_elt_t *header = part->elts; ngx_uint_t i; if(r->headers_out.status == NGX_HTTP_OK) { conn_str = (char *)valcf->mongodb_conn_str.data; port = (int)valcf->mongodb_conn_port; status = mongo_client( conn, conn_str, port ); if( status == MONGO_OK ) { mongo_set_op_timeout( conn, 10000 );//time oust 10000 ms time ( &timenow ); bson b[1]; bson_init( b ); bson_append_new_oid( b, "_id" ); if( r->headers_in.x_forwarded_for == NULL ) { bson_append_string_n( b, "realclientip" , (char *)r->connection->addr_text.data, r->connection->addr_text.len); } else { bson_append_string_n( b, "realclientip" , (char *)r->headers_in.x_forwarded_for->value.data,r->headers_in.x_forwarded_for->value.len); } bson_append_int( b,"statuscode",r->headers_out.status); bson_append_int( b,"usetime",timeuse); bson_append_long( b,"requestsize",r->request_length); bson_append_long( b,"responsesize",r->headers_out.content_length_n); bson_append_time_t( b,"invoketime",timenow ); if(vallcf->app_name.data != NULL) { bson_append_string_n( b,"appname",(char *)vallcf->app_name.data,vallcf->app_name.len); } else { bson_append_string( b,"appname","undefine"); } /*get method name*/ for(i=0;/* void */; ++i) { if(i >= part->nelts) { if(part->next == NULL) { break; } part = part->next; header = part->elts; i=0; } if(header[i].hash == 0) { continue; } if(ngx_strstr(header[i].key.data,"SOAPAction") != NULL) { bson_append_string_n( b,"SOAPAction",(char *)header[i].value.data,header[i].value.len); } else if(ngx_strstr(header[i].key.data,"Content-Type") != NULL) { bson_append_string_n( b,"Content-Type",(char *)header[i].value.data,header[i].value.len); } } bson_finish( b ); insertstatus = mongo_insert( conn,"vesb.tracking", b , NULL ); if( insertstatus != MONGO_OK ) { ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "insert tracking log in mongodb is failed!(error:%d)",conn[0].err); } bson_destroy( b ); } else { ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "mongodb is unconnection!(error:%d)",status); } mongo_destroy( conn ); } }
int main() { INIT_SOCKETS_FOR_WINDOWS; if ( mongo_client( conn, TEST_SERVER, 27017 ) != MONGO_OK ) { printf( "failed to connect\n" ); exit( 1 ); } clean(); printf( "-----\n" ); TIME( serialize_small_test, 0 ); TIME( serialize_medium_test, 0 ); TIME( serialize_large_test, 0 ); printf( "-----\n" ); TIME( single_insert_small_test, 1 ); TIME( single_insert_medium_test, 1 ); TIME( single_insert_large_test, 1 ); printf( "-----\n" ); TIME( index_insert_small_test, 1 ); TIME( index_insert_medium_test, 1 ); TIME( index_insert_large_test, 1 ); printf( "-----\n" ); TIME( batch_insert_small_test, 1 ); TIME( batch_insert_medium_test, 1 ); TIME( batch_insert_large_test, 1 ); #if DO_SLOW_TESTS printf( "-----\n" ); TIME( find_one_noindex_small_test, 0 ); TIME( find_one_noindex_medium_test, 0 ); TIME( find_one_noindex_large_test, 0 ); #endif printf( "-----\n" ); TIME( find_one_index_small_test, 0 ); TIME( find_one_index_medium_test, 0 ); TIME( find_one_index_large_test, 0 ); #if DO_SLOW_TESTS printf( "-----\n" ); TIME( find_noindex_small_test, 0 ); TIME( find_noindex_medium_test, 0 ); TIME( find_noindex_large_test, 0 ); #endif printf( "-----\n" ); TIME( find_index_small_test, 0 ); TIME( find_index_medium_test, 0 ); TIME( find_index_large_test, 0 ); printf( "-----\n" ); TIME( find_range_small_test, 0 ); TIME( find_range_medium_test, 0 ); TIME( find_range_large_test, 0 ); mongo_destroy( conn ); return 0; }
int main(int argc, char **argv) { bson b[1]; bson query[1]; mongo conn[1]; mongo_cursor cursor[1]; int result; if(mongo_client(conn, "127.0.0.1", 27017) != MONGO_OK) { switch(conn->err) { case MONGO_CONN_SUCCESS: fprintf(stderr, "OK: Connected to MongoDB!\n"); break; case MONGO_CONN_NO_SOCKET: fprintf(stderr, "FAIL: Cloud not create a socket!\n"); break; case MONGO_CONN_FAIL: fprintf(stderr, "FAIL: Could not connect to mongod!."); break; default: fprintf(stderr, "MongoDB connection error number: %d.\n", conn->err); } } bson_init(query); bson_append_string(query, "city_name", "南京"); bson_finish(query); mongo_cursor_init(cursor, conn, "bangboox.city_shop"); mongo_cursor_set_query(cursor, query); while(mongo_cursor_next(cursor) == MONGO_OK) { bson_print((bson *)mongo_cursor_bson(cursor)); } bson_init(b); bson_append_new_oid(b, "_id"); bson_append_new_oid(b, "record_id"); bson_append_start_array(b, "items"); bson_append_start_object(b, "0"); bson_append_string(b, "name", "roy.lieu"); bson_append_int(b, "age", 30); bson_append_finish_object(b); bson_append_start_object(b, "1"); bson_append_string(b, "name", "jimmy.chen"); bson_append_int(b, "age", 35); bson_append_finish_object(b); bson_append_finish_object(b); bson_append_start_object(b, "address"); bson_append_string(b, "stree", "Jufeng RD."); bson_append_int(b, "zip", 232044); bson_append_finish_object(b); bson_finish(b); //printf("\n\n"); //bson_print(b); if(mongo_insert(conn, "test.record", b, NULL) != MONGO_OK) { fprintf(stderr, "FAIL: Failed to insert document whth err: %d\n", conn->err); } return 0; }
int flush_to_mongo(st_http_request * p, int counter) { mongo conn; mongo_init(&conn); mongo_set_op_timeout(&conn, 10000); int status = mongo_client(&conn, globalArgs.host, globalArgs.port); if (status != MONGO_OK) { switch (conn.err) { case MONGO_CONN_SUCCESS: printf("Connected to mongo\n"); case MONGO_CONN_NO_SOCKET: printf("FAIL: Could not create a socket!\n"); break; case MONGO_CONN_ADDR_FAIL: printf("FAIL: MONGO_CONN_ADDR_FAIL: %s\n", globalArgs.host); break; case MONGO_CONN_NOT_MASTER: printf("FAIL: MONGO_CONN_NOT_MASTER\n"); break; case MONGO_CONN_BAD_SET_NAME: printf("FAIL: MONGO_CONN_BAD_SET_NAME\n"); break; case MONGO_CONN_NO_PRIMARY: printf("FAIL: MONGO_CONN_NO_PRIMARY\n"); break; case MONGO_IO_ERROR: printf("FAIL: MONGO_IO_ERROR\n"); break; case MONGO_SOCKET_ERROR: printf("FAIL: MONGO_SOCKET_ERROR\n"); break; case MONGO_READ_SIZE_ERROR: printf("FAIL: MONGO_READ_SIZE_ERROR\n"); break; case MONGO_COMMAND_FAILED: printf("FAIL: MONGO_COMMAND_FAILED\n"); break; case MONGO_WRITE_ERROR: printf("FAIL: MONGO_WRITE_ERROR\n"); break; case MONGO_NS_INVALID: printf("FAIL: MONGO_NS_INVALID\n"); break; case MONGO_BSON_INVALID: printf("FAIL: MONGO_BSON_INVALIDr\n"); break; case MONGO_BSON_NOT_FINISHED: printf("FAIL: MONGO_BSON_NOT_FINISHED\n"); break; case MONGO_BSON_TOO_LARGE: printf("FAIL: MONGO_BSON_TOO_LARGEr\n"); break; case MONGO_WRITE_CONCERN_INVALID: printf("FAIL: Mongo write concern invalid\n"); break; case MONGO_CONN_FAIL: printf ("FAIL: Mongo connection fail. Make sure it's listening at %s:%d\n", globalArgs.host, globalArgs.port); break; } exit(EXIT_FAILURE); } bson **bps; bps = (bson **) malloc(sizeof(bson *) * counter); char *fields = globalArgs.fields; int i = 0; for (i = 0; i < counter; i++) { bson *bp = (bson *) malloc(sizeof(bson)); bson_init(bp); bson_append_new_oid(bp, "_id"); if (fields == NULL || strstr(fields, "req_ip") != NULL) bson_append_string(bp, "req_ip", p[i].req_ip); if (fields == NULL || strstr(fields, "req_ident") != NULL) bson_append_string(bp, "req_ident", p[i].req_ident); if (fields == NULL || strstr(fields, "req_user") != NULL) bson_append_string(bp, "req_user", p[i].req_user); if (fields == NULL || strstr(fields, "req_datetime") != NULL) bson_append_string(bp, "req_datetime", p[i].req_datetime); if (fields == NULL || strstr(fields, "req_method") != NULL) bson_append_string(bp, "req_method", p[i].req_method); if (fields == NULL || strstr(fields, "req_uri") != NULL) bson_append_string(bp, "req_uri", p[i].req_uri); if (fields == NULL || strstr(fields, "req_proto") != NULL) bson_append_string(bp, "req_proto", p[i].req_proto); if (fields == NULL || strstr(fields, "resp_code") != NULL) bson_append_int(bp, "resp_code", p[i].resp_code); if (fields == NULL || strstr(fields, "resp_bytes") != NULL) bson_append_int(bp, "resp_bytes", atoi(p[i].resp_bytes)); if (fields == NULL || strstr(fields, "req_referer") != NULL) bson_append_string(bp, "req_referer", p[i].req_referer); if (fields == NULL || strstr(fields, "req_agent") != NULL) bson_append_string(bp, "req_agent", p[i].req_agent); bson_finish(bp); bps[i] = bp; } mongo_insert_batch(&conn, globalArgs.collection, (const bson **) bps, counter, 0, MONGO_CONTINUE_ON_ERROR); mongo_destroy(&conn); int j = 0; for (j = 0; j < counter; j++) { bson_destroy(bps[j]); free(bps[j]); } free(bps); return (0); }
/** * \brief This function tries to connect to MongoDB server */ int vs_mongo_conn_init(struct VS_CTX *vs_ctx) { int status; vs_ctx->mongo_conn = mongo_alloc(); mongo_init(vs_ctx->mongo_conn); /* Set connection timeout */ mongo_set_op_timeout(vs_ctx->mongo_conn, 1000); /* Connect to MongoDB server */ status = mongo_client(vs_ctx->mongo_conn, vs_ctx->mongodb_server, vs_ctx->mongodb_port); if( status != MONGO_OK ) { switch ( vs_ctx->mongo_conn->err ) { case MONGO_CONN_NO_SOCKET: v_print_log(VRS_PRINT_ERROR, "No MongoDB server %s:%d socket\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); break; case MONGO_CONN_FAIL: v_print_log(VRS_PRINT_ERROR, "Connection to MongoDB server %s:%d failed\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); break; case MONGO_CONN_NOT_MASTER: v_print_log(VRS_PRINT_ERROR, "MongoDB server %s:%d is not master\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); break; default: v_print_log(VRS_PRINT_ERROR, "Failed to connect to MongoDB server %s:%d , error: %d\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port, vs_ctx->mongo_conn->err); break; } mongo_dealloc(vs_ctx->mongo_conn); vs_ctx->mongo_conn = NULL; return 0; } v_print_log(VRS_PRINT_DEBUG_MSG, "Connection to MongoDB server %s:%d succeeded\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); /* There has to be some db name defined */ if(vs_ctx->mongodb_db_name == NULL) { v_print_log(VRS_PRINT_ERROR, "No database name defined\n"); mongo_dealloc(vs_ctx->mongo_conn); vs_ctx->mongo_conn = NULL; return 0; } /* Try to do when authentication is configured */ if(vs_ctx->mongodb_user != NULL && vs_ctx->mongodb_pass != NULL) { status = mongo_cmd_authenticate(vs_ctx->mongo_conn, vs_ctx->mongodb_db_name, vs_ctx->mongodb_user, vs_ctx->mongodb_pass); if(status != MONGO_OK) { v_print_log(VRS_PRINT_ERROR, "Authentication to %s database failed, error: %s\n", vs_ctx->mongodb_db_name, mongo_get_server_err_string(vs_ctx->mongo_conn)); mongo_dealloc(vs_ctx->mongo_conn); vs_ctx->mongo_conn = NULL; return 0; } v_print_log(VRS_PRINT_DEBUG_MSG, "Authentication to %s database succeeded\n", vs_ctx->mongodb_db_name); } else { v_print_log(VRS_PRINT_DEBUG_MSG, "No MongoDB authentication required\n"); } /* Namespace used for storing nodes */ vs_ctx->mongo_node_ns = (char*)malloc(sizeof(char) * (strlen(vs_ctx->mongodb_db_name) + 6 + 1)); strcpy(vs_ctx->mongo_node_ns, vs_ctx->mongodb_db_name); strcat(vs_ctx->mongo_node_ns, ".nodes"); /* Namespace used for storing tag groups and tags */ vs_ctx->mongo_tg_ns = (char*)malloc(sizeof(char) * (strlen(vs_ctx->mongodb_db_name) + 11 + 1)); strcpy(vs_ctx->mongo_tg_ns, vs_ctx->mongodb_db_name); strcat(vs_ctx->mongo_tg_ns, ".tag_groups"); /* Namespace used for storing layers */ vs_ctx->mongo_layer_ns = (char*)malloc(sizeof(char) * (strlen(vs_ctx->mongodb_db_name) + 7 + 1)); strcpy(vs_ctx->mongo_layer_ns, vs_ctx->mongodb_db_name); strcat(vs_ctx->mongo_layer_ns, ".layers"); return 1; }