Class: Moped::Connection Private

Inherits:
Object show all
Defined in:
lib/moped/connection.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.

This class contains behaviour of database socket connections.

Defined Under Namespace

Classes: TCPSocket

Instance Method Summary collapse

Constructor Details

#initializeConnection

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 connection.

Examples:

Initialize the connection.

Connection.new

Since:

  • 1.0.0



71
72
73
74
# File 'lib/moped/connection.rb', line 71

def initialize
  @sock = nil
  @request_id = 0
end

Instance Method Details

#alive?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.

Is the connection alive?

Examples:

Is the connection alive?

connection.alive?

Returns:

  • (true, false)

    If the connection is alive.

Since:

  • 1.0.0



18
19
20
# File 'lib/moped/connection.rb', line 18

def alive?
  connected? ? @sock.alive? : false
end

#connect(host, port, timeout) ⇒ TCPSocket

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.

Connect to the server.

Examples:

Connect to the server.

connection.connect("127.0.0.1", 27017, 30)

Parameters:

  • host (String)

    The host to connect to.

  • post (Integer)

    The server port.

  • timeout (Integer)

    The connection timeout.

Returns:

Since:

  • 1.0.0



34
35
36
# File 'lib/moped/connection.rb', line 34

def connect(host, port, timeout)
  @sock = TCPSocket.connect host, port, timeout
end

#connected?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.

Is the connection connected?

Examples:

Is the connection connected?

connection.connected?

Returns:

  • (true, false)

    If the connection is connected.

Since:

  • 1.0.0



46
47
48
# File 'lib/moped/connection.rb', line 46

def connected?
  !!@sock
end

#disconnectnil

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.

Disconnect from the server.

Examples:

Disconnect from the server.

connection.disconnect

Returns:

  • (nil)

    nil.

Since:

  • 1.0.0



58
59
60
61
62
63
# File 'lib/moped/connection.rb', line 58

def disconnect
  @sock.close
rescue
ensure
  @sock = nil
end

#readHash

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.

Read from the connection.

Examples:

Read from the connection.

connection.read

Returns:

  • (Hash)

    The returned document.

Since:

  • 1.0.0



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/moped/connection.rb', line 84

def read
  reply = Protocol::Reply.allocate
  reply.length,
    reply.request_id,
    reply.response_to,
    reply.op_code,
    reply.flags,
    reply.cursor_id,
    reply.offset,
    reply.count = @sock.read(36).unpack('l<5q<l<2')

  if reply.count == 0
    reply.documents = []
  else
    buffer = StringIO.new(@sock.read(reply.length - 36))

    reply.documents = reply.count.times.map do
      BSON::Document.deserialize(buffer)
    end
  end
  reply
end

#receive_replies(operations) ⇒ Array<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 the replies to the database operation.

Examples:

Get the replies.

connection.receive_replies(operations)

Parameters:

  • operations (Array<Message>)

    The query or get more ops.

Returns:

  • (Array<Hash>)

    The returned deserialized documents.

Since:

  • 1.0.0



117
118
119
120
121
# File 'lib/moped/connection.rb', line 117

def receive_replies(operations)
  operations.map do |operation|
    operation.receive_replies(self)
  end
end

#write(operations) ⇒ Integer

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.

Write to the connection.

Examples:

Write to the connection.

connection.write(data)

Parameters:

  • operations (Array<Message>)

    The database operations.

Returns:

  • (Integer)

    The number of bytes written.

Since:

  • 1.0.0



133
134
135
136
137
138
139
140
# File 'lib/moped/connection.rb', line 133

def write(operations)
  buf = ""
  operations.each do |operation|
    operation.request_id = (@request_id += 1)
    operation.serialize(buf)
  end
  @sock.write(buf)
end