Permalink
Browse files

CDRIVER-95 continue_on_error flag

  • Loading branch information...
1 parent 8f3bf8e commit 213f951e64166cc4585437d0bcea79bd4894f246 @banker banker committed May 16, 2012
Showing with 23 additions and 11 deletions.
  1. +6 −2 src/mongo.c
  2. +10 −2 src/mongo.h
  3. +2 −2 test/errors_test.c
  4. +1 −1 test/validate_test.c
  5. +4 −4 test/write_concern_test.c
View
@@ -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;
@@ -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++ ) {
View
@@ -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. */
@@ -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.
View
@@ -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 );
@@ -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;
View
@@ -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 );
@@ -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 );
@@ -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 );

0 comments on commit 213f951

Please sign in to comment.