mixed_gauge 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +12 -12
- data/lib/mixed_gauge.rb +2 -0
- data/lib/mixed_gauge/all_shards_in_parallel.rb +39 -0
- data/lib/mixed_gauge/model.rb +11 -2
- data/lib/mixed_gauge/version.rb +1 -1
- data/mixed_gauge.gemspec +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1287ac868f8d04e5a084973b66d9cb3592ec1658
|
4
|
+
data.tar.gz: 096992dc8eb46a2fb67bd6cfad16ce348ce3ae27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0d19dc2806f36b88c851da29e77eefa0f63466ff320a2c246c9801b7f60ee80161dedd7c5b20ae9bd8fc46ec1f619c688c2a58019091aa99f0803341bbf5fa8
|
7
|
+
data.tar.gz: fb370f43a0a9231fac9b35009460b53917cb86fb89c70b58e6511ced16268eeed8392580cc271133170adacd0bd5c9e4a913982cc95810d0cebf7b94045ea0fe
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -65,6 +65,7 @@ end
|
|
65
65
|
|
66
66
|
Use `.get` to retrive single model class which is connected to proper
|
67
67
|
database node. Use `.put!` to create new record to proper database node.
|
68
|
+
|
68
69
|
`.all_shards` enables you to all model class which is connected to all
|
69
70
|
database nodes in the cluster.
|
70
71
|
|
@@ -78,6 +79,15 @@ alice.save!
|
|
78
79
|
User.all_shards.flat_map {|m| m.find_by(name: 'alice') }.compact
|
79
80
|
```
|
80
81
|
|
82
|
+
When you want to execute queries in parallel, use `.all_shards_in_parallel`.
|
83
|
+
It returns `Mixedgauge::AllShardsInParallel` and it offers some collection
|
84
|
+
actions which runs in parallel. It is aliased to `.parallel`.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
User.all_shards_in_parallel.map(&count) #=> 1
|
88
|
+
User.parallel.flat_map {|m| m.where(age: 1) }.size #=> 1
|
89
|
+
```
|
90
|
+
|
81
91
|
When you want find by non-distkey, not recomended though, you can define finder
|
82
92
|
methods to model class.
|
83
93
|
|
@@ -89,7 +99,7 @@ class User < ActiveRecord::Base
|
|
89
99
|
|
90
100
|
parent_methods do
|
91
101
|
def find_from_all_by_name(name)
|
92
|
-
|
102
|
+
all_shards_in_parallel.map {|m| m.find_by(name: name) }.compact.first
|
93
103
|
end
|
94
104
|
end
|
95
105
|
end
|
@@ -157,16 +167,6 @@ Or install it yourself as:
|
|
157
167
|
|
158
168
|
$ gem install mixed_gauge
|
159
169
|
|
160
|
-
## Development
|
161
|
-
|
162
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
163
|
-
|
164
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
165
|
-
|
166
170
|
## Contributing
|
167
171
|
|
168
|
-
|
169
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
170
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
171
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
172
|
-
5. Create a new Pull Request
|
172
|
+
Feel free to pull request and issue :)
|
data/lib/mixed_gauge.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_record'
|
2
|
+
require 'expeditor'
|
2
3
|
|
3
4
|
require 'mixed_gauge/version'
|
4
5
|
require 'mixed_gauge/errors'
|
@@ -6,6 +7,7 @@ require 'mixed_gauge/cluster_config'
|
|
6
7
|
require 'mixed_gauge/config'
|
7
8
|
require 'mixed_gauge/routing'
|
8
9
|
require 'mixed_gauge/sub_model_repository'
|
10
|
+
require 'mixed_gauge/all_shards_in_parallel'
|
9
11
|
require 'mixed_gauge/model'
|
10
12
|
|
11
13
|
module MixedGauge
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module MixedGauge
|
2
|
+
# Support parallel execution with each shard and deal with AR connection
|
3
|
+
# management in parallel execution.
|
4
|
+
class AllShardsInParallel
|
5
|
+
# @param [Array<Class>] An array of sub model class
|
6
|
+
def initialize(shards)
|
7
|
+
@shards = shards
|
8
|
+
end
|
9
|
+
|
10
|
+
# @yield [Class] A sub model class
|
11
|
+
# @return [Array] A result
|
12
|
+
# @example
|
13
|
+
# User.all_shards_in_parallel.map(&:count).reduce(&:+)
|
14
|
+
def map(&block)
|
15
|
+
commands = @shards.map do |m|
|
16
|
+
Expeditor::Command.new { m.connection_pool.with_connection { yield m } }
|
17
|
+
end
|
18
|
+
commands.each(&:start)
|
19
|
+
commands.map(&:get)
|
20
|
+
end
|
21
|
+
|
22
|
+
# @yield [Class] A sub model class
|
23
|
+
# @return [Array] A result
|
24
|
+
# @example
|
25
|
+
# User.all_shards_in_parallel.flat_map {|m| m.where(age: 1) }
|
26
|
+
def flat_map(&block)
|
27
|
+
map(&block).flatten
|
28
|
+
end
|
29
|
+
|
30
|
+
# @yield [Class] A sub model class
|
31
|
+
# @return [MixedGauge::AllShardsInParallel]
|
32
|
+
# @example
|
33
|
+
# User.all_shards_in_parallel.each {|m| puts m.count }
|
34
|
+
def each(&block)
|
35
|
+
map(&block) if block_given?
|
36
|
+
self
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/mixed_gauge/model.rb
CHANGED
@@ -48,6 +48,7 @@ module MixedGauge
|
|
48
48
|
# @return [ActiveRecord::Base] A sub class instance of included model
|
49
49
|
# @raise [MixedGauge::MissingDistkeyAttribute]
|
50
50
|
def put!(attributes)
|
51
|
+
raise '`distkey` is not defined. Use `def_distkey`.' unless distkey
|
51
52
|
@before_put_callback.call(attributes) if @before_put_callback
|
52
53
|
|
53
54
|
if key = attributes[distkey] || attributes[distkey.to_s]
|
@@ -110,6 +111,14 @@ module MixedGauge
|
|
110
111
|
sub_model_repository.all
|
111
112
|
end
|
112
113
|
|
114
|
+
# @return [Mixedgauge::AllShardsInParallel]
|
115
|
+
# @example
|
116
|
+
# User.all_shards_in_parallel.map {|m| m.where.find_by(name: 'Alice') }.compact
|
117
|
+
def all_shards_in_parallel
|
118
|
+
AllShardsInParallel.new(all_shards)
|
119
|
+
end
|
120
|
+
alias_method :parallel, :all_shards_in_parallel
|
121
|
+
|
113
122
|
# Define utility methods which uses all shards or specific shard.
|
114
123
|
# These methods can be called from included model class.
|
115
124
|
# @example
|
@@ -119,11 +128,11 @@ module MixedGauge
|
|
119
128
|
# def_distkey :name
|
120
129
|
# parent_methods do
|
121
130
|
# def all_count
|
122
|
-
#
|
131
|
+
# parallel.map {|m| m.count }.reduce(&:+)
|
123
132
|
# end
|
124
133
|
#
|
125
134
|
# def find_from_all_by(condition)
|
126
|
-
#
|
135
|
+
# parallel.flat_map {|m m.find_by(condition) }.compact.first
|
127
136
|
# end
|
128
137
|
# end
|
129
138
|
# end
|
data/lib/mixed_gauge/version.rb
CHANGED
data/mixed_gauge.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
22
|
spec.add_dependency 'activerecord', '>= 4.1.0'
|
23
|
+
spec.add_dependency 'expeditor', '>= 0.1.0'
|
23
24
|
spec.add_development_dependency 'appraisal'
|
24
25
|
spec.add_development_dependency 'coveralls'
|
25
26
|
spec.add_development_dependency 'pry'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mixed_gauge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Taiki Ono
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 4.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: expeditor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.1.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.1.0
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: appraisal
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -177,6 +191,7 @@ files:
|
|
177
191
|
- gemfiles/ar_4.2.gemfile
|
178
192
|
- gemfiles/rails_edge.gemfile
|
179
193
|
- lib/mixed_gauge.rb
|
194
|
+
- lib/mixed_gauge/all_shards_in_parallel.rb
|
180
195
|
- lib/mixed_gauge/cluster_config.rb
|
181
196
|
- lib/mixed_gauge/config.rb
|
182
197
|
- lib/mixed_gauge/database_tasks.rb
|