Skip to content

CDRIVER-3755 Provide an index creation helper #1303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jun 14, 2023
Merged

Conversation

kevinAlbs
Copy link
Collaborator

@kevinAlbs kevinAlbs commented Jun 9, 2023

This PR proposes a new API for creating indexes: mongoc_collection_create_indexes_with_opts.

API for index creation

The Index Management specification includes API for creating, dropping, and listing indexes.
libmongoc includes some API for Index Management:

MONGOC_EXPORT (bool)
mongoc_collection_drop_index (mongoc_collection_t *collection,
                              const char *index_name,
                              bson_error_t *error);
MONGOC_EXPORT (bool)
mongoc_collection_drop_index_with_opts (mongoc_collection_t *collection,
                                        const char *index_name,
                                        const bson_t *opts,
                                        bson_error_t *error);
MONGOC_EXPORT (bool)
mongoc_collection_create_index (mongoc_collection_t *collection,
                                const bson_t *keys,
                                const mongoc_index_opt_t *opt,
                                bson_error_t *error) BSON_GNUC_DEPRECATED;
MONGOC_EXPORT (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)
   BSON_GNUC_DEPRECATED;
MONGOC_EXPORT (bool)
mongoc_collection_ensure_index (mongoc_collection_t *collection,
                                const bson_t *keys,
                                const mongoc_index_opt_t *opt,
                                bson_error_t *error) BSON_GNUC_DEPRECATED;
MONGOC_EXPORT (mongoc_cursor_t *)
mongoc_collection_find_indexes (mongoc_collection_t *collection,
                                bson_error_t *error)
   BSON_GNUC_WARN_UNUSED_RESULT
   BSON_GNUC_DEPRECATED_FOR (mongoc_collection_find_indexes_with_opts);
MONGOC_EXPORT (mongoc_cursor_t *)
mongoc_collection_find_indexes_with_opts (mongoc_collection_t *collection,
                                          const bson_t *opts)
   BSON_GNUC_WARN_UNUSED_RESULT;

There are no non-deprecated functions to create indexes. *_create_index and *_create_index_with_opts are deprecated as of CDRIVER-2096.

Users are suggested to run the createIndexes command with the mongoc_database_write_command_with_opts in Creating Indexes guide.

CDRIVER-3755 notes wrapping drivers are required to do additional validation when the commitQuorum option is passed.

Since libmongoc does not provide an index creation helper, and generic command helpers should not be validating command specific options, this forces the user / wrapping driver to do this validation. In particular, this required additional server selection and wire version checking in the C++ driver's support of commitQuorum (CXX-1998).

This PR proposes adding a mongoc_collection_create_indexes_with_opts to loosely model the createIndexes(models: Iterable<IndexModel>, options: Optional<CreateIndexesOptions>): Iterable<String>; API from the specification.

Accepting bson_t for options.

The specification defines the types IndexOptions and CreateIndexOptions. These options are represented by a bson_t.

An advantage of representing the options with bson_t is forward compatibility. If new options are accepted by future server versions, no driver changes are required. And users do not need to upgrade the driver to use the new options.

Accepting bson_t for command options is consistent with other libmongoc APIs. Example: mongoc_collection_find_with_opts

@kevinAlbs kevinAlbs changed the title CDRIVER-3755 CDRIVER-3755 Provide an index creation helper Jun 9, 2023
@kevinAlbs kevinAlbs marked this pull request as ready for review June 12, 2023 18:50
Copy link
Contributor

@vector-of-bool vector-of-bool left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@eramongodb eramongodb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider also updating the following tests to use the new index helpers:

  • test_drop_index in test-mongoc-client-collection.c
  • _test_unique_index_on_keyaltnames_setup in test-mongoc-client-side-encryption.c
  • test_sample_indexes in test-mongoc-sample-commands.c

#include <stdlib.h>
#include <stdlib.h> // abort

#define FAIL(...) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define FAIL(...) \
#define HANDLE_ERROR(...) \

For purposes of example code in documentation, suggest renaming the macro to be a bit more descriptive given the embedded code has no further context.

Comment on lines 87 to 93
if (!mongoc_collection_drop_index_with_opts (
coll, index_name, NULL /* opts */, &error)) {
bson_free (index_name);
bson_destroy (keys);
FAIL ("Failed to drop index: %s", error.message);
}
printf ("Dropped index\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity of embedded code in documentation, suggest explicitly documenting the "success" and "failure" paths by structure:

if (mongoc_collection_create_indexes_with_opts (
         coll, &im, 1, NULL /* opts */, NULL /* reply */, &error)) {
   printf ("Successfully created index\n");
} else {
   HANDLE_ERROR ("Failed to create index: %s", error.message);
}

Also applies to other branches on success/failure in this example code (explicitly denote both success and failure branches, i.e. with a printf()).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea. Will apply.

@@ -3,23 +3,28 @@
Creating Indexes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this file/page be renamed to something more general such as "Collection Indexes" or "Using Collection Indexes"? It now describes more than just their creation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea.
Updated name from create-indexes.rst to manage-collection-indexes.rst.
Update name of example from example-create-indexes.c to example-manage-collection-indexes.c.


Example
-------
To drop an index, use :symbol:`mongoc_collection_drop_index_with_opts`. The index name may be obtained from the ``keys`` document with :symbol:`mongoc_collection_keys_to_index_string`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update mongoc_collection_drop_index_with_opts page with "See Also" referencing this page and/or the mongoc_collection_keys_to_index_string page.

Also suggest updating documentation page for mongoc_collection_keys_to_index_string to describe (or point to a reference describing) details concerning the keys document and its expected structure. Currently the mongoc_collection_keys_to_index_string just points back to this page without further elaboration (what's keys supposed to look like?).

:end-before: // Create an index ... end
:dedent: 6

To list indexes, use :symbol:`mongoc_collection_find_indexes_with_opts`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update mongoc_collection_find_indexes_with_opts page with "See Also" referencing this page.


MONGOC_EXPORT (bool)
mongoc_collection_create_indexes_with_opts (mongoc_collection_t *collection,
mongoc_index_model_t **models,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mongoc_index_model_t **models,
mongoc_index_model_t *const *models,

Pointers may be const, e.g.:

mongoc_index_model_t *const im = mongoc_index_model_new (keys, NULL);
if (mongoc_collection_create_indexes_with_opts (
         coll, &im, 1, NULL /* opts */, NULL /* reply */, &error)) {
   // ...
}

Comment on lines +11 to +12
/* hex_to_bin parses a hexadecimal string to an array of bytes. `NULL` is
* returned on error. `len` is set to the number of bytes written. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If NULL on error, suggest replacing the BSON_ASSERT in the definition with a return NULL; for consistency.

@kevinAlbs kevinAlbs requested a review from eramongodb June 14, 2023 12:51
@kevinAlbs kevinAlbs merged commit ca45383 into mongodb:master Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants