activerecord-sharding 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74c9354eb637b74252c1ee7631037140ad0326ea
4
- data.tar.gz: c6adaf64d167afd9dbf16a0e3982158181255a9e
3
+ metadata.gz: 8b2713a535cf9f95e2904a4840a0b79481eb3839
4
+ data.tar.gz: 6dbda049b7d70c553b26d5b242c7b5ff58666070
5
5
  SHA512:
6
- metadata.gz: 5c74278a9bae13cf05ccbf4fda5abf98cebec86c90ad3489cb2f18ea3eb0c4e150a0725b8151003d95712a9f989b7812f6b8da007fe01ea78e9738f94c77c0b5
7
- data.tar.gz: b0ed70dc49f1d1cb389ed6313991b1ac21013f7720211d62ff57e1e253a82c3fcdbaa6ebd0d938227c0d80d324ca6044532160abdac7f9ac2c1f569168c8accb
6
+ metadata.gz: 9fbfdfea19ab12464e7ed5756434adb7ec025f00a5f9084ac5e195361d360f178869ffa146afe7701e169f1583fb30ee6701db1dd1f14d7641cd8b5298bed498
7
+ data.tar.gz: 5f109afa415f5eadbda3fcb3275c61d157c4a5b2901d63958774c96191c0bddc1fac2018301092d4dcae6a26dbe9a0a430b5f06ce57bc8049417e6ef49c0d320
data/.rubocop.yml CHANGED
@@ -9,6 +9,17 @@ Style/AsciiComments:
9
9
  Style/BracesAroundHashParameters:
10
10
  Enabled: false
11
11
 
12
+ Style/CollectionMethods:
13
+ Description: Preferred collection methods.
14
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#map-find-select-reduce-size
15
+ Enabled: true
16
+ PreferredMethods:
17
+ collect: map
18
+ collect!: map!
19
+ find: detect
20
+ find_all: select
21
+ reduce: reduce
22
+
12
23
  Style/Documentation:
13
24
  Enabled: false
14
25
 
data/README.md CHANGED
@@ -152,6 +152,12 @@ for all shards query
152
152
  User.all_shards.flat_map { |model| model.find_by(name: 'foorbar') }.compact
