From c19f90c0181b222b459c91e548ee95e38404c8ee Mon Sep 17 00:00:00 2001 From: John Mettraux Date: Wed, 23 Mar 2011 15:40:24 +0900 Subject: [PATCH] StorageParticipant :count uniformization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes gh-22 Thanks Jan Topiński --- CHANGELOG.txt | 1 + CREDITS.txt | 1 + lib/ruote/part/storage_participant.rb | 66 ++++++++++++++------ test/functional/ft_20_storage_participant.rb | 37 ++++++++++- 4 files changed, 86 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 7b988826..8a694350 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -4,6 +4,7 @@ == ruote - 2.2.1 not yet released +- StorageParticipant :count => true uniformization (Thanks Jan Topiński) - StorageParticipant#by_fei (alias for #[]) diff --git a/CREDITS.txt b/CREDITS.txt index 27d36a5b..73132750 100644 --- a/CREDITS.txt +++ b/CREDITS.txt @@ -47,6 +47,7 @@ Richard Jennings Feedback -------- +Jan Topiński - https://github.com/simcha Nando Sola - many help Iuri Gagnidze - ProcessStatus#definition_name issues, Engine#leftovers John Le - https://github.com/sandbox diff --git a/lib/ruote/part/storage_participant.rb b/lib/ruote/part/storage_participant.rb index 9b724bd2..2cbcc084 100644 --- a/lib/ruote/part/storage_participant.rb +++ b/lib/ruote/part/storage_participant.rb @@ -153,7 +153,9 @@ def each(&block) # def all(opts={}) - fetch_all(opts).map { |hwi| Ruote::Workitem.new(hwi) } + res = fetch_all(opts) + + res.is_a?(Array) ? res.map { |hwi| Ruote::Workitem.new(hwi) } : res end # A convenience method (especially when testing), returns the first @@ -161,18 +163,20 @@ def all(opts={}) # def first - hwi = fetch_all.first - - hwi ? Ruote::Workitem.new(hwi) : nil + wi(fetch_all.first) end # Return all workitems for the specified wfid # - def by_wfid(wfid) + def by_wfid(wfid, opts={}) - @context.storage.get_many('workitems', wfid).collect { |hwi| - Ruote::Workitem.new(hwi) - } + hwis = if @context.storage.respond_to?(:by_wfid) + @context.storage.by_wfid('workitems', wfid, opts) + else + @context.storage.get_many('workitems', wfid, opts) + end + + wis(hwis) end # Returns all workitems for the specified participant name @@ -185,12 +189,16 @@ def by_participant(participant_name, opts={}) else - fetch_all(opts).select { |wi| + count = opts.delete(:count) + + res = fetch_all(opts).select { |wi| wi['participant_name'] == participant_name } + + count ? res.size : res end - hwis.collect { |hwi| Ruote::Workitem.new(hwi) } + wis(hwis) end # field : returns all the workitems with the given field name present. @@ -202,21 +210,27 @@ def by_participant(participant_name, opts={}) # CouchStorage), the others will load all the workitems and then filter # them. # - def by_field(field, value=nil) + def by_field(field, value=nil, opts={}) + + (value, opts = nil, value) if value.is_a?(Hash) hwis = if @context.storage.respond_to?(:by_field) - @context.storage.by_field('workitems', field, value) + @context.storage.by_field('workitems', field, value, opts) else - fetch_all.select { |hwi| + count = opts.delete(:count) + + res = fetch_all(opts).select { |hwi| hwi['fields'].keys.include?(field) && (value.nil? || hwi['fields'][field] == value) } + + count ? res.size : res end - hwis.collect { |hwi| Ruote::Workitem.new(hwi) } + wis(hwis) end # Queries the store participant for workitems. @@ -252,18 +266,22 @@ def query(criteria) opts[:count] = cr.delete('count') wfid = cr.delete('wfid') + + count = opts[:count] + pname = cr.delete('participant_name') || cr.delete('participant') + opts.delete(:count) if pname hwis = wfid ? @context.storage.get_many('workitems', wfid, opts) : fetch_all(opts) - return hwis if opts[:count] + return hwis unless hwis.is_a?(Array) - hwis.select { |hwi| + hwis = hwis.select { |hwi| Ruote::StorageParticipant.matches?(hwi, pname, cr) - }.collect { |hwi| - Ruote::Workitem.new(hwi) } + + count ? hwis.size : wis(hwis) end # Cleans this participant out completely @@ -339,6 +357,18 @@ def to_id(fei) a.join('!') end + + def wi(hwi) + + hwi ? Ruote::Workitem.new(hwi) : nil + end + + def wis(workitems_or_count) + + workitems_or_count.is_a?(Array) ? + workitems_or_count.collect { |wi| Ruote::Workitem.new(wi) } : + workitems_or_count + end end end diff --git a/test/functional/ft_20_storage_participant.rb b/test/functional/ft_20_storage_participant.rb index 8dc00a92..e278a329 100644 --- a/test/functional/ft_20_storage_participant.rb +++ b/test/functional/ft_20_storage_participant.rb @@ -72,7 +72,33 @@ def test_purge assert alpha.first.nil? end - def test_find_by_wfid + def test_all + + n = 3 + + pdef = Ruote.process_definition :name => 'def0' do + alpha + end + + @engine.register_participant :alpha, Ruote::StorageParticipant + + wfids = [] + + n.times { wfids << @engine.launch(pdef) } + + while @engine.storage_participant.size < n + sleep 0.400 + end + + assert_equal( + [ Ruote::Workitem ] * 3, + @engine.storage_participant.all.collect { |wi| wi.class }) + + assert_equal 3, @engine.storage_participant.size + assert_equal 3, @engine.storage_participant.all(:count => true) + end + + def test_by_wfid pdef = Ruote.process_definition :name => 'def0' do concurrence do @@ -94,6 +120,7 @@ def test_find_by_wfid assert_equal 2, alpha.size assert_equal 2, alpha.by_wfid(wfid).size + assert_equal 2, alpha.by_wfid(wfid, :count => true) end CON_AL_BRAVO = Ruote.process_definition :name => 'con_al_bravo' do @@ -133,6 +160,9 @@ def test_by_participant assert_equal Ruote::Workitem, @part.by_participant('alpha').first.class assert_equal 1, @part.by_participant('alpha').size assert_equal 1, @part.by_participant('bravo').size + + assert_equal 1, @part.by_participant('alpha', :count => true) + assert_equal 1, @part.by_participant('bravo', :count => true) end def test_by_field @@ -144,6 +174,7 @@ def test_by_field assert_equal 2, @part.by_field('place').size assert_equal 2, @part.by_field('character').size assert_equal 1, @part.by_field('adversary').size + assert_equal 2, @part.by_field('character', :count => true) end def test_by_field_and_value @@ -154,6 +185,7 @@ def test_by_field_and_value assert_equal 0, @part.by_field('place', 'nara').size assert_equal 2, @part.by_field('place', 'heiankyou').size assert_equal 1, @part.by_field('character', 'minamoto no hirosama').size + assert_equal 2, @part.by_field('place', 'heiankyou', :count => true) end def test_query @@ -193,6 +225,9 @@ def test_query assert_equal( 2, @part.query('place' => 'heiankyou', :participant => 'alpha').size) + + assert_equal 2, @part.query(:participant => 'alpha').size + assert_equal 2, @part.query(:participant => 'alpha', :count => true) end # Issue reported in