Skip to content

Commit 95408ed

Browse files
committed
fix pre-opmsg command construction for auth
1 parent 2c7355d commit 95408ed

File tree

2 files changed

+44
-79
lines changed

2 files changed

+44
-79
lines changed

src/libmongoc/examples/example-client.c

Lines changed: 42 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,104 +7,69 @@
77
#include <stdio.h>
88
#include <stdlib.h>
99

10-
static void
11-
command_started (const mongoc_apm_command_started_t *event)
10+
int
11+
main (int argc, char *argv[])
1212
{
13-
char *s;
14-
15-
s = bson_as_relaxed_extended_json (
16-
mongoc_apm_command_started_get_command (event), NULL);
17-
printf ("Command %s started on %s:\n%s\n\n",
18-
mongoc_apm_command_started_get_command_name (event),
19-
mongoc_apm_command_started_get_host (event)->host,
20-
s);
21-
22-
bson_free (s);
23-
}
24-
25-
static void example_listcollections (mongoc_client_t *client) {
26-
mongoc_database_t *db;
13+
mongoc_client_t *client;
14+
mongoc_collection_t *collection;
2715
mongoc_cursor_t *cursor;
16+
bson_error_t error;
2817
const bson_t *doc;
29-
bson_t* opts = BCON_NEW ("cursor", "{", "batchSize", BCON_INT32(1), "}");
18+
const char *collection_name = "test";
19+
bson_t query;
3020
char *str;
31-
bson_error_t error;
21+
const char *uri_string = "mongodb://127.0.0.1/?appname=client-example";
22+
mongoc_uri_t *uri;
3223

33-
MONGOC_DEBUG ("Example listCollections");
34-
MONGOC_DEBUG ("listCollections with the option {cursor: {batchSize: 1}}");
35-
db = mongoc_client_get_database(client, "test");
36-
cursor = mongoc_database_find_collections_with_opts (db, opts);
24+
mongoc_init ();
25+
if (argc > 1) {
26+
uri_string = argv[1];
27+
}
3728

38-
MONGOC_DEBUG ("listCollections results:");
39-
while (mongoc_cursor_next (cursor, &doc)) {
40-
str = bson_as_canonical_extended_json (doc, NULL);
41-
fprintf (stdout, "- %s\n", str);
42-
bson_free (str);
29+
if (argc > 2) {
30+
collection_name = argv[2];
4331
}
4432

45-
if (mongoc_cursor_error (cursor, &error)) {
46-
fprintf (stderr, "listCollections error: %s\n", error.message);
47-
return;
33+
uri = mongoc_uri_new_with_error (uri_string, &error);
34+
if (!uri) {
35+
fprintf (stderr,
36+
"failed to parse URI: %s\n"
37+
"error message: %s\n",
38+
uri_string,
39+
error.message);
40+
return EXIT_FAILURE;
4841
}
49-
mongoc_cursor_destroy (cursor);
50-
mongoc_database_destroy (db);
51-
bson_destroy (opts);
52-
}
5342

54-
static void example_listindexes (mongoc_client_t *client) {
55-
mongoc_collection_t *coll;
56-
mongoc_cursor_t *cursor;
57-
const bson_t *doc;
58-
bson_t* opts = BCON_NEW ("cursor", "{", "batchSize", BCON_INT32(1), "}");
59-
char *str;
60-
bson_error_t error;
43+
client = mongoc_client_new_from_uri (uri);
44+
if (!client) {
45+
return EXIT_FAILURE;
46+
}
6147

62-
MONGOC_DEBUG ("Example listIndexes");
63-
MONGOC_DEBUG ("listIndexes with the option {cursor: {batchSize: 1}}");
64-
coll = mongoc_client_get_collection(client, "test", "test");
65-
cursor = mongoc_collection_find_indexes_with_opts (coll, opts);
48+
mongoc_client_set_error_api (client, 2);
49+
50+
bson_init (&query);
51+
collection = mongoc_client_get_collection (client, "test", collection_name);
52+
cursor = mongoc_collection_find_with_opts (
53+
collection,
54+
&query,
55+
NULL, /* additional options */
56+
NULL); /* read prefs, NULL for default */
6657

67-
MONGOC_DEBUG ("listIndexes results:");
6858
while (mongoc_cursor_next (cursor, &doc)) {
6959
str = bson_as_canonical_extended_json (doc, NULL);
70-
fprintf (stdout, "- %s\n", str);
60+
fprintf (stdout, "%s\n", str);
7161
bson_free (str);
7262
}
7363

7464
if (mongoc_cursor_error (cursor, &error)) {
75-
fprintf (stderr, "listCollections error: %s\n", error.message);
76-
return;
77-
}
78-
mongoc_cursor_destroy (cursor);
79-
mongoc_collection_destroy (coll);
80-
bson_destroy (opts);
81-
}
82-
83-
int
84-
main (int argc, char *argv[])
85-
{
86-
mongoc_client_t *client;
87-
const char *uri_string = "mongodb://127.0.0.1/?appname=client-example";
88-
mongoc_apm_callbacks_t *callbacks;
89-
90-
mongoc_init ();
91-
if (argc > 1) {
92-
uri_string = argv[1];
93-
}
94-
95-
client = mongoc_client_new (uri_string);
96-
if (!client) {
65+
fprintf (stderr, "Cursor Failure: %s\n", error.message);
9766
return EXIT_FAILURE;
9867
}
9968

100-
callbacks = mongoc_apm_callbacks_new ();
101-
mongoc_apm_set_command_started_cb (callbacks, command_started);
102-
mongoc_client_set_apm_callbacks (client, callbacks, NULL);
103-
mongoc_client_set_error_api (client, 2);
104-
105-
// example_listcollections (client);
106-
example_listindexes (client);
107-
69+
bson_destroy (&query);
70+
mongoc_cursor_destroy (cursor);
71+
mongoc_collection_destroy (collection);
72+
mongoc_uri_destroy (uri);
10873
mongoc_client_destroy (client);
10974
mongoc_cleanup ();
11075

src/libmongoc/src/mongoc/mongoc-cmd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,6 @@ mongoc_cmd_parts_assemble (mongoc_cmd_parts_t *parts,
824824

825825
BSON_ASSERT (parts);
826826
BSON_ASSERT (server_stream);
827-
828827
server_type = server_stream->sd->type;
829828
cs = parts->prohibit_lsid ? NULL : parts->assembled.session;
830829

@@ -1022,7 +1021,8 @@ mongoc_cmd_parts_assemble (mongoc_cmd_parts_t *parts,
10221021
}
10231022

10241023
ret = true;
1025-
} else if (server_type == MONGOC_SERVER_MONGOS) {
1024+
} else if (server_type == MONGOC_SERVER_MONGOS ||
1025+
server_stream->topology_type == MONGOC_TOPOLOGY_LOAD_BALANCED) {
10261026
_mongoc_cmd_parts_assemble_mongos (parts, server_stream);
10271027
ret = true;
10281028
} else {

0 commit comments

Comments
 (0)