Permalink
Browse files

CDRIVER-192 alllow optional index name - index name should follow con…

…vention (API change)
  • Loading branch information...
1 parent 8a44df7 commit a3e00d82a894196e96d0326527cbe2c82520cbef @gjmurakami-10gen gjmurakami-10gen committed Jan 18, 2013
Showing with 33 additions and 21 deletions.
  1. +2 −2 docs/source/sphinx/source/tutorial.rst
  2. +2 −2 src/gridfs.c
  3. +19 −10 src/mongo.c
  4. +4 −3 src/mongo.h
  5. +6 −4 test/helpers_test.c
@@ -373,7 +373,7 @@ the second is a compound index on ``name`` and ``age``.
bson_append_int( key, "name", 1 );
bson_finish( key );
- mongo_create_index( conn, "tutorial.persons", key, 0, NULL );
+ mongo_create_index( conn, "tutorial.persons", key, NULL, 0, NULL );
bson_destroy( key );
@@ -384,7 +384,7 @@ the second is a compound index on ``name`` and ``age``.
bson_append_int( key, "name", 1 );
bson_finish( key );
- mongo_create_index( conn, "tutorial.persons", key, 0, NULL );
+ mongo_create_index( conn, "tutorial.persons", key, NULL, 0, NULL );
bson_destroy( key );
View
@@ -97,7 +97,7 @@ int gridfs_init( mongo *client, const char *dbname, const char *prefix,
bson_append_int( &b, "filename", 1 );
bson_finish( &b );
options = 0;
- success = ( mongo_create_index( gfs->client, gfs->files_ns, &b, options, NULL ) == MONGO_OK );
+ success = ( mongo_create_index( gfs->client, gfs->files_ns, &b, NULL, options, NULL ) == MONGO_OK );
bson_destroy( &b );
if ( !success ) {
bson_free( ( char * )gfs->dbname );
@@ -112,7 +112,7 @@ int gridfs_init( mongo *client, const char *dbname, const char *prefix,
bson_append_int( &b, "n", 1 );
bson_finish( &b );
options = MONGO_INDEX_UNIQUE;
- success = ( mongo_create_index( gfs->client, gfs->chunks_ns, &b, options, NULL ) == MONGO_OK );
+ success = ( mongo_create_index( gfs->client, gfs->chunks_ns, &b, NULL, options, NULL ) == MONGO_OK );
bson_destroy( &b );
if ( !success ) {
bson_free( ( char * )gfs->dbname );
View
@@ -1445,24 +1445,33 @@ MONGO_EXPORT int mongo_cursor_destroy( mongo_cursor *cursor ) {
/* MongoDB Helper Functions */
-MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns, const bson *key, int options, bson *out ) {
+#define INDEX_NAME_BUFFER_SIZE 255
+#define INDEX_NAME_MAX_LENGTH (INDEX_NAME_BUFFER_SIZE - 1)
+
+MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns, const bson *key, const char *name, int options, bson *out ) {
bson b;
bson_iterator it;
- char name[255] = {'_'};
- int i = 1;
+ char default_name[INDEX_NAME_BUFFER_SIZE] = {'\0'};
+ size_t len = 0;
+ size_t remaining;
char idxns[1024];
- bson_iterator_init( &it, key );
- while( i < 255 && bson_iterator_next( &it ) ) {
- strncpy( name + i, bson_iterator_key( &it ), 255 - i );
- i += strlen( bson_iterator_key( &it ) );
+ if ( !name ) {
+ bson_iterator_init( &it, key );
+ while( len < INDEX_NAME_MAX_LENGTH && bson_iterator_next( &it ) ) {
+ remaining = INDEX_NAME_MAX_LENGTH - len;
+ strncat( default_name, bson_iterator_key( &it ), remaining );
+ len = strlen( default_name );
+ remaining = INDEX_NAME_MAX_LENGTH - len;
+ strncat( default_name, ( bson_iterator_int( &it ) < 0 ) ? "_-1" : "_1", remaining );
+ len = strlen( default_name );
+ }
}
- name[254] = '\0';
bson_init( &b );
bson_append_bson( &b, "key", key );
bson_append_string( &b, "ns", ns );
- bson_append_string( &b, "name", name );
+ bson_append_string( &b, "name", name ? name : default_name );
if ( options & MONGO_INDEX_UNIQUE )
bson_append_bool( &b, "unique", 1 );
if ( options & MONGO_INDEX_DROP_DUPS )
@@ -1490,7 +1499,7 @@ MONGO_EXPORT bson_bool_t mongo_create_simple_index( mongo *conn, const char *ns,
bson_append_int( &b, field, 1 );
bson_finish( &b );
- success = mongo_create_index( conn, ns, &b, options, out );
+ success = mongo_create_index( conn, ns, &b, NULL, options, out );
bson_destroy( &b );
return success;
}
View
@@ -657,16 +657,17 @@ MONGO_EXPORT double mongo_count( mongo *conn, const char *db, const char *coll,
*
* @param conn a mongo object.
* @param ns the namespace.
- * @param data the bson index data.
+ * @param key the bson index key.
+ * @param name the optional name, use NULL to generate a default name.
* @param options a bitfield for setting index options. Possibilities include
* MONGO_INDEX_UNIQUE, MONGO_INDEX_DROP_DUPS, MONGO_INDEX_BACKGROUND,
* and MONGO_INDEX_SPARSE.
* @param out a bson document containing errors, if any.
*
* @return MONGO_OK if index is created successfully; otherwise, MONGO_ERROR.
*/
-MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns,
- const bson *key, int options, bson *out );
+MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns, const bson *key,
+ const char *name, int options, bson *out );
/**
* Create a capped collection.
View
@@ -8,27 +8,29 @@
#include <time.h>
void test_index_helper( mongo *conn ) {
+ int ret;
bson b, out;
bson_iterator it;
bson_init( &b );
- bson_append_int( &b, "foo", 1 );
+ bson_append_int( &b, "foo", -1 );
bson_finish( &b );
- mongo_create_index( conn, "test.bar", &b, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, &out );
+ mongo_create_index( conn, "test.bar", &b, NULL, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, &out );
bson_destroy( &b );
bson_destroy( &out );
bson_init( &b );
bson_append_start_object( &b, "key" );
- bson_append_int( &b, "foo", 1 );
+ bson_append_int( &b, "foo", -1 );
bson_append_finish_object( &b );
bson_finish( &b );
- mongo_find_one( conn, "test.system.indexes", &b, NULL, &out );
+ ret = mongo_find_one( conn, "test.system.indexes", &b, NULL, &out );
+ ASSERT( ret == MONGO_OK );
bson_print( &out );

0 comments on commit a3e00d8

Please sign in to comment.