Navigation Menu

Skip to content

Commit

Permalink
CDRIVER-95 continue_on_error flag
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed May 16, 2012
1 parent 8f3bf8e commit 213f951
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/mongo.c
Expand Up @@ -850,7 +850,8 @@ MONGO_EXPORT int mongo_insert( mongo *conn, const char *ns,
}

MONGO_EXPORT int mongo_insert_batch( mongo *conn, const char *ns,
const bson **bsons, int count, mongo_write_concern *custom_write_concern ) {
const bson **bsons, int count, mongo_write_concern *custom_write_concern,
int flags ) {

mongo_message *mm;
mongo_write_concern *write_concern = NULL;
Expand Down Expand Up @@ -881,7 +882,10 @@ MONGO_EXPORT int mongo_insert_batch( mongo *conn, const char *ns,
mm = mongo_message_create( size , 0 , 0 , MONGO_OP_INSERT );

data = &mm->data;
data = mongo_data_append32( data, &ZERO );
if( flags & MONGO_CONTINUE_ON_ERROR )
data = mongo_data_append32( data, &ONE );
else
data = mongo_data_append32( data, &ZERO );
data = mongo_data_append( data, ns, strlen( ns ) + 1 );

for( i=0; i<count; i++ ) {
Expand Down
12 changes: 10 additions & 2 deletions src/mongo.h
Expand Up @@ -87,6 +87,10 @@ enum mongo_update_opts {
MONGO_UPDATE_BASIC = 0x4
};

enum mongo_insert_opts {
MONGO_CONTINUE_ON_ERROR = 0x1
};

enum mongo_cursor_opts {
MONGO_TAILABLE = ( 1<<1 ), /**< Create a tailable cursor. */
MONGO_SLAVE_OK = ( 1<<2 ), /**< Allow queries on a non-primary node. */
Expand Down Expand Up @@ -377,12 +381,16 @@ MONGO_EXPORT int mongo_insert( mongo *conn, const char *ns, const bson *data,
* @param num the number of documents in data.
* @param custom_write_concern a write concern object that will
* override any write concern set on the conn object.
* @param flags flags on this batch insert. Currently, this value
* may be 0 or MONGO_CONTINUE_ON_ERROR, which will cause the
* batch insert to continue even if a given insert in the batch fails.
*
* @return MONGO_OK or MONGO_ERROR.
*
*/
MONGO_EXPORT int mongo_insert_batch( mongo *conn , const char *ns ,
const bson **data , int num, mongo_write_concern *custom_write_concern );
MONGO_EXPORT int mongo_insert_batch( mongo *conn, const char *ns,
const bson **data, int num, mongo_write_concern *custom_write_concern,
int flags );

/**
* Update a document in a MongoDB server.
Expand Down
4 changes: 2 additions & 2 deletions test/errors_test.c
Expand Up @@ -117,7 +117,7 @@ int test_namespace_validation_on_insert( void ) {
objs[0] = b;
objs[1] = b2;

ASSERT( mongo_insert_batch( conn, "tet.fo$o", (const bson **)objs, 2, NULL ) == MONGO_ERROR );
ASSERT( mongo_insert_batch( conn, "tet.fo$o", (const bson **)objs, 2, NULL, 0 ) == MONGO_ERROR );
ASSERT( conn->err == MONGO_NS_INVALID );
ASSERT( strncmp( conn->errstr, "Collection may not contain '$'", 29 ) == 0 );

Expand Down Expand Up @@ -169,7 +169,7 @@ int test_insert_limits( void ) {
objs[0] = b;
objs[1] = b2;

ASSERT( mongo_insert_batch( conn, "test.foo", (const bson **)objs, 2, NULL ) == MONGO_ERROR );
ASSERT( mongo_insert_batch( conn, "test.foo", (const bson **)objs, 2, NULL, 0 ) == MONGO_ERROR );
ASSERT( conn->err == MONGO_BSON_TOO_LARGE );

return 0;
Expand Down
2 changes: 1 addition & 1 deletion test/validate_test.c
Expand Up @@ -109,7 +109,7 @@ int main() {
for ( j=0; j < BATCH_SIZE; j++ )
make_small_invalid( &bs[j], i );

result = mongo_insert_batch( conn, ns, (const bson **)bp, BATCH_SIZE, NULL );
result = mongo_insert_batch( conn, ns, (const bson **)bp, BATCH_SIZE, NULL, 0 );
ASSERT( result == MONGO_ERROR );
ASSERT( conn->err == MONGO_BSON_INVALID );

Expand Down
8 changes: 4 additions & 4 deletions test/write_concern_test.c
Expand Up @@ -43,7 +43,7 @@ void test_update_and_remove( mongo *conn ) {
}

ASSERT( mongo_insert_batch( conn, "test.wc", (const bson **)objs, 5,
NULL ) == MONGO_OK );
NULL, 0 ) == MONGO_OK );

ASSERT( mongo_count( conn, "test", "wc", bson_empty( &empty ) ) == 5 );

Expand Down Expand Up @@ -188,19 +188,19 @@ void test_insert( mongo *conn ) {
/* Insert two new documents by insert_batch. */
conn->write_concern = NULL;
ASSERT( mongo_count( conn, TEST_DB, TEST_COL, bson_empty( empty ) ) == 1 );
ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 2, NULL ) == MONGO_OK );
ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 2, NULL, 0 ) == MONGO_OK );
ASSERT( mongo_count( conn, TEST_DB, TEST_COL, bson_empty( empty ) ) == 3 );

/* 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 ) == MONGO_ERROR );
ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 2, wc, 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" );
ASSERT( conn->lasterrcode == 11000 );

/* But it will succeed without the write concern set. */
ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 2, NULL ) == MONGO_OK );
ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 2, NULL, 0 ) == MONGO_OK );

bson_destroy( b );
bson_destroy( b2 );
Expand Down

0 comments on commit 213f951

Please sign in to comment.