class Roma::Client::RomaClient

Class to access ROMA .

Attributes

default_hash_name[RW]
retry_count_read[RW]
retry_count_write[RW]
rttable[RW]
sender[RW]

Public Class Methods

new(ini_nodes, plugin_modules = nil, start_sync_routing_proc = true) click to toggle source

ROMA client constractor .

ini_nodes

ROMA nodes array

plugin_modules

set plugin modules if you use .

   # File lib/roma/client/rclient.rb
26 def initialize(ini_nodes, plugin_modules = nil, start_sync_routing_proc = true)
27   @retry_count_write = 10
28   @retry_count_read = 5
29   @default_hash_name = 'roma'
30 
31   if plugin_modules
32     plugin_modules.each do |plugin|
33       self.extend plugin
34     end
35   end
36 
37   init_sender
38   update_rttable(ini_nodes.map { |n| n.sub(':', '_') })
39   init_sync_routing_proc if start_sync_routing_proc
40 end

Public Instance Methods

[](key) click to toggle source

Get value from ROMA .

key

key for roma.

returen

value sotored roma .
If key don't exit ROMA, return nil .
If coneect error, throw Exception .
    # File lib/roma/client/rclient.rb
111 def [](key)
112   get(key)
113 end
[]=(key, value) click to toggle source

Set value to ROMA . please see set method .

    # File lib/roma/client/rclient.rb
101 def []=(key, value)
102   set(key, value)
103 end
add(key, val, expt = 0, raw = false) click to toggle source

Add value to ROMA .

If same key exists in ROMA, this method don't overwrite value and return NOT_STORED .

key

key for store .

value

store value .

exp

expire seconds .

raw

You set this value true, value isn't marshaled .

return

return follow set status .

  • If method is success, return STORED .

  • If same key exists in ROMA, return NOT_STORED .

  • If server error, return SERVER_ERROR .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
155 def add(key, val, expt = 0, raw = false)
156   val = Marshal.dump(val) unless raw
157   sender(:oneline_receiver, key, val, "add %s 0 %d %d", expt.to_i, val.length)
158 end
append(key, val, expt = 0) click to toggle source

Append value to exists string .

If same key exists in ROMA, this method append value . If same key doesn't exist in ROMA this method don't store value and return NOT_STORE .

key

key for append .

value

append value .

exp

expire seconds .

return

return follow set status .

  • If method is success, return STORED .

  • If same key exists in ROMA, return NOT_STORED .

  • If server error, return SERVER_ERROR .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
202 def append(key, val, expt = 0)
203   sender(:oneline_receiver, key, val, "append %s 0 %d %d", expt.to_i, val.length)
204 end
cas(key, expt = 0, raw = false) { |val| ... } click to toggle source

Compare And Swap value .

key

key for cas .

value

store value .

exp

expire seconds .

return

return follow set status .

  • If method is success, return STORED .

  • If method cannot update value, return EXISTS .

  • If key doesn't exist in ROMA, this method return NOT_FOUND.

  • If server error, return SERVER_ERROR .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
243 def cas(key, expt = 0, raw = false)
244   raise "A block is required" unless block_given?
245 
246   (val, casid) = gets_with_casid(key, raw)
247   return "NOT_FOUND" unless val
248   updated = yield val
249   val = raw ? updated : Marshal.dump(updated)
250 
251   sender(:oneline_receiver, key, val, "cas %s 0 %d %d %s", expt.to_i, val.length, casid)
252 end
decr(key, val = 1) click to toggle source

decrement value .

key

key for decremental .

val

decremental value .

return

Fixnum decrementaled value .

If same key doesn't exist in ROMA, return NOT_FOUND .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
373 def decr(key, val = 1)
374   ret = sender(:oneline_receiver, key, nil, "decr %s %d", val.to_i)
375   return ret if ret =~ /\D/
376   ret.to_i
377 end
delete(key) click to toggle source

Delete value .

key

key for delete .

return

