Skip to content

Commit b3ba233

Browse files
committed
CXX-722: add MaxAwaitTimeMs option to find opts
1 parent 8d77340 commit b3ba233

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
lines changed

src/mongocxx/collection.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,16 @@ cursor collection::find(bsoncxx::document::view filter, const options::find& opt
133133
rp_ptr = options.read_preference()->_impl->read_preference_t;
134134
}
135135

136-
return cursor(libmongoc::collection_find(
136+
auto mongoc_cursor = libmongoc::collection_find(
137137
_impl->collection_t, mongoc_query_flags_t(0), options.skip().value_or(0),
138138
options.limit().value_or(0), options.batch_size().value_or(0), filter_bson.bson(),
139-
projection.bson(), rp_ptr));
139+
projection.bson(), rp_ptr);
140+
141+
if (options.max_await_time_ms()) {
142+
libmongoc::cursor_set_max_await_time_ms(mongoc_cursor, *options.max_await_time_ms());
143+
}
144+
145+
return cursor{mongoc_cursor};
140146
}
141147

142148
stdx::optional<bsoncxx::document::value> collection::find_one(bsoncxx::document::view filter,

src/mongocxx/options/find.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@ void find::comment(std::string comment) {
3434
_comment = comment;
3535
}
3636

37-
void find::cursor_type(enum cursor_type cursor_type) {
37+
void find::cursor_type(enum find::cursor_type cursor_type) {
3838
_cursor_type = cursor_type;
3939
}
4040

4141
void find::limit(std::int32_t limit) {
4242
_limit = limit;
4343
}
4444

45+
void find::max_await_time_ms(std::int64_t max_await_time_ms) {
46+
_max_await_time_ms = max_await_time_ms;
47+
}
48+
4549
void find::max_time_ms(std::int64_t max_time_ms) {
4650
_max_time_ms = max_time_ms;
4751
}
@@ -86,14 +90,18 @@ const stdx::optional<std::string>& find::comment() const {
8690
return _comment;
8791
}
8892

89-
const stdx::optional<cursor_type>& find::cursor_type() const {
93+
const stdx::optional<enum find::cursor_type>& find::cursor_type() const {
9094
return _cursor_type;
9195
}
9296

9397
const stdx::optional<std::int32_t>& find::limit() const {
9498
return _limit;
9599
}
96100

101+
const stdx::optional<std::int64_t>& find::max_await_time_ms() const {
102+
return _max_await_time_ms;
103+
}
104+
97105
const stdx::optional<std::int64_t>& find::max_time_ms() const {
98106
return _max_time_ms;
99107
}

src/mongocxx/options/find.hpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ namespace mongocxx {
2626
MONGOCXX_INLINE_NAMESPACE_BEGIN
2727
namespace options {
2828

29-
enum class cursor_type : std::uint8_t { k_non_tailable, k_tailable, k_tailable_await };
30-
3129
///
3230
/// Class representing the optional arguments to a MongoDB query.
3331
///
3432
class MONGOCXX_API find {
35-
3633
public:
34+
enum class cursor_type : std::uint8_t { k_non_tailable, k_tailable, k_tailable_await };
3735

3836
///
3937
/// Sets whether to allow partial results from a mongos if some shards are down (instead of
@@ -128,6 +126,29 @@ class MONGOCXX_API find {
128126
///
129127
const stdx::optional<std::int32_t>& limit() const;
130128

129+
///
130+
/// The maximum amount of time for the server to wait on new documents to satisfy a tailable
131+
/// cursor query. This only applies to a TAILABLE_AWAIT cursor. When the cursor is not a
132+
/// TAILABLE_AWAIT cursor, this option is ignored. The default on the server is to wait for one
133+
/// second.
134+
///
135+
/// @note On servers < 3.2, this option is ignored.
136+
///
137+
/// @param max_await_time_ms
138+
/// The max amount of time (in milliseconds) to wait for new documents.
139+
///
140+
/// @see http://docs.mongodb.org/manual/reference/operator/meta/maxTimeMS
141+
///
142+
void max_await_time_ms(std::int64_t max_await_time_ms);
143+
144+
///
145+
/// The maximum amount of time for the server to wait on new documents to satisfy a tailable
146+
/// cursor query.
147+
///
148+
/// @return The current max await time (in milliseconds).
149+
///
150+
const stdx::optional<std::int64_t>& max_await_time_ms() const;
151+
131152
///
132153
/// Sets the maximum amount of time for this operation to run (server-side) in milliseconds.
133154
///
@@ -271,6 +292,7 @@ class MONGOCXX_API find {
271292
stdx::optional<std::string> _comment;
272293
stdx::optional<enum cursor_type> _cursor_type;
273294
stdx::optional<std::int32_t> _limit;
295+
stdx::optional<std::int64_t> _max_await_time_ms;
274296
stdx::optional<std::int64_t> _max_time_ms;
275297
stdx::optional<bsoncxx::document::view> _modifiers;
276298
stdx::optional<bool> _no_cursor_timeout;

src/mongocxx/private/libmongoc_symbols.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ MONGOCXX_LIBMONGOC_SYMBOL(collection_set_write_concern)
6363
MONGOCXX_LIBMONGOC_SYMBOL(cursor_destroy)
6464
MONGOCXX_LIBMONGOC_SYMBOL(cursor_error)
6565
MONGOCXX_LIBMONGOC_SYMBOL(cursor_next)
66+
MONGOCXX_LIBMONGOC_SYMBOL(cursor_set_max_await_time_ms)
6667
MONGOCXX_LIBMONGOC_SYMBOL(database_command)
6768
MONGOCXX_LIBMONGOC_SYMBOL(database_command_simple)
6869
MONGOCXX_LIBMONGOC_SYMBOL(database_create_collection)

src/mongocxx/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(mongocxx_test_sources
1515
model/update_many.cpp
1616
options/aggregate.cpp
1717
options/create_collection.cpp
18+
options/find.cpp
1819
options/insert.cpp
1920
options/update.cpp
2021
pool.cpp

src/mongocxx/test/options/find.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "catch.hpp"
2+
#include "helpers.hpp"
3+
4+
#include <bsoncxx/document/view.hpp>
5+
#include <mongocxx/options/find.hpp>
6+
7+
using namespace mongocxx;
8+
9+
TEST_CASE("find", "[find][option]") {
10+
options::find find_opts{};
11+
using cursor_type = enum options::find::cursor_type;
12+
13+
CHECK_OPTIONAL_ARGUMENT(find_opts, allow_partial_results, true);
14+
CHECK_OPTIONAL_ARGUMENT(find_opts, batch_size, 3);
15+
CHECK_OPTIONAL_ARGUMENT(find_opts, comment, "comment");
16+
CHECK_OPTIONAL_ARGUMENT(find_opts, cursor_type, cursor_type::k_non_tailable);
17+
CHECK_OPTIONAL_ARGUMENT(find_opts, limit, 3);
18+
CHECK_OPTIONAL_ARGUMENT(find_opts, max_await_time_ms, 300);
19+
CHECK_OPTIONAL_ARGUMENT(find_opts, max_time_ms, 300);
20+
CHECK_OPTIONAL_ARGUMENT(find_opts, no_cursor_timeout, true);
21+
CHECK_OPTIONAL_ARGUMENT(find_opts, oplog_replay, true);
22+
CHECK_OPTIONAL_ARGUMENT(find_opts, skip, 3);
23+
24+
CHECK_OPTIONAL_ARGUMENT_WITHOUT_EQUALITY(find_opts, read_preference, read_preference{});
25+
CHECK_OPTIONAL_ARGUMENT(find_opts, modifiers, bsoncxx::document::view{});
26+
CHECK_OPTIONAL_ARGUMENT(find_opts, projection, bsoncxx::document::view{});
27+
CHECK_OPTIONAL_ARGUMENT(find_opts, sort, bsoncxx::document::view{});
28+
}

0 commit comments

Comments
 (0)