activerecord-shard_for 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a145bb643f713e31825e2bf87446c2657d10a1a4
4
- data.tar.gz: 01eedb436a6b730078c624cf539fb5cd402c9495
3
+ metadata.gz: 511906085a601bbdbaf769215dbd7e471480ee8c
4
+ data.tar.gz: de31a223085a86f97315a259b32ed0e3a12e9f18
5
5
  SHA512:
6
- metadata.gz: 7c642c62650b872380169af15830b0c96c772f6aaf90bfa74340bb8e499eb794a90f5f905cc4fc74c8414189ee7faffa894a576baffac8085d26d01d56a4704e
7
- data.tar.gz: 338d2512639fc8ababdad1d3b8f4afb4d1915026adc5d65622662be63237b6449b7d3cd4f546384461ef11c1bc3e881f7d8b1c879c10b3d024652930350fde3b
6
+ metadata.gz: 691b4787767101d7d36cf3227b93635fa6a340f6628a7a63376e2f94a5fa1f282d3882bc2a3945238d27ab5ca34a2323ad2f713741330f3161778f252fc67f91
7
+ data.tar.gz: e11f634a244d6e04bcf01541d37facde6d85026e4df76ce9490a08f68b1e6c7b1ba2d70e249551d93ac226aea4510a96dc1dedb98b83bfbb5b95792826ff9bc2
data/.rubocop.yml CHANGED
@@ -4,6 +4,7 @@ AllCops:
4
4
  - activerecord-shard_for.gemspec
5
5
  - Guardfile
6
6
  - lib/activerecord/tasks/activerecord_shard_for.rake
7
+ - gemfiles/**/*
7
8
  DisplayCopNames: true
8
9
 
9
10
  Style/FrozenStringLiteralComment:
data/.travis.yml CHANGED
@@ -1,4 +1,31 @@
1
1
  language: ruby
2
+ sudo: false
2
3
  rvm:
3
- - 2.3.0
4
- before_install: gem install bundler -v 1.11.2
4
+ - 2.2.4
5
+ - 2.3.1
6
+ before_install: gem install bundler -v 1.12.5
7
+ cache: bundler
8
+ script:
9
+ - CODECLIMATE_REPO_TOKEN=dfc951e2d70b56ee62ffe989eb6286a6f21bf57209ccbd111bd336c372d8d06e bundle exec rspec
10
+ - bundle exec rubocop
11
+ - bundle exec ruby spec/performance_test.rb
12
+ gemfile:
13
+ - gemfiles/ar_4.1.0.gemfile
14
+ - gemfiles/ar_4.1.7.gemfile
15
+ - gemfiles/ar_4.1.8.gemfile
16
+ - gemfiles/ar_4.2.gemfile
17
+ - gemfiles/ar_5.0.gemfile
18
+ - gemfiles/rails_edge.gemfile
19
+ matrix:
20
+ allow_failures:
21
+ - gemfile: gemfiles/rails_edge.gemfile
22
+ exclude:
23
+ - rvm: 2.1.9
24
+ gemfile: gemfiles/rails_edge.gemfile
25
+ - rvm: 2.1.9
26
+ gemfile: gemfiles/ar_5.0.gemfile
27
+ notifications:
28
+ slack:
29
+ secure: S1W/Lw+dH3wb8FfkMIWPZmr6M4Q6S2WMkSlanpKva1HM7K5QL7hXdmUl2yBUxJE26BHSsb1ScozMEzadyda2+i/W34UvZ7LXgKeHkUKEdjy/AmsSJPK1ZjMfgnv10tVgbEIusNb4bF/sSuChdZKK3ILwOlqIPDlQNdMwF1xRA2xt5J7tb26UgyIzoCI4P3bJYMULWsEkk+UwHiJH0YO9ulkTZI/j0N+hLXQLJTZPjmKtMk/tE0NbBmFVL4md89hUcR5gKTGGrNzEMJ58K+zqeDG/DubkcIbA5ZuqKv+oE5m0pDODZExxnC+oeENTvq/VfYwOfD0pTDrBNYjj+Bm3YiyGDzQAgov9XPDG8g/fKEs/LNAT79UZXkZlFO99Yn/vrYH9o5DKpOE9smENUXylb55MgLTUiYe17CTp7pB3trbJl3wwIbLjSmTjAdSUNgPv8qDP4uk3K4U32mknXCDDkU9EI7f6F731ocdoxsGarEBcPcgjs73Y84iwDteQp847Gigtgo4Y4TCWH657uzLolR2O8NSw+vWT0VNI9qtR5PZD7iVYtSp1qHtPAKowCztodewY2Nu+Ds9Z95udf4GPUkFg/SNEJPTPrQFLiiJZ8UYP8NJEuA+IP1tc2zG3zU/ADrjenRC1ZiupQG7OMH82y11408U6PcHFSlF+7NuDkac=
30
+ on_success: never
31
+ on_failure: change
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # CHANGELOG for activerecord-shard_for
2
+
3
+ ## 0.2.0
4
+
5
+ - Enable Range support for cluster key
6
+ - Add built-in connection router of DistkeyRouter
7
+
8
+ ## 0.1.2
9
+
10
+ - Fix `cluster_router` is now working
data/Guardfile CHANGED
@@ -1,4 +1,4 @@
1
- guard :rspec, cmd: 'bundle exec appraisal rspec' do
1
+ guard :rspec, cmd: 'bundle exec rspec' do
2
2
  require 'guard/rspec/dsl'
