Skip to content

Commit

Permalink
StorageParticipant :count uniformization
Browse files Browse the repository at this point in the history
closes gh-22

Thanks Jan Topiński
  • Loading branch information
jmettraux committed Mar 23, 2011
1 parent a9aea6c commit c19f90c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Expand Up @@ -4,6 +4,7 @@

== ruote - 2.2.1 not yet released

- StorageParticipant :count => true uniformization (Thanks Jan Topiński)
- StorageParticipant#by_fei (alias for #[])


Expand Down
1 change: 1 addition & 0 deletions CREDITS.txt
Expand Up @@ -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
Expand Down
66 changes: 48 additions & 18 deletions lib/ruote/part/storage_participant.rb
Expand Up @@ -153,26 +153,30 @@ 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
# (only ?) workitem in the participant.
#
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
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

37 changes: 36 additions & 1 deletion test/functional/ft_20_storage_participant.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c19f90c

Please sign in to comment.