-
Notifications
You must be signed in to change notification settings - Fork 455
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
Conversation
Includes the caller's file and line number on error.
- return NULL if `hex` is NULL - set `len` to 0 on error This fixes a possible crash if LOCAL_MASTERKEY is not set in the example.
…n_with_encryptedFields`
`strlen` was called was before `to_encrypt.value.v_utf8.str` was assigned. This resulted in a `strlen` call with `NULL` argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Co-authored-by: vector-of-bool <[email protected]>
There was a problem hiding this 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.ctest_sample_indexes
in test-mongoc-sample-commands.c
#include <stdlib.h> | ||
#include <stdlib.h> // abort | ||
|
||
#define FAIL(...) \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#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.
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"); |
There was a problem hiding this comment.
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()
).
There was a problem hiding this comment.
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.
src/libmongoc/doc/create-indexes.rst
Outdated
@@ -3,23 +3,28 @@ | |||
Creating Indexes |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
src/libmongoc/doc/create-indexes.rst
Outdated
|
||
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`: |
There was a problem hiding this comment.
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?).
src/libmongoc/doc/create-indexes.rst
Outdated
:end-before: // Create an index ... end | ||
:dedent: 6 | ||
|
||
To list indexes, use :symbol:`mongoc_collection_find_indexes_with_opts`: |
There was a problem hiding this comment.
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.
src/libmongoc/doc/mongoc_collection_create_indexes_with_opts.rst
Outdated
Show resolved
Hide resolved
|
||
MONGOC_EXPORT (bool) | ||
mongoc_collection_create_indexes_with_opts (mongoc_collection_t *collection, | ||
mongoc_index_model_t **models, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) {
// ...
}
/* 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. */ |
There was a problem hiding this comment.
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.
Co-authored-by: Ezra Chung <[email protected]>
Co-authored-by: Ezra Chung <[email protected]>
Co-authored-by: Ezra Chung <[email protected]>
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:
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 themongoc_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.This PR proposes adding a
mongoc_collection_create_indexes_with_opts
to loosely model thecreateIndexes(models: Iterable<IndexModel>, options: Optional<CreateIndexesOptions>): Iterable<String>;
API from the specification.Accepting
bson_t
for options.The specification defines the types
IndexOptions
andCreateIndexOptions
. These options are represented by abson_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