3
3
  dsl = Guard::RSpec::Dsl.new(self)
4
4
 
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # ActiveRecord::ShardFor
2
2
 
3
+ [![Build Status](https://travis-ci.org/yuemori/activerecord-shard_for.svg?branch=master)](https://travis-ci.org/yuemori/activerecord-shard_for) [![Dependency Status](https://gemnasium.com/badges/github.com/yuemori/activerecord-shard_for.svg)](https://gemnasium.com/github.com/yuemori/activerecord-shard_for) [![Code Climate](https://codeclimate.com/github/yuemori/activerecord-shard_for/badges/gpa.svg)](https://codeclimate.com/github/yuemori/activerecord-shard_for) [![Test Coverage](https://codeclimate.com/github/yuemori/activerecord-shard_for/badges/coverage.svg)](https://codeclimate.com/github/yuemori/activerecord-shard_for/coverage)
4
+
3
5
  This is Sharding Library for ActiveRecord, inspire and import codes from [mixed_gauge](https://github.com/taiki45/mixed_gauge) and [activerecord-sharding](https://github.com/hirocaster/activerecord-sharding) (Thanks!).
4
6
 
5
7
  ## Concept
@@ -108,7 +110,6 @@ User.all_shards_in_parallel.map(&count) #=> 1
108
110
  User.parallel.flat_map {|m| m.where(age: 1) }.size #=> 1
109
111
  ```
110
112
 
111
-
112
113
  Sometimes you want to generates distkey value before validation. Since activerecord-shard_for generates sub class of your models, AR's callback is usesless for this usecase, so activerecord-shard_for offers its own callback method.
113
114
 
114
115
  ```ruby
@@ -134,6 +135,34 @@ access_token = AccessToken.put!
134
135
  access_token.token #=> a generated token
135
136
  ```
136
137
 
138
+ If you want to range sharding, `Range` object set to cluster key.
139
+
140
+ ```ruby
141
+ ActiveRecord::ShardFor.configure do |config|
142
+ config.define_cluster(:user) do |cluster|
143
+ # unique identifier, connection name
144
+ cluster.register(0...100, :production_user_001)
145
+ cluster.register(100...200, :production_user_002)
146
+ cluster.register(200...300, :production_user_003)
147
+ cluster.register(300..Float::INFINITY, :production_user_004)
148
+ end
149
+ end
150
+
151
+ class User < ActiveRecord::Base
152
+ include ActiveRecord::ShardFor::Model
153
+ use_cluster :user, :distkey
154
+ def_distkey :id
155
+
156
+ def self.generate_unique_id
157
+ # Implement to generate unique id
158
+ end
159
+
160
+ before_put do |attributes|
161
+ attributes[:id] = generate_unique_id unless attributes[:id]
162
+ end
163
+ end
164
+ ```
165
+
137
166
  ## Sharding with Replication
138
167
 
139
168
  activerecord-shard_for also supports replication.
@@ -187,7 +216,7 @@ You can split read/write by defining AR model class for each connection:
187
216
  ```ruby
188
217
  class User < ActiveRecord::Base
189
218
  include ActiveRecord::ShardFor::Model
190
- use_cluster :user
219
+ use_cluster :user, :hash_modulo
191
220
  def_distkey :email
192
221
  end
193
222
 
@@ -195,7 +224,7 @@ class UserReadonly < ActiveRecord::Base
195
224
  self.table_name = 'users'
196
225
 
197
226
  include ActiveRecord::ShardFor::Model
198
- use_cluster :user_readonly
227
+ use_cluster :user_readonly, :hash_modulo
199
228
  def_distkey :email
200
229
  end
201
230
 
@@ -236,11 +265,38 @@ UserReadonly.all_shards do |readonly|
236
265
  end
237
266
  ```
238
267
 
239
- ## Plugin of cluster router
268
+ ## Plugin of connection router
269
+
270
+ If you need to advanced connection routing, implement router class and register this.
271
+
272
+ ### Embeded
240
273
 
241
- If you need to advanced cluster routing, implement router class and register this.
274
+ Embeded routers:
242
275
 
243
- Reference a interface to [HashModuloRouter](https://github.com/yuemori/activerecord-shard_for/blob/master/lib/activerecord/shard_for/hash_modulo_router.rb) and [ConnectionRouter](https://github.com/yuemori/activerecord-shard_for/blob/master/lib/activerecord/shard_for/connection_router.rb).
276
+ |name|class|description|
277
+ |:---:|:---:|:---|
278
+ |:hash_modulo|[HashModuloRouter](https://github.com/yuemori/activerecord-shard_for/blob/master/lib/activerecord/shard_for/hash_modulo_router.rb)|use `hash(key) mod connection_count`|
279
+ |:distkey|[DistkeyRouter](https://github.com/yuemori/activerecord-shard_for/blob/master/lib/activerecord/shard_for/distkey_router.rb)|use `distkey` at it is|
280
+
281
+ Connection Routers specific with cluster in AR model.
282
+
283
+ ```ruby
284
+ class User < ActiveRecord::Base
285
+ include ActiveRecord::ShardFor::Model
286
+ use_cluster :user, :hash_modulo # use hash_modulo
287
+ def_distkey :email
288
+ end
289
+
290
+ class Character < ActiveRecord::Base
291
+ include ActiveRecord::ShardFor::Model
292
+ use_cluster :character, :distkey # use distkey at it is
293
+ def_distkey :shard_no
294
+ end
295
+ ```
296
+
297
+ ### Implement
298
+
299
+ Reference a interface to [HashModuloRouter](https://github.com/yuemori/activerecord-shard_for/blob/master/lib/activerecord/shard_for/hash_modulo_router.rb), [DistkeyRouter](https://github.com/yuemori/activerecord-shard_for/blob/master/lib/activerecord/shard_for/distkey_router.rb) and [ConnectionRouter](https://github.com/yuemori/activerecord-shard_for/blob/master/lib/activerecord/shard_for/connection_router.rb).
244
300
 
245
301
  Example, simple modulo router:
246
302
 
@@ -256,7 +312,7 @@ Your initializer for activerecord-shard_for might be like this:
256
312
 
257
313
  ```ruby
258
314
  ActiveRecord::ShardFor.configure do |config|
259
- config.register_cluster_router(:modulo, SimpleModuloRouter)
315
+ config.register_connection_router(:modulo, SimpleModuloRouter)
260
316
  end
261
317
  ```
262
318
 
@@ -278,6 +334,47 @@ class User < ActiveRecord::Base
278
334
  end
279
335
  ```
280
336
 
337
+ ## Advanced
338
+
339
+ More example, sharding key is String:
340
+
341
+ ```ruby
342
+ ActiveRecord::ShardFor.configure do |config|
343
+ config.define_cluster(:user) do |cluster|
344
+ # unique identifier, connection name
345
+ cluster.register('a'..'z', :production_user_001)
346
+ cluster.register('A'..'Z', :production_user_002)
347
+ cluster.register('0'..'9', :production_user_003)
348
+ end
349
+ end
350
+
351
+ class InitialStringRouter < ActiveRecord::ShardFor::ConnectionRouter
352
+ def route(key)
353
+ key.to_s.first
354
+ end
355
+ end
356
+
357
+ ActiveRecord::ShardFor.configure do |config|
358
+ config.register_connection_router(:initial_string, InitialStringRouter)
359
+ end
360
+
361
+ class User < ActiveRecord::Base
362
+ include ActiveRecord::ShardFor::Model
363
+ use_cluster :user, :initial_string
364
+ def_distkey :id
365
+
366
+ def self.generate_unique_id
367
+ SecureRandom.uuid
368
+ end
369
+
370
+ before_put do |attributes|
371
+ attributes[:id] = generate_unique_id unless attributes[:id]
372
+ end
373
+ end
374
+
375
+ ```
376
+
377
+
281
378
  ## Contributing with ActiveRecord::ShardFor
282
379
 
283
380
  Contributors are welcome! This is what you need to setup your Octopus development environment:
@@ -37,4 +37,5 @@ Gem::Specification.new do |spec|
37
37
  spec.add_development_dependency 'appraisal'
38
38
  spec.add_development_dependency 'sqlite3'
39
39
  spec.add_development_dependency 'rspec-parameterized'
40
+ spec.add_development_dependency 'codeclimate-test-reporter'
40
41
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- activerecord-shard_for (0.1.0)
4
+ activerecord-shard_for (0.1.2)
5
5
  activerecord (>= 4.1.0)
6
6
  activesupport (>= 4.1.0)
7
7
  expeditor (>= 0.1.0)
@@ -35,6 +35,8 @@ GEM
35
35
  binding_of_caller (0.7.2)
36
36
  debug_inspector (>= 0.0.1)
37
37
  builder (3.2.2)
38
+ codeclimate-test-reporter (0.6.0)
39
+ simplecov (>= 0.7.1, < 1.0.0)
38
40
  coderay (1.1.1)
39
41
  concord (0.1.5)
40
42
  adamantium (~> 0.2.0)
@@ -44,6 +46,7 @@ GEM
44
46
  concurrent-ruby (~> 1.0.2)
45
47
  debug_inspector (0.0.2)
46
48
  diff-lcs (1.2.5)
49
+ docile (1.1.5)
47
50
  equalizer (0.0.11)
48
51
  expeditor (0.4.0)
49
52
  concurrent-ruby (~> 1.0.0)
@@ -143,6 +146,11 @@ GEM
143
146
  ruby-progressbar (1.8.1)
144
147
  ruby_dep (1.3.1)
145
148
  shellany (0.0.1)
149
+ simplecov (0.12.0)
150
+ docile (~> 1.1.0)
151
+ json (>= 1.8, < 3)
152
+ simplecov-html (~> 0.10.0)
153
+ simplecov-html (0.10.0)
146
154
  slop (3.6.0)
147
155
  sqlite3 (1.3.11)
148
156
  thor (0.19.1)
@@ -169,6 +177,7 @@ DEPENDENCIES
169
177
  activerecord-shard_for!
170
178
  appraisal
171
179
  bundler (~> 1.11)
180
+ codeclimate-test-reporter
172
181
  guard-rspec
173
182
  guard-rubocop
174
183
  pry
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- activerecord-shard_for (0.1.0)
4
+ activerecord-shard_for (0.1.2)
5
5
  activerecord (>= 4.1.0)
6
6
  activesupport (>= 4.1.0)
7
7
  expeditor (>= 0.1.0)
@@ -35,6 +35,8 @@ GEM
35
35
  binding_of_caller (0.7.2)
36
36
  debug_inspector (>= 0.0.1)
37
37
  builder (3.2.2)
38
+ codeclimate-test-reporter (0.6.0)
39
+ simplecov (>= 0.7.1, < 1.0.0)
38
40
  coderay (1.1.1)
39
41
  concord (0.1.5)
40
42
  adamantium (~> 0.2.0)
@@ -44,6 +46,7 @@ GEM
44
46
  concurrent-ruby (~> 1.0.2)
45
47
  debug_inspector (0.0.2)
46
48
  diff-lcs (1.2.5)
49
+ docile (1.1.5)
47
50
  equalizer (0.0.11)
48
51
  expeditor (0.4.0)
49
52
  concurrent-ruby (~> 1.0.0)
@@ -143,6 +146,11 @@ GEM
143
146
  ruby-progressbar (1.8.1)
144
147
  ruby_dep (1.3.1)
145
148
  shellany (0.0.1)
149
+ simplecov (0.12.0)
150
+ docile (~> 1.1.0)
151
+ json (>= 1.8, < 3)
152
+ simplecov-html (~> 0.10.0)
153
+ simplecov-html (0.10.0)
146
154
  slop (3.6.0)
147
155
  sqlite3 (1.3.11)
148
156
  thor (0.19.1)
@@ -169,6 +177,7 @@ DEPENDENCIES
169
177
  activerecord-shard_for!
170
178
  appraisal
171
179
  bundler (~> 1.11)
180
+ codeclimate-test-reporter
172
181
  guard-rspec
173
182
  guard-rubocop
174
183
  pry
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- activerecord-shard_for (0.1.0)
4
+ activerecord-shard_for (0.1.2)
5
5
  activerecord (>= 4.1.0)
6
6
  activesupport (>= 4.1.0)
7
7
  expeditor (>= 0.1.0)
@@ -35,6 +35,8 @@ GEM
35
35
  binding_of_caller (0.7.2)
36
36
  debug_inspector (>= 0.0.1)
37
37
  builder (3.2.2)
38
+ codeclimate-test-reporter (0.6.0)
39
+ simplecov (>= 0.7.1, < 1.0.0)
38
40
  coderay (1.1.1)
39
41
  concord (0.1.5)
40
42
  adamantium (~> 0.2.0)
@@ -44,6 +46,7 @@ GEM
44
46
  concurrent-ruby (~> 1.0.2)
45
47
  debug_inspector (0.0.2)
46
48
  diff-lcs (1.2.5)
49
+ docile (1.1.5)
47
50
  equalizer (0.0.11)
48
51
  expeditor (0.4.0)
49
52
  concurrent-ruby (~> 1.0.0)
@@ -143,6 +146,11 @@ GEM
143
146
  ruby-progressbar (1.8.1)
144
147
  ruby_dep (1.3.1)
145
148
  shellany (0.0.1)
149
+ simplecov (0.12.0)
150
+ docile (~> 1.1.0)
151
+ json (>= 1.8, < 3)
152
+ simplecov-html (~> 0.10.0)
153
+ simplecov-html (0.10.0)
146
154
  slop (3.6.0)
147
155
  sqlite3 (1.3.11)
148
156
  thor (0.19.1)
@@ -169,6 +177,7 @@ DEPENDENCIES
169
177
  activerecord-shard_for!
170
178
  appraisal
171
179
  bundler (~> 1.11)
180
+ codeclimate-test-reporter
172
181
  guard-rspec
173
182
  guard-rubocop
174
183
  pry
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- activerecord-shard_for (0.1.0)
4
+ activerecord-shard_for (0.1.2)
5
5
  activerecord (>= 4.1.0)
6
6
  activesupport (>= 4.1.0)
7
7
  expeditor (>= 0.1.0)
@@ -35,6 +35,8 @@ GEM
35
35
  binding_of_caller (0.7.2)
36
36
  debug_inspector (>= 0.0.1)
37
37
  builder (3.2.2)
38
+ codeclimate-test-reporter (0.6.0)
39
+ simplecov (>= 0.7.1, < 1.0.0)
38
40
  coderay (1.1.1)
39
41
  concord (0.1.5)
40
42
  adamantium (~> 0.2.0)
@@ -44,6 +46,7 @@ GEM
44
46
  concurrent-ruby (~> 1.0.2)
45
47
  debug_inspector (0.0.2)
46
48
  diff-lcs (1.2.5)
49
+ docile (1.1.5)
47
50
  equalizer (0.0.11)
48
51
  expeditor (0.4.0)
49
52
  concurrent-ruby (~> 1.0.0)
@@ -143,6 +146,11 @@ GEM
143
146
  ruby-progressbar (1.8.1)
144
147
  ruby_dep (1.3.1)
145
148
  shellany (0.0.1)
149
+ simplecov (0.12.0)
150
+ docile (~> 1.1.0)
151
+ json (>= 1.8, < 3)
152
+ simplecov-html (~> 0.10.0)
153
+ simplecov-html (0.10.0)
146
154
  slop (3.6.0)
147
155
  sqlite3 (1.3.11)
148
156
  thor (0.19.1)
@@ -169,6 +177,7 @@ DEPENDENCIES
169
177
  activerecord-shard_for!
170
178
  appraisal
171
179
  bundler (~> 1.11)
180
+ codeclimate-test-reporter
172
181
  guard-rspec
173
182
  guard-rubocop
174
183
  pry
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- activerecord-shard_for (0.1.0)
4
+ activerecord-shard_for (0.1.2)
5
5
  activerecord (>= 4.1.0)
6
6
  activesupport (>= 4.1.0)
7
7
  expeditor (>= 0.1.0)
@@ -32,6 +32,8 @@ GEM
32
32
  ast (2.3.0)
33
33
  binding_of_caller (0.7.2)
34
34
  debug_inspector (>= 0.0.1)
35
+ codeclimate-test-reporter (0.6.0)
36
+ simplecov (>= 0.7.1, < 1.0.0)
35
37
  coderay (1.1.1)
36
38
  concord (0.1.5)
37
39
  adamantium (~> 0.2.0)
@@ -41,6 +43,7 @@ GEM
41
43
  concurrent-ruby (~> 1.0.2)
42
44
  debug_inspector (0.0.2)
43
45
  diff-lcs (1.2.5)
46
+ docile (1.1.5)
44
47
  equalizer (0.0.11)
45
48
  expeditor (0.4.0)
46
49
  concurrent-ruby (~> 1.0.0)
@@ -68,6 +71,7 @@ GEM
68
71
  i18n (0.7.0)
69
72
  ice_nine (0.11.2)
70
73
  interception (0.5)
74
+ json (2.0.2)
71
75
  listen (3.1.5)
72
76
  rb-fsevent (~> 0.9, >= 0.9.4)
73
77
  rb-inotify (~> 0.9, >= 0.9.7)
@@ -139,6 +143,11 @@ GEM
139
143
  ruby-progressbar (1.8.1)
140
144
  ruby_dep (1.3.1)
141
145
  shellany (0.0.1)
146
+ simplecov (0.12.0)
147
+ docile (~> 1.1.0)
148
+ json (>= 1.8, < 3)
149
+ simplecov-html (~> 0.10.0)
150
+ simplecov-html (0.10.0)
142
151
  slop (3.6.0)
143
152
  sqlite3 (1.3.11)
144
153
  thor (0.19.1)
@@ -165,6 +174,7 @@ DEPENDENCIES
165
174
  activerecord-shard_for!
166
175
  appraisal
167
176
  bundler (~> 1.11)
177
+ codeclimate-test-reporter
168
178
  guard-rspec
169
179
  guard-rubocop
170
180
  pry
@@ -23,7 +23,7 @@ GIT
23
23
  PATH
24
24
  remote: ../
25
25
  specs:
26
- activerecord-shard_for (0.1.0)
26
+ activerecord-shard_for (0.1.2)
27
27
  activerecord (>= 4.1.0)
28
28
  activesupport (>= 4.1.0)
29
29
  expeditor (>= 0.1.0)
@@ -42,6 +42,8 @@ GEM
42
42
  ast (2.3.0)
43
43
  binding_of_caller (0.7.2)
44
44
  debug_inspector (>= 0.0.1)
45
+ codeclimate-test-reporter (0.6.0)
46
+ simplecov (>= 0.7.1, < 1.0.0)
45
47
  coderay (1.1.1)
46
48
  concord (0.1.5)
47
49
  adamantium (~> 0.2.0)
@@ -51,6 +53,7 @@ GEM
51
53
  concurrent-ruby (~> 1.0.2)
52
54
  debug_inspector (0.0.2)
53
55
  diff-lcs (1.2.5)
56
+ docile (1.1.5)
54
57
  equalizer (0.0.11)
55
58
  expeditor (0.4.0)
56
59
  concurrent-ruby (~> 1.0.0)
@@ -78,6 +81,7 @@ GEM
78
81
  i18n (0.7.0)
79
82
  ice_nine (0.11.2)
80
83
  interception (0.5)
84
+ json (2.0.2)
81
85
  listen (3.1.5)
82
86
  rb-fsevent (~> 0.9, >= 0.9.4)
83
87
  rb-inotify (~> 0.9, >= 0.9.7)
@@ -149,6 +153,11 @@ GEM
149
153
  ruby-progressbar (1.8.1)
150
154
  ruby_dep (1.3.1)
151
155
  shellany (0.0.1)
156
+ simplecov (0.12.0)
157
+ docile (~> 1.1.0)
158
+ json (>= 1.8, < 3)
159
+ simplecov-html (~> 0.10.0)
160
+ simplecov-html (0.10.0)
152
161
  slop (3.6.0)
153
162
  sqlite3 (1.3.11)
154
163
  thor (0.19.1)
@@ -176,6 +185,7 @@ DEPENDENCIES
176
185
  appraisal
177
186
  arel!
178
187
  bundler (~> 1.11)
188
+ codeclimate-test-reporter
179
189
  guard-rspec
180
190
  guard-rubocop
181
191
  pry
@@ -25,7 +25,12 @@ module ActiveRecord
25
25
  # @param [Object] key
26
26
  # @return [Symbol] registered connection name
27
27
  def fetch(key)
28
- connection_registry.fetch(key)
28
+ connection_registry.find do |connection_key, _connection|
29
+ case connection_key
30
+ when Range then connection_key.include?(key)
31
+ else connection_key == key
32
+ end
33
+ end.second
29
34
  end
30
35
  end
31
36
  end
@@ -35,10 +35,11 @@ module ActiveRecord
35
35
  connection_routers[router_name] = router_class
36
36
  end
37
37
 
38
- # @param [Symbol] router_name
38
+ # @param [Symbol] connection_router_name
39
39
  # @return [Class] registered class by [#register_router]
40
- def fetch_connection_router(router_name)
41
- connection_routers[router_name]
40
+ # @raise [KeyError] when not registerd router_name given
41
+ def fetch_connection_router(connection_router_name)
42
+ connection_routers.fetch(connection_router_name)
42
43
  end
43
44
  end
44
45
  end
@@ -0,0 +1,11 @@
1
+ module ActiveRecord
2
+ module ShardFor
3
+ class DistkeyRouter < ConnectionRouter
4
+ # @param [Object] key sharding key
5
+ # @return [Object] key given key
6
+ def route(key)
7
+ key
8
+ end
9
+ end
10
+ end
11
+ end
@@ -43,7 +43,7 @@ module ActiveRecord
43
43
  key = attributes[distkey]
44
44
  raise ActiveRecord::ShardFor::MissingDistkeyAttribute unless key || attributes[distkey.to_s]
45
45
 
46
- @before_put_callback.call(attributes) if @before_put_callback
46
+ @before_put_callback.call(attributes) if defined?(@before_put_callback) && @before_put_callback
47
47
 
48
48
  shard_for(key).create!(attributes)
49
49
  end
@@ -85,8 +85,8 @@ module ActiveRecord
85
85
  raise ActiveRecord::ShardFor::RecordNotFound
86
86
  end
87
87
 
88
- # Distkey is a column. activerecord-shard_for gave to cluster_router that value
89
- # and cluster_router determine which shard to store.
88
+ # Distkey is a column. activerecord-shard_for gave to connection_router that value
89
+ # and connection_router determine which shard to store.
90
90
  # @param [Symbol] column
91
91
  def def_distkey(column)
92
92
  self.distkey = column.to_sym
@@ -24,7 +24,12 @@ module ActiveRecord
24
24
  # @param [Object] key sharding key object for connection
25
25
  # @return [Class, nil] A AR model class.
26
26
  def fetch_by_key(key)
27
- @shards.find { |_, model| model.assigned_key == key }[1]
27
+ @shards.values.find do |model|
28
+ case model.assigned_key
29
+ when Range then model.assigned_key.include?(key)
30
+ else model.assigned_key == key
31
+ end
32
+ end
28
33
  end
29
34
 
30
35
  # @return [Array<Class>]
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module ShardFor
3
- VERSION = '0.1.2'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
@@ -7,6 +7,7 @@ require 'activerecord/shard_for/model'
7
7
  require 'activerecord/shard_for/errors'
8
8
  require 'activerecord/shard_for/connection_router'
9
9
  require 'activerecord/shard_for/hash_modulo_router'
10
+ require 'activerecord/shard_for/distkey_router'
10
11
  require 'activerecord/shard_for/database_tasks'
11
12
  require 'activerecord/shard_for/shard_repogitory'
12
13
  require 'activerecord/shard_for/all_shards_in_parallel'
@@ -29,4 +30,7 @@ module ActiveRecord
29
30
  end
30
31
  end
31
32
 
32
- ActiveRecord::ShardFor.config.register_connection_router(:hash_modulo, ActiveRecord::ShardFor::HashModuloRouter)
33
+ ActiveRecord::ShardFor.configure do |config|
34
+ config.register_connection_router(:hash_modulo, ActiveRecord::ShardFor::HashModuloRouter)
35
+ config.register_connection_router(:distkey, ActiveRecord::ShardFor::DistkeyRouter)
36
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-shard_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yuemori
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-30 00:00:00.000000000 Z
11
+ date: 2016-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -248,6 +248,20 @@ dependencies:
248
248
  - - ">="
249
249
  - !ruby/object:Gem::Version
250
250
  version: '0'
251
+ - !ruby/object:Gem::Dependency
252
+ name: codeclimate-test-reporter
253
+ requirement: !ruby/object:Gem::Requirement
254
+ requirements:
255
+ - - ">="
256
+ - !ruby/object:Gem::Version
257
+ version: '0'
258
+ type: :development
259
+ prerelease: false
260
+ version_requirements: !ruby/object:Gem::Requirement
261
+ requirements:
262
+ - - ">="
263
+ - !ruby/object:Gem::Version
264
+ version: '0'
251
265
  description: Database sharding library for ActiveRecord
252
266
  email:
253
267
  - yuemori@aiming-inc.com
@@ -260,7 +274,7 @@ files:
260
274
  - ".rubocop.yml"
261
275
  - ".travis.yml"
262
276
  - Appraisals
263
- - CODE_OF_CONDUCT.md
277
+ - CHANGELOG.md
264
278
  - Gemfile
265
279
  - Guardfile
266
280
  - LICENSE.txt
@@ -288,6 +302,7 @@ files:
288
302
  - lib/activerecord/shard_for/config.rb
289
303
  - lib/activerecord/shard_for/connection_router.rb
290
304
  - lib/activerecord/shard_for/database_tasks.rb
305
+ - lib/activerecord/shard_for/distkey_router.rb
291
306
  - lib/activerecord/shard_for/errors.rb
292
307
  - lib/activerecord/shard_for/hash_modulo_router.rb
293
308
  - lib/activerecord/shard_for/model.rb
data/CODE_OF_CONDUCT.md DELETED
@@ -1,49 +0,0 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, and in the interest of
4
- fostering an open and welcoming community, we pledge to respect all people who
5
- contribute through reporting issues, posting feature requests, updating
6
- documentation, submitting pull requests or patches, and other activities.
7
-
8
- We are committed to making participation in this project a harassment-free
9
- experience for everyone, regardless of level of experience, gender, gender
10
- identity and expression, sexual orientation, disability, personal appearance,
11
- body size, race, ethnicity, age, religion, or nationality.
12
-
13
- Examples of unacceptable behavior by participants include:
14
-
15
- * The use of sexualized language or imagery
16
- * Personal attacks
17
- * Trolling or insulting/derogatory comments
18
- * Public or private harassment
19
- * Publishing other's private information, such as physical or electronic
20
- addresses, without explicit permission
21
- * Other unethical or unprofessional conduct
22
-
23
- Project maintainers have the right and responsibility to remove, edit, or
24
- reject comments, commits, code, wiki edits, issues, and other contributions
25
- that are not aligned to this Code of Conduct, or to ban temporarily or
26
- permanently any contributor for other behaviors that they deem inappropriate,
27
- threatening, offensive, or harmful.
28
-
29
- By adopting this Code of Conduct, project maintainers commit themselves to
30
- fairly and consistently applying these principles to every aspect of managing
31
- this project. Project maintainers who do not follow or enforce the Code of
32
- Conduct may be permanently removed from the project team.
33
-
34
- This code of conduct applies both within project spaces and in public spaces
35
- when an individual is representing the project or its community.
36
-
37
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
- reported by contacting a project maintainer at yuemori@aiming-inc.com. All
39
- complaints will be reviewed and investigated and will result in a response that
40
- is deemed necessary and appropriate to the circumstances. Maintainers are
41
- obligated to maintain confidentiality with regard to the reporter of an
42
- incident.
43
-
44
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
- version 1.3.0, available at
46
- [http://contributor-covenant.org/version/1/3/0/][version]
47
-
48
- [homepage]: http://contributor-covenant.org
49
- [version]: http://contributor-covenant.org/version/1/3/0/