roulette 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env bash
2
+
3
+ ruby_string="1.9.2"
4
+ gemset_name="roulette"
5
+
6
+ if rvm list strings | grep -q "${ruby_string}" ; then
7
+
8
+ rvm use "${ruby_string}@${gemset_name}" --create
9
+
10
+ # Complain if bundler isn't installed
11
+ if [[ -z "`gem which bundler 2>&1 | grep -v ERROR`" ]]; then
12
+ echo "You need bundler:"
13
+ echo ""
14
+ echo " gem install bundler"
15
+ echo ""
16
+ fi
17
+ else
18
+ echo "${ruby_string} was not found, please run 'rvm install ${ruby_string}' and then cd back into the project directory."
19
+ fi
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development, :test do
4
+ gem 'rake', '~>0.8.7'
5
+ gem 'jeweler', '~>1.5.2'
6
+ gem "autotest-standalone"
7
+ gem "autotest-growl"
8
+ end
9
+
10
+ group :test do
11
+ gem 'rspec', '~> 2.5'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ autotest-growl (0.2.9)
5
+ autotest-standalone (4.5.5)
6
+ diff-lcs (1.1.2)
7
+ git (1.2.5)
8
+ jeweler (1.5.2)
9
+ bundler (~> 1.0.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ rake (0.8.7)
13
+ rspec (2.5.0)
14
+ rspec-core (~> 2.5.0)
15
+ rspec-expectations (~> 2.5.0)
16
+ rspec-mocks (~> 2.5.0)
17
+ rspec-core (2.5.2)
18
+ rspec-expectations (2.5.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.5.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ autotest-growl
27
+ autotest-standalone
28
+ jeweler (~> 1.5.2)
29
+ rake (~> 0.8.7)
30
+ rspec (~> 2.5)
data/README.md ADDED
@@ -0,0 +1,114 @@
1
+ Roulette
2
+ ======
3
+ `Super easy pre-sharding for your ruby NOSQL store. Presharding is used to naively scale your data-stores horizontally. `
4
+
5
+ [What is presharding](http://antirez.com/post/redis-presharding.html)
6
+
7
+ [Presharding at Craigslist](http://blog.zawodny.com/2011/02/26/redis-sharding-at-craigslist/)
8
+
9
+ Bang
10
+ ----
11
+ ### Works with Redis ###
12
+ redis_array = [Redis.new(...), Redis.new(...), Redis.new(...)]
13
+
14
+ roulette = Roulette.new(redis_array)
15
+ roulette.set("foo", "bar")
16
+ roulette.get("foo")
17
+ => "bar"
18
+
19
+ ### Works with Memcache ###
20
+ memcache_array = [Memcache.new(...), Memcache.new(...), Memcache.new(...)]
21
+
22
+ roulette = Roulette.new(memcache_array)
23
+ roulette.set("foo", "bar")
24
+ roulette.get("foo")
25
+ => "bar"
26
+
27
+ ### Works with ANY store that takes a key as it's first argument ###
28
+
29
+ Simply call roulette like you would call any of your client libraries. Roulette will automatically split the data between different machines.
30
+
31
+ Limitations
32
+ -----------
33
+ If your operator needs all the keys to be on one machine, such as reddis' SUNION, then you're out of luck. Don't use pre-sharding (yet) because this library doesn't support that.
34
+
35
+
36
+ .____.
37
+ xuu$``$$$uuu.
38
+ . $``$ $$$`$$$
39
+ dP*$ $ $$$ $$$
40
+ ?k $ $ $$$ $$$
41
+ $ $ $ $$$ $$$
42
+ ":$ $ $$$ $$$
43
+ N$ $ $$$ $$$
44
+ $$ $ $$$ $$$
45
+ $ $ $$$ $$$
46
+ $ $ $$$ $$$
47
+ $ $ $$$ $$$
48
+ $ $ $$$ $$$
49
+ $ $ $$$ $$$
50
+ $$#$ $$$ $$$
51
+ $$'$ $$$ $$$
52
+ $$`R $$$ $$$
53
+ $$$& $$$ $$$
54
+ $#*$ $$$ $$$
55
+ $ $ $$$ @$$
56
+ $ $ $$$ $$$
57
+ $ $ $$$ $$$
58
+ $ $ $B$ $$&.
59
+ $ $ $D$ $$$$$muL.
60
+ $ $ $Q$ $$$$$ `"**mu..
61
+ $ $ $R$ $$$$$ k `$$*t
62
+ $ @ $$$ $$$$$ k $$!4
63
+ $ x$uu@B8u$NB@$uuuu6...$$X?
64
+ $ $(`RF`$`````R$ $$5`"""#"R
65
+ $ $" M$ $ $$ $$$ ?
66
+ $ $ ?$ $ T$ $$$ $
67
+ $ $F H$ $ M$ $$K $ ..
68
+ $ $L $$ $ $$ $$R. "d$$$$Ns.
69
+ $ $~ $$ $ N$ $$X ." "%2h
70
+ $ 4k f $ *$ $$& R "iN
71
+ $ $$ %uz! tuuR$$: Buu ?`:
72
+ $ $F $??$8B | '*Ned*$~L$
73
+ $ $k $'@$$$ |$.suu+!' !$
74
+ $ ?N $'$$@$ $*` d:"
75
+ $ dL..........M.$&$$ 5 d"P
76
+ ..$.^"*I$RR*$C""??77*? "nu...n*L*
77
+ '$C"R ``""!$*@#""` .uor bu8BUU+!`
78
+ '*@m@. *d" *$Rouxxd"```$
79
+ R*@mu. "#$R *$ !
80
+ *%x. "*L $ %.
81
+ "N `%. ...u.d!` ..ue$$$o..
82
+ @ ". $*"""" .u$$$$$$$$$$$$beu...
83
+ 8 .mL % :R` x$$$$$$$$$$$$$$$$$$$$$$$$$$WmeemeeWc
84
+ |$e!" "s:k 4 d$N"`"#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$>
85
+ $$ "N @ $?$ <F$$$$$$$$$$$$$$$$$$$$$$$$$$$$>
86
+ $@ ^%Uu.. R#8buu$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$>
87
+ ```""*u$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$>
88
+ #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$>
89
+ "5$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$>
90
+ `*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$>
91
+ ^#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$>
92
+ "*$$$$$$$$$$$$$$$$$$$$$$$$$$>
93
+ `"*$$$$$$$$$$$$$$$$$$$$$$$>
94
+ ^!$$$$$$$$$$$$$$$$$$$$>
95
+ `"#+$$$$$$$$$$$$$$>
96
+ ""**$$$$$$$$>
97
+ ```""
98
+
99
+
100
+ Next Steps
101
+ ==========
102
+ - Configuration Library
103
+ - Support specific operations to specific shards
104
+
105
+
106
+ Contribution
107
+ ============
108
+
109
+ Fork away. If you want to chat about a feature idea, or a question you can find me on the twitters [@schneems](http://twitter.com/schneems). Put any major changes into feature branches. Make sure all tests stay green, and make sure your changes are covered.
110
+
111
+
112
+ licensed under MIT License
113
+ Copyright (c) 2011 Schneems. See LICENSE.txt for
114
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "roulette"
16
+ gem.homepage = "http://github.com/Schnems/roulette"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Pre-Sharding so simple you'll shoot yourself :p }
19
+ gem.description = %Q{
20
+ Super easy pre-sharding for your ruby NOSQL store. Presharding is used to naively scale your data-stores horizontally.
21
+ }
22
+ gem.email = "richard.schneeman@gmail.com"
23
+ gem.authors = ["Schneems"]
24
+ gem.add_development_dependency "rspec"
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
data/lib/roulette.rb ADDED
@@ -0,0 +1,51 @@
1
+
2
+ # Roulette.config([Redis.new(..), Redis.new(...)])
3
+ #
4
+ # r = Roulette.new([Redis.new(..), Redis.new(...)])
5
+ #
6
+ #
7
+ # Roulette.shard(:whatevs).sadd("key", "value")
8
+ # roulette = Roulette.new(REDIS, REDIS2, REDIS3, ...)
9
+ # roulette.set("key", "value")
10
+ # roulette.get("key")
11
+ # >> "value"
12
+
13
+ require 'digest/sha1'
14
+
15
+ class Roulette
16
+ attr_accessor :stores
17
+
18
+ def initialize(*stores)
19
+ self.stores = stores
20
+ end
21
+
22
+ def method_missing(method_name, *args, &blk)
23
+ Transaction.new(@stores, *args).fire(method_name)
24
+ end
25
+
26
+ class Transaction
27
+ attr_accessor :key, :args, :stores
28
+
29
+ def initialize(stores, *args)
30
+ self.stores = stores
31
+ self.args = *args
32
+ extract_args
33
+ end
34
+
35
+ def extract_args
36
+ self.key = args.first.to_s
37
+ end
38
+
39
+ def fire(method)
40
+ select_store.send method.to_sym, *args
41
+ end
42
+
43
+ private
44
+ def select_store
45
+ val = Digest::SHA1.hexdigest(self.key).unpack('Q').join.to_i
46
+ store_index = val % stores.count
47
+ @stores[store_index]
48
+ end
49
+ end
50
+
51
+ end
data/license.txt ADDED
@@ -0,0 +1,22 @@
1
+ licensed under MIT License:
2
+
3
+ Copyright (c) 2011 Schneems
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/pkg/roulette-.gem ADDED
Binary file
Binary file
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ class FakeKeyValueStore
3
+
4
+ end
5
+
6
+
7
+ describe Roulette do
8
+ describe 'method missing' do
9
+ before do
10
+ @kv_store = FakeKeyValueStore.new
11
+ @roulette = Roulette.new(@kv_store)
12
+ end
13
+
14
+ it 'passes, key and value to the stored KV_store' do
15
+ @kv_store.should_receive(:sadd).with("foo", "bar")
16
+ @roulette.sadd("foo", "bar")
17
+ end
18
+
19
+ it 'passes, key and value to the stored KV_store' do
20
+ @kv_store.should_receive(:sadd).with("foo", "bar")
21
+ @roulette.sadd("foo", "bar")
22
+ end
23
+
24
+ it 'passes key to the stored KV_store' do
25
+ @kv_store.should_receive(:scard).with("foo")
26
+ @roulette.scard("foo")
27
+ end
28
+
29
+ it 'passes arbitrary number of elements to the stored KV_store' do
30
+ @kv_store.should_receive(:fake_method).with(1,2,3,4,5,6,7,8,9,0)
31
+ @roulette.fake_method(1,2,3,4,5,6,7,8,9,0)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ class FakeKeyValueStore
3
+
4
+ end
5
+
6
+
7
+ describe Roulette::Transaction do
8
+ before do
9
+ @kv_store = FakeKeyValueStore.new
10
+ @key = "key"
11
+ @args = [@key, "value"]
12
+ end
13
+
14
+ describe '#new' do
15
+ it "keeps args, stores, and key" do
16
+ transaction = Roulette::Transaction.new([@kv_store], *@args)
17
+ transaction.stores.should eq([@kv_store])
18
+ transaction.key.should eq(@key)
19
+ transaction.args.should eq(@args)
20
+ end
21
+ end
22
+
23
+ describe '#extract_args' do
24
+ it "pulls out the key" do
25
+ transaction = Roulette::Transaction.new([@kv_store], *@args)
26
+ transaction.extract_args.should eq(@key)
27
+ end
28
+ end
29
+
30
+ describe '#select_store' do
31
+ it "picks a store" do
32
+ transaction = Roulette::Transaction.new([@kv_store], *@args)
33
+ store = transaction.send :select_store
34
+ store.should eq(@kv_store)
35
+ end
36
+
37
+ it "selects different stores for different keys" do
38
+ @kv_store_deux = FakeKeyValueStore.new
39
+ store = Roulette::Transaction.new([@kv_store, @kv_store_deux], "key").send :select_store
40
+ store_deux = Roulette::Transaction.new([@kv_store, @kv_store_deux], "different_key").send :select_store
41
+ store.should_not eq(store_deux)
42
+ end
43
+ end
44
+
45
+ describe "#fire" do
46
+ it "sends a method to a kv_store" do
47
+ fired_method = :sadd
48
+ @kv_store.should_receive(fired_method)
49
+ transaction = Roulette::Transaction.new([@kv_store], *@args)
50
+ transaction.fire(fired_method)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../..', 'lib'))
6
+
7
+ require 'roulette'
8
+ require 'rspec'
9
+ require 'rspec/autorun'
10
+
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roulette
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Schneems
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-08 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rake
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.8.7
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: jeweler
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.5.2
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: autotest-standalone
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: autotest-growl
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rspec
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ description: "\n Super easy pre-sharding for your ruby NOSQL store. Presharding is used to naively scale your data-stores horizontally. \n "
72
+ email: richard.schneeman@gmail.com
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files:
78
+ - README.md
79
+ files:
80
+ - .rvmrc
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - README.md
84
+ - Rakefile
85
+ - VERSION
86
+ - autotest/discover.rb
87
+ - lib/roulette.rb
88
+ - license.txt
89
+ - pkg/roulette-.gem
90
+ - pkg/roulette-0.0.1.gem
91
+ - spec/roulette/roulette_spec.rb
92
+ - spec/roulette/transaction_spec.rb
93
+ - spec/spec_helper.rb
94
+ has_rdoc: true
95
+ homepage: http://github.com/Schnems/roulette
96
+ licenses:
97
+ - MIT
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3776716065281298446
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: "0"
118
+ requirements: []
119
+
120
+ rubyforge_project:
121
+ rubygems_version: 1.6.2
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Pre-Sharding so simple you'll shoot yourself :p
125
+ test_files:
126
+ - spec/roulette/roulette_spec.rb
127
+ - spec/roulette/transaction_spec.rb
128
+ - spec/spec_helper.rb