Class: Monga::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/monga/request.rb

Constant Summary collapse

FLAGS =
{}
OP_CODES =
{
  reply:           1,
  msg:          1000,
  update:       2001,
  insert:       2002,
  reserved:     2003,
  query:        2004,
  get_more:     2005,
  delete:       2006,
  kill_cursors: 2007,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, db_name, collection_name, options = {}) ⇒ Request

Returns a new instance of Request.



18
19
20
21
22
23
24
25
26
# File 'lib/monga/request.rb', line 18

def initialize(connection, db_name, collection_name, options = {})
  @connection = connection
  @db_name = db_name
  @collection_name = collection_name
  @options = options

  check_flags
  @request_id = self.class.request_id
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/monga/request.rb', line 3

def connection
  @connection
end

#request_idObject (readonly)

Returns the value of attribute request_id.



3
4
5
# File 'lib/monga/request.rb', line 3

def request_id
  @request_id
end

Instance Method Details

#callback_performObject

Fire and wait



43
44
45
46
47
48
49
50
51
52
# File 'lib/monga/request.rb', line 43

def callback_perform
  @connection.send_command(command, @request_id) do |data|
    err, resp = parse_response(data)
    if block_given?
      yield(err, resp)
    else
      err ? raise(err) : resp
    end
  end
end

#commandObject



28
29
30
# File 'lib/monga/request.rb', line 28

def command
  header + body
end

#headerObject



32
33
34
# File 'lib/monga/request.rb', line 32

def header
  ::BinUtils.append_int32_le!(nil, command_length, @request_id, 0, op_code)
end

#parse_response(data) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/monga/request.rb', line 54

def parse_response(data)
  if Exception === data
    [data, nil]
  else
    flags = data[4]
    docs = data.last
    if flags & 2**0 > 0
      Monga::Exceptions::CursorNotFound.new(docs.first)
    elsif flags & 2**1 > 0
      Monga::Exceptions::QueryFailure.new(docs.first)
    elsif docs.first && (docs.first["err"] || docs.first["errmsg"])
      Monga::Exceptions::QueryFailure.new(docs.first)
    else
      [nil, data]
    end
  end
end

#performObject

Fire and Forget



37
38
39
40
# File 'lib/monga/request.rb', line 37

def perform
  @connection.send_command(command, @request_id)
  self
end