Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ruby client library updated. Important changes in this new version!
  • Loading branch information
antirez committed Mar 27, 2009
1 parent 1a46014 commit 29fac61
Show file tree
Hide file tree
Showing 14 changed files with 699 additions and 810 deletions.
3 changes: 3 additions & 0 deletions client-libraries/ruby/.gitignore
@@ -0,0 +1,3 @@
nohup.out
redis/*
rdsrv
12 changes: 0 additions & 12 deletions client-libraries/ruby/README.rdoc

This file was deleted.

2 changes: 1 addition & 1 deletion client-libraries/ruby/Rakefile
Expand Up @@ -7,7 +7,7 @@ require 'tasks/redis.tasks'


GEM = 'redis'
GEM_VERSION = '0.0.2'
GEM_VERSION = '0.0.3'
AUTHORS = ['Ezra Zygmuntowicz', 'Taylor Weibley']
EMAIL = "ez@engineyard.com"
HOMEPAGE = "http://github.com/ezmobius/redis-rb"
Expand Down
13 changes: 13 additions & 0 deletions client-libraries/ruby/examples/test_server.rb
@@ -0,0 +1,13 @@
require 'socket'
require 'pp'
require File.join(File.dirname(__FILE__), '../lib/redis')

#require File.join(File.dirname(__FILE__), '../lib/server')


#r = Redis.new
#loop do

# puts "--------------------------------------"
# sleep 12
#end
11 changes: 0 additions & 11 deletions client-libraries/ruby/fill.rb

This file was deleted.

3 changes: 3 additions & 0 deletions client-libraries/ruby/lib/better_timeout.rb
Expand Up @@ -163,6 +163,9 @@ def timeout(n, e=Timeout::Error, &block) # :nodoc:
# Another name for Timeout::Error, defined for backwards compatibility with
# earlier versions of timeout.rb.

class Object
remove_const(:TimeoutError) if const_defined?(:TimeoutError)
end
TimeoutError = Timeout::Error # :nodoc:

if __FILE__ == $0
Expand Down
12 changes: 6 additions & 6 deletions client-libraries/ruby/lib/dist_redis.rb
Expand Up @@ -24,7 +24,7 @@ def add_server(server)
end

def method_missing(sym, *args, &blk)
if redis = node_for_key(args.first)
if redis = node_for_key(args.first.to_s)
redis.send sym, *args, &blk
else
super
Expand Down Expand Up @@ -94,11 +94,11 @@ def delete_cloud!
r.push_tail 'listor', 'foo4'
r.push_tail 'listor', 'foo5'

p r.pop_tail 'listor'
p r.pop_tail 'listor'
p r.pop_tail 'listor'
p r.pop_tail 'listor'
p r.pop_tail 'listor'
p r.pop_tail('listor')
p r.pop_tail('listor')
p r.pop_tail('listor')
p r.pop_tail('listor')
p r.pop_tail('listor')

puts "key distribution:"

Expand Down
88 changes: 71 additions & 17 deletions client-libraries/ruby/lib/hash_ring.rb
@@ -1,10 +1,15 @@
require 'digest/md5'
require 'zlib'

class HashRing

POINTS_PER_SERVER = 160 # this is the default in libmemcached

attr_reader :ring, :sorted_keys, :replicas, :nodes

# nodes is a list of objects that have a proper to_s representation.
# replicas indicates how many virtual points should be used pr. node,
# replicas are required to improve the distribution.
def initialize(nodes=[], replicas=3)
def initialize(nodes=[], replicas=POINTS_PER_SERVER)
@replicas = replicas
@ring = {}
@nodes = []
Expand All @@ -18,7 +23,7 @@ def initialize(nodes=[], replicas=3)
def add_node(node)
@nodes << node
@replicas.times do |i|
key = gen_key("#{node}:#{i}")
key = Zlib.crc32("#{node}:#{i}")
@ring[key] = node
@sorted_keys << key
end
Expand All @@ -27,7 +32,7 @@ def add_node(node)

def remove_node(node)
@replicas.times do |i|
key = gen_key("#{node}:#{count}")
key = Zlib.crc32("#{node}:#{count}")
@ring.delete(key)
@sorted_keys.reject! {|k| k == key}
end
Expand All @@ -40,15 +45,9 @@ def get_node(key)

def get_node_pos(key)
return [nil,nil] if @ring.size == 0
key = gen_key(key)
nodes = @sorted_keys
nodes.size.times do |i|
node = nodes[i]
if key <= node
return [@ring[node], i]
end
end
[@ring[nodes[0]], 0]
crc = Zlib.crc32(key)
idx = HashRing.binary_search(@sorted_keys, crc)
return [@ring[@sorted_keys[idx]], idx]
end

def iter_nodes(key)
Expand All @@ -59,11 +58,66 @@ def iter_nodes(key)
end
end

def gen_key(key)
key = Digest::MD5.hexdigest(key)
((key[3] << 24) | (key[2] << 16) | (key[1] << 8) | key[0])
class << self

# gem install RubyInline to use this code
# Native extension to perform the binary search within the hashring.
# There's a pure ruby version below so this is purely optional
# for performance. In testing 20k gets and sets, the native
# binary search shaved about 12% off the runtime (9sec -> 8sec).
begin
require 'inline'
inline do |builder|
builder.c <<-EOM
int binary_search(VALUE ary, unsigned int r) {
int upper = RARRAY_LEN(ary) - 1;
int lower = 0;
int idx = 0;
while (lower <= upper) {
idx = (lower + upper) / 2;
VALUE continuumValue = RARRAY_PTR(ary)[idx];
unsigned int l = NUM2UINT(continuumValue);
if (l == r) {
return idx;
}
else if (l > r) {
upper = idx - 1;
}
else {
lower = idx + 1;
}
}
return upper;
}
EOM
end
rescue Exception => e
# Find the closest index in HashRing with value <= the given value
def binary_search(ary, value, &block)
upper = ary.size - 1
lower = 0
idx = 0

while(lower <= upper) do
idx = (lower + upper) / 2
comp = ary[idx] <=> value

if comp == 0
return idx
elsif comp > 0
upper = idx - 1
else
lower = idx + 1
end
end
return upper
end

end
end

end

# ring = HashRing.new ['server1', 'server2', 'server3']
Expand Down

0 comments on commit 29fac61

Please sign in to comment.