[CDRIVER-2096] Flexible index-creation Created: 23/Mar/17  Updated: 12/Sep/17  Resolved: 10/Aug/17

Status: Closed
Project: C Driver
Component/s: libmongoc
Affects Version/s: None
Fix Version/s: 1.8.0

Type: New Feature Priority: Major - P3
Reporter: David Golden Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by CDRIVER-1493 Flexible index-creation function Closed
Related
is related to CDRIVER-2189 Test maxTimeMS is an accepted option ... Closed

 Description   

mongoc_collection_create_index_with_opts takes a mongoc_index_opt_t parameter. This has the unfortunately consequence that it must be updated as the server changes adds or deprecates index options. It also makes it harder for wrapper drivers to do anything other than implement a similar API.

We'd like a API to just provide a bson_t* for index options and pass it through to the server and make construction of a correct index options document a user responsibility.



 Comments   
Comment by Ramon Fernandez Marina [ 12/Sep/17 ]

Author:

{'username': u'ajdavis', 'name': u'A. Jesse Jiryu Davis', 'email': u'jesse@mongodb.com'}

Message:deprecate createIndexes helpers

CDRIVER-2096 and CDRIVER-1493
Branch:master
https://github.com/mongodb/mongo-c-driver/commit/48750743091175d771ff4aa7b25097657bda828e

Comment by Githook User [ 11/Aug/17 ]

Author:

{'name': 'A. Jesse Jiryu Davis', 'username': 'ajdavis', 'email': 'jesse@mongodb.com'}

Message: add createIndexes guide

CDRIVER-2096 and CDRIVER-1493
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/ff6b44e9c38b783f4238b55916da0a1677bd3b80

Comment by Githook User [ 11/Aug/17 ]

Author:

{'name': 'A. Jesse Jiryu Davis', 'username': 'ajdavis', 'email': 'jesse@mongodb.com'}

Message: add createIndexes example

CDRIVER-2096 and CDRIVER-1493
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/c0a1afbc4a6a5f7454062fda183329dd768f1c3a

Comment by Bernie Hackett [ 24/Mar/17 ]

Oh, I thought it was introduced in 3.0, but I'm probably confusing it with listIndexes.

Comment by A. Jesse Jiryu Davis [ 24/Mar/17 ]

Definitely requires dropping support for 2.4. But "createIndexes" was introduced in 2.6, so we'd be ok?:

https://docs.mongodb.com/manual/reference/command/createIndexes/

Comment by Bernie Hackett [ 24/Mar/17 ]

Dropping the existing helpers requires dropping support for MongoDB 2.4 and 2.6.

Comment by A. Jesse Jiryu Davis [ 24/Mar/17 ]

Here's the current APIs:

bool
mongoc_collection_create_index (mongoc_collection_t *collection,
                                const bson_t *keys,
                                const mongoc_index_opt_t *opt,
                                bson_error_t *error);
 
bool
mongoc_collection_create_index_with_opts (mongoc_collection_t *collection,
                                          const bson_t *keys,
                                          const mongoc_index_opt_t *opt,
                                          const bson_t *opts,
                                          bson_t *reply,
                                          bson_error_t *error);

The "opts" version was created to allow writeConcern and collation: opts are appended to the createIndexes command, not to the index spec itself. So if MongoDB introduces a new index option "foo", there's no way to send a command like this to MongoDB with mongoc_collection_create_index_with_opts:

{ createIndexes: "collection", indexes: [ { key: "theKey", foo: true } ] }

One approach is a third index function like David proposes.

Another approach is to document how users can run "createIndexes" themselves via mongoc_collection_write_command_with_opts, and deprecate the existing family of index-creation functions and types. Users only need help with two things that mongoc_collection_write_command_with_opts doesn't provide: they need help generating the index name from its keys, and creating indexes on MongoDB 2.4. We can expose a new function that helps with the former and our commitment to the latter is ending imminently.

Comment by David Golden [ 24/Mar/17 ]

Hypothetical API (with terrible name):

bool
mongoc_collection_create_passthrough_index_with_opts (mongoc_collection_t *collection,
                                          const bson_t *keys,
                                          const bson_t *index_opts,
                                          const bson_t *opts,
                                          bson_t *reply,
                                          bson_error_t *error)

Conceptually, that might create the index model command like this:

   /*
    * Build our createIndexes command to send to the server.
    */
   BSON_APPEND_UTF8 (&cmd, "createIndexes", collection->collection);
   bson_append_array_begin (&cmd, "indexes", 7, &ar);
   bson_append_document_begin (&ar, "0", 1, &doc);
   BSON_APPEND_DOCUMENT (&doc, "key", keys);
   BSON_APPEND_UTF8 (&doc, "name", name);
   bson_concat(&doc, index_opts);
   bson_append_document_end (&ar, &doc);
   bson_append_array_end (&cmd, &ar);

Instead of like this.

Generated at Wed Feb 07 21:14:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.