153
153
  ```
154
154
 
155
+ for all shards query in parallel
156
+
157
+ ```ruby
158
+ User.parallel.flat_map { |model| model.find_by(name: 'foorbar') }.compact
159
+ ```
160
+
155
161
  #### Association/Relation
156
162
 
157
163
  if use database association/relation in sharding databases.
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.required_ruby_version = ">= 2.0"
22
+ spec.add_dependency 'expeditor', ">= 0.1.0"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.5"
24
25
  spec.add_development_dependency "rake"
@@ -0,0 +1,29 @@
1
+ require "active_support/concern"
2
+ require "expeditor"
3
+
4
+ module ActiveRecord
5
+ module Sharding
6
+ class AllShardsInParallel
7
+ def initialize(shards)
8
+ @shards = shards
9
+ end
10
+
11
+ def map(&_block)
12
+ commands = @shards.map do |model|
13
+ Expeditor::Command.new { model.connection_pool.with_connection { yield model } }
14
+ end
15
+ commands.each(&:start)
16
+ commands.map(&:get)
17
+ end
18
+
19
+ def flat_map(&block)
20
+ map(&block).flatten
21
+ end
22
+
23
+ def each(&block)
24
+ map(&block) if block_given?
25
+ self
26
+ end
27
+ end
28
+ end
29
+ end
@@ -59,6 +59,11 @@ module ActiveRecord
59
59
  shard_repository.all
60
60
  end
61
61
 
62
+ def all_shards_in_parallel
63
+ AllShardsInParallel.new(all_shards)
64
+ end
65
+ alias_method :parallel, :all_shards_in_parallel
66
+
62
67
  def define_parent_methods(&block)
63
68
  instance_eval(&block)
64
69
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Sharding
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -8,6 +8,7 @@ require "active_record/sharding/modulo_router"
8
8
  require "active_record/sharding/abstract_repository"
9
9
  require "active_record/sharding/shard_repository"
10
10
  require "active_record/sharding/database_tasks"
11
+ require "active_record/sharding/all_shards_in_parallel"
11
12
  require "active_record/sharding/model"
12
13
  require "active_record/sharding/sequencer"
13
14
  require "active_record/sharding/sequencer_repository"
@@ -0,0 +1,37 @@
1
+ RSpec.describe ActiveRecord::Sharding::AllShardsInParallel do
2
+ let(:model_class) { User }
3
+ let(:instance) { described_class.new(model_class.all_shards) }
4
+
5
+ describe "#map" do
6
+ it "maps in parallel" do
7
+ expect(instance.map(&:count).reduce(&:+)).to eq 0
8
+ model_class.put!(name: "Alice")
9
+ expect(instance.map(&:count).reduce(&:+)).to eq 1
10
+ end
11
+ end
12
+
13
+ describe '#flat_map' do
14
+ before do
15
+ model_class.put!(name: "Alice")
16
+ model_class.put!(name: "Humpty")
17
+ model_class.put!(name: "Alice")
18
+ end
19
+
20
+ it "flat_maps in parallel" do
21
+ result = instance.flat_map { |m| m.where(name: "Alice") }
22
+ expect(result.size).to eq 2
23
+ end
24
+ end
25
+
26
+ describe '#each' do
27
+ it "enables to query in parallel" do
28
+ expect do
29
+ instance.each { |_| print "XXX" }
30
+ end.to output("XXX" * 3).to_stdout
31
+ end
32
+
33
+ it "returns self when block is not given" do
34
+ expect(instance.each).to be_a(described_class)
35
+ end
36
+ end
37
+ end
@@ -90,6 +90,12 @@ describe ActiveRecord::Sharding::Model do
90
90
  end
91
91
  end
92
92
 
93
+ describe ".all_shards_in_parallel" do
94
+ it "returns a ActiveRecord::Sharding::AllShardsInParallel" do
95
+ expect(User.all_shards_in_parallel).to be_a ActiveRecord::Sharding::AllShardsInParallel
96
+ end
97
+ end
98
+
93
99
  describe ".define_parent_methods" do
94
100
  before do
95
101
  model.put! name: "foo"
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-sharding
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hirocaster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-15 00:00:00.000000000 Z
11
+ date: 2016-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: expeditor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -178,6 +192,7 @@ files:
178
192
  - bin/setup
179
193
  - lib/active_record/sharding.rb
180
194
  - lib/active_record/sharding/abstract_repository.rb
195
+ - lib/active_record/sharding/all_shards_in_parallel.rb
181
196
  - lib/active_record/sharding/cluster_config.rb
182
197
  - lib/active_record/sharding/config.rb
183
198
  - lib/active_record/sharding/database_tasks.rb
@@ -193,6 +208,7 @@ files:
193
208
  - lib/activerecord-sharding.rb
194
209
  - lib/tasks/activerecord-sharding.rake
195
210
  - spec/active_record/sharding/abstract_repository_spec.rb
211
+ - spec/active_record/sharding/all_shards_in_parallel_spec.rb
196
212
  - spec/active_record/sharding/cluster_config_spec.rb
197
213
  - spec/active_record/sharding/errors_spec.rb
198
214
  - spec/active_record/sharding/model_spec.rb
@@ -230,6 +246,7 @@ specification_version: 4
230
246
  summary: Sharding library for ActiveRecord(MySQL)
231
247
  test_files:
232
248
  - spec/active_record/sharding/abstract_repository_spec.rb
249
+ - spec/active_record/sharding/all_shards_in_parallel_spec.rb
233
250
  - spec/active_record/sharding/cluster_config_spec.rb
234
251
  - spec/active_record/sharding/errors_spec.rb
235
252
  - spec/active_record/sharding/model_spec.rb