batched_query 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd2d283bc329bbab254c956269ad1c2d3fd65c29
4
+ data.tar.gz: 795174375fcc60ec9f0a2fc84e1ddbd1d727de12
5
+ SHA512:
6
+ metadata.gz: a60627f3311c91736deee2da5fd00256a8de30e7afbff7f451f93bb444f2c713e18f8e57a523d3886026b40787adb7934661e7cad34f5a839f9f6104266c72c1
7
+ data.tar.gz: 723c80e1bec705245b80482bf045100b294f7b0f7aa0b803b54f6410ca7ccf5e3ad4f4b5c717fa8ae721514341366795a3398b9ad933cf4d893b048208b012b6
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in batched_query.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Edmund Mai
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,29 @@
1
+ # BatchedQuery
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'batched_query'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install batched_query
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/batched_query/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'batched_query/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "batched_query"
8
+ spec.version = BatchedQuery::VERSION
9
+ spec.authors = ["Edmund Mai"]
10
+ spec.email = ["edmundmai@gmail.com"]
11
+ spec.summary = %q{Substitute for ActiveRecord's find_each and find_in_batches methods}
12
+ spec.description = %q{Allows you to separate queries that return large results into several subsets to lower memory consumption}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+ spec.add_development_dependency "rspec"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,5 @@
1
+ require "batched_query/version"
2
+ require "batched_query/runner"
3
+
4
+ module BatchedQuery
5
+ end
@@ -0,0 +1,53 @@
1
+ module BatchedQuery
2
+ class Runner
3
+ DEFAULT_LIMIT = 500
4
+
5
+ def self.limit=(limit)
6
+ @limit = limit
7
+ end
8
+
9
+ def self.limit
10
+ @limit || DEFAULT_LIMIT
11
+ end
12
+
13
+ def self.each_set(query, &block)
14
+ cached_ids = get_ordered_list_of_ids(query)
15
+
16
+ cached_ids.each_slice(limit).each do |batch_of_ids|
17
+ results = query.where(:id => batch_of_ids)
18
+ block.call results
19
+ end
20
+ end
21
+
22
+ def self.each_result(query, &block)
23
+ each_set(query) do |set|
24
+ set.each do |result|
25
+ block.call result
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def self.get_ordered_list_of_ids(query)
33
+ query.pluck(:id)
34
+ end
35
+ end
36
+ end
37
+
38
+ # Any ActiveRecord query
39
+ cars = Car.where("brand_name = 'Ferrari'")order("created_at desc")
40
+
41
+ # Set the limit of each subquery
42
+ BatchedQuery::Runner.limit = 100
43
+ BatchedQuery::Runner.each_set(cars) do |batch_of_cars|
44
+ # process results in manageable subsets
45
+ batch_of_cars.map { |car| ... }
46
+ end
47
+
48
+ BatchedQuery::Runner.limit = 200
49
+ BatchedQuery::Runner.each_result(cars) do |car|
50
+ # access each car as if you loaded all of the records at once
51
+ car.start!
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module BatchedQuery
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe BatchedQuery::Runner do
4
+ describe ".each_set" do
5
+ let(:query) do
6
+ double("User", pluck: [1,2], where: [1, 2])
7
+ end
8
+
9
+ before do
10
+ BatchedQuery::Runner.limit = 1
11
+ end
12
+
13
+ it "calls the block twice" do
14
+ expect { |b| BatchedQuery::Runner.each_set(query, &b) }.to yield_control.exactly(2).times
15
+ end
16
+
17
+ it "splits the query into two parts" do
18
+ BatchedQuery::Runner.each_set(query) do |current_number|
19
+ #do nothing
20
+ end
21
+
22
+ expect(query).to have_received(:where).with(:id => [1])
23
+ expect(query).to have_received(:where).with(:id => [2])
24
+ end
25
+
26
+ it "calls block with each query subset" do
27
+ total = []
28
+ BatchedQuery::Runner.each_set(query) do |current_subset|
29
+ total = total + current_subset
30
+ end
31
+
32
+ expect(total).to eq([1, 2, 1, 2])
33
+ end
34
+ end
35
+
36
+ describe ".each_result" do
37
+ let(:query) do
38
+ double("User", pluck: [1,2], where: [1, 2])
39
+ end
40
+
41
+ before do
42
+ BatchedQuery::Runner.limit = 2
43
+ end
44
+
45
+ it "calls the block twice" do
46
+ expect { |b| BatchedQuery::Runner.each_result(query, &b) }.to yield_control.exactly(2).times
47
+ end
48
+
49
+ it "calls block with each query result" do
50
+ total = 0
51
+ BatchedQuery::Runner.each_result(query) do |current_result|
52
+ total = total + current_result
53
+ end
54
+
55
+ expect(total).to eq(3)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe BatchedQuery do
4
+ end
@@ -0,0 +1,99 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ $LOAD_PATH << '../lib'
19
+ require "batched_query/runner"
20
+ require "batched_query/runner_spec"
21
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
22
+ RSpec.configure do |config|
23
+ # rspec-expectations config goes here. You can use an alternate
24
+ # assertion/expectation library such as wrong or the stdlib/minitest
25
+ # assertions if you prefer.
26
+ config.expect_with :rspec do |expectations|
27
+ # This option will default to `true` in RSpec 4. It makes the `description`
28
+ # and `failure_message` of custom matchers include text for helper methods
29
+ # defined using `chain`, e.g.:
30
+ # be_bigger_than(2).and_smaller_than(4).description
31
+ # # => "be bigger than 2 and smaller than 4"
32
+ # ...rather than:
33
+ # # => "be bigger than 2"
34
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
+ end
36
+
37
+ # rspec-mocks config goes here. You can use an alternate test double
38
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
39
+ config.mock_with :rspec do |mocks|
40
+ # Prevents you from mocking or stubbing a method that does not exist on
41
+ # a real object. This is generally recommended, and will default to
42
+ # `true` in RSpec 4.
43
+ mocks.verify_partial_doubles = true
44
+ end
45
+
46
+ # The settings below are suggested to provide a good initial experience
47
+ # with RSpec, but feel free to customize to your heart's content.
48
+ =begin
49
+ # These two settings work together to allow you to limit a spec run
50
+ # to individual examples or groups you care about by tagging them with
51
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
52
+ # get run.
53
+ config.filter_run :focus
54
+ config.run_all_when_everything_filtered = true
55
+
56
+ # Allows RSpec to persist some state between runs in order to support
57
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
58
+ # you configure your source control system to ignore this file.
59
+ config.example_status_persistence_file_path = "spec/examples.txt"
60
+
61
+ # Limits the available syntax to the non-monkey patched syntax that is
62
+ # recommended. For more details, see:
63
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
64
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
65
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
66
+ config.disable_monkey_patching!
67
+
68
+ # This setting enables warnings. It's recommended, but in some cases may
69
+ # be too noisy due to issues in dependencies.
70
+ config.warnings = true
71
+
72
+ # Many RSpec users commonly either run the entire suite or an individual
73
+ # file, and it's useful to allow more verbose output when running an
74
+ # individual spec file.
75
+ if config.files_to_run.one?
76
+ # Use the documentation formatter for detailed output,
77
+ # unless a formatter has already been configured
78
+ # (e.g. via a command-line flag).
79
+ config.default_formatter = 'doc'
80
+ end
81
+
82
+ # Print the 10 slowest examples and example groups at the
83
+ # end of the spec run, to help surface which specs are running
84
+ # particularly slow.
85
+ config.profile_examples = 10
86
+
87
+ # Run specs in random order to surface order dependencies. If you find an
88
+ # order dependency and want to debug it, you can fix the order by providing
89
+ # the seed, which is printed after each run.
90
+ # --seed 1234
91
+ config.order = :random
92
+
93
+ # Seed global randomization in this process using the `--seed` CLI option.
94
+ # Setting this allows you to use `--seed` to deterministically reproduce
95
+ # test failures related to randomization by passing the same `--seed` value
96
+ # as the one that triggered the failure.
97
+ Kernel.srand config.seed
98
+ =end
99
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: batched_query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Edmund Mai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Allows you to separate queries that return large results into several
56
+ subsets to lower memory consumption
57
+ email:
58
+ - edmundmai@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - batched_query.gemspec
70
+ - lib/batched_query.rb
71
+ - lib/batched_query/runner.rb
72
+ - lib/batched_query/version.rb
73
+ - spec/batched_query/runner_spec.rb
74
+ - spec/batched_query_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Substitute for ActiveRecord's find_each and find_in_batches methods
100
+ test_files:
101
+ - spec/batched_query/runner_spec.rb
102
+ - spec/batched_query_spec.rb
103
+ - spec/spec_helper.rb