couch_potato 1.10.1 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e0a3143ad59fbb9f3391e381841412de37afda869b90c3b105b3efe41859fa2
4
- data.tar.gz: 452e6d3cc546b0d45feb5ea94ea81779400adf6474e22580e6637d15f58858bb
3
+ metadata.gz: 5a7bdcd820492f1ac93c757f8438d2659610ed06dc8b6dd2dcb62d6bcfa8434f
4
+ data.tar.gz: 4d3d52005c4d7647291d35f1437c7be05326526f71ab3232d46556f398b2d0fe
5
5
  SHA512:
6
- metadata.gz: f517b4ecfaf2c57eceade400298c68ef2d189f1274709c95f7bd54d97721b9131b79430956e5c517cd2fbd3781149ff9fe54c4640eaea8e82b940e97c696fd04
7
- data.tar.gz: a668623b7cd70ab1acb86aff299f8729f8645625b560f3b45247447117c9ac69d5aa335d57ff9dacaa980cd021d2f01f5e21c0a88f5faa45d5add8045e016e21
6
+ metadata.gz: ac20227b8dfb0288d09e70dc515e90f0625660606ab130b0d0b8433591a2fd3247030b88c6137e60f20cf9e87862fa683276143ddfee3c8f7016e42deb69ea9d
7
+ data.tar.gz: 4c57160eb81d99070ee30bcb53723ece6de752af76b97ea7349f1bd0a5266bdd27f50b71461203d9b57799891a5f933333689a18a53e0badc1d304957d70337b
@@ -16,11 +16,8 @@ jobs:
16
16
  strategy:
17
17
  fail-fast: false
18
18
  matrix:
19
- ruby: [2.7, '3.0', "jruby"]
19
+ ruby: [2.7, "3.0", "3.1", "jruby"]
20
20
  gemfile:
21
- - "active_support_5_0"
22
- - "active_support_5_1"
23
- - "active_support_5_2"
24
21
  - "active_support_6_0"
25
22
  - "active_support_6_1"
26
23
  - "active_support_7_0"
@@ -28,12 +25,8 @@ jobs:
28
25
  - ruby: "jruby"
29
26
  gemfile: "active_support_7_0"
30
27
  - ruby: "3.0"
31
- gemfile: "active_support_5_0"
32
- - ruby: "3.0"
33
- gemfile: "active_support_5_1"
34
- - ruby: "3.0"
35
- gemfile: "active_support_5_2"
36
- - ruby: "3.0"
28
+ gemfile: "active_support_6_0"
29
+ - ruby: "3.1"
37
30
  gemfile: "active_support_6_0"
38
31
  steps:
39
32
  - uses: actions/checkout@v2
data/CHANGES.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## Changes
2
2
 
3
+ # 1.12.0
4
+
5
+ - remove active_support 5.x
6
+ - add Ruby 3.1 support (active_support 6.1, 7.0)
7
+
8
+ # 1.11.0
9
+
10
+ - improve view_in_batches performance by switching to using startkey_docid over skip
11
+
3
12
  ### 1.10.1
4
13
 
5
14
  - support passing an empty array to CouchPotato::Database#load
@@ -1,6 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rails', '~>6.0.2.2'
3
+ gem 'railties', '~>6.0.2.2'
4
+ gem 'activemodel'
4
5
  gem 'execjs'
5
6
 
6
7
  gemspec name: 'couch_potato', path: '..'
@@ -1,6 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rails', '~>6.1'
3
+ gem 'railties', '~>6.1'
4
+ gem 'activemodel'
4
5
  gem 'execjs'
5
6
 
6
7
  gemspec name: 'couch_potato', path: '..'
@@ -1,6 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rails', '~>7.0'
3
+ gem 'railties', '~>7.0.4'
4
+ gem 'activemodel'
4
5
  gem 'execjs'
5
6
 
6
7
  gemspec name: 'couch_potato', path: '..'
@@ -65,12 +65,27 @@ module CouchPotato
65
65
  # to a given block in batches of the given size, making multiple
66
66
  # requests with according skip/limit params sent to CouchDB.
67
67
  def view_in_batches(spec, batch_size: default_batch_size)
68
+ rows = nil
68
69
  batch = 0
69
70
  loop do
