ar-ondemand 1.1.6 → 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmZhMmNkZmQzM2E0M2RlOGVhY2NkZDJmMjE1OTBlNDQ2Zjg2Zjk4Mw==
4
+ ZDlkODQyOThhNDU2OWIwOWIyYWE1MzAxNzBiZjQ3YzNjOWViOWRlYw==
5
5
  data.tar.gz: !binary |-
6
- ZDFmNzAzZjRmN2E5ZTQ1NzM1NTdkNDEzZTA2YThlNGE5Yjc5OTlhMQ==
6
+ OGVlODFkZjdmMzA3OGVmNjQwYzY3NzY4NjE2OTY3YzA2NDJkN2NlYQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDA3MDQzNTg2MDYyMjViNWYwYTdjNzVjOWJlOWYzZGM5Njc5NTlmYmYzNWMz
10
- MzVlYzkwY2EyYzI2ZTJiNmE2ZTRkMzZjNzNjY2VhYjFkNDkwMzUxNDU1YTkz
11
- YzRmNjBiMWJmMzg3YmM0YTUwNTk2OGE1NTQzZWQ2YThiYjRhYmI=
9
+ MTA4NDJlZDljZmU5MTM3MDE0Njg5ZTg3YmYzNzEzMGY0MTI5YjA4ZjFjNDVi
10
+ ODBkNDg2YWI4NWI3N2I0NWMyNzgwZmJjMTYwYTVjM2Y1YmNiZGI3Yzc1YjI0
11
+ ZjQ4YjFhZjJmZWI4ODY4YzlmNjY2MjY1NjI2M2Q1OGNlZjQ0NDc=
12
12
  data.tar.gz: !binary |-
13
- YTA3MTE5ZDE4ZGM1ZGIxNzA0NzJmZjZiNmM3ZmE1Y2NlNjJjZjg3NTQxYWI0
14
- OGRjOGZhNWM1M2Q5NTE4ZWRlZGMzYzM0MWM4YmI0OTRiYjk4MDY1NTEzMzdh
15
- ZWNlZjcxYmZiZjljY2JmMGMzYmE4ZmJkN2NmMTc1NmIyMzlkZTA=
13
+ OWU1NWViMGY0MjA5YmFiOWIxN2ZjZjI3OWExOGJiYWY0OTgxODQwNmQ1Yjhh
14
+ Mzc1OGJiMzY0MDUzYjA1ZWQ4NGU4OThkZTQyOTQ5YmM4ZjMyYjU1NzNlYmMw
15
+ ZGMzNDhjNTYwYjZhY2VhNTE1ZTExNTdlZGM0ZDY4MzcyYWQxZTY=
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ar-ondemand (1.1.6)
4
+ ar-ondemand (1.1.7)
5
5
  activerecord (~> 3.2)
6
6
  activesupport (~> 3.2)
7
7
 
@@ -2,4 +2,5 @@ require_dependency 'ar-ondemand/on_demand'
2
2
  require_dependency 'ar-ondemand/for_reading'
3
3
  require_dependency 'ar-ondemand/for_streaming'
4
4
  require_dependency 'ar-ondemand/for_enumeration_reading'
5
+ require_dependency 'ar-ondemand/for_enumeration_streaming'
5
6
  require_dependency 'ar-ondemand/delete_all_by_pk'
@@ -33,6 +33,10 @@ module ActiveRecord
33
33
  @row = nil
34
34
  end
35
35
 
36
+ def size
37
+ @result_set.rows.size
38
+ end
39
+
36
40
  end
37
41
  end
38
42
  end
@@ -0,0 +1,37 @@
1
+ require 'active_support/concern'
2
+ require 'ar-ondemand/fast_enumeration'
3
+
4
+ module ActiveRecord
5
+ module OnDemand
6
+ module EnumerationStreaming
7
+ extend ::ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ def for_enumeration_streaming(options = {})
11
+ query_for_enumeration_streaming self, options
12
+ end
13
+
14
+ private
15
+ def query_for_enumeration_streaming(ar, options)
16
+ options[:batch_size] ||= 50_000
17
+ options[:id_column] ||= 'id'
18
+ ::Enumerator.new do |eblock|
19
+ start_id = 0
20
+ loop do
21
+ batch = ar.where("#{options[:id_column]} > #{start_id}").order(options[:id_column]).limit(options[:batch_size]).for_enumeration_reading
22
+ break if batch.size == 0
23
+ batch.each do |r|
24
+ eblock << r
25
+ start_id = r.id
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ ::ActiveRecord::Base.send :include, ::ActiveRecord::OnDemand::EnumerationStreaming
36
+ ::ActiveRecord::Relation.send :include, ::ActiveRecord::OnDemand::EnumerationStreaming::ClassMethods
37
+
@@ -1,3 +1,3 @@
1
1
  module ArOnDemand
