123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Protocol::Reply::Upconverter

Relationships & Source Files
Inherits: Object
Defined in: lib/mongo/protocol/reply.rb

Overview

Upconverts legacy replies to new op command replies.

Since:

  • 2.1.0

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(documents, cursor_id, starting_from) ⇒ Upconverter

Initialize the new upconverter.

Examples:

Create the upconverter.

Upconverter.new(docs, 1, 3)

Parameters:

  • documents (Array<BSON::Document>)

    The documents.

  • cursor_id (Integer)

    The cursor id.

  • starting_from (Integer)

    The starting position.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/reply.rb', line 152

def initialize(documents, cursor_id, starting_from)
  @documents = documents
  @cursor_id = cursor_id
  @starting_from = starting_from
end

Instance Attribute Details

#commandBSON::Document (readonly)

Get the upconverted command.

Examples:

Get the command.

upconverter.command

Returns:

  • (BSON::Document)

    The command.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/reply.rb', line 175

def command
  command? ? op_command : find_command
end

#command?Boolean (readonly, private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/reply.rb', line 185

def command?
  !documents.empty? && documents.first.key?(Operation::Result::OK)
end

#cursor_idInteger (readonly)

Returns:

  • (Integer)

    cursor_id The cursor id.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/reply.rb', line 162

attr_reader :cursor_id

#documentsArray<BSON::Document> (readonly)

Returns:

  • (Array<BSON::Document>)

    documents The documents.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/reply.rb', line 159

attr_reader :documents

#starting_fromInteger (readonly)

Returns:

  • (Integer)

    starting_from The starting point in the cursor.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/reply.rb', line 165

attr_reader :starting_from

Instance Method Details

#batch_field (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/reply.rb', line 181

def batch_field
  starting_from > 0 ? NEXT_BATCH : FIRST_BATCH
end

#find_command (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/reply.rb', line 189

def find_command
  document = BSON::Document.new
  cursor_document = BSON::Document.new
  cursor_document.store(ID, cursor_id)
  cursor_document.store(batch_field, documents)
  document.store(Operation::Result::OK, 1)
  document.store(CURSOR, cursor_document)
  document
end

#op_command (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/protocol/reply.rb', line 199

def op_command
  documents.first
end