Skip to content

Commit 6a81ebc

Browse files
antirezjmedrano
authored andcommitted
Ruby client lib updated to the latest git version
Signed-off-by: jmedrano <[email protected]>
1 parent 7a7a635 commit 6a81ebc

File tree

9 files changed

+172
-122
lines changed

9 files changed

+172
-122
lines changed

client-libraries/ruby/Rakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require 'tasks/redis.tasks'
99
GEM = 'redis'
1010
GEM_NAME = 'redis'
1111
GEM_VERSION = '0.1'
12-
AUTHORS = ['Ezra Zygmuntowicz', 'Taylor Weibley', 'Matthew Clark']
12+
AUTHORS = ['Ezra Zygmuntowicz', 'Taylor Weibley', 'Matthew Clark', 'Brian McKinney', 'Salvatore Sanfilippo', 'Luca Guidi']
1313
1414
HOMEPAGE = "http://github.com/ezmobius/redis-rb"
1515
SUMMARY = "Ruby client library for redis key value storage server"
@@ -28,7 +28,7 @@ spec = Gem::Specification.new do |s|
2828
s.add_dependency "rspec"
2929
s.require_path = 'lib'
3030
s.autorequire = GEM
31-
s.files = %w(LICENSE README.markdown Rakefile) + Dir.glob("{lib,spec}/**/*")
31+
s.files = %w(LICENSE README.markdown Rakefile) + Dir.glob("{lib,tasks,spec}/**/*")
3232
end
3333

3434
task :default => :spec

client-libraries/ruby/examples/test_server.rb

-13
This file was deleted.

client-libraries/ruby/lib/dist_redis.rb

+40-34
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,80 @@ class DistRedis
44
attr_reader :ring
55
def initialize(opts={})
66
hosts = []
7-
7+
88
db = opts[:db] || nil
9-
timeout = opts[:timeout] || nil
9+
timeout = opts[:timeout] || nil
1010

1111
raise Error, "No hosts given" unless opts[:hosts]
1212

1313
opts[:hosts].each do |h|
1414
host, port = h.split(':')
15-
hosts << Redis.new(:host => host, :port => port, :db => db, :timeout => timeout, :db => db)
15+
hosts << Redis.new(:host => host, :port => port, :db => db, :timeout => timeout)
1616
end
1717

18-
@ring = HashRing.new hosts
18+
@ring = HashRing.new hosts
1919
end
20-
20+
2121
def node_for_key(key)
22-
if key =~ /\{(.*)?\}/
23-
key = $1
24-
end
22+
key = $1 if key =~ /\{(.*)?\}/
2523
@ring.get_node(key)
2624
end
27-
25+
2826
def add_server(server)
2927
server, port = server.split(':')
3028
@ring.add_node Redis.new(:host => server, :port => port)
3129
end
32-
30+
3331
def method_missing(sym, *args, &blk)
3432
if redis = node_for_key(args.first.to_s)
3533
redis.send sym, *args, &blk
3634
else
3735
super
3836
end
3937
end
40-
38+
4139
def keys(glob)
42-
keyz = []
43-
@ring.nodes.each do |red|
44-
keyz.concat red.keys(glob)
40+
@ring.nodes.map do |red|
41+
red.keys(glob)
4542
end
46-
keyz
4743
end
48-
44+
4945
def save
50-
@ring.nodes.each do |red|
51-
red.save
52-
end
46+
on_each_node :save
5347
end
54-
48+
5549
def bgsave
56-
@ring.nodes.each do |red|
57-
red.bgsave
58-
end
50+
on_each_node :bgsave
5951
end
60-
52+
6153
def quit
62-
@ring.nodes.each do |red|
63-
red.quit
64-
end
54+
on_each_node :quit
55+
end
56+
57+
def flush_all
58+
on_each_node :flush_all
59+
end
60+
alias_method :flushall, :flush_all
61+
62+
def flush_db
63+
on_each_node :flush_db
6564
end
66-
65+
alias_method :flushdb, :flush_db
66+
6767
def delete_cloud!
6868
@ring.nodes.each do |red|
6969
red.keys("*").each do |key|
7070
red.delete key
71-
end
71+
end
7272
end
7373
end
74-
74+
75+
def on_each_node(command, *args)
76+
@ring.nodes.each do |red|
77+
red.send(command, *args)
78+
end
79+
end
80+
7581
end
7682

