Class: Moped::Cursor Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Readable, Retryable
Defined in:
lib/moped/cursor.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Contains logic for cursor behaviour.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, query_operation) ⇒ Cursor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the new cursor.

Examples:

Create the new cursor.

Cursor.new(session, message)

Parameters:

  • session (Session)

    The session.

  • query_operation (Message)

    The query message.

Since:

  • 1.0.0



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/moped/cursor.rb', line 81

def initialize(session, query_operation)
  @session = session

  @database    = query_operation.database
  @collection  = query_operation.collection
  @selector    = query_operation.selector

  @cursor_id = 0
  @limit = query_operation.limit
  @limited = @limit > 0
  @batch_size = query_operation.batch_size || @limit

  @options = {
    request_id: query_operation.request_id,
    flags: query_operation.flags,
    limit: query_operation.limit,
    skip: query_operation.skip,
    fields: query_operation.fields,
  }
end

Instance Attribute Details

#get_more_opObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/moped/cursor.rb', line 15

def get_more_op
  @get_more_op
end

#get_more_op The get more message.(Thegetmoremessage.) ⇒ Object (readonly)



15
# File 'lib/moped/cursor.rb', line 15

attr_reader :get_more_op, :kill_cursor_op, :query_op, :session

#kill_cursor_opObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/moped/cursor.rb', line 15

def kill_cursor_op
  @kill_cursor_op
end

#kill_cursor_op The kill cursor message.(Thekillcursormessage.) ⇒ Object (readonly)



15
# File 'lib/moped/cursor.rb', line 15

attr_reader :get_more_op, :kill_cursor_op, :query_op, :session

#query_opObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/moped/cursor.rb', line 15

def query_op
  @query_op
end

#query_op The query message.(Thequerymessage.) ⇒ Object (readonly)



15
# File 'lib/moped/cursor.rb', line 15

attr_reader :get_more_op, :kill_cursor_op, :query_op, :session

#sessionObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/moped/cursor.rb', line 15

def session
  @session
end

#session The session.(Thesession.) ⇒ Object (readonly)



15
# File 'lib/moped/cursor.rb', line 15

attr_reader :get_more_op, :kill_cursor_op, :query_op, :session

Instance Method Details

#eachEnumerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Iterate over the results of the query.

Examples:

Iterate over the results.

cursor.each do |doc|
  #...
end

Returns:

  • (Enumerator)

    The cursor enum.

Since:

  • 1.0.0



27
28
29
30
31
32
33
34
35
# File 'lib/moped/cursor.rb', line 27

def each
  documents = load_docs
  documents.each { |doc| yield doc }
  while more?
    return kill if limited? && @limit <= 0
    documents = get_more
    documents.each { |doc| yield doc }
  end
end

#get_moreArray<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get more documents from the database for the cursor. Executes a get more command.

Examples:

Get more docs.

cursor.get_more

Returns:

  • (Array<Hash>)

    The next batch of documents.

Since:

  • 1.0.0



46
47
48
49
50
51
52
53
# File 'lib/moped/cursor.rb', line 46

def get_more
  with_retry(session.cluster) do
    reply = @node.get_more @database, @collection, @cursor_id, request_limit
    @limit -= reply.count if limited?
    @cursor_id = reply.cursor_id
    reply.documents
  end
end

#killObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Kill the cursor.

Examples:

Kill the cursor.

cursor.kill

Returns:

  • (Object)

    The result of the kill cursors command.

Since:

  • 1.0.0



110
111
112
# File 'lib/moped/cursor.rb', line 110

def kill
  @node.kill_cursors([ @cursor_id ])
end

#limited?true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Does the cursor have a limit provided in the query?

Examples:

Is the cursor limited?

cursor.limited?

Returns:

  • (true, false)

    If a limit has been provided over zero.

Since:

  • 1.0.0



122
123
124
# File 'lib/moped/cursor.rb', line 122

def limited?
  @limited
end

#load_docsArray<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load the documents from the database.

Examples:

Load the documents.

cursor.load_docs

Returns:

  • (Array<Hash>)

    The documents.

Since:

  • 1.0.0



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/moped/cursor.rb', line 134

def load_docs
  @options[:flags] |= [:no_cursor_timeout] if @options[:no_timeout]
  options = @options.clone
  options[:limit] = request_limit

  reply, @node = read_preference.with_node(session.cluster) do |node|
    [ node.query(@database, @collection, @selector, query_options(options)), node ]
  end

  @limit -= reply.count if limited?
  @cursor_id = reply.cursor_id
  reply.documents
end

#more?true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Are there more documents to be returned from the database?

Examples:

Are there more documents?

cursor.more?

Returns:

  • (true, false)

    If there are more documents to load.

Since:

  • 1.0.0



156
157
158
# File 'lib/moped/cursor.rb', line 156

def more?
  @cursor_id != 0
end

#request_limitInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine the request limit for the query

Examples:

What is the cursor request_limit

cursor.request_limit

Returns:

  • (Integer)

Since:

  • 1.0.0



64
65
66
67
68
69
70
# File 'lib/moped/cursor.rb', line 64

def request_limit
  if limited?
    @batch_size < @limit ? @batch_size : @limit
  else
    @batch_size
  end
end