Skip to content

Commit

Permalink
mongodb refactor duplicate code into one function
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 authored and s-ludwig committed Feb 8, 2024
1 parent 8e35c8c commit c31d14f
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions mongodb/vibe/db/mongo/connection.d
Original file line number Diff line number Diff line change
Expand Up @@ -517,29 +517,7 @@ final class MongoConnection {

if (m_supportsOpMsg)
{
enum needsDup = hasIndirections!T || is(T == Bson);

debug (VibeVerboseMongo)
logDiagnostic("getMore: [db=%s] %s", database, command);

auto id = sendMsg(-1, 0, command);
recvMsg!needsDup(id, (flags, scope root) @safe {
if (root["ok"].get!double != 1.0)
throw new MongoDriverException(formatErrorInfo("getMore failed: "
~ root["errmsg"].opt!string("(no message)")));

auto cursor = root["cursor"];
auto batch = cursor["nextBatch"].get!(Bson[]);
on_header(cursor["id"].get!long, cursor["ns"].get!string, batch.length);

foreach (ref push; batch)
{
T doc = deserializeBson!T(push);
on_doc(doc);
}
}, (scope ident, size) @safe {}, (scope ident, scope push) @safe {
throw new MongoDriverException(formatErrorInfo("unexpected section type 1 in getMore response"));
});
startFind!T(command, on_header, on_doc, "nextBatch", errorInfo ~ " (getMore)", errorFile, errorLine);
}
else
{
Expand Down Expand Up @@ -591,6 +569,7 @@ final class MongoConnection {
package void startFind(T)(Bson command,
scope GetMoreHeaderDelegate on_header,
scope GetMoreDocumentDelegate!T on_doc,
string batchKey = "firstBatch",
string errorInfo = __FUNCTION__, string errorFile = __FILE__, size_t errorLine = __LINE__)
{
string formatErrorInfo(string msg) @safe
Expand All @@ -605,7 +584,7 @@ final class MongoConnection {
enum needsDup = hasIndirections!T || is(T == Bson);

debug (VibeVerboseMongo)
logDiagnostic("startFind: %s", command);
logDiagnostic("%s: %s", errorInfo, command);

auto id = sendMsg(-1, 0, command);
recvMsg!needsDup(id, (flags, scope root) @safe {
Expand All @@ -614,7 +593,10 @@ final class MongoConnection {
~ root["errmsg"].opt!string("(no message)")));

auto cursor = root["cursor"];
auto batch = cursor["firstBatch"].get!(Bson[]);
if (cursor.type == Bson.Type.null_)
throw new MongoDriverException(formatErrorInfo("no cursor in response: "
~ root["errmsg"].opt!string("(no error message)")));
auto batch = cursor[batchKey].get!(Bson[]);
on_header(cursor["id"].get!long, cursor["ns"].get!string, batch.length);

foreach (ref push; batch)
Expand Down

0 comments on commit c31d14f

Please sign in to comment.