70
- spec.view_parameters = spec.view_parameters.merge({ skip: batch * batch_size, limit: batch_size })
71
- results = view(spec)
72
- yield results
73
- break if results.size < batch_size
71
+ spec.view_parameters = spec
72
+ .view_parameters
73
+ .merge({limit: batch_size})
74
+ .merge(
75
+ if rows
76
+ {
77
+ startkey: rows&.last&.dig('key'),
78
+ startkey_docid: rows&.last&.dig('id'),
79
+ skip: 1
80
+ }
81
+ else
82
+ {}
83
+ end
84
+ )
85
+ result = raw_view(spec)
86
+ rows = result['rows']
87
+ yield process_view_results(result, spec)
88
+ break if rows.size < batch_size
74
89
 
75
90
  batch += 1
76
91
  end
@@ -198,27 +213,34 @@ module CouchPotato
198
213
 
199
214
  def view_without_caching(spec)
200
215
  ActiveSupport::Notifications.instrument('couch_potato.view', name: "#{spec.design_document}/#{spec.view_name}") do
201
- results = CouchPotato::View::ViewQuery.new(
202
- couchrest_database,
203
- spec.design_document,
204
- { spec.view_name => {
205
- map: spec.map_function,
206
- reduce: spec.reduce_function
207
- } },
208
- ({ spec.list_name => spec.list_function } unless spec.list_name.nil?),
209
- spec.lib,
210
- spec.language
211
- ).query_view!(spec.view_parameters)
212
- processed_results = spec.process_results results
213
- if processed_results.respond_to?(:database=)
214
- processed_results.database = self
215
- elsif processed_results.respond_to?(:each)
216
- processed_results.each do |document|
217
- document.database = self if document.respond_to?(:database=)
218
- end
216
+ process_view_results(raw_view(spec), spec)
217
+ end
218
+ end
219
+
220
+ def process_view_results(results, spec)
221
+ processed_results = spec.process_results results
222
+ if processed_results.respond_to?(:database=)
223
+ processed_results.database = self
224
+ elsif processed_results.respond_to?(:each)
225
+ processed_results.each do |document|
226
+ document.database = self if document.respond_to?(:database=)
219
227
  end
220
- processed_results
221
228
  end
229
+ processed_results
230
+ end
231
+
232
+ def raw_view(spec)
233
+ CouchPotato::View::ViewQuery.new(
234
+ couchrest_database,
235
+ spec.design_document,
236
+ { spec.view_name => {
237
+ map: spec.map_function,
238
+ reduce: spec.reduce_function
239
+ } },
240
+ ({ spec.list_name => spec.list_function } unless spec.list_name.nil?),
241
+ spec.lib,
242
+ spec.language
243
+ ).query_view!(spec.view_parameters)
222
244
  end
223
245
 
224
246
  def load_document_without_caching(id)
@@ -8,7 +8,10 @@ module CouchPotato
8
8
  path = Rails.root.join('config/couchdb.yml')
9
9
  if File.exist?(path)
10
10
  require 'yaml'
11
- config = YAML.safe_load(ERB.new(File.read(path)).result, [Symbol], [], ['default'])[Rails.env]
11
+ config = YAML.safe_load(
12
+ ERB.new(File.read(path)).result,
13
+ permitted_classes: [Symbol],
14
+ )[Rails.env]
12
15
  CouchPotato.configure(config)
13
16
  else
14
17
  Rails.logger.warn 'Rails.root/config/couchdb.yml does not exist. Not configuring a database.'
@@ -1,4 +1,4 @@
1
1
  module CouchPotato
2
- VERSION = '1.10.1'.freeze
2
+ VERSION = '1.12.0'.freeze
3
3
  RSPEC_VERSION = '4.0.2'.freeze
4
4
  end
@@ -430,11 +430,11 @@ describe CouchPotato::Database, '#view_in_batches' do
430
430
  let(:view_query) do
431
431
  instance_double(
432
432
  CouchPotato::View::ViewQuery,
433
- query_view!: { 'rows' => [result] }
433
+ query_view!: { 'rows' => [] }
434
434
  )
435
435
  end
436
- let(:result) { double('result') }
437
- let(:spec) { double('view spec', process_results: [result]).as_null_object }
436
+ let(:processed_result) { double(:processed_result) }
437
+ let(:spec) { double('view spec', process_results: processed_result).as_null_object }
438
438
  let(:couchrest_db) { double('couchrest db').as_null_object }
439
439
  let(:db) { CouchPotato::Database.new(couchrest_db) }
440
440
 
@@ -443,22 +443,41 @@ describe CouchPotato::Database, '#view_in_batches' do
443
443
  .to receive_messages(new: view_query)
444
444
  end
445
445
 
446
- it 'sets skip/limit for each batch' do
447
- allow(spec).to receive(:process_results).and_return([result, result], [result]) # run twice
446
+ it 'sets no skip/startkey/startkey_docid for the first batch' do
448
447
  allow(spec).to receive(:view_parameters) { { key: 'x' } }
