Skip to content

Commit

Permalink
PERF-2159 Adding options support for find and findOne in CrudActor (m…
Browse files Browse the repository at this point in the history
…ongodb#423)

* CrudActor options

* CrudActor options template

* a coupleof tests added
  • Loading branch information
apodelko authored Feb 22, 2021
1 parent db500ad commit cf6bb26
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/cast_core/src/CrudActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,39 @@ struct convert<mongocxx::options::count> {
}
};

template <>
struct convert<mongocxx::options::find> {
using FindOptions = mongocxx::options::find;
static Node encode(const FindOptions& rhs) {
return {};
}

static bool decode(const Node& node, FindOptions& rhs) {
if (!node.IsMap()) {
return false;
}
if (node["Hint"]) {
auto h = node["Hint"].as<std::string>();
auto hint = mongocxx::hint(h);
rhs.hint(hint);
}
if (node["Limit"]) {
auto limit = node["Limit"].as<int>();
rhs.limit(limit);
}
if (node["MaxTime"]) {
auto maxTime = node["MaxTime"].as<genny::TimeSpec>();
rhs.max_time(std::chrono::milliseconds{maxTime});
}
if (node["ReadPreference"]) {
auto readPref = node["ReadPreference"].as<mongocxx::read_preference>();
rhs.read_preference(readPref);
}
return true;
}
};


template <>
struct convert<mongocxx::options::estimated_document_count> {
using CountOptions = mongocxx::options::estimated_document_count;
Expand Down Expand Up @@ -799,7 +832,11 @@ struct FindOperation : public BaseOperation {
_onSession{onSession},
_collection{std::move(collection)},
_operation{operation},
_filter{opNode["Filter"].to<DocumentGenerator>(context, id)} {}
_filter{opNode["Filter"].to<DocumentGenerator>(context, id)} {
if (opNode["Options"]) {
_options = opNode["Options"].to<mongocxx::options::find>();
}
}

void run(mongocxx::client_session& session) override {
auto filter = _filter();
Expand Down Expand Up @@ -834,7 +871,11 @@ struct FindOneOperation : public BaseOperation {
_onSession{onSession},
_collection{std::move(collection)},
_operation{operation},
_filter{opNode["Filter"].to<DocumentGenerator>(context, id)} {}
_filter{opNode["Filter"].to<DocumentGenerator>(context, id)} {
if (opNode["Options"]) {
_options = opNode["Options"].to<mongocxx::options::find>();
}
}

void run(mongocxx::client_session& session) override {
auto filter = _filter();
Expand Down
24 changes: 24 additions & 0 deletions src/cast_core/test/CrudActorYamlTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,30 @@ Tests:
$readPreference:
mode: secondaryPreferred

- Description: Read preference is 'secondaryPreferred' in find.
Operations:
- OperationName: find
OperationCommand:
Filter: { a : 1 }
Options:
ReadPreference:
ReadMode: secondaryPreferred
ExpectAllEvents:
$readPreference:
mode: secondaryPreferred

- Description: Read preference is 'secondaryPreferred' in findOne.
Operations:
- OperationName: findOne
OperationCommand:
Filter: { a : 1 }
Options:
ReadPreference:
ReadMode: secondaryPreferred
ExpectAllEvents:
$readPreference:
mode: secondaryPreferred

- Description: Read preference is 'nearest' with MaxStaleness set.
Operations:
- OperationName: countDocuments
Expand Down

0 comments on commit cf6bb26

Please sign in to comment.