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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, timeout, options = {}) ⇒ 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.

  • options (Hash) (defaults to: {})

    Options for the connection.

Options Hash (options):

  • :ssl (Boolean)

    Connect using SSL

Since:

  • 1.0.0



82
83
84
85
86
# File 'lib/moped/connection.rb', line 82

def initialize(host, port, timeout, options = {})
  @sock = nil
  @request_id = 0
  @host, @port, @timeout, @options = host, port, timeout, options
end

Instance Attribute Details

#hostObject (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.



13
14
15
# File 'lib/moped/connection.rb', line 13

def host
  @host
end

#optionsObject (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.



13
14
15
# File 'lib/moped/connection.rb', line 13

def options
  @options
end

#portObject (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.



13
14
15
# File 'lib/moped/connection.rb', line 13

def port
  @port
end

#timeoutObject (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.



13
14
15
# File 'lib/moped/connection.rb', line 13

def 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



23
24
25
# File 'lib/moped/connection.rb', line 23

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:

  • (TCPSocket)

    The socket.

Since:

  • 1.0.0



35
36
37
38
39
40
41
# File 'lib/moped/connection.rb', line 35

def connect
  @sock = if !!options[:ssl]
    Sockets::SSL.connect(host, port, timeout)
  else
    Sockets::TCP.connect(host, port, timeout)
  end
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



51
52
53
# File 'lib/moped/connection.rb', line 51

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



63
64
65
66
67
68
# File 'lib/moped/connection.rb', line 63

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/moped/connection.rb', line 96

def read
  with_connection do |socket|
    reply = Protocol::Reply.allocate
    data = read_data(socket, 36)
    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 = read_data(socket, 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



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

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



149
150
151
152
153
154
155
156
157
158
# File 'lib/moped/connection.rb', line 149

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