449
448
 
450
449
  expect(spec).to receive(:view_parameters=)
451
- .with({key: 'x', skip: 0, limit: 2})
450
+ .with({key: 'x', limit: 2})
451
+
452
+ db.view_in_batches(spec, batch_size: 2) { |results| }
453
+ end
454
+
455
+ it 'sets skip/startkey/startkey_docid for each other batch' do
456
+ allow(spec).to receive(:view_parameters) { { key: 'x' } }
457
+ allow(view_query).to receive(:query_view!)
458
+ .and_return({'rows' => [{}, {'key' => 'k1', 'id' => 'id1'}]}, {'rows' => [{}]})
459
+ allow(spec).to receive(:view_parameters=)
460
+
452
461
  expect(spec).to receive(:view_parameters=)
453
- .with({key: 'x', skip: 2, limit: 2})
462
+ .with({key: 'x', limit: 2, startkey: 'k1', startkey_docid: 'id1', skip: 1})
454
463
 
455
464
  db.view_in_batches(spec, batch_size: 2) { |results| }
456
465
  end
457
466
 
467
+ it 'yields processed results to the block' do
468
+ allow(view_query).to receive(:query_view!)
469
+ .and_return({'rows' => [{'key' => 'k1', 'id' => 'id1'}]})
470
+ allow(spec).to receive(:view_parameters=)
471
+
472
+ expect { |x| db.view_in_batches(spec, batch_size: 2, &x) }.to yield_with_args(processed_result)
473
+ end
474
+
458
475
  it 'yields batches until running out of data' do
459
- allow(spec).to receive(:process_results).and_return([result, result], [result])
476
+ allow(view_query).to receive(:query_view!)
477
+ .and_return({'rows' => [{}, {}]}, {'rows' => [{}]})
478
+ allow(spec).to receive(:process_results).and_return([processed_result, processed_result], [processed_result])
460
479
 
461
- expect { |b| db.view_in_batches(spec, batch_size: 2, &b) }.to yield_successive_args([result, result], [result])
480
+ expect { |b| db.view_in_batches(spec, batch_size: 2, &b) }.to yield_successive_args([processed_result, processed_result], [processed_result])
462
481
  end
463
482
  end
464
483
 
data/spec/views_spec.rb CHANGED
@@ -405,4 +405,15 @@ describe 'views' do
405
405
  expect(@db.view(Build.timeline(stale: 'ok'))).to be_empty
406
406
  end
407
407
  end
408
+
409
+ describe 'view_in_batches' do
410
+ it 'yields docs in batches until all gone' do
411
+ build1 = Build.new(time: 1).tap {|b| @db.save!(b) }
412
+ build2 = Build.new(time: 2).tap {|b| @db.save!(b) }
413
+ build3 = Build.new(time: 3).tap {|b| @db.save!(b) }
414
+
415
+ expect {|block| @db.view_in_batches(Build.timeline, batch_size: 2, &block)}
416
+ .to yield_successive_args([build1, build2], [build3])
417
+ end
418
+ end
408
419
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couch_potato
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.1
4
+ version: 1.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Lang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-07 00:00:00.000000000 Z
11
+ date: 2022-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -130,9 +130,6 @@ files:
130
130
  - Rakefile
131
131
  - couch_potato-rspec.gemspec
132
132
  - couch_potato.gemspec
133
- - gemfiles/active_support_5_0
134
- - gemfiles/active_support_5_1
135
- - gemfiles/active_support_5_2
136
133
  - gemfiles/active_support_6_0
137
134
  - gemfiles/active_support_6_1
138
135
  - gemfiles/active_support_7_0
@@ -234,7 +231,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
234
231
  - !ruby/object:Gem::Version
235
232
  version: '0'
236
233
  requirements: []
237
- rubygems_version: 3.2.32
234
+ rubygems_version: 3.3.3
238
235
  signing_key:
239
236
  specification_version: 4
240
237
  summary: Ruby persistence layer for CouchDB
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~>5.0.0'
4
- gem 'execjs'
5
-
6
- gemspec name: 'couch_potato', path: '..'
@@ -1,7 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'activemodel', '~>5.1.7'
4
- gem 'rails', '~>5.1.7'
5
- gem 'execjs'
6
-
7
- gemspec name: 'couch_potato', path: '..'
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~>5.2.3'
4
- gem 'execjs'
5
-
6
- gemspec name: 'couch_potato', path: '..'