return follow set status .

  • If method is success, return DELETED .

  • If same key doesn't exist in ROMA, return NOT_FOUND .

  • If server error, return SERVER_ERROR .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
266 def delete(key)
267   sender(:oneline_receiver, key, nil, "delete %s")
268 end
flush_all() click to toggle source
    # File lib/roma/client/rclient.rb
340 def flush_all()
341   raise RuntimeError.new("Unsupported yet") # TODO
342   @sender.send_flush_all_command
343 end
get(key, raw = false) click to toggle source

get value

key

key for get .

raw

If you set this value true, value isn't Marshal.load value .

return

return stored value in ROMA .

If key doesn't exist in ROMA, this method return nil .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
301 def get(key, raw = false)
302   val = sender(:value_list_receiver, key, nil, "get %s")[0]
303   return nil if val.nil?
304   val = Marshal.load(val) unless raw
305   val
306 end
gets(keys, raw = false) click to toggle source

get values .

keys

key array for get .

raw

If you set this value true, value isn't Marshal.load value .

return

return key and sotored value hash .

If all key doesn't exist in ROMA, return empty hash . If some key doesn't exist in ROMA, return exist key and sotred value hash .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
320 def gets(keys, raw = false)
321   kn = {}
322   keys.each{|key|
323     nid, d = @rttable.search_node(key)
324     kn[nid] ||= []
325     kn[nid] << key
326   }
327 
328   res = {}
329   kn.each_pair{|nid,ks|
330     res.merge!(gets_sender(nid, ks))
331   }
332   unless raw
333     res.each do |key, val|
334       res[key] = Marshal.load(val)
335     end
336   end
337   res
338 end
incr(key, val = 1) click to toggle source

increment value .

key

key for incremental .

val

incremental value .

return

Fixnum incrementaled value .

If same key doesn't exist in ROMA, return NOT_FOUND .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
356 def incr(key, val = 1)
357   ret = sender(:oneline_receiver, key, nil, "incr %s %d", val.to_i)
358   return ret if ret =~ /\D/
359   ret.to_i
360 end
make_rttable(node) click to toggle source
   # File lib/roma/client/rclient.rb
80 def make_rttable(node)
81   mklhash = @sender.send_route_mklhash_command(node)
82   return nil unless mklhash
83 
84   if @rttable && @rttable.mklhash == mklhash
85     return @rttable
86   end
87 
88   rd = @sender.send_routedump_command(node)
89   if rd
90     ret = ClientRoutingTable.new(rd)
91     ret.mklhash = mklhash
92     return ret
93   end
94   nil
95 rescue
96   nil
97 end
out(key) click to toggle source

Delete value completely .

This method delete value completely. “completely” means Don't set delete flag in server, but delete value in storage . Delete method set delete flag, but delete value soon .

key

key for delete .

return

return follow set status .

  • If method is success, return DELETED .

  • If same key doesn't exist in ROMA, return NOT_FOUND .

  • If server error, return SERVER_ERROR .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
286 def out(key)
287   sender(:oneline_receiver, key, nil, "out %s")
288 end
prepend(key, val, expt = 0) click to toggle source

Prepend value to exists string .

If same key exists in ROMA, this method prepend value . If same key doesn't exist in ROMA this method don't store value and return NOT_STORE .

key

key for prepend .

value

prepend value .

exp

expire seconds .

return

return follow set status .

  • If method is success, return STORED .

  • If same key exists in ROMA, return NOT_STORED .

  • If server error, return SERVER_ERROR .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
224 def prepend(key, val, expt = 0)
225   sender(:oneline_receiver, key, val, "prepend %s 0 %d %d", expt.to_i, val.length)
226 end
replace(key, val, expt = 0, raw = false) click to toggle source

Add value to ROMA .

If same key exists in ROMA, this method overwrite value . If same key doesn't exist in ROMA this method don't store value and return NOT_STORE .

key

key for store .

value

store value .

exp

expire seconds .

raw

You set this value true, value isn't marshaled .

