123456789_123456789_123456789_123456789_123456789_

Class: Mongo::CachingCursor

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Cursor, Forwardable
Instance Chain:
self, Cursor, Retryable, Enumerable
Inherits: Mongo::Cursor
Defined in: lib/mongo/caching_cursor.rb

Overview

A Cursor that attempts to load documents from memory first before hitting the database if the same query has already been executed.

Class Method Summary

Cursor - Inherited

.finalize

Finalize the cursor for garbage collection.

.new

Creates a Cursor object.

Instance Attribute Summary

Cursor - Inherited

#closed?

Is the cursor closed?

#connection, #context, #fully_iterated?, #initial_result,
#resume_token

The resume token tracked by the cursor for change stream resuming.

#server, #view, #exhausted?, #explicitly_closed?, #limited?, #use_limit?

Instance Method Summary

  • #each

    We iterate over the cached documents if they exist already in the cursor otherwise proceed as normal.

  • #inspect ⇒ String

    Get a human-readable string representation of Cursor.

  • #try_next Internal use only Internal use only

    Acquires the next document for cursor iteration and then inserts that document in the @cached_docs array.

Cursor - Inherited

#batch_size

Get the batch size.

#close

Closes this cursor, freeing any associated resources on the client and the server.

#collection_name

Get the parsed collection name.

#each

Iterate through documents returned from the query.

#get_more

Execute a getMore command and return the batch of documents obtained from the server.

#id

Get the cursor id.

#inspect

Get a human-readable string representation of Cursor.

#kill_spec,
#to_return

Get the number of documents to return.

#try_next

Return one document from the query, if one is available.

#batch_size_for_get_more, #cache_batch_resume_token, #cache_resume_token,
#check_in_connection

Returns the connection that was used to create the cursor back to the corresponding connection pool.

#connection_global_id_for_context

Because a context must not have a connection_global_id if the session is already pinned to one, this method checks to see whether or not there’s pinned connection_global_id on the session and returns nil if so.

#end_session, #execute_operation,
#fresh_context

Returns a newly instantiated operation context based on the default values from the view.

#get_more_operation, #limit,
#possibly_refreshed_context

Considers the timeout mode and will either return the cursor’s context directly, or will return a new (refreshed) context.

#process, #register,
#set_cursor_id

Sets @cursor_id from the operation result.

#unregister

Retryable - Included

#read_worker

Returns the read worker for handling retryable reads.

#select_server

This is a separate method to make it possible for the test suite to assert that server selection is performed during retry attempts.

#write_worker

Returns the write worker for handling retryable writes.

Instance Attribute Details

#cached_docsArray <BSON::Document> (readonly)

This method is for internal use only.

Returns:

  • (Array <BSON::Document>)

    The cursor’s cached documents.

[ GitHub ]

  
# File 'lib/mongo/caching_cursor.rb', line 28

attr_reader :cached_docs

Instance Method Details

#each

We iterate over the cached documents if they exist already in the cursor otherwise proceed as normal.

Examples:

Iterate over the documents.

cursor.each do |doc|
  # ...
end
[ GitHub ]

  
# File 'lib/mongo/caching_cursor.rb', line 37

def each
  if @cached_docs
    @cached_docs.each do |doc|
      yield doc
    end

    unless closed?
      # StopIteration raised by try_next ends this loop.
      loop do
        document = try_next
        yield document if document
      end
    end
  else
    super
  end
end

#inspectString

Get a human-readable string representation of Cursor.

Examples:

Inspect the cursor.

cursor.inspect

Returns:

  • (String)

    A string representation of a Cursor instance.

[ GitHub ]

  
# File 'lib/mongo/caching_cursor.rb', line 61

def inspect
  "#<Mongo::CachingCursor:0x#{object_id} @view=#{@view.inspect}>"
end

#try_next

This method is for internal use only.

Acquires the next document for cursor iteration and then inserts that document in the @cached_docs array.

[ GitHub ]

  
# File 'lib/mongo/caching_cursor.rb', line 69

def try_next
  @cached_docs ||= []
  document = super
  @cached_docs << document if document

  document
end