ezmobius-redis 0.0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ezra Zygmuntowicz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,31 @@
1
+ # redis-rb
2
+
3
+ A ruby client library for the redis key value storage system.
4
+
5
+ ## Information about redis
6
+
7
+ Redis is a key value store with some interesting features:
8
+ 1. It's fast.
9
+ 2. Keys are strings but values can have types of "NONE", "STRING", "LIST", or "SET". List's can be atomically push'd, pop'd, lpush'd, lpop'd and indexed. This allows you to store things like lists of comments under one key while retaining the ability to append comments without reading and putting back the whole list.
10
+
11
+ See [redis on code.google.com](http://code.google.com/p/redis/wiki/README) for more information.
12
+
13
+ ## Dependencies
14
+
15
+ 1. redis -
16
+
17
+ rake redis:install
18
+
19
+ 2. dtach -
20
+
21
+ rake dtach:install
22
+
23
+ 3. svn - git is the new black, but we need it for the google codes.
24
+
25
+ ## Setup
26
+
27
+ Use the tasks mentioned above (in Dependencies) to get your machine setup.
28
+
29
+ ## Examples
30
+
31
+ Check the examples/ directory. *Note* you need to have redis-server running first.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'date'
5
+ require 'spec/rake/spectask'
6
+ require 'tasks/redis.tasks'
7
+
8
+
9
+ GEM = 'redis'
10
+ GEM_NAME = 'redis'
11
+ GEM_VERSION = '0.0.3.3'
12
+ AUTHORS = ['Ezra Zygmuntowicz', 'Taylor Weibley', 'Matthew Clark']
13
+ EMAIL = "matt.clark@punchstock.com"
14
+ HOMEPAGE = "http://github.com/winescout/redis-rb"
15
+ SUMMARY = "Ruby client library for redis key value storage server"
16
+
17
+ spec = Gem::Specification.new do |s|
18
+ s.name = GEM
19
+ s.version = GEM_VERSION
20
+ s.platform = Gem::Platform::RUBY
21
+ s.has_rdoc = true
22
+ s.extra_rdoc_files = ["LICENSE"]
23
+ s.summary = SUMMARY
24
+ s.description = s.summary
25
+ s.authors = AUTHORS
26
+ s.email = EMAIL
27
+ s.homepage = HOMEPAGE
28
+
29
+ # Uncomment this to add a dependency
30
+ # s.add_dependency "foo"
31
+
32
+ s.require_path = 'lib'
33
+ s.autorequire = GEM
34
+ s.files = %w(LICENSE README.markdown Rakefile) + Dir.glob("{lib,spec}/**/*")
35
+ end
36
+
37
+ task :default => :spec
38
+
39
+ desc "Run specs"
40
+ Spec::Rake::SpecTask.new do |t|
41
+ t.spec_files = FileList['spec/**/*_spec.rb']
42
+ t.spec_opts = %w(-fs --color)
43
+ end
44
+
45
+ Rake::GemPackageTask.new(spec) do |pkg|
46
+ pkg.gem_spec = spec
47
+ end
48
+
49
+ desc "install the gem locally"
50
+ task :install => [:package] do
51
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
52
+ end
53
+
54
+ desc "create a gemspec file"
55
+ task :make_spec do
56
+ File.open("#{GEM}.gemspec", "w") do |file|
57
+ file.puts spec.to_ruby
58
+ end
59
+ end
data/lib/dist_redis.rb ADDED
@@ -0,0 +1,111 @@
1
+ require 'redis'
2
+ require 'hash_ring'
3
+ class DistRedis
4
+ attr_reader :ring
5
+ def initialize(*servers)
6
+ srvs = []
7
+ servers.each do |s|
8
+ server, port = s.split(':')
9
+ srvs << Redis.new(:host => server, :port => port)
10
+ end
11
+ @ring = HashRing.new srvs
12
+ end
13
+
14
+ def node_for_key(key)
15
+ if key =~ /\{(.*)?\}/
16
+ key = $1
17
+ end
18
+ @ring.get_node(key)
19
+ end
20
+
21
+ def add_server(server)
22
+ server, port = server.split(':')
23
+ @ring.add_node Redis.new(:host => server, :port => port)
24
+ end
25
+
26
+ def method_missing(sym, *args, &blk)
27
+ if redis = node_for_key(args.first.to_s)
28
+ redis.send sym, *args, &blk
29
+ else
30
+ super
31
+ end
32
+ end
33
+
34
+ def keys(glob)
35
+ keyz = []
36
+ @ring.nodes.each do |red|
37
+ keyz.concat red.keys(glob)
38
+ end
39
+ keyz
40
+ end
41
+
42
+ def save
43
+ @ring.nodes.each do |red|
44
+ red.save
45
+ end
46
+ end
47
+
48
+ def bgsave
49
+ @ring.nodes.each do |red|
50
+ red.bgsave
51
+ end
52
+ end
53
+
54
+ def quit
55
+ @ring.nodes.each do |red|
56
+ red.quit
57
+ end
58
+ end
59
+
60
+ def delete_cloud!
61
+ @ring.nodes.each do |red|
62
+ red.keys("*").each do |key|
63
+ red.delete key
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+
71
+ if __FILE__ == $0
72
+
73
+ r = DistRedis.new 'localhost:6379', 'localhost:6380', 'localhost:6381', 'localhost:6382'
74
+ r['urmom'] = 'urmom'
75
+ r['urdad'] = 'urdad'
76
+ r['urmom1'] = 'urmom1'
77
+ r['urdad1'] = 'urdad1'
78
+ r['urmom2'] = 'urmom2'
79
+ r['urdad2'] = 'urdad2'
80
+ r['urmom3'] = 'urmom3'
81
+ r['urdad3'] = 'urdad3'
82
+ p r['urmom']
83
+ p r['urdad']
84
+ p r['urmom1']
85
+ p r['urdad1']
86
+ p r['urmom2']
87
+ p r['urdad2']
88
+ p r['urmom3']
89
+ p r['urdad3']
90
+
91
+ r.push_tail 'listor', 'foo1'
92
+ r.push_tail 'listor', 'foo2'
93
+ r.push_tail 'listor', 'foo3'
94
+ r.push_tail 'listor', 'foo4'
95
+ r.push_tail 'listor', 'foo5'
96
+
97
+ p r.pop_tail('listor')
98
+ p r.pop_tail('listor')
99
+ p r.pop_tail('listor')
100
+ p r.pop_tail('listor')
101
+ p r.pop_tail('listor')
102
+
103
+ puts "key distribution:"
104
+
105
+ r.ring.nodes.each do |red|
106
+ p [red.port, red.keys("*")]
107
+ end
108
+ r.delete_cloud!
109
+ p r.keys('*')
110
+
111
+ end
data/lib/hash_ring.rb ADDED
@@ -0,0 +1,127 @@
1
+ require 'zlib'
2
+
3
+ class HashRing
4
+
5
+ POINTS_PER_SERVER = 160 # this is the default in libmemcached
6
+
7
+ attr_reader :ring, :sorted_keys, :replicas, :nodes
8
+
9
+ # nodes is a list of objects that have a proper to_s representation.
10
+ # replicas indicates how many virtual points should be used pr. node,
11
+ # replicas are required to improve the distribution.
12
+ def initialize(nodes=[], replicas=POINTS_PER_SERVER)
13
+ @replicas = replicas
14
+ @ring = {}
15
+ @nodes = []
16
+ @sorted_keys = []
17
+ nodes.each do |node|
18
+ add_node(node)
19
+ end
20
+ end
21
+
22
+ # Adds a `node` to the hash ring (including a number of replicas).
23
+ def add_node(node)
24
+ @nodes << node
25
+ @replicas.times do |i|
26
+ key = Zlib.crc32("#{node}:#{i}")
27
+ @ring[key] = node
28
+ @sorted_keys << key
29
+ end
30
+ @sorted_keys.sort!
31
+ end
32
+
33
+ def remove_node(node)
34
+ @replicas.times do |i|
35
+ key = Zlib.crc32("#{node}:#{count}")
36
+ @ring.delete(key)
37
+ @sorted_keys.reject! {|k| k == key}
38
+ end
39
+ end
40
+
41
+ # get the node in the hash ring for this key
42
+ def get_node(key)
43
+ get_node_pos(key)[0]
44
+ end
45
+
46
+ def get_node_pos(key)
47
+ return [nil,nil] if @ring.size == 0
48
+ crc = Zlib.crc32(key)
49
+ idx = HashRing.binary_search(@sorted_keys, crc)
50
+ return [@ring[@sorted_keys[idx]], idx]
51
+ end
52
+
53
+ def iter_nodes(key)
54
+ return [nil,nil] if @ring.size == 0
55
+ node, pos = get_node_pos(key)
56
+ @sorted_keys[pos..-1].each do |k|
57
+ yield @ring[k]
58
+ end
59
+ end
60
+
61
+ class << self
62
+
63
+ # gem install RubyInline to use this code
64
+ # Native extension to perform the binary search within the hashring.
65
+ # There's a pure ruby version below so this is purely optional
66
+ # for performance. In testing 20k gets and sets, the native
67
+ # binary search shaved about 12% off the runtime (9sec -> 8sec).
68
+ begin
69
+ require 'inline'
70
+ inline do |builder|
71
+ builder.c <<-EOM
72
+ int binary_search(VALUE ary, unsigned int r) {
73
+ int upper = RARRAY_LEN(ary) - 1;
74
+ int lower = 0;
75
+ int idx = 0;
76
+
77
+ while (lower <= upper) {
78
+ idx = (lower + upper) / 2;
79
+
80
+ VALUE continuumValue = RARRAY_PTR(ary)[idx];
81
+ unsigned int l = NUM2UINT(continuumValue);
82
+ if (l == r) {
83
+ return idx;
84
+ }
85
+ else if (l > r) {
86
+ upper = idx - 1;
87
+ }
88
+ else {
89
+ lower = idx + 1;
90
+ }
91
+ }
92
+ return upper;
93
+ }
94
+ EOM
95
+ end
96
+ rescue Exception => e
97
+ # Find the closest index in HashRing with value <= the given value
98
+ def binary_search(ary, value, &block)
99
+ upper = ary.size - 1
100
+ lower = 0
101
+ idx = 0
102
+
103
+ while(lower <= upper) do
104
+ idx = (lower + upper) / 2
105
+ comp = ary[idx] <=> value
106
+
107
+ if comp == 0
108
+ return idx
109
+ elsif comp > 0
110
+ upper = idx - 1
111
+ else
112
+ lower = idx + 1
113
+ end
114
+ end
115
+ return upper
116
+ end
117
+
118
+ end
119
+ end
120
+
121
+ end
122
+
123
+ # ring = HashRing.new ['server1', 'server2', 'server3']
124
+ # p ring
125
+ # #
126
+ # p ring.get_node "kjhjkjlkjlkkh"
127
+ #
data/lib/pipeline.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "redis"
2
+
3
+ class Redis
4
+ class Pipeline < Redis
5
+ BUFFER_SIZE = 50_000
6
+
7
+ def initialize(redis)
8
+ @redis = redis
9
+ @commands = []
10
+ end
11
+
12
+ def get_response
13
+ end
14
+
15
+ def write(data)
16
+ @commands << data
17
+ write_and_read if @commands.size >= BUFFER_SIZE
18
+ end
19
+
20
+ def finish
21
+ write_and_read
22
+ end
23
+
24
+ def write_and_read
25
+ @redis.write @commands.join
26
+ @redis.read_socket
27
+ @commands.clear
28
+ end
29
+
30
+ end
31
+ end