2
- VERSION = '1.1.6'
2
+ VERSION = '1.1.7'
3
3
  end
@@ -69,6 +69,10 @@ describe 'ForEnumerationReading' do
69
69
  expect{cache.first[1].id}.to raise_error(RuntimeError)
70
70
  end
71
71
 
72
+ it 'should return the correct size' do
73
+ expect(AuditRecord.where(customer_id: 1).for_enumeration_reading.size).to be 25
74
+ end
75
+
72
76
  end
73
77
  end
74
78
  end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ require 'ar-ondemand'
3
+
4
+ describe 'ForEnumerationStreaming' do
5
+ context 'Testing' do
6
+ context 'Ensure Persistance Works' do
7
+ before(:each) do
8
+ create(:audit_record)
9
+ end
10
+
11
+ it 'should do persist' do
12
+ expect(AuditRecord.all.length).to be 1
13
+ end
14
+
15
+ it 'should do foo' do
16
+ expect(AuditRecord.first.action).to eq 'create'
17
+ end
18
+ end
19
+
20
+ context 'Iterating' do
21
+ before(:each) do
22
+ (1..25).each do
23
+ create(:audit_record)
24
+ end
25
+ end
26
+
27
+ it 'should support iterating' do
28
+ total = 0
29
+ AuditRecord.where(customer_id: 1).for_enumeration_streaming.each do |r|
30
+ total += 1
31
+ end
32
+ expect(total).to be 25
33
+ end
34
+
35
+ it 'should support iterating with small batch size' do
36
+ total = 0
37
+ AuditRecord.where(customer_id: 1).for_enumeration_streaming(batch_size: 1).each do |r|
38
+ total += 1
39
+ end
40
+ expect(total).to be 25
41
+ end
42
+
43
+
44
+
45
+ it 'should produce same results as regular iterating' do
46
+ records_a = Set.new
47
+ AuditRecord.where(customer_id: 1).for_enumeration_streaming(batch_size: 2).each do |r|
48
+ records_a.add r.id
49
+ end
50
+
51
+ records_b = Set.new
52
+ AuditRecord.where(customer_id: 1).each do |r|
53
+ records_b.add r.id
54
+ end
55
+
56
+ expect(records_a).to eq records_b
57
+ end
58
+
59
+ it 'should not allow access outside each enumeration' do
60
+ obj = nil
61
+ AuditRecord.where(customer_id: 1).for_enumeration_streaming.each do |r|
62
+ obj = r
63
+ end
64
+ expect{obj.id}.to raise_error(RuntimeError)
65
+ end
66
+
67
+ it 'should not allow bad id column' do
68
+ expect {
69
+ AuditRecord.where(customer_id: 1).for_enumeration_streaming(id_column: 'foo').each do |r|
70
+ obj = r
71
+ end
72
+ }.to raise_error(ActiveRecord::StatementInvalid)
73
+ end
74
+ end
75
+ end
76
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar-ondemand
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
4
+ version: 1.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Frank
@@ -59,6 +59,7 @@ files:
59
59
  - lib/ar-ondemand/delete_all_by_pk.rb
60
60
  - lib/ar-ondemand/fast_enumeration.rb
61
61
  - lib/ar-ondemand/for_enumeration_reading.rb
62
+ - lib/ar-ondemand/for_enumeration_streaming.rb
62
63
  - lib/ar-ondemand/for_reading.rb
63
64
  - lib/ar-ondemand/for_streaming.rb
64
65
  - lib/ar-ondemand/on_demand.rb
@@ -74,6 +75,7 @@ files:
74
75
  - spec/factories/widget.rb
75
76
  - spec/lib/delete_all_by_pk_spec.rb
76
77
  - spec/lib/for_enumeration_reading_spec.rb
78
+ - spec/lib/for_enumeration_streaming_spec.rb
77
79
  - spec/lib/for_reading_spec.rb
78
80
  - spec/lib/for_streaming_spec.rb
79
81
  - spec/lib/on_demand_spec.rb