activerecord-precount 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.travis.yml +28 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE +22 -0
  6. data/README.md +91 -0
  7. data/Rakefile +14 -0
  8. data/activerecord-precount.gemspec +31 -0
  9. data/benchmark.rb +42 -0
  10. data/ci/Gemfile.activerecord-4.1.x +4 -0
  11. data/ci/Gemfile.activerecord-4.2.x +4 -0
  12. data/ci/travis.rb +14 -0
  13. data/lib/active_record/associations/builder/count_loader.rb +13 -0
  14. data/lib/active_record/associations/count_loader.rb +27 -0
  15. data/lib/active_record/associations/preloader/count_loader.rb +41 -0
  16. data/lib/active_record/precount/base_extension.rb +7 -0
  17. data/lib/active_record/precount/extend.rb +18 -0
  18. data/lib/active_record/precount/has_many_extension.rb +25 -0
  19. data/lib/active_record/precount/join_dependency_extension.rb +23 -0
  20. data/lib/active_record/precount/preloader_extension.rb +15 -0
  21. data/lib/active_record/precount/reflection_extension.rb +50 -0
  22. data/lib/active_record/precount/relation_extension.rb +42 -0
  23. data/lib/active_record/precount/version.rb +5 -0
  24. data/lib/activerecord-precount.rb +8 -0
  25. data/sample/.gitignore +16 -0
  26. data/sample/Gemfile +19 -0
  27. data/sample/README.md +6 -0
  28. data/sample/Rakefile +6 -0
  29. data/sample/app/assets/images/.keep +0 -0
  30. data/sample/app/assets/javascripts/application.js +16 -0
  31. data/sample/app/assets/stylesheets/application.css +15 -0
  32. data/sample/app/controllers/application_controller.rb +13 -0
  33. data/sample/app/controllers/concerns/.keep +0 -0
  34. data/sample/app/helpers/application_helper.rb +2 -0
  35. data/sample/app/mailers/.keep +0 -0
  36. data/sample/app/models/.keep +0 -0
  37. data/sample/app/models/concerns/.keep +0 -0
  38. data/sample/app/models/favorite.rb +4 -0
  39. data/sample/app/models/tweet.rb +6 -0
  40. data/sample/app/models/user.rb +4 -0
  41. data/sample/app/views/application/index.html.erb +49 -0
  42. data/sample/app/views/layouts/application.html.erb +14 -0
  43. data/sample/bin/bundle +3 -0
  44. data/sample/bin/rails +8 -0
  45. data/sample/bin/rake +8 -0
  46. data/sample/bin/spring +18 -0
  47. data/sample/config.ru +4 -0
  48. data/sample/config/application.rb +30 -0
  49. data/sample/config/boot.rb +4 -0
  50. data/sample/config/database.yml +11 -0
  51. data/sample/config/environment.rb +5 -0
  52. data/sample/config/environments/development.rb +40 -0
  53. data/sample/config/environments/production.rb +82 -0
  54. data/sample/config/environments/test.rb +39 -0
  55. data/sample/config/initializers/assets.rb +8 -0
  56. data/sample/config/initializers/backtrace_silencers.rb +7 -0
  57. data/sample/config/initializers/cookies_serializer.rb +3 -0
  58. data/sample/config/initializers/filter_parameter_logging.rb +4 -0
  59. data/sample/config/initializers/inflections.rb +16 -0
  60. data/sample/config/initializers/mime_types.rb +4 -0
  61. data/sample/config/initializers/session_store.rb +3 -0
  62. data/sample/config/initializers/wrap_parameters.rb +14 -0
  63. data/sample/config/locales/en.yml +23 -0
  64. data/sample/config/routes.rb +3 -0
  65. data/sample/config/secrets.yml +22 -0
  66. data/sample/db/migrate/20141122002518_create_tweets.rb +10 -0
  67. data/sample/db/migrate/20141122002548_create_favorites.rb +10 -0
  68. data/sample/db/migrate/20141122002555_create_users.rb +8 -0
  69. data/sample/db/schema.rb +35 -0
  70. data/sample/db/seeds.rb +12 -0
  71. data/sample/lib/assets/.keep +0 -0
  72. data/sample/lib/tasks/.keep +0 -0
  73. data/sample/log/.keep +0 -0
  74. data/sample/public/404.html +67 -0
  75. data/sample/public/422.html +67 -0
  76. data/sample/public/500.html +66 -0
  77. data/sample/public/favicon.ico +0 -0
  78. data/sample/public/robots.txt +5 -0
  79. data/sample/vendor/assets/javascripts/.keep +0 -0
  80. data/sample/vendor/assets/stylesheets/.keep +0 -0
  81. data/test/cases/associations/eager_load_test.rb +20 -0
  82. data/test/cases/associations/includes_test.rb +30 -0
  83. data/test/cases/associations/precount_test.rb +36 -0
  84. data/test/cases/associations/preload_test.rb +30 -0
  85. data/test/cases/db_config.rb +24 -0
  86. data/test/cases/helper.rb +4 -0
  87. data/test/cases/test_case.rb +86 -0
  88. data/test/config.example.yml +140 -0
  89. data/test/config.rb +5 -0
  90. data/test/models/favorite.rb +3 -0
  91. data/test/models/tweet.rb +4 -0
  92. data/test/schema/schema.rb +16 -0
  93. data/test/support/autorun.rb +5 -0
  94. data/test/support/config.rb +43 -0
  95. data/test/support/connection.rb +15 -0
  96. metadata +293 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1621a4f3c4f5d53c03ad4f6e2a62e9e23119a734
