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

#initialize(host, port, timeout) ⇒ Connection

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("localhost", 27017, 5)

Parameters:

  • host (String)

    The host to connect to.

  • post (Integer)

    The server port.

  • timeout (Integer)

    The connection timeout.

Since:

  • 1.0.0



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

def initialize(host, port, timeout)
  @sock = nil
  @request_id = 0
  @host = host
  @port = port
  @timeout = timeout
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



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

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

#connectTCPSocket

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 defined by @host, @port without timeout @timeout.

Examples:

Open the connection

connection.connect

Returns:

Since:

  • 1.0.0



29
30
31
# File 'lib/moped/connection.rb', line 29

def connect
  create_connection
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



41
42
43
# File 'lib/moped/connection.rb', line 41

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



53
54
55
56
57
58
# File 'lib/moped/connection.rb', line 53

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/moped/connection.rb', line 85

def read
  with_connection do |socket|
    reply = Protocol::Reply.allocate
    data = socket.read(36)
    unless data
      raise Errors::ConnectionFailure, "Attempted read from socket which returned no data."
    end
    response = data.unpack('l<5q<l<2')
    reply.length,
        reply.request_id,
        reply.response_to,
        reply.op_code,
        reply.flags,
        reply.cursor_id,
        reply.offset,
        reply.count = response

    if reply.count == 0
      reply.documents = []
    else
      sock_read = socket.read(reply.length - 36)

      buffer = StringIO.new(sock_read)

      reply.documents = reply.count.times.map do
        BSON::Document.deserialize(buffer)
      end
    end
    reply
  end
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



127
128
129
130
131
# File 'lib/moped/connection.rb', line 127

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



143
144
145
146
147
148
149
150
151
152
# File 'lib/moped/connection.rb', line 143

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