class Monga::Cursor

Constants

CLOSED_CURSOR
CLOSE_TIMEOUT

Batch kill cursors marked to be killed each CLOSE_TIMEOUT seconds

CURSORS

Attributes

cursor_id[R]

Public Class Methods

batch_kill(conn) click to toggle source
# File lib/monga/cursor.rb, line 66
def self.batch_kill(conn)
  cursors = CURSORS.select{ |k,v| v }
  cursor_ids = cursors.keys
  if cursor_ids.any?
    Monga.logger.debug("Following cursors are going to be deleted: #{cursor_ids}")
    kill_cursors(conn, cursor_ids)
    cursor_ids.each{ |id| CURSORS.delete id }
  end
end
create(connection, db_name, collection_name, options = {}, flags = {}) click to toggle source
# File lib/monga/cursor.rb, line 5
def self.create(connection, db_name, collection_name, options = {}, flags = {})
  if connection.type == :em
    CallbackCursor.new(connection, db_name, collection_name, options, flags)
  else
    BlockCursor.new(connection, db_name, collection_name, options, flags)
  end
end
new(connection, db_name, collection_name, options = {}, flags = {}) click to toggle source
# File lib/monga/cursor.rb, line 18
def initialize(connection, db_name, collection_name, options = {}, flags = {})
  @connection = connection
  @db_name = db_name
  @collection_name = collection_name
  @options = options
  @options.merge!(flags)

  @fetched_docs = []
  @count = 0

  @options[:limit] ||= 0
end

Private Class Methods

kill_cursors(connection, cursor_ids) click to toggle source
# File lib/monga/cursor.rb, line 140
def self.kill_cursors(connection, cursor_ids)
  Monga::Protocol::KillCursors.new(connection, cursor_ids: [*cursor_ids]).perform
end

Public Instance Methods

batch_size(val) click to toggle source
# File lib/monga/cursor.rb, line 43
def batch_size(val)
  @options[:batch_size] = val and self
end
explain() click to toggle source
# File lib/monga/cursor.rb, line 47
def explain
  @options[:explain] = true and self
end
flag(opt) click to toggle source
# File lib/monga/cursor.rb, line 31
def flag(opt)
  @options.merge!(opt) and self
end
hint() click to toggle source
# File lib/monga/cursor.rb, line 51
def hint
  @options[:hint] = true and self
end
kill() click to toggle source
# File lib/monga/cursor.rb, line 59
def kill
  return if @cursor_id == CLOSED_CURSOR
  self.class.kill_cursors(@connection, @cursor_id)
  CURSORS.delete @cursor_id
  @cursor_id = 0
end
limit(val) click to toggle source
# File lib/monga/cursor.rb, line 35
def limit(val)
  @options[:limit] = val and self
end
mark_to_kill() click to toggle source

Sometime in future all marked cursors will be killed in batch

# File lib/monga/cursor.rb, line 77
def mark_to_kill
  CURSORS[@cursor_id] = true if @cursor_id && alive?
  @cursor_id = 0
end
skip(val) click to toggle source
# File lib/monga/cursor.rb, line 39
def skip(val)
  @options[:skip] = val and self
end
sort(val) click to toggle source
# File lib/monga/cursor.rb, line 55
def sort(val)
  @options[:sort] = val and self
end

Private Instance Methods

alive?() click to toggle source

If cursor_id is not setted, or if isn't CLOSED_CURSOR - cursor is alive

# File lib/monga/cursor.rb, line 115
def alive?
  @cursor_id != CLOSED_CURSOR
end
get_batch_size() click to toggle source

Cursor will get exact amount of docs as user passed with `limit` opr

# File lib/monga/cursor.rb, line 132
def get_batch_size
  if @options[:limit] > 0 && @options[:batch_size]
    rest < @options[:batch_size] ? rest : @options[:batch_size]
  else @options[:batch_size]
    @options[:batch_size]
  end
end
get_more(batch_size, &block) click to toggle source
# File lib/monga/cursor.rb, line 84
def get_more(batch_size, &block)
  blk = proc do |err, data|
    if err
      mark_to_kill
      block.call(err)
    else
      @cursor_id = data[5]
      fetched_docs = data.last
      @count += fetched_docs.count
      mark_to_kill unless more?
      block.call(nil, fetched_docs, more?)
    end
  end
  if @cursor_id
    if @cursor_id == CLOSED_CURSOR
      err = Monga::Exceptions::ClosedCursor.new "You are trying to use closed cursor"
      block.call(err)
    else
      opts = @options.merge(cursor_id: @cursor_id, batch_size: batch_size)
      Monga::Protocol::GetMore.new(@connection, @db_name, @collection_name, opts).callback_perform(&blk)
    end
  else
    Monga::Protocol::Query.new(@connection, @db_name, @collection_name, @options).callback_perform(&blk)
  end
end
more?() click to toggle source
# File lib/monga/cursor.rb, line 110
def more?
  alive? && !satisfied?
end
rest() click to toggle source

How many docs should be returned

# File lib/monga/cursor.rb, line 127
def rest
  @options[:limit] - @count if @options[:limit] > 0
end
satisfied?() click to toggle source

If global limit is setted we will be satisfied when we will get limit amount of documents. Otherwise we are not satisfied untill crsor is alive

# File lib/monga/cursor.rb, line 122
def satisfied?
  @options[:limit] > 0 && @count >= @options[:limit]
end