4
+ data.tar.gz: c0c2e63f3ef20f39f98ca38a0339395212597104
5
+ SHA512:
6
+ metadata.gz: 9a29284daa7c6fa23bb26cdc713fa7a9b71449a19bbd65b89eb7e48fe664c0dfe54fc3c64e38ee4227de8122728ed3421a6cb1287f73596593bf7f6d8c2c7c47
7
+ data.tar.gz: a07629cc1a93ccb7c250d74a1be50f7c143f615b32785eeaae059a56ec5dc1d0b19ad2cb7964de2a3af68cf190c6e8a5706e69c91f4b529effd8645a20997352
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.sqlite3
19
+ test/config.yml
data/.travis.yml ADDED
@@ -0,0 +1,28 @@
1
+ script: ci/travis.rb
2
+ language: ruby
3
+ sudo: false
4
+ matrix:
5
+ include:
6
+ - rvm: 2.0
7
+ env: TASK=test ARCONN=mysql2
8
+ gemfile: ci/Gemfile.activerecord-4.2.x
9
+ - rvm: 2.1
10
+ env: TASK=test ARCONN=mysql2
11
+ gemfile: ci/Gemfile.activerecord-4.2.x
12
+ - rvm: 2.2
13
+ env: TASK=test ARCONN=mysql2
14
+ gemfile: ci/Gemfile.activerecord-4.1.x
15
+ - rvm: 2.2
16
+ env: TASK=test ARCONN=sqlite3
17
+ gemfile: ci/Gemfile.activerecord-4.2.x
18
+ - rvm: 2.2
19
+ env: TASK=test ARCONN=mysql2
20
+ gemfile: ci/Gemfile.activerecord-4.2.x
21
+ - rvm: 2.2
22
+ env: TASK=test ARCONN=postgresql
23
+ gemfile: ci/Gemfile.activerecord-4.2.x
24
+ - rvm: 2.2
25
+ env: TASK=benchmark ARCONN=mysql2
26
+ gemfile: ci/Gemfile.activerecord-4.2.x
27
+ allow_failures:
28
+ - env: TASK=benchmark ARCONN=mysql2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-precount.gemspec
4
+ gemspec
5
+
6
+ gem 'rbench', github: 'miloops/rbench'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Takashi Kokubun
2
+
3
+ MIT License
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/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # ActiveRecord::Precount [![Build Status](https://travis-ci.org/k0kubun/activerecord-precount.svg?branch=master)](https://travis-ci.org/k0kubun/activerecord-precount)
2
+
3
+ N+1 count query killer for ActiveRecord.
4
+ ActiveRecord::Precount allows you to cache count of associated records by eager loading.
5
+
6
+ ## Why ActiveRecord::Precount?
7
+ Rails provides a way to resolve N+1 count query, which is [belongs\_to's counter\_cache option](http://guides.rubyonrails.org/association_basics.html#counter-cache).
8
+ It requires a column to cache the count. But adding a column just for count cache is overkill.
9
+
10
+ Thus this plugin enables you to preload counts in the same way as `has_many` and `belongs_to`.
11
+ `count_loader` is an ActiveRecord's association, which is preloadable by `preload` or `includes`.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'activerecord-precount'
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### Enable count\_loader option
24
+ First, enable your has\_many association's count\_loader option.
25
+
26
+ ```diff
27
+ class Tweet
28
+ - has_many :favorites
29
+ + has_many :favorites, count_loader: true
30
+ end
31
+ ```
32
+
33
+ The option defines an additional association whose name is `favorites_count`.
34
+ Its association type is not an ordinary one (i.e. `has_many`, `belongs_to`) but `count_loader`.
35
+
36
+ ### Preload the association
37
+ This association works well by default.
38
+
39
+ ```rb
40
+ @tweets = Tweet.all
41
+ @tweets.each do |tweet|
42
+ p tweets.favorites_count # same as tweets.favorites.count
43
+ end
44
+ ```
45
+
46
+ You can eagerly load `count_loader` association by `includes` or `preload`.
47
+
48
+ ```rb
49
+ @tweets = Tweet.all.preload(:favorites_count)
50
+ @tweets.each do |tweet|
51
+ p tweets.favorites_count # this line doesn't execute an additional query
52
+ end
53
+ ```
54
+
55
+ Since it is association, you can preload nested `count_loader` association.
56
+
57
+ ```rb
58
+ @users = User.all.preload(tweets: :favorites_count)
59
+ @users.each do |user|
60
+ user.tweets.each do |tweet|
61
+ p tweet.favorites_count # this line doesn't execute an additional query
62
+ end
63
+ end
64
+ ```
65
+
66
+ ## Supported Versions
67
+
68
+ - Ruby
69
+ - 2.0, 2.1, 2.2
70
+ - Rails
71
+ - 4.1, 4.2
72
+
73
+ ### Databases
74
+
75
+ - sqlite
76
+ - mysql
77
+ - postgresql
78
+
79
+ ## Testing
80
+
81
+ ```bash
82
+ $ bundle exec rake
83
+ ```
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it ( https://github.com/k0kubun/activerecord-precount/fork )
88
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
89
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
90
+ 4. Push to the branch (`git push origin my-new-feature`)
91
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ desc 'Run count_loader benchmarks'
5
+ task :benchmark do
6
+ ruby('benchmark.rb')
7
+ end
8
+
9
+ Rake::TestTask.new do |t|
10
+ t.libs << "lib" << "test"
11
+ t.test_files = Dir.glob("test/**/*_test.rb")
12
+ end
13
+
14
+ task default: :test
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'active_record/precount/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "activerecord-precount"
7
+ spec.version = ActiveRecord::Precount::VERSION
8
+ spec.authors = ["Takashi Kokubun"]
9
+ spec.email = ["takashikkbn@gmail.com"]
10
+ spec.summary = %q{N+1 count query killer for ActiveRecord}
11
+ spec.description = %q{N+1 count query killer for ActiveRecord}
12
+ spec.homepage = "https://github.com/k0kubun/activerecord-precount"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.test_files = spec.files.grep(%r{^spec/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.required_ruby_version = ">= 2.0"
20
+ spec.add_runtime_dependency "activerecord", ">= 3.2.0"
21
+ spec.add_development_dependency "minitest"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "erubis"
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "sqlite3"
27
+ spec.add_development_dependency "mysql2"
28
+ spec.add_development_dependency "postgres"
29
+ spec.add_development_dependency "rbench"
30
+ spec.add_development_dependency "dalli"
31
+ end
data/benchmark.rb ADDED
@@ -0,0 +1,42 @@
1
+ $LOAD_PATH.unshift File.expand_path('../test', __FILE__)
2
+
3
+ require 'rbench'
4
+ require 'cases/db_config'
5
+ require 'models/favorite'
6
+ require 'models/tweet'
7
+
8
+ RBench.run(50) do
9
+ column :counter_cache, title: 'counter_cache'
10
+ column :left_join, title: 'LEFT JOIN'
11
+ column :count_loader, title: 'precount has_many'
12
+ column :has_many, title: 'preload has_many'
13
+ column :count_query, title: 'N+1 COUNT'
14
+
15
+ join_relation = Tweet.joins('LEFT JOIN favorites ON tweets.id = favorites.tweet_id').
16
+ select('tweets.*, COUNT(favorites.id) AS joined_count').group('tweets.id')
17
+
18
+ def prepare_records(tweets_count, favorites_count)
19
+ tweets_count.times do
20
+ t = Tweet.create(favorites_count_cache: 0)
21
+ favorites_count.times { Favorite.create(tweet: t) }
22
+ end
23
+ end
24
+
25
+ test_cases = [
26
+ [10, 5],
27
+ [20, 20],
28
+ [30, 100],
29
+ ]
30
+
31
+ test_cases.each do |tweets_count, favorites_count|
32
+ prepare_records(tweets_count, favorites_count)
33
+
34
+ report "N = #{tweets_count}, count = #{favorites_count}" do
35
+ counter_cache { Tweet.first(tweets_count).map(&:favorites_count_cache) }
36
+ left_join { join_relation.first(tweets_count).map(&:joined_count) }
37
+ count_loader { Tweet.preload(:favorites_count).first(tweets_count).map(&:favorites_count) }
38
+ has_many { Tweet.preload(:favorites).first(tweets_count).map{ |t| t.favorites.size } }
39
+ count_query { Tweet.first(tweets_count).map{ |t| t.favorites.count } }
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'rails', '~> 4.1.0'
4
+ gemspec path: '..'
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'rails', '~> 4.2.0'
4
+ gemspec path: '..'
data/ci/travis.rb ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ commands = [
4
+ 'mysql -e "create database activerecord_unittest;"',
5
+ 'mysql -e "create database activerecord_unittest2;"',
6
+ 'psql -c "create database activerecord_unittest;" -U postgres',
7
+ 'psql -c "create database activerecord_unittest2;" -U postgres'
8
+ ]
9
+
10
+ commands.each do |command|
11
+ system("#{command} > /dev/null 2>&1")
12
+ end
13
+
14
+ exit system("bundle exec rake $TASK")
@@ -0,0 +1,13 @@
1
+ module ActiveRecord::Associations::Builder
2
+ class CountLoader < SingularAssociation
3
+ self.valid_options = [:class, :class_name, :foreign_key]
4
+
5
+ def macro
6
+ :count_loader
7
+ end
8
+
9
+ def self.valid_dependent_options
10
+ []
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ module ActiveRecord
2
+ module Associations
3
+ class CountLoader < SingularAssociation
4
+ # Not preloaded behaviour of count_loader association
5
+ # When this method is called, it will be N+1 query
6
+ def load_target
7
+ count_target = reflection.name_without_count.to_sym
8
+ @target = owner.association(count_target).count
9
+
10
+ loaded! unless loaded?
11
+ target
12
+ rescue ActiveRecord::RecordNotFound
13
+ reset
14
+ end
15
+ end
16
+ end
17
+
18
+ module Reflection
19
+ class CountLoaderReflection < AssociationReflection
20
+ def initialize(name, scope, options, active_record)
21
+ super(name, scope, options, active_record)
22
+ end
23
+
24
+ def macro; :count_loader; end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ module ActiveRecord
2
+ module Associations
3
+ class Preloader
4
+ class CountLoader < SingularAssociation
5
+ def association_key_name
6
+ reflection.foreign_key
7
+ end
8
+
9
+ def owner_key_name
10
+ reflection.active_record_primary_key
11
+ end
12
+
13
+ private
14
+
15
+ def preload(preloader)
16
+ associated_records_by_owner(preloader).each do |owner, associated_records|
17
+ count = associated_records.count
18
+
19
+ association = owner.association(reflection.name)
20
+ association.target = count
21
+ end
22
+ end
23
+
24
+ def load_slices(slices)
25
+ @preloaded_records = slices.flat_map { |slice|
26
+ records_for(slice)
27
+ }
28
+
29
+ @preloaded_records.map { |record|
30
+ key = record
31
+ [record, key]
32
+ }
33
+ end
34
+
35
+ def query_scope(ids)
36
+ scope.where(association_key.in(ids)).pluck(association_key_name)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveRecord
2
+ module Precount
3
+ module BaseExtension
4
+ delegate :precount, to: :all
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ require "active_record/precount/base_extension"
2
+ require "active_record/precount/has_many_extension"
3
+ require "active_record/precount/join_dependency_extension"
4
+ require "active_record/precount/preloader_extension"
5
+ require "active_record/precount/reflection_extension"
6
+ require "active_record/precount/relation_extension"
7
+
8
+ ActiveSupport.on_load(:active_record) do
9
+ module ActiveRecord
10
+ Base.send(:extend, Precount::BaseExtension)
11
+ Relation.send(:prepend, Precount::RelationExtension)
12
+ Reflection.send(:prepend, Precount::ReflectionExtension)
13
+ Associations::Preloader.send(:prepend, Precount::PreloaderExtension)
14
+ Associations::JoinDependency.send(:prepend, Precount::JoinDependencyExtension)
15
+ Associations::Builder::HasMany.send(:prepend, Precount::Builder::HasManyExtension)
16
+ Reflection::AssociationReflection.send(:prepend, Precount::AssociationReflectionExtension)
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveRecord
2
+ module Precount
3
+ module Builder
4
+ module HasManyExtension
5
+ def valid_options
6
+ super + [:count_loader]
7
+ end
8
+
9
+ def build(model)
10
+ define_count_loader(model) if options[:count_loader]
11
+ super
12
+ end
13
+
14
+ def define_count_loader(model)
15
+ name_with_count = :"#{name}_count"
16
+ name_with_count = options[:count_loader] if options[:count_loader].is_a?(Symbol)
17
+
18
+ valid_options = options.slice(*Associations::Builder::CountLoader.valid_options)
19
+ reflection = Associations::Builder::CountLoader.build(model, name_with_count, nil, valid_options)
20
+ Reflection.add_reflection(model, name_with_count, reflection)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end