Class: EventMachine::Protocols::Couchbase::Node

Inherits:
Connection
  • Object
show all
Includes:
EM::Deferrable
Defined in:
lib/em-couchbase/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Node

Returns a new instance of Node.



35
36
37
38
39
40
41
42
# File 'lib/em-couchbase/node.rb', line 35

def initialize(options = {})
  @data = ""  # naive buffer implementation
  options.each do |k, v|
    if respond_to?(k)
      instance_variable_set("@#{k}", v)
    end
  end
end

Instance Attribute Details

#adminObject (readonly)

Returns the value of attribute admin.



31
32
33
# File 'lib/em-couchbase/node.rb', line 31

def admin
  @admin
end

#clientObject (readonly)

Returns the value of attribute client.



26
27
28
# File 'lib/em-couchbase/node.rb', line 26

def client
  @client
end

#couchObject (readonly)

Returns the value of attribute couch.



30
31
32
# File 'lib/em-couchbase/node.rb', line 30

def couch
  @couch
end

#directObject (readonly)

Returns the value of attribute direct.



29
30
31
# File 'lib/em-couchbase/node.rb', line 29

def direct
  @direct
end

#proxyObject (readonly)

Returns the value of attribute proxy.



28
29
30
# File 'lib/em-couchbase/node.rb', line 28

def proxy
  @proxy
end

#statusObject (readonly)

Returns the value of attribute status.



32
33
34
# File 'lib/em-couchbase/node.rb', line 32

def status
  @status
end

#versionObject (readonly)

Returns the value of attribute version.



33
34
35
# File 'lib/em-couchbase/node.rb', line 33

def version
  @version
end

Class Method Details

.connect(options) ⇒ Object



44
45
46
47
# File 'lib/em-couchbase/node.rb', line 44

def self.connect(options)
  host, port = options[:direct].split(':')
  EventMachine.connect(host, port, self, options)
end

Instance Method Details

#==(other) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/em-couchbase/node.rb', line 101

def ==(other)
  other = Node.new(other) if other.is_a?(Hash)
  self.class == other.class &&
    self.direct == other.direct &&
    self.couch == other.couch &&
    self.status == other.status
end

#arithm(opcode, opaque, vbucket, key, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/em-couchbase/node.rb', line 74

def arithm(opcode, opaque, vbucket, key, options = {})
  if options.is_a?(Fixnum)
    options = {:delta => options}
  end
  packet = Packet.build(opaque, vbucket,
                        opcode, key,
                        options[:delta],
                        options[:initial],
                        options[:expiration],
                        options[:cas])
  client.register_packet(opaque, packet)
  send_data(packet)
end

#authenticateObject (protected)



111
112
113
114
115
116
117
118
119
120
# File 'lib/em-couchbase/node.rb', line 111

def authenticate
  if client.config.sasl_password
    # currently Couchbase supports PLAIN only authentication
    # this is why it doesn't ask list of mechanisms
    packet = Packet.build(0, 0, :sasl_auth, "PLAIN",
                          client.config.bucket_name,
                          client.config.sasl_password)
    send_data(packet)
  end
end

#connection_completedObject (protected)



122
123
124
125
# File 'lib/em-couchbase/node.rb', line 122

def connection_completed
  authenticate
  succeed
end

#get(tuples, options = {}) ⇒ Object

Parameters:

  • opaque (Fixnum)
  • pairs (Array)

    array of tuples [opaque, vbucket, key]

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


91
92
93
94
95
96
97
98
99
# File 'lib/em-couchbase/node.rb', line 91

def get(tuples, options = {})
  packets = ""
  tuples.each do |opaque, vbucket, key|
    packet = Packet.build(opaque, vbucket, :get, key)
    client.register_packet(opaque, packet)
    packets << packet
  end
  send_data(packets)
end

#receive_data(data) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/em-couchbase/node.rb', line 49

def receive_data(data)
  @data << data
  Packet.parse(@data) do |op, opaque, result|
    if result.error.class == Error::NotMyVbucket
      client.retry(:not_my_vbucket, opaque)
    else
      if op == :sasl_auth
        raise result.error unless result.success?
      else
        client.run_callback(opaque, result)
      end
    end
  end
end

#set(opaque, vbucket, key, value, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/em-couchbase/node.rb', line 64

def set(opaque, vbucket, key, value, options = {})
  packet = Packet.build(opaque, vbucket, :set,
                        key, value,
                        options[:flags],
                        options[:expiration],
                        options[:cas])
  client.register_packet(opaque, packet)
  send_data(packet)
end