7783

@@ -94,21 +100,21 @@ def delete_cloud!
94100
p r['urdad2']
95101
p r['urmom3']
96102
p r['urdad3']
97-
103+
98104
r.push_tail 'listor', 'foo1'
99105
r.push_tail 'listor', 'foo2'
100106
r.push_tail 'listor', 'foo3'
101107
r.push_tail 'listor', 'foo4'
102108
r.push_tail 'listor', 'foo5'
103-
109+
104110
p r.pop_tail('listor')
105111
p r.pop_tail('listor')
106112
p r.pop_tail('listor')
107113
p r.pop_tail('listor')
108114
p r.pop_tail('listor')
109-
115+
110116
puts "key distribution:"
111-
117+
112118
r.ring.nodes.each do |red|
113119
p [red.port, red.keys("*")]
114120
end

client-libraries/ruby/lib/hash_ring.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ def add_node(node)
3131
end
3232

3333
def remove_node(node)
34+
@nodes.reject!{|n| n.to_s == node.to_s}
3435
@replicas.times do |i|
35-
key = Zlib.crc32("#{node}:#{count}")
36+
key = Zlib.crc32("#{node}:#{i}")
3637
@ring.delete(key)
3738
@sorted_keys.reject! {|k| k == key}
3839
end

client-libraries/ruby/lib/redis.rb

+36-10
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Redis
3636
"smove" => true
3737
}
3838

39-
BOOLEAN_PROCESSOR = lambda{|r| r == 0 ? false : r}
39+
BOOLEAN_PROCESSOR = lambda{|r| r == 1 }
4040

4141
REPLY_PROCESSOR = {
4242
"exists" => BOOLEAN_PROCESSOR,
@@ -95,21 +95,34 @@ class Redis
9595
"type?" => "type"
9696
}
9797

98+
DISABLED_COMMANDS = {
99+
"monitor" => true,
100+
"sync" => true
101+
}
102+
98103
def initialize(options = {})
99104
@host = options[:host] || '127.0.0.1'
100105
@port = (options[:port] || 6379).to_i
101106
@db = (options[:db] || 0).to_i
102107
@timeout = (options[:timeout] || 5).to_i
103-
$debug = options[:debug]
108+
@password = options[:password]
109+
@logger = options[:logger]
110+
111+
@logger.info { self.to_s } if @logger
104112
connect_to_server
105113
end
106114

107115
def to_s
108-
"Redis Client connected to #{@host}:#{@port} against DB #{@db}"
116+
"Redis Client connected to #{server} against DB #{@db}"
117+
end
118+
119+
def server
120+
"#{@host}:#{@port}"
109121
end
110122

111123
def connect_to_server
112124
@sock = connect_to(@host, @port, @timeout == 0 ? nil : @timeout)
125+
call_command(["auth",@password]) if @password
113126
call_command(["select",@db]) unless @db == 0
114127
end
115128

@@ -147,15 +160,18 @@ def method_missing(*argv)
147160
end
148161

