From 9a0f740f1e2a2709f8402c46fcc2f5cc1e381a0f Mon Sep 17 00:00:00 2001 From: Jethro Carr Date: Wed, 17 Jul 2019 16:05:51 +1200 Subject: [PATCH 1/2] Ported work from https://github.com/AlimovSV/mongodb/tree/docdb as part of fixing https://github.com/ankhers/mongodb/issues/273 --- lib/mongo.ex | 51 ++++++++++++++++++++++++++++++----------- lib/mongo/auth/scram.ex | 10 ++++---- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index d4bd1011..7f46c6b7 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -541,26 +541,49 @@ defmodule Mongo do @doc false def raw_find(conn, coll, query, select, opts) do - params = [query, select] - query = %Query{action: :find, extra: coll} + query = filter_nils([ + find: coll, + filter: query, + projection: select, + batchSize: opts[:batch_size], + skip: opts[:skip], + ]) + + opts = Keyword.drop(opts, [:skip, :batch_size]) + + with {:ok, %{"cursor" => %{"id" => id, "firstBatch" => docs}}} <- + direct_command(conn, query, opts) do + {:ok, %{from: 0, num: Enum.count(docs), cursor_id: id, docs: docs}} + end - with {:ok, _query, reply} <- DBConnection.execute(conn, query, params, defaults(opts)), - :ok <- maybe_failure(reply), - op_reply(docs: docs, cursor_id: cursor_id, from: from, num: num) = reply, - do: {:ok, %{from: from, num: num, cursor_id: cursor_id, docs: docs}} + # params = [query, select] + # query = %Query{action: :find, extra: coll} + # with {:ok, reply} <- DBConnection.execute(conn, query, params, defaults(opts)), + # :ok <- maybe_failure(reply), + # op_reply(docs: docs, cursor_id: cursor_id, from: from, num: num) = reply, + # do: {:ok, %{from: from, num: num, cursor_id: cursor_id, docs: docs}} end @doc false def get_more(conn, coll, cursor, opts) do - query = - filter_nils( - getMore: cursor, - collection: coll, - batchSize: Keyword.get(opts, :batch_size), - maxTimeMS: Keyword.get(opts, :max_time_ms) - ) + query = filter_nils([ + getMore: cursor, + collection: coll, + batchSize: opts[:batch_size], + ]) + + opts = Keyword.drop(opts, [:batch_size]) + + with {:ok, %{"cursor" => %{"id" => id, "nextBatch" => docs}}} <- + direct_command(conn, query, opts) do + {:ok, %{from: 0, num: Enum.count(docs), cursor_id: id, docs: docs}} + end - direct_command(conn, query, opts) + # query = %Query{action: :get_more, extra: {coll, cursor}} + # with {:ok, reply} <- DBConnection.execute(conn, query, [], defaults(opts)), + # :ok <- maybe_failure(reply), + # op_reply(docs: docs, cursor_id: cursor_id, from: from, num: num) = reply, + # do: {:ok, %{from: from, num: num, cursor_id: cursor_id, docs: docs}} end @doc false diff --git a/lib/mongo/auth/scram.ex b/lib/mongo/auth/scram.ex index 3ad36a71..495a4204 100644 --- a/lib/mongo/auth/scram.ex +++ b/lib/mongo/auth/scram.ex @@ -34,7 +34,7 @@ defmodule Mongo.Auth.SCRAM do end defp first( - %{"conversationId" => 1, "payload" => server_payload, "done" => false}, + %{"conversationId" => conversation_id, "payload" => server_payload, "done" => false}, first_bare, username, password, @@ -54,18 +54,18 @@ defmodule Mongo.Auth.SCRAM do server_signature = generate_signature(salted_password, auth_message) proof = generate_proof(salted_password, auth_message) client_final_message = %BSON.Binary{binary: "#{client_message},#{proof}"} - message = [saslContinue: 1, conversationId: 1, payload: client_final_message] + message = [saslContinue: 1, conversationId: conversation_id, payload: client_final_message] {message, server_signature} end - defp second(%{"conversationId" => 1, "payload" => payload, "done" => false}, signature) do + defp second(%{"conversationId" => conversation_id, "payload" => payload}, signature) do params = parse_payload(payload) ^signature = Base.decode64!(params["v"]) - [saslContinue: 1, conversationId: 1, payload: %BSON.Binary{binary: ""}] + [saslContinue: 1, conversationId: conversation_id, payload: %BSON.Binary{binary: ""}] end - defp final(%{"conversationId" => 1, "payload" => %BSON.Binary{binary: ""}, "done" => true}) do + defp final(%{"conversationId" => _, "payload" => %BSON.Binary{binary: ""}, "done" => true}) do :ok end From a7820262ea9a611121d4d1a09d1554973b3749d6 Mon Sep 17 00:00:00 2001 From: Jethro Carr Date: Thu, 18 Jul 2019 09:04:20 +1200 Subject: [PATCH 2/2] Cleaning up old commented out code --- lib/mongo.ex | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/mongo.ex b/lib/mongo.ex index 7f46c6b7..a40c5814 100644 --- a/lib/mongo.ex +++ b/lib/mongo.ex @@ -555,13 +555,6 @@ defmodule Mongo do direct_command(conn, query, opts) do {:ok, %{from: 0, num: Enum.count(docs), cursor_id: id, docs: docs}} end - - # params = [query, select] - # query = %Query{action: :find, extra: coll} - # with {:ok, reply} <- DBConnection.execute(conn, query, params, defaults(opts)), - # :ok <- maybe_failure(reply), - # op_reply(docs: docs, cursor_id: cursor_id, from: from, num: num) = reply, - # do: {:ok, %{from: from, num: num, cursor_id: cursor_id, docs: docs}} end @doc false @@ -578,12 +571,6 @@ defmodule Mongo do direct_command(conn, query, opts) do {:ok, %{from: 0, num: Enum.count(docs), cursor_id: id, docs: docs}} end - - # query = %Query{action: :get_more, extra: {coll, cursor}} - # with {:ok, reply} <- DBConnection.execute(conn, query, [], defaults(opts)), - # :ok <- maybe_failure(reply), - # op_reply(docs: docs, cursor_id: cursor_id, from: from, num: num) = reply, - # do: {:ok, %{from: from, num: num, cursor_id: cursor_id, docs: docs}} end @doc false