Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CDRIVER-175 Implement default write acknowledgement with new MongoCli…

…ent class
  • Loading branch information...
commit 20ebaaa86ef18f294a8ecdf28f0f6f6ce0646515 1 parent aee9ff2
@gjmurakami-10gen gjmurakami-10gen authored
View
23 HISTORY.md
@@ -1,5 +1,28 @@
# MongoDB C Driver History
+## 0.7
+2012-11-19
+** API CHANGE **
+
+In version 0.7, mongo_client and mongo_replset_client are the connection functions,
+replacing the deprecated functions mongo_connect and mongo_replset_connect, respectively.
+The mongo_client and mongo_replset_client functions now have a default write concern
+specifying the acknowledgement of writes.
+Please see the Write Concern document for explicit details.
+
+BCON (BSON C Object Notation) provides JSON-like (or BSON-like) initializers in C
+and readable, and maintainable data-driven definition of BSON documents.
+
+Other features and fixes include:
+
+* support for Unix domain sockets
+* three memory leaks in library code
+* proper cursor termination at the end of a set of large documents
+* mongo_get_primary initialization to avoid memory overrun
+* Mac dynamic library linking
+* example.c compilation
+* various other minor fixes since 2012-6-28
+
## 0.6
2012-6-3
** API CHANGE **
View
2  Makefile
@@ -30,7 +30,7 @@ BSON_LIBNAME=libbson
ENV?=posix
# TODO: add replica set test, cpp test, platform tests, json_test
-TESTS=test_auth test_bcon test_bson test_bson_subobject test_count_delete \
+TESTS=test_auth test_bcon test_bson test_bson_subobject test_connect test_count_delete \
test_cursors test_endian_swap test_errors test_examples \
test_functions test_gridfs test_helpers \
test_oid test_resize test_simple test_sizes test_update \
View
2  docs/examples/example.c
@@ -74,7 +74,7 @@ int main() {
bson_print( &sub );
/* Now make a connection to MongoDB. */
- if( mongo_connect( &conn, "127.0.0.1", 27017 ) != MONGO_OK ) {
+ if( mongo_client( &conn, "127.0.0.1", 27017 ) != MONGO_OK ) {
switch( conn.err ) {
case MONGO_CONN_NO_SOCKET:
printf( "FAIL: Could not create a socket!\n" );
View
12 docs/source/sphinx/source/connections.rst
@@ -15,10 +15,10 @@ to the router for a shard cluster (``mongos``).
mongo conn[1];
int result;
- result = mongo_connect( conn, "127.0.0.1", 27017 );
+ result = mongo_client( conn, "127.0.0.1", 27017 );
First we create the ``mongo`` object to manage the connection. Then we connect
-using ``mongo_connect``. If the function returns ``MONGO_OK``, then we've
+using ``mongo_client``. If the function returns ``MONGO_OK``, then we've
successfully connected.
Notice that when specifying the host, we must use dot-decimal notation. If you'd like
@@ -27,12 +27,12 @@ option and ensure that ``_MONGO_USE_GETADDRINFO`` is defined.
The C driver now also supports connecting to mongodb through unix domain
sockets (only on POSIX systems, of course). To connect to a unix domain socket,
-pass the path to the socket in place of the host address to ``mongo_connect``
+pass the path to the socket in place of the host address to ``mongo_client``
and pass a negative number in as the port number. For instance,
.. code-block:: c
- result = mongo_connect( conn, "/tmp/mongodb-27017.sock", -1 );
+ result = mongo_client( conn, "/tmp/mongodb-27017.sock", -1 );
In the event of an error, the result will be ``MONGO_ERROR``. You can then check the error
value by examining the connection's ``err`` field. Continuing:
@@ -87,11 +87,11 @@ Here's how you go about that:
mongo_replset_add_seed( conn, '10.4.3.1', 27017 );
mongo_replset_add_seed( conn, '10.4.3.2', 27017 );
- result = mongo_replset_connect( conn );
+ result = mongo_replset_client( conn );
First we initiaize the connection object, providing the name of the replica set,
in this case, "rs-dc-1." Next, we add two seed nodes. Finally, we connect
-by pass the connection to ``mongo_replset_connect``.
+by pass the connection to ``mongo_replset_client``.
As with the basic connection, we'll want to check for any errors on connect. Notice
that there are two more error conditions we check for:
View
4 docs/source/sphinx/source/errors.rst
@@ -21,7 +21,7 @@ A good example of an operating system error is a connection failure.
mongo conn[1];
- if ( mongo_connect( conn, "foo.example.com", 27017 ) == MONGO_ERROR ) {
+ if ( mongo_client( conn, "foo.example.com", 27017 ) == MONGO_ERROR ) {
printf( "mongo_error_t: %d\n", conn->err );
printf( "errno (or WSAGetLastError() on Windows: %d\n", conn->errcode );
printf( "Error string: %s\n", conn->errstr );
@@ -53,7 +53,7 @@ We can force this sort of error by trying to run an invalid command:
mongo conn[1];
int res;
- if( mongo_connect( conn, "foo.example.com", 27017 ) == MONGO_ERROR ) {
+ if( mongo_client( conn, "foo.example.com", 27017 ) == MONGO_ERROR ) {
exit( 1 );
}
View
8 docs/source/sphinx/source/tutorial.rst
@@ -32,12 +32,12 @@ Next, initialize it:
mongo_init( &conn );
-Set any optional values, like a timeout, and then call ``mongo_connect``:
+Set any optional values, like a timeout, and then call ``mongo_client``:
.. code-block:: c
mongo_set_op_timeout( &conn, 1000 );
- mongo_connect( &conn, "127.0.0.1", 27017 );
+ mongo_client( &conn, "127.0.0.1", 27017 );
When you're finished, destroy the mongo object:
@@ -60,7 +60,7 @@ Let's start by that connects to the database:
int main() {
mongo conn[1];
- int status = mongo_connect( conn, "127.0.0.1", 27017 );
+ int status = mongo_client( conn, "127.0.0.1", 27017 );
if( status != MONGO_OK ) {
switch ( conn->err ) {
@@ -107,7 +107,7 @@ then you add seed nodes, and finally you connect. Here's an example:
mongo_replset_add_seed( conn, "10.4.3.22", 27017 );
mongo_replset_add_seed( conn, "10.4.3.32", 27017 );
- status = mongo_replset_connect( conn );
+ status = mongo_replset_client( conn );
if( status != MONGO_OK ) {
/* Check conn->err for error code. */
View
33 docs/source/sphinx/source/write_concern.rst
@@ -1,25 +1,28 @@
-Write Concern (a.k.a. "Safe Mode")
-==================================
+Write Concern
+=============
-All writes issued from the drivers for MongoDB are "fire-and-forget" by default.
-In practice, this means that by default, failed writes aren't reported.
-For this reason, "fire-and-forget" writes are recommended
-only for cases where losing a few writes is acceptable (logging, anayltics, etc.).
-
-In all other scenarios, you should ensure that your writes run as a round trip
-to the server. This requires that you enable write concern or "safe mode", as it's
-called elsewhere.
+The ``mongo_client`` and ``mongo_replset_client`` functions set a default write concern
+at the top level specifying acknowledgements for writes.
In addition to reporting write errors, write concern also allows you to ensure
that your write are replicated to a particular number of servers to a set
-of servers tagged with a given value. See the
-`write concern docs <http://www.mongodb.org/display/DOCS/getLastError+Command>`_ for details.
+of servers tagged with a given value.
+
+Or for very-high write performance, write concern can specify non-blocking writes
+without acknowledgment.
+This is recommended only for cases that can tolerate the potential loss of a few writes
+such as logging, analytics, etc.
+
+The old ``mongo_connect`` and ``mongo_replset_connect`` functions have a default write concern
+that is non-blocking without write acknowledgement.
+They are deprecated and temporarily available for backward compatibility to smooth transition to
+the ``mongo_client`` and ``mongo_replset_client`` functions.
Implementation and API
----------------------
Write concern is implemented by appending a call to the ``getlasterror``
-command after each write. You can certainly do this manually, but nearly all of the drivers
+command after each write. You can certainly do this manually, but nearly all of the drivers
provide a write concern API for simplicty. To read about the options for ``getlasterror``,
and hence the options for write concern,
`see the MongoDB getlasterror docs <http://www.mongodb.org/display/DOCS/getLastError+Command>`_.
@@ -52,7 +55,7 @@ Example
mongo_write_concern write_concern[1];
bson b[1];
- if( mongo_connect( conn, "127.0.0.1", 27017 ) == MONGO_ERROR ) {
+ if( mongo_client( conn, "127.0.0.1", 27017 ) == MONGO_ERROR ) {
printf( "Failed to connect!\n" );
exit(1);
}
@@ -82,7 +85,7 @@ Example
/* We'll get the same error if we set a default write concern
on the connection object but don't set it on insert.*/
mongo_set_write_concern( conn, write_concern );
- ASSERT( mongo_insert( conn, "test.foo", b, write_concern ) == MONGO_ERROR );
+ ASSERT( mongo_insert( conn, "test.foo", b, 0 ) == MONGO_ERROR );
ASSERT( conn->err == MONGO_WRITE_ERROR );
printf( "Error message: %s\n", conn->lasterrstr );
View
30 src/mongo.c
@@ -397,12 +397,16 @@ MONGO_EXPORT void mongo_init_sockets( void ) {
mongo_env_sock_init();
}
+static mongo_write_concern DEFAULT_WRITE_CONCERN = { 1, 0, 0, 0, 0, 0 }; /* w = 1 */
+
MONGO_EXPORT void mongo_init( mongo *conn ) {
memset( conn, 0, sizeof( mongo ) );
conn->max_bson_size = MONGO_DEFAULT_MAX_BSON_SIZE;
+ mongo_write_concern_finish( &DEFAULT_WRITE_CONCERN ); /* TODO fix bson cmd leak */
+ mongo_set_write_concern( conn, &DEFAULT_WRITE_CONCERN );
}
-MONGO_EXPORT int mongo_connect( mongo *conn , const char *host, int port ) {
+MONGO_EXPORT int mongo_client( mongo *conn , const char *host, int port ) {
mongo_init( conn );
conn->primary = bson_malloc( sizeof( mongo_host_port ) );
@@ -419,6 +423,14 @@ MONGO_EXPORT int mongo_connect( mongo *conn , const char *host, int port ) {
return MONGO_OK;
}
+MONGO_EXPORT int mongo_connect( mongo *conn , const char *host, int port ) {
+ int ret;
+ fprintf(stderr, "WARNING: mongo_connect() is deprecated, please use mongo_client()\n");
+ ret = mongo_client( conn, host, port );
+ mongo_set_write_concern( conn, 0 );
+ return ret;
+}
+
MONGO_EXPORT void mongo_replset_init( mongo *conn, const char *name ) {
mongo_init( conn );
@@ -580,7 +592,7 @@ static int mongo_replset_check_host( mongo *conn ) {
return MONGO_OK;
}
-MONGO_EXPORT int mongo_replset_connect( mongo *conn ) {
+MONGO_EXPORT int mongo_replset_client( mongo *conn ) {
int res = 0;
mongo_host_port *node;
@@ -640,6 +652,14 @@ MONGO_EXPORT int mongo_replset_connect( mongo *conn ) {
return MONGO_ERROR;
}
+MONGO_EXPORT int mongo_replset_connect( mongo *conn ) {
+ int ret;
+ fprintf(stderr, "WARNING: mongo_replset_connect() is deprecated, please use mongo_replset_client()\n");
+ ret = mongo_replset_client( conn );
+ mongo_set_write_concern( conn, 0 );
+ return ret;
+}
+
MONGO_EXPORT int mongo_set_op_timeout( mongo *conn, int millis ) {
conn->op_timeout_ms = millis;
if( conn->sock && conn->connected )
@@ -794,7 +814,9 @@ static int mongo_choose_write_concern( mongo *conn,
else if( conn->write_concern ) {
*write_concern = conn->write_concern;
}
-
+ if ( *write_concern && (*write_concern)->w < 1 ) {
+ *write_concern = 0; /* do not generate getLastError request */
+ }
if( *write_concern && !((*write_concern)->cmd) ) {
__mongo_set_error( conn, MONGO_WRITE_CONCERN_INVALID,
"Must call mongo_write_concern_finish() before using *write_concern.", 0 );
@@ -1037,7 +1059,7 @@ MONGO_EXPORT int mongo_write_concern_finish( mongo_write_concern *write_concern
bson_append_string( command, "w", write_concern->mode );
}
- else if( write_concern->w ) {
+ else if( write_concern->w && write_concern->w > 1 ) {
bson_append_int( command, "w", write_concern->w );
}
View
25 src/mongo.h
@@ -223,6 +223,18 @@ MONGO_EXPORT void mongo_init( mongo *conn );
* @return MONGO_OK or MONGO_ERROR on failure. On failure, a constant of type
* mongo_error_t will be set on the conn->err field.
*/
+MONGO_EXPORT int mongo_client( mongo *conn , const char *host, int port );
+
+/**
+ * DEPRECATED. Connect to a single MongoDB server.
+ *
+ * @param conn a mongo object.
+ * @param host a numerical network address or a network hostname.
+ * @param port the port to connect to.
+ *
+ * @return MONGO_OK or MONGO_ERROR on failure. On failure, a constant of type
+ * mongo_error_t will be set on the conn->err field.
+ */
MONGO_EXPORT int mongo_connect( mongo *conn , const char *host, int port );
/**
@@ -276,6 +288,19 @@ MONGO_EXPORT int mongo_validate_ns( mongo *conn, const char *ns );
* @return MONGO_OK or MONGO_ERROR on failure. On failure, a constant of type
* mongo_conn_return_t will be set on the conn->err field.
*/
+MONGO_EXPORT int mongo_replset_client( mongo *conn );
+
+/**
+ * DEPRECATED. Connect to a replica set.
+ *
+ * Before passing a connection object to this function, you must already have called
+ * mongo_set_replset and mongo_replset_add_seed.
+ *
+ * @param conn a mongo object.
+ *
+ * @return MONGO_OK or MONGO_ERROR on failure. On failure, a constant of type
+ * mongo_conn_return_t will be set on the conn->err field.
+ */
MONGO_EXPORT int mongo_replset_connect( mongo *conn );
/** Set a timeout for operations on this connection. This
View
2  test/auth_test.c
@@ -12,7 +12,7 @@ int main() {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn , TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn , TEST_SERVER, 27017 ) ) {
printf( "failed to connect\n" );
exit( 1 );
}
View
2  test/benchmark_test.c
@@ -391,7 +391,7 @@ static void clean( void ) {
int main() {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn, TEST_SERVER, 27017 ) != MONGO_OK ) {
+ if ( mongo_client( conn, TEST_SERVER, 27017 ) != MONGO_OK ) {
printf( "failed to connect\n" );
exit( 1 );
}
View
2  test/commands_test.c
@@ -18,7 +18,7 @@ int main() {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn , TEST_SERVER , 27017 ) ) {
+ if ( mongo_client( conn , TEST_SERVER , 27017 ) ) {
printf( "failed to connect\n" );
exit( 1 );
}
View
27 test/connect_test.c
@@ -0,0 +1,27 @@
+/* connect_test.c */
+/* this file should be removed when mongo_connect changes from deprecated to eliminated */
+
+#include "test.h"
+#include "mongo.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+int main() {
+ mongo conn[1];
+ char version[10];
+
+ INIT_SOCKETS_FOR_WINDOWS;
+
+ if( mongo_connect( conn, TEST_SERVER, 27017 ) != MONGO_OK ) {
+ printf( "failed to connect\n" );
+ exit( 1 );
+ }
+
+ /* mongo_connect should print a warning to stderr that it is deprecated */
+
+ ASSERT( conn->write_concern == (void*)0 ); /* write_concern should be 0 for backwards compatibility */
+
+ mongo_destroy( conn );
+ return 0;
+}
View
2  test/count_delete_test.c
@@ -17,7 +17,7 @@ int main() {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn , TEST_SERVER , 27017 ) ) {
+ if ( mongo_client( conn , TEST_SERVER , 27017 ) ) {
printf( "failed to connect\n" );
exit( 1 );
}
View
2  test/cpptest.cpp
@@ -14,7 +14,7 @@ int main(){
INIT_SOCKETS_FOR_WINDOWS;
- if (mongo_connect( conn, TEST_SERVER, 27017 )){
+ if (mongo_client( conn, TEST_SERVER, 27017 )){
cout << "failed to connect" << endl;
return 1;
}
View
2  test/cursors_test.c
@@ -188,7 +188,7 @@ int main() {
INIT_SOCKETS_FOR_WINDOWS;
- if( mongo_connect( conn, TEST_SERVER, 27017 ) != MONGO_OK ) {
+ if( mongo_client( conn, TEST_SERVER, 27017 ) != MONGO_OK ) {
printf( "Failed to connect" );
exit( 1 );
}
View
4 test/env_posix_test.c
@@ -17,7 +17,7 @@ int test_read_timeout( void ) {
bson b, obj, out, fields;
int res;
- if ( mongo_connect( conn, TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn, TEST_SERVER, 27017 ) ) {
printf( "failed to connect\n" );
exit( 1 );
}
@@ -56,7 +56,7 @@ int test_getaddrinfo( void ) {
bson b[1];
char *ns = "test.foo";
- if( mongo_connect( conn, "localhost", 27017 ) != MONGO_OK ) {
+ if( mongo_client( conn, "localhost", 27017 ) != MONGO_OK ) {
printf( "failed to connect\n" );
exit( 1 );
}
View
6 test/env_win32_test.c
@@ -25,7 +25,7 @@ int test_read_timeout( void ) {
bson b, obj, out, fields;
int res;
- if ( mongo_connect( conn, TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn, TEST_SERVER, 27017 ) ) {
printf( "failed to connect\n" );
exit( 1 );
}
@@ -63,7 +63,7 @@ int test_getaddrinfo( void ) {
const char *ns = "test.foo";
const char *errmsg = "getaddrinfo failed";
- if( mongo_connect( conn, "badhost", 27017 ) == MONGO_OK ) {
+ if( mongo_client( conn, "badhost", 27017 ) == MONGO_OK ) {
printf( "connected to bad host!\n" );
exit( 1 );
} else {
@@ -71,7 +71,7 @@ int test_getaddrinfo( void ) {
}
- if( mongo_connect( conn, "localhost", 27017 ) != MONGO_OK ) {
+ if( mongo_client( conn, "localhost", 27017 ) != MONGO_OK ) {
printf( "failed to connect\n" );
exit( 1 );
}
View
6 test/errors_test.c
@@ -96,7 +96,7 @@ int test_namespace_validation_on_insert( void ) {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn , TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn , TEST_SERVER, 27017 ) ) {
printf( "failed to connect\n" );
exit( 1 );
}
@@ -145,7 +145,7 @@ int test_insert_limits( void ) {
if( mongo_get_server_version( version ) != -1 && version[0] <= '1' )
return 0;
- if ( mongo_connect( conn , TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn , TEST_SERVER, 27017 ) ) {
printf( "failed to connect\n" );
exit( 1 );
}
@@ -191,7 +191,7 @@ int test_get_last_error_commands( void ) {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn , TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn , TEST_SERVER, 27017 ) ) {
printf( "failed to connect\n" );
exit( 1 );
}
View
6 test/gridfs_test.c
@@ -103,7 +103,7 @@ void test_basic( void ) {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn, TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn, TEST_SERVER, 27017 ) ) {
printf( "failed to connect 2\n" );
exit( 1 );
}
@@ -153,7 +153,7 @@ void test_streaming( void ) {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn , TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn , TEST_SERVER, 27017 ) ) {
printf( "failed to connect 3\n" );
exit( 1 );
}
@@ -205,7 +205,7 @@ void test_large( void ) {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn, TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn, TEST_SERVER, 27017 ) ) {
printf( "failed to connect 1\n" );
exit( 1 );
}
View
2  test/helpers_test.c
@@ -47,7 +47,7 @@ int main() {
INIT_SOCKETS_FOR_WINDOWS;
- if( mongo_connect( conn, TEST_SERVER, 27017 ) != MONGO_OK ) {
+ if( mongo_client( conn, TEST_SERVER, 27017 ) != MONGO_OK ) {
printf( "Failed to connect" );
exit( 1 );
}
View
2  test/platform/linux/timeouts.c
@@ -13,7 +13,7 @@ int main() {
bson b;
int res;
- if( mongo_connect( conn, TEST_SERVER, 27017 ) != MONGO_OK ) {
+ if( mongo_client( conn, TEST_SERVER, 27017 ) != MONGO_OK ) {
printf("Failed to connect");
exit(1);
}
View
37 test/replica_set_test.c
@@ -15,7 +15,7 @@
#define REPLICA_SET_NAME "replica-set-foo"
#endif
-int test_connect( const char *set_name ) {
+int test_connect_deprecated( const char *set_name ) {
mongo conn[1];
int res;
@@ -28,6 +28,36 @@ int test_connect( const char *set_name ) {
res = mongo_replset_connect( conn );
+ /* mongo_replset_connect should print a warning to stderr that it is deprecated */
+
+ ASSERT( conn->write_concern == (void*)0 ); /* write_concern should be 0 for backwards compatibility */
+
+ if( res != MONGO_OK ) {
+ res = conn->err;
+ return res;
+ }
+
+ ASSERT( conn->primary->port == SEED_START_PORT ||
+ conn->primary->port == SEED_START_PORT + 1 ||
+ conn->primary->port == SEED_START_PORT + 2 );
+
+ mongo_destroy( conn );
+ return res;
+}
+
+int test_connect( const char *set_name ) {
+
+ mongo conn[1];
+ int res;
+
+ INIT_SOCKETS_FOR_WINDOWS;
+
+ mongo_replset_init( conn, set_name );
+ mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );
+ mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT );
+
+ res = mongo_replset_client( conn );
+
if( res != MONGO_OK ) {
res = conn->err;
return res;
@@ -55,7 +85,7 @@ int test_reconnect( const char *set_name ) {
mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );
- if( ( mongo_replset_connect( conn ) != MONGO_OK ) ) {
+ if( ( mongo_replset_client( conn ) != MONGO_OK ) ) {
mongo_destroy( conn );
return MONGO_ERROR;
} else {
@@ -102,7 +132,7 @@ int test_insert_limits( const char *set_name ) {
mongo_replset_init( conn, set_name );
mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT + 1 );
mongo_replset_add_seed( conn, TEST_SERVER, SEED_START_PORT );
- res = mongo_replset_connect( conn );
+ res = mongo_replset_client( conn );
if( res != MONGO_OK ) {
res = conn->err;
@@ -142,6 +172,7 @@ int test_insert_limits( const char *set_name ) {
}
int main() {
+ ASSERT( test_connect_deprecated( REPLICA_SET_NAME ) == MONGO_OK );
ASSERT( test_connect( REPLICA_SET_NAME ) == MONGO_OK );
ASSERT( test_connect( "test-foobar" ) == MONGO_CONN_BAD_SET_NAME );
ASSERT( test_insert_limits( REPLICA_SET_NAME ) == MONGO_OK );
View
8 test/simple_test.c
@@ -18,13 +18,13 @@ int main() {
const char *col = "c.simple";
const char *ns = "test.c.simple";
- /* mongo_connect( conn, TEST_SERVER, 27017 ); */
+ /* mongo_client( conn, TEST_SERVER, 27017 ); */
/* Simple connect API
mongo conn[1];
mongo_init( conn );
- mongo_connect( conn, TEST_SERVER, 27017 );
+ mongo_client( conn, TEST_SERVER, 27017 );
mongo_destroy( conn );
* Advanced and replica set API
@@ -32,7 +32,7 @@ int main() {
mongo_replset_init( conn, "foobar" );
mongo_set_connect_timeout( conn, 1000 );
- mongo_replset_connect( conn );
+ mongo_replset_client( conn );
mongo_destroy( conn );
* BSON API
@@ -63,7 +63,7 @@ int main() {
INIT_SOCKETS_FOR_WINDOWS;
- if( mongo_connect( conn , TEST_SERVER, 27017 ) != MONGO_OK ) {
+ if( mongo_client( conn , TEST_SERVER, 27017 ) != MONGO_OK ) {
printf( "failed to connect\n" );
exit( 1 );
}
View
2  test/test.h
@@ -36,7 +36,7 @@ int mongo_get_server_version( char *version ) {
bson_iterator it[1];
const char *result;
- mongo_connect( conn, TEST_SERVER, 27017 );
+ mongo_client( conn, TEST_SERVER, 27017 );
bson_init( cmd );
bson_append_int( cmd, "buildinfo", 1 );
View
2  test/unix_socket_test.c
@@ -11,7 +11,7 @@ int main() {
const char *ns = "test.c.unix_socket";
const char *col = "c.unix_socket";
- ASSERT( mongo_connect( conn, sock_path, -1 ) == MONGO_OK );
+ ASSERT( mongo_client( conn, sock_path, -1 ) == MONGO_OK );
mongo_cmd_drop_collection( conn, "test", col, NULL );
View
2  test/update_test.c
@@ -15,7 +15,7 @@ int main() {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn , TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn , TEST_SERVER, 27017 ) ) {
printf( "failed to connect\n" );
exit( 1 );
}
View
2  test/validate_test.c
@@ -34,7 +34,7 @@ int main() {
INIT_SOCKETS_FOR_WINDOWS;
- if ( mongo_connect( conn, TEST_SERVER, 27017 ) ) {
+ if ( mongo_client( conn, TEST_SERVER, 27017 ) ) {
printf( "failed to connect\n" );
exit( 1 );
}
View
51 test/write_concern_test.c
@@ -12,6 +12,19 @@ void create_capped_collection( mongo *conn ) {
mongo_create_capped_collection( conn, "test", "wc", 1000000, 0, NULL );
}
+/*
+ * TODO - test for conflicting options - error return for associated functions as per spec
+ * If conflicting write concern options are passed (w, j=true, fsync=true)
+ * in the URI or as creation parameters to MongoClient an exception must be raised.
+ */
+
+void test_write_concern_finish( void ) {
+ bson_iterator it;
+ mongo_write_concern DEFAULT_WRITE_CONCERN = { 1, 0, 0, 0, 0, 0 }; /* w = 1 */
+ mongo_write_concern_finish( &DEFAULT_WRITE_CONCERN );
+ ASSERT( bson_find( &it, DEFAULT_WRITE_CONCERN.cmd, "w" ) == BSON_EOO ); /* should be { getLastError : 1 } - assert no "w" set */
+}
+
void test_batch_insert_with_continue( mongo *conn ) {
bson *objs[5];
bson *objs2[5];
@@ -182,23 +195,26 @@ void test_write_concern_input( mongo *conn ) {
}
void test_insert( mongo *conn ) {
- mongo_write_concern wc[1];
+ mongo_write_concern wc0[1], wc1[1];
bson b[1], b2[1], b3[1], b4[1], empty[1];
bson *objs[2];
mongo_cmd_drop_collection( conn, TEST_DB, TEST_COL, NULL );
- mongo_write_concern_init( wc );
- wc->w = 1;
- mongo_write_concern_finish( wc );
+ mongo_write_concern_init( wc0 );
+ wc0->w = 0;
+ mongo_write_concern_finish( wc0 );
+ mongo_write_concern_init( wc1 );
+ wc1->w = 1;
+ mongo_write_concern_finish( wc1 );
bson_init( b4 );
bson_append_string( b4, "foo", "bar" );
bson_finish( b4 );
- ASSERT( mongo_insert( conn, TEST_NS, b4, wc ) == MONGO_OK );
+ ASSERT( mongo_insert( conn, TEST_NS, b4, wc1 ) == MONGO_OK );
- ASSERT( mongo_remove( conn, TEST_NS, bson_empty( empty ), wc ) == MONGO_OK );
+ ASSERT( mongo_remove( conn, TEST_NS, bson_empty( empty ), wc1 ) == MONGO_OK );
bson_init( b );
bson_append_new_oid( b, "_id" );
@@ -206,21 +222,21 @@ void test_insert( mongo *conn ) {
ASSERT( mongo_insert( conn, TEST_NS, b, NULL ) == MONGO_OK );
- /* This fails but returns OK because it doesn't use a write concern. */
- ASSERT( mongo_insert( conn, TEST_NS, b, NULL ) == MONGO_OK );
+ /* This fails but returns OK with write concern w = 0 */
+ ASSERT( mongo_insert( conn, TEST_NS, b, wc0 ) == MONGO_OK ); /* no getLastError request */
- ASSERT( mongo_insert( conn, TEST_NS, b, wc ) == MONGO_ERROR );
+ ASSERT( mongo_insert( conn, TEST_NS, b, wc1 ) == MONGO_ERROR );
ASSERT( conn->err == MONGO_WRITE_ERROR );
ASSERT_EQUAL_STRINGS( conn->errstr, "See conn->lasterrstr for details." );
ASSERT_EQUAL_STRINGS( conn->lasterrstr, "E11000 duplicate key error index" );
ASSERT( conn->lasterrcode == 11000 );
mongo_clear_errors( conn );
- /* Still fails but returns OK because it doesn't use a write concern. */
- ASSERT( mongo_insert( conn, TEST_NS, b, NULL ) == MONGO_OK );
+ /* Still fails but returns OK with write concern w = 0 */
+ ASSERT( mongo_insert( conn, TEST_NS, b, wc0 ) == MONGO_OK );
/* But not when we set a default write concern on the conn. */
- mongo_set_write_concern( conn, wc );
+ mongo_set_write_concern( conn, wc1 );
ASSERT( mongo_insert( conn, TEST_NS, b, NULL ) != MONGO_OK );
ASSERT( conn->err == MONGO_WRITE_ERROR );
ASSERT_EQUAL_STRINGS( conn->errstr, "See conn->lasterrstr for details." );
@@ -247,7 +263,7 @@ void test_insert( mongo *conn ) {
/* This should definitely fail if we try again with write concern. */
mongo_clear_errors( conn );
- ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 2, wc, 0 ) == MONGO_ERROR );
+ ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 2, wc1, 0 ) == MONGO_ERROR );
ASSERT( conn->err == MONGO_WRITE_ERROR );
ASSERT_EQUAL_STRINGS( conn->errstr, "See conn->lasterrstr for details." );
ASSERT_EQUAL_STRINGS( conn->lasterrstr, "E11000 duplicate key error index" );
@@ -260,7 +276,8 @@ void test_insert( mongo *conn ) {
bson_destroy( b2 );
bson_destroy( b3 );
bson_destroy( b4 );
- mongo_write_concern_destroy( wc );
+ mongo_write_concern_destroy( wc0 );
+ mongo_write_concern_destroy( wc1 );
}
int main() {
@@ -269,11 +286,15 @@ int main() {
INIT_SOCKETS_FOR_WINDOWS;
- if( mongo_connect( conn, TEST_SERVER, 27017 ) != MONGO_OK ) {
+ 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 );
Please sign in to comment.
Something went wrong with that request. Please try again.