Class: Mongo::Operation::Result
| Relationships & Source Files | |
| Namespace Children | |
|
Modules:
| |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
Aggregate::Result, CollectionsInfo::Result, CursorCommand::Result, Delete::BulkResult, Delete::Result, Explain::Result, Find::Result, GetMore::Result, Indexes::Result, Insert::BulkResult, Insert::Result, ListCollections::Result, MapReduce::Result, ParallelScan::Result, Update::BulkResult, Update::Result, UsersInfo::Result
|
|
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
Forwardable
|
|
|
Instance Chain:
self,
Enumerable
|
|
| Inherits: | Object |
| Defined in: | lib/mongo/operation/result.rb, lib/mongo/operation/shared/result/aggregatable.rb |
Overview
Result wrapper for wire protocol replies.
An operation has zero or one replies. The only operations producing zero replies are unacknowledged writes; all other operations produce one reply. This class provides an object that can be operated on (for example, to check whether an operation succeeded) even when the operation did not produce a reply (in which case it is assumed to have succeeded).
Constant Summary
-
CURSOR =
# File 'lib/mongo/operation/result.rb', line 39
The field name for the cursor document in an aggregation.
'cursor' -
CURSOR_ID =
# File 'lib/mongo/operation/result.rb', line 45
The cursor id field in the cursor document.
'id' -
FIRST_BATCH =
# File 'lib/mongo/operation/result.rb', line 51
The field name for the first batch of a cursor.
'firstBatch' -
N =
# File 'lib/mongo/operation/result.rb', line 69
The number of documents updated in the write.
'n' -
NAMESPACE =
# File 'lib/mongo/operation/result.rb', line 63
The namespace field in the cursor document.
'ns' -
NEXT_BATCH =
# File 'lib/mongo/operation/result.rb', line 57
The field name for the next batch of a cursor.
'nextBatch' -
OK =
# File 'lib/mongo/operation/result.rb', line 75
The ok status field in the result.
'ok' -
RESULT =
# File 'lib/mongo/operation/result.rb', line 81
The result field constant.
'result'
Class Method Summary
-
.new(replies, connection_description = nil, connection_global_id = nil, context: nil, connection: nil) ⇒ Result
constructor
Internal use only
Initialize a new result.
Instance Attribute Summary
-
#acknowledged? ⇒ true, false
readonly
Is the result acknowledged?
- #connection readonly
- #connection_description ⇒ Server::Description readonly Internal use only
- #connection_global_id ⇒ Object readonly Internal use only
- #context ⇒ Operation::Context | nil readonly Internal use only
-
#ok? ⇒ true, false
readonly
Check the first document's ok field.
- #replies ⇒ Array<Protocol::Message> readonly Internal use only
-
#successful? ⇒ true, false
readonly
If the result was a command then determine if it was considered a success.
-
#write_concern_error? ⇒ Boolean
readonly
Internal use only
Whether the operation failed with a write concern error.
- #query_failure? ⇒ Boolean readonly private
Instance Method Summary
-
#cluster_time ⇒ ClusterTime | nil
Get the cluster time reported in the server response.
-
#cursor_id ⇒ Integer
Internal use only
Get the cursor id if the response is acknowledged.
-
#documents ⇒ Array<BSON::Document>
Get the documents in the result.
-
#each {|Each| ... } ⇒ Enumerator
Iterate over the documents in the replies.
-
#error ⇒ Error::OperationFailure::Family
Internal use only
The exception instance (of Error::OperationFailure::Family) that would be raised during processing of this result.
-
#inspect ⇒ String
Get the pretty formatted inspection of the result.
-
#labels ⇒ Array
Internal use only
Gets the set of error labels associated with the result.
-
#n
Alias for #written_count.
-
#namespace ⇒ Nil
Internal use only
Get the namespace of the cursor.
-
#operation_time ⇒ Object | nil
Get the operation time reported in the server response.
-
#reply ⇒ Protocol::Message
Internal use only
Get the reply from the result.
-
#returned_count ⇒ Integer
Get the number of documents returned by the server in this batch.
- #snapshot_timestamp
- #topology_version ⇒ TopologyVersion | nil Internal use only
-
#validate! ⇒ Result
Internal use only
Validate the result by checking for any errors.
-
#written_count ⇒ Integer
(also: #n)
Get the number of documents written by the server.
- #first_document private
- #operation_failure_class private
- #parser private
-
#raise_operation_failure
private
Raises a Mongo::OperationFailure exception corresponding to the error information in this result.
Constructor Details
.new(replies, connection_description = nil, connection_global_id = nil, context: nil, connection: nil) ⇒ Result
Initialize a new result.
For an unkacknowledged write, pass nil in replies.
For all other operations, replies must be a ::Mongo::Protocol::Message instance
or an array containing a single ::Mongo::Protocol::Message instance.
# File 'lib/mongo/operation/result.rb', line 104
def initialize(replies, connection_description = nil, connection_global_id = nil, context: nil, connection: nil) @context = context return unless replies if replies.is_a?(Array) raise ArgumentError, "Only one (or zero) reply is supported, given #{replies.length}" if replies.length != 1 reply = replies.first else reply = replies end unless reply.is_a?(Protocol::Message) raise ArgumentError, "Argument must be a Message instance, but is a #{reply.class}: #{reply.inspect}" end @replies = [ reply ] @connection_description = connection_description @connection_global_id = connection_global_id @connection = connection end
Instance Attribute Details
#acknowledged? ⇒ true, false (readonly)
Is the result acknowledged?
# File 'lib/mongo/operation/result.rb', line 161
def acknowledged? !!@replies end
#connection (readonly)
# File 'lib/mongo/operation/result.rb', line 149
attr_reader :connection
#connection_description ⇒ Server::Description (readonly)
# File 'lib/mongo/operation/result.rb', line 135
attr_reader :connection_description
#connection_global_id ⇒ Object (readonly)
# File 'lib/mongo/operation/result.rb', line 141
attr_reader :connection_global_id
#context ⇒ Operation::Context | nil (readonly)
# File 'lib/mongo/operation/result.rb', line 147
attr_reader :context
#ok? ⇒ true, false (readonly)
Check the first document's ok field.
# File 'lib/mongo/operation/result.rb', line 309
def ok? # first_document[OK] is a float, and the server can return # ok as a BSON int32, BSON int64 or a BSON double. # The number 1 is exactly representable in a float, hence # 1.0 == 1 is going to perform correctly all of the time # (until the server returns something other than 1 for success, that is) first_document[OK] == 1 end
#query_failure? ⇒ Boolean (readonly, private)
#replies ⇒ Array<Protocol::Message> (readonly)
# File 'lib/mongo/operation/result.rb', line 129
attr_reader :replies
#successful? ⇒ true, false (readonly)
If the write was unacknowledged, then this will always return true.
If the result was a command then determine if it was considered a success.
# File 'lib/mongo/operation/result.rb', line 290
def successful? return true unless acknowledged? if first_document.has_key?(OK) ok? && parser..empty? else !query_failure? && parser..empty? end end
#write_concern_error? ⇒ Boolean (readonly)
Whether the operation failed with a write concern error.
# File 'lib/mongo/operation/result.rb', line 449
def write_concern_error? !!(first_document && first_document['writeConcernError']) end
Instance Method Details
#cluster_time ⇒ ClusterTime | nil
Get the cluster time reported in the server response.
Changed in version 2.9.0: This attribute became an instance of
::Mongo::ClusterTime, which is a subclass of BSON::Document.
Previously it was an instance of BSON::Document.
# File 'lib/mongo/operation/result.rb', line 429
def cluster_time first_document && ClusterTime[first_document['$clusterTime']] end
#cursor_id ⇒ Integer
::Mongo::Cursor ids of 0 indicate there is no cursor on the server.
Get the cursor id if the response is acknowledged.
# File 'lib/mongo/operation/result.rb', line 176
def cursor_id return 0 unless acknowledged? if replies.last.respond_to?(:cursor_id) # Legacy OP_REPLY replies expose the cursor id directly. replies.last.cursor_id elsif first_document.key?(CURSOR) # OP_MSG command replies carry the cursor id in the `cursor` subdocument. first_document[CURSOR][CURSOR_ID] || 0 else 0 end end
#documents ⇒ Array<BSON::Document>
Get the documents in the result.
# File 'lib/mongo/operation/result.rb', line 210
def documents if acknowledged? replies.flat_map(&:documents) else [] end end
#each {|Each| ... } ⇒ Enumerator
Iterate over the documents in the replies.
# File 'lib/mongo/operation/result.rb', line 231
def each(&block) documents.each(&block) end
#error ⇒ Error::OperationFailure::Family
The exception instance (of Error::OperationFailure::Family) that would be raised during processing of this result.
This method should only be called when result is not successful.
# File 'lib/mongo/operation/result.rb', line 345
def error @error ||= operation_failure_class.new( parser., self, code: parser.code, code_name: parser.code_name, write_concern_error_document: parser.write_concern_error_document, write_concern_error_code: parser.write_concern_error_code, write_concern_error_code_name: parser.write_concern_error_code_name, write_concern_error_labels: parser.write_concern_error_labels, labels: parser.labels, wtimeout: parser.wtimeout, connection_description: connection_description, document: parser.document, server_message: parser., server_address: connection_description ) end
#first_document (private)
# File 'lib/mongo/operation/result.rb', line 473
def first_document @first_document ||= first || BSON::Document.new end
#inspect ⇒ String
Get the pretty formatted inspection of the result.
# File 'lib/mongo/operation/result.rb', line 244
def inspect "#<#{self.class.name}:0x#{object_id} documents=#{documents}>" end
#labels ⇒ Array
Gets the set of error labels associated with the result.
# File 'lib/mongo/operation/result.rb', line 442
def labels @labels ||= parser.labels end
#n
Alias for #written_count.
# File 'lib/mongo/operation/result.rb', line 401
alias n written_count
#namespace ⇒ Nil
Get the namespace of the cursor. The method should be defined in result classes where 'ns' is in the server response.
# File 'lib/mongo/operation/result.rb', line 197
def namespace nil end
#operation_failure_class (private)
# File 'lib/mongo/operation/result.rb', line 461
def operation_failure_class if context&.csot? && parser.code == 50 Error::ServerTimeoutError else Error::OperationFailure end end
#operation_time ⇒ Object | nil
Get the operation time reported in the server response.
# File 'lib/mongo/operation/result.rb', line 412
def operation_time first_document && first_document[OPERATION_TIME] end
#parser (private)
# File 'lib/mongo/operation/result.rb', line 469
def parser @parser ||= Error::Parser.new(first_document, replies) end
#raise_operation_failure (private)
Raises a Mongo::OperationFailure exception corresponding to the error information in this result.
# File 'lib/mongo/operation/result.rb', line 368
private def raise_operation_failure raise error end
#reply ⇒ Protocol::Message
Get the reply from the result.
Returns nil if there is no reply (i.e. the operation was an unacknowledged write).
# File 'lib/mongo/operation/result.rb', line 257
def reply return unless acknowledged? replies.first end
#returned_count ⇒ Integer
Get the number of documents returned by the server in this batch.
# File 'lib/mongo/operation/result.rb', line 269
def returned_count if acknowledged? reply.number_returned else 0 end end
#snapshot_timestamp
#topology_version ⇒ TopologyVersion | nil
# File 'lib/mongo/operation/result.rb', line 375
def topology_version unless defined?(@topology_version) @topology_version = first_document['topologyVersion'] && TopologyVersion.new(first_document['topologyVersion']) end @topology_version end
#validate! ⇒ Result
This only checks for errors with writes since authentication is
handled at the connection level and any authentication errors would
be raised there, before a Result is ever created.
Validate the result by checking for any errors.
# File 'lib/mongo/operation/result.rb', line 333
def validate! successful? ? self : raise_operation_failure end
#written_count ⇒ Integer
Also known as: #n
Get the number of documents written by the server.
# File 'lib/mongo/operation/result.rb', line 392
def written_count if acknowledged? first_document[N] || 0 else 0 end end