ar-find-in-batches-with-order 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +1 -0
- data/ar-find-in-batches-with-order.gemspec +23 -0
- data/lib/ar-find-in-batches-with-order.rb +62 -0
- data/lib/ar-find-in-batches-with-order/version.rb +5 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9a925a70757e5aba077a829c8390721aafb758e6
|
4
|
+
data.tar.gz: 6662ce7e8fb81f22f807be5e5b3c7fbd42759258
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ba8c62855896049ebf287bf1b5435e40bf11570561360e20ce1bd5af543c216a7ae817b31a7d67339ebd21b1c496922887a8b381183510e2df7b72d7b5a3d2a
|
7
|
+
data.tar.gz: 68704bce9b02c094c168ae4320fc575f7091056c0f462d8061bdae449e7921d8a444250caf9cd6f876bd4acc3439579075623536b2f70bda7289431c44bb8d3e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Nam Chu Hoai
|
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,35 @@
|
|
1
|
+
# ar-find-in-batches-with-order
|
2
|
+
|
3
|
+
Allows you to use find_each and find_each_in_batches with custom ordering.
|
4
|
+
|
5
|
+
This is useful if your domain knowledge allows youo to make assumptions about the order of your records. In the vanilla find_each/find_each_in_batches implementation, Rails disables custom ordering to ensure consistency in case ordering changes between batchings.
|
6
|
+
|
7
|
+
However, in many cases you know that this would never happen. For example, in acitivity feeds, you might want to batch-find activitites sorted by newest items. You know that new items cannot disrupt the ordering once batching started.
|
8
|
+
|
9
|
+
That being said, depending on your data model, deletions of records might screw you over during the batch-overlap detection, so use with caution.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'ar-find-in-batches-with-order'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install ar-find-in-batches-with-order
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Usage is pretty much identical to find_each/find_each_in_batches, you'd just want to use the `:property_key` and `:direction` options to specify the ordering explicitly.
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it ( http://github.com/nambrot/ar-find-in-batches-with-order/fork )
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ar-find-in-batches-with-order/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ar-find-in-batches-with-order"
|
8
|
+
spec.version = ActiveRecord::FindInBatchesWithOrder::VERSION
|
9
|
+
spec.authors = ["Nam Chu Hoai"]
|
10
|
+
spec.email = ["nambrot@googlemail.com"]
|
11
|
+
spec.summary = "Allow find_in_batches with custom order property"
|
12
|
+
spec.description = "Allow find_in_batches with custom order property"
|
13
|
+
spec.homepage = "https://github.com/nambrot/ar-find-in-batches-with-order"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "ar-find-in-batches-with-order/version"
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module FindInBatchesWithOrder
|
5
|
+
def find_in_batches_with_order(options = {})
|
6
|
+
relation = self
|
7
|
+
|
8
|
+
# we have to be explicit about the options to ensure proper ordering and retrieval
|
9
|
+
|
10
|
+
direction = options.delete(:direction) || (arel.orders.first.try(:ascending?) ? :asc : nil) || (arel.orders.first.try(:descending?) ? :desc : nil) || :desc
|
11
|
+
start = options.delete(:start)
|
12
|
+
batch_size = options.delete(:batch_size) || 1000
|
13
|
+
|
14
|
+
# try to deduct the property_key, but safer to specificy directly
|
15
|
+
property_key = options.delete(:property_key) || arel.orders.first.try(:value).try(:name)
|
16
|
+
relation = relation.limit(batch_size)
|
17
|
+
|
18
|
+
# in strictmode, we return records with same values as the last record of the last batch
|
19
|
+
strict_mode = options.delete(:strict_mode) || true
|
20
|
+
|
21
|
+
|
22
|
+
records = start ? (direction == :desc ? relation.where(table[property_key].lteq(start)).to_a : relation.where(table[property_key].gteq(start)).to_a) : relation.to_a
|
23
|
+
|
24
|
+
while records.any?
|
25
|
+
records_size = records.size
|
26
|
+
|
27
|
+
yield records
|
28
|
+
|
29
|
+
|
30
|
+
break if records_size < batch_size
|
31
|
+
|
32
|
+
start = records.last.try(property_key)
|
33
|
+
|
34
|
+
records = strict_mode ? (direction == :desc ? relation.where(table[property_key].lteq(start)).to_a : relation.where(table[property_key].gteq(start)).to_a) : (direction == :desc ? relation.where(table[property_key].lt(start)).to_a : relation.where(table[property_key].gt(start)).to_a)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# note that in strict mode we might itereate perpetually if the overlap in values is too high in relation to the batch size
|
39
|
+
def find_each_with_order(options = {})
|
40
|
+
last_record = nil
|
41
|
+
find_in_batches_with_order(options) do |records|
|
42
|
+
|
43
|
+
records.each do |record|
|
44
|
+
# we need to find the last record of the previous batch
|
45
|
+
next if last_record and (record != last_record)
|
46
|
+
if last_record
|
47
|
+
last_record = nil
|
48
|
+
next
|
49
|
+
end
|
50
|
+
yield record
|
51
|
+
end
|
52
|
+
last_record = records.last
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
class Relation
|
60
|
+
include FindInBatchesWithOrder
|
61
|
+
end
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ar-find-in-batches-with-order
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nam Chu Hoai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Allow find_in_batches with custom order property
|
42
|
+
email:
|
43
|
+
- nambrot@googlemail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- ar-find-in-batches-with-order.gemspec
|
54
|
+
- lib/ar-find-in-batches-with-order.rb
|
55
|
+
- lib/ar-find-in-batches-with-order/version.rb
|
56
|
+
homepage: https://github.com/nambrot/ar-find-in-batches-with-order
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.2.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Allow find_in_batches with custom order property
|
80
|
+
test_files: []
|