return

return follow set status .

  • If method is success, return STORED .

  • If same key exists in ROMA, return NOT_STORED .

  • If server error, return SERVER_ERROR .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
179 def replace(key, val, expt = 0, raw = false)
180   val = Marshal.dump(val) unless raw
181   sender(:oneline_receiver, key, val, "replace %s 0 %d %d", expt.to_i, val.length)
182 end
rttable_last_update() click to toggle source
   # File lib/roma/client/rclient.rb
76 def rttable_last_update
77   @rttable_last_update
78 end
set(key, val, expt = 0, raw = false) click to toggle source

Set value to ROMA .

Both same same key exists or not exists in ROMA, this method set value .

key

key for store .

value

store value .

exp

expire seconds .

raw

You set this value true, value isn't marshaled .

return

return follow set status .

  • If method is success, return STORED .

  • If method is not stored, return NOT_STORED .

  • If server error, return SERVER_ERROR .

If socket error occured, throw Exception .

If socket timeout occured, throw TimeoutError .

    # File lib/roma/client/rclient.rb
132 def set(key, val, expt = 0, raw = false)
133   val = Marshal.dump(val) unless raw
134   sender(:oneline_receiver, key, val, "set %s 0 %d %d", expt.to_i, val.length)
135 end
stats(filter: "", node: @rttable.nodes.first) click to toggle source
    # File lib/roma/client/rclient.rb
379 def stats(filter: "", node: @rttable.nodes.first)
380   @sender.send_stats_command(filter, node)
381 end
update_rttable(nodes = self.rttable.nodes) click to toggle source
   # File lib/roma/client/rclient.rb
61 def update_rttable(nodes = self.rttable.nodes)
62   raise RuntimeError.new("nodes must not be nil.") unless nodes
63 
64   nodes.each { |node|
65     rt = make_rttable(node)
66     if rt
67       @rttable = rt
68       @rttable_last_update = Time.now
69       return
70     end
71   }
72 
73   raise RuntimeError.new("fatal error")
74 end
verbosity() click to toggle source
    # File lib/roma/client/rclient.rb
388 def verbosity
389   raise RuntimeError.new("Unsupported yet") # TODO
390   @sender.send_verbosity_command
391 end
version() click to toggle source
    # File lib/roma/client/rclient.rb
383 def version
384   raise RuntimeError.new("Unsupported yet") # TODO
385   @sender.send_version_command
386 end

Private Instance Methods

gets_sender(nid, keys) click to toggle source
    # File lib/roma/client/rclient.rb
419 def gets_sender(nid, keys)
420   cmd = "gets"
421   keys.each{ |k|
422     cmd << " #{k}\e#{@default_hash_name}"
423   }
424 
425   timeout(@@timeout){
426     return @sender.send_command(nid, cmd, nil, :value_hash_receiver)
427   }
428 rescue => e
429   unless e.instance_of?(RuntimeError)
430     @rttable.proc_failed(nid)
431     ConPool.instance.delete_connection(nid)
432   end
433   sleep 0.3
434   retry if (cnt ||= 0; cnt += 1) < @retry_count_write
435   raise e
436 end
gets_with_casid(key, raw = false) click to toggle source
    # File lib/roma/client/rclient.rb
395 def gets_with_casid(key, raw = false)
396   ret = sender(:value_casid_receiver, key, nil, "gets %s")
397   return [nil, nil] if ret.size <= 0
398   ret[0] = Marshal.load(ret[0]) unless raw
399   ret
400 end
init_sender() click to toggle source
   # File lib/roma/client/rclient.rb
56 def init_sender
57   @sender = Sender.new
58 end
init_sync_routing_proc() click to toggle source
   # File lib/roma/client/rclient.rb
42 def init_sync_routing_proc
43   Thread.new do
44     begin
45       loop do
46         sleep 10
47         update_rttable
48       end
49     rescue => e
50       puts "#{e}\n#{$@}"
51     end
52   end
53 end