activerecord-stream 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +1 -0
- data/activerecord-stream.gemspec +27 -0
- data/lib/activerecord/stream.rb +39 -0
- data/lib/activerecord/stream/version.rb +5 -0
- data/lib/core_ext/enumerator/drop.rb +9 -0
- data/lib/core_ext/enumerator/filter.rb +27 -0
- data/lib/core_ext/enumerator/take.rb +13 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bf405467f9cb3c18ceaba65b4023e89d60ccea1f
|
4
|
+
data.tar.gz: 51130ccf858bbda74eeb6545ca5803e13e163f9d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c3d9edf0589f1a9948d483d57e6152ba4d5924a50c0e7b625c7c2b3488d9ec3fa35331c5ec37bc669c280460747fc41137fb0385d2c46fd76bd8b45dbb059cf1
|
7
|
+
data.tar.gz: 1c769ccf0a5abdb0e451957dd951fd5b3fc3450f7f0458f8d946f1888e329d5f11de2050a000e8655b11ba44e8a56159ea3552ad6827e71d99cd77224dc09cf9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Scott Fleckenstein
|
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,48 @@
|
|
1
|
+
# Activerecord::Stream
|
2
|
+
|
3
|
+
Enables lazy-ish, Enumerator based traversal of data within ActiveRecord.
|
4
|
+
|
5
|
+
NOTE: yes, this uses an inefficient paging mechanism (offset/limit) that will get progressively
|
6
|
+
slower with each batch load. A future goal is to provide a pluggable paging mechanism to compensate,
|
7
|
+
allowing the integrating developer to choose a more efficient method when the schema and query allows.
|
8
|
+
|
9
|
+
Offset/limit is the very flexible method for streaming but is susceptible to [Schlemiel the Painter's algorithm](http://en.wikipedia.org/wiki/Joel_Spolsky#Schlemiel_the_Painter.27s_algorithm). As you get deaper into the stream, performance per batch will decrease.
|
10
|
+
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'activerecord-stream'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install activerecord-stream
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
|
30
|
+
# get an enumerator
|
31
|
+
users = User.order("id desc").stream
|
32
|
+
|
33
|
+
|
34
|
+
users.next # => #<User:...>
|
35
|
+
# The preceding call loads a batch of records
|
36
|
+
# User Load (330.4ms) SELECT "users".* FROM "users" ORDER BY id desc LIMIT 500 OFFSET 0
|
37
|
+
user.next # => #<User:...>
|
38
|
+
# No DB load happens until we exhaust the batch loaded in the previous call
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activerecord/stream/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerecord-stream"
|
8
|
+
spec.version = ActiveRecord::Stream::VERSION
|
9
|
+
spec.authors = ["Scott Fleckenstein"]
|
10
|
+
spec.email = ["nullstyle@gmail.com"]
|
11
|
+
spec.description = %q{lazy-ish, Enumerator based traversal of data within ActiveRecord}
|
12
|
+
spec.summary = %q{lazy-ish, Enumerator based traversal of data within ActiveRecord}
|
13
|
+
spec.homepage = "https://github.com/nullstyle/activerecord-stream"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
|
25
|
+
spec.add_dependency "activerecord", ">= 3.2.0"
|
26
|
+
spec.add_dependency "activesupport", ">= 3.2.0"
|
27
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "activerecord/stream/version"
|
2
|
+
require 'core_ext/enumerator/take'
|
3
|
+
require 'core_ext/enumerator/drop'
|
4
|
+
require 'core_ext/enumerator/filter'
|
5
|
+
|
6
|
+
module ActiveRecord
|
7
|
+
module Stream
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def stream(options={})
|
13
|
+
offset = options[:start] || 0
|
14
|
+
batch_size = options[:batch_size] || 500
|
15
|
+
scope = respond_to?(:scoped) ? scoped : all
|
16
|
+
|
17
|
+
Enumerator.new do |yielder|
|
18
|
+
loop do
|
19
|
+
current_batch = load_batch(scope, batch_size, offset)
|
20
|
+
current_batch.each{|r| yielder << r}
|
21
|
+
|
22
|
+
break if current_batch.length < batch_size
|
23
|
+
offset += batch_size
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def load_batch(scope, limit, offset)
|
30
|
+
scope.limit(limit).offset(offset).to_a
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
ActiveRecord::Base.send(:include, ActiveRecord::Stream)
|
39
|
+
ActiveRecord::Relation.send(:include, ActiveRecord::Stream)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Enumerator::Filter
|
2
|
+
def select(&block)
|
3
|
+
filter(true, &block)
|
4
|
+
end
|
5
|
+
|
6
|
+
def reject(&block)
|
7
|
+
filter(false, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
def filter(filter_val, &block)
|
12
|
+
Enumerator.new do |yielder|
|
13
|
+
|
14
|
+
begin
|
15
|
+
loop do
|
16
|
+
val = self.next
|
17
|
+
should_yield = block.call(val) == filter_val
|
18
|
+
yielder << val if should_yield
|
19
|
+
end
|
20
|
+
rescue StopIteration
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
Enumerator.send(:include, Enumerator::Filter)
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-stream
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scott Fleckenstein
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-16 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.2.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.2.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.2.0
|
69
|
+
description: lazy-ish, Enumerator based traversal of data within ActiveRecord
|
70
|
+
email:
|
71
|
+
- nullstyle@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- activerecord-stream.gemspec
|
82
|
+
- lib/activerecord/stream.rb
|
83
|
+
- lib/activerecord/stream/version.rb
|
84
|
+
- lib/core_ext/enumerator/drop.rb
|
85
|
+
- lib/core_ext/enumerator/filter.rb
|
86
|
+
- lib/core_ext/enumerator/take.rb
|
87
|
+
homepage: https://github.com/nullstyle/activerecord-stream
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.2.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: lazy-ish, Enumerator based traversal of data within ActiveRecord
|
111
|
+
test_files: []
|