activerecord-sharding 0.2.0 → 0.3.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 +4 -4
- data/.rubocop.yml +11 -0
- data/README.md +6 -0
- data/activerecord-sharding.gemspec +1 -0
- data/lib/active_record/sharding/all_shards_in_parallel.rb +29 -0
- data/lib/active_record/sharding/model.rb +5 -0
- data/lib/active_record/sharding/version.rb +1 -1
- data/lib/activerecord-sharding.rb +1 -0
- data/spec/active_record/sharding/all_shards_in_parallel_spec.rb +37 -0
- data/spec/active_record/sharding/model_spec.rb +6 -0
- metadata +19 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8b2713a535cf9f95e2904a4840a0b79481eb3839
|
|
4
|
+
data.tar.gz: 6dbda049b7d70c553b26d5b242c7b5ff58666070
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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.
|
|
@@ -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
|
|
@@ -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.
|
|
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-
|
|
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
|