149162
def call_command(argv)
150-
puts argv.inspect if $debug
163+
@logger.debug { argv.inspect } if @logger
164+
151165
# this wrapper to raw_call_command handle reconnection on socket
152166
# error. We try to reconnect just one time, otherwise let the error
153167
# araise.
154168
connect_to_server if !@sock
169+
155170
begin
156171
raw_call_command(argv.dup)
157172
rescue Errno::ECONNRESET, Errno::EPIPE
158173
@sock.close
174+
@sock = nil
159175
connect_to_server
160176
raw_call_command(argv.dup)
161177
end
@@ -176,12 +192,13 @@ def raw_call_command(argvp)
176192
bulk = nil
177193
argv[0] = argv[0].to_s.downcase
178194
argv[0] = ALIASES[argv[0]] if ALIASES[argv[0]]
195+
raise "#{argv[0]} command is disabled" if DISABLED_COMMANDS[argv[0]]
179196
if BULK_COMMANDS[argv[0]] and argv.length > 1
180197
bulk = argv[-1].to_s
181-
argv[-1] = bulk.length
198+
argv[-1] = bulk.respond_to?(:bytesize) ? bulk.bytesize : bulk.size
182199
end
183-
command << argv.join(' ') + "\r\n"
184-
command << bulk + "\r\n" if bulk
200+
command << "#{argv.join(' ')}\r\n"
201+
command << "#{bulk}\r\n" if bulk
185202
end
186203

187204
@sock.write(command)
@@ -199,7 +216,7 @@ def select(*args)
199216
end
200217

201218
def [](key)
202-
get(key)
219+
self.get(key)
203220
end
204221

205222
def []=(key,value)
@@ -213,8 +230,8 @@ def set(key, value, expiry=nil)
213230
end
214231

215232
def sort(key, options = {})
216-
cmd = []
217-
cmd << "SORT #{key}"
233+
cmd = ["SORT"]
234+
cmd << key
218235
cmd << "BY #{options[:by]}" if options[:by]
219236
cmd << "GET #{[options[:get]].flatten * ' GET '}" if options[:get]
220237
cmd << "#{options[:order]}" if options[:order]
@@ -230,6 +247,15 @@ def decr(key,decrement = nil)
230247
call_command(decrement ? ["decrby",key,decrement] : ["decr",key])
231248
end
232249

250+
# Similar to memcache.rb's #get_multi, returns a hash mapping
251+
# keys to values.
252+
def mapped_mget(*keys)
253+
mget(*keys).inject({}) do |hash, value|
254+
key = keys.shift
255+
value.nil? ? hash : hash.merge(key => value)
256+
end
257+
end
258+
233259
# Ruby defines a now deprecated type method so we need to override it here
234260
# since it will never hit method_missing
235261
def type(key)

client-libraries/ruby/redis-rb.gemspec

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
Gem::Specification.new do |s|
44
s.name = %q{redis}
5-
s.version = "0.0.5"
5+
s.version = "0.1"
66

77
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8-
s.authors = ["Ezra Zygmuntowicz", "Taylor Weibley", "Matthew Clark"]
9-
#s.autorequire = %q{redis}
10-
s.date = %q{2009-03-31}
8+
s.authors = ["Ezra Zygmuntowicz", "Taylor Weibley", "Matthew Clark", "Brian McKinney", "Salvatore Sanfilippo", "Luca Guidi"]
9+
# s.autorequire = %q{redis-rb}
10+
s.date = %q{2009-06-23}
1111
s.description = %q{Ruby client library for redis key value storage server}
1212
s.email = %q{[email protected]}
1313
s.extra_rdoc_files = ["LICENSE"]
14-
s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/redis.rb", "lib/dist_redis.rb", "lib/hash_ring.rb", "lib/pipeline.rb", "lib/server.rb", "spec/redis_spec.rb", "spec/spec_helper.rb"]
14+
s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/dist_redis.rb", "lib/hash_ring.rb", "lib/pipeline.rb", "lib/redis.rb", "spec/redis_spec.rb", "spec/spec_helper.rb"]
1515
s.has_rdoc = true
16-
s.homepage = %q{http://github.com/winescout/redis-rb}
16+
s.homepage = %q{http://github.com/ezmobius/redis-rb}
1717
s.require_paths = ["lib"]
1818
s.rubygems_version = %q{1.3.1}
1919
s.summary = %q{Ruby client library for redis key value storage server}

0 commit comments

Comments
 (0)