[CDRIVER-645] Support setting "uri options" explicitly Created: 12/May/15  Updated: 17/Aug/15  Resolved: 13/Jul/15

Status: Closed
Project: C Driver
Component/s: libmongoc
Affects Version/s: 1.2.0
Fix Version/s: 1.2-beta0

Type: Bug Priority: Major - P3
Reporter: Hannes Magnusson Assignee: Hannes Magnusson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to PHPC-229 Support options array in second param... Closed

 Description   

Currently the only way to set many of the options is via long, tedious and annoying URI.

This includes credentials, timeout options and ssl.

It would be nice to be able to set these explicitly via mongoc_client_set_read_pref() and so on



 Comments   
Comment by Githook User [ 13/Jul/15 ]

Author:

{u'username': u'bjori', u'name': u'Hannes Magnusson', u'email': u'bjori@php.net'}

Message: CDRIVER-645: Improve mongoc_uri_*() tests
Branch: 1.2.0-dev
https://github.com/mongodb/mongo-c-driver/commit/59ef964561911d9c565e1294ae057d9dc6b8d736

Comment by Githook User [ 13/Jul/15 ]

Author:

{u'username': u'bjori', u'name': u'Hannes Magnusson', u'email': u'bjori@php.net'}

Message: CDRIVER-645: Use the actual mongoc_uri_t when applicable

Previously the code over-complicated things by reconstructing the uri by
parsing the original uri->str, which is the original value used to
construct it – not the computed values, or subsequently overwritten
values (using mongoc_uri_set_option_as_... for example).

This also removes the now useless _mongoc_client_new ()
Branch: 1.2.0-dev
https://github.com/mongodb/mongo-c-driver/commit/c27743b268043bd7df19dafd0c7fd003b2394639

Comment by Hannes Magnusson [ 09/Jul/15 ]

I don't think 'w' should be in the int32 options.
Its a complex type that has its own explicit way of setting value: mongoc_client_set_write_concern ().
This is inline with having explicit mongoc_client_set_read_prefs () and mongoc_client_set_ssl_opts () as these aren't normal scalar values.

As for mongoc_client_new_from_uri (..), I hadn't considered that this could be used as a "URI builder".
I suppose that is valid usecase – but will there then be no way to get the original uri that was passed?

I think the best fix for that is to adapt mongoc_uri_get_string () to return the "compiled uri".
mongoc_uri_copy () should in that case also be adapted to mean "copy the compiled uri", and should use the mongoc_uri_get_string ().

Sounds like a plan?

Comment by A. Jesse Jiryu Davis [ 09/Jul/15 ]

Let's add "w" to the list of int32 options.

Additionally this fails:

   mongoc_uri_t *uri;
   mongoc_client_t *client;
 
   uri = mongoc_uri_new("mongodb://localhost/?sockettimeoutms=1");
   ASSERT_CMPINT (
      1, ==,
      mongoc_uri_get_option_as_int32 (uri, "sockettimeoutms", 0));
 
   mongoc_uri_set_option_as_int32 (uri, "sockettimeoutms", 2);
   /* this works */
   ASSERT_CMPINT (
      2, ==,
      mongoc_uri_get_option_as_int32 (uri, "sockettimeoutms", 0));
 
   client = mongoc_client_new_from_uri (uri);
 
   /* fails, sockettimeoutms is still 1, from the URI string */
   ASSERT_CMPINT (
      2, ==,
      client->cluster.sockettimeoutms);
 
   mongoc_client_destroy (client);
   mongoc_uri_destroy(uri);

... because mongoc_client_new_from_uri doesn't actually use the parsed options in the uri. It just passes uri->str to mongoc_client_new, which parses the uri again! And forgets the options we overwrote in the original uri.

We need to swap the relationship: mongoc_client_new (const char *) should make a URI and call mongoc_client_new_from_uri (mongoc_uri_t *), instead of the opposite relationship, which is what we have now.

Comment by Githook User [ 07/Jul/15 ]

Author:

{u'username': u'bjori', u'name': u'Hannes Magnusson', u'email': u'bjori@php.net'}

Message: CDRIVER-645: Support setting "uri options" explicitly

Simplifies and consistentifies fetching uri options

Created & added -private API for:

  • mongoc_uri_option_is_[int32|bool|utf8]()
  • mongoc_uri_[s|g]et_option_as_[int32|bool|utf8]()
  • mongoc_uri_set_username()
  • mongoc_uri_set_password()
  • mongoc_uri_set_database() (needed because of mongoc_client_get_default_database())
  • mongoc_uri_set_auth_source()

Updated the existing URI accessing code to use the new functions rather then
repeating the same logic all over the place.

Only expose this to internal API (e.g. wrapping libs/drivers) as this
isn't something a native user of the cdriver should use – it however
highly convenient for libs

Also added mongoc_uri_append_host() to the -private API.
Branch: 1.2.0-dev
https://github.com/mongodb/mongo-c-driver/commit/a6df8f7a3fedb204489c76c2219b4a32f1a62148

Comment by Hannes Magnusson [ 11/Jun/15 ]

https://github.com/mongodb/mongo-c-driver/pull/243

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