lib/ruote/storage/base.rb in ruote-2.1.10 vs lib/ruote/storage/base.rb in ruote-2.1.11

- old
+ new

@@ -93,26 +93,47 @@ } end def empty? (type) - (get_many(type) == []) + (get_many(type, nil, :count => true) == 0) end #-- # expressions #++ def find_root_expression (wfid) - get_many('expressions', /!#{wfid}$/).sort { |a, b| - a['fei']['expid'] <=> b['fei']['expid'] + get_many('expressions', wfid).sort_by { |fexp| + fexp['fei']['expid'] }.select { |e| e['parent_id'].nil? }.first end + # Given all the expressions stored here, returns a sorted list of unique + # wfids (this is used in Engine#processes(opts). + # + # Understands the :skip, :limit and :descending options. + # + # This is a base implementation, different storage implementations may + # come up with different implementations (think CouchDB, which could + # provide a view for it). + # + def expression_wfids (opts) + + wfids = ids('expressions').collect { |fei| fei.split('!').last }.uniq.sort + + wfids = wfids.reverse if opts[:descending] + + skip = opts[:skip] || 0 + limit = opts[:limit] || wfids.length + + wfids[skip, limit] + end + #-- # trackers #++ def get_trackers @@ -143,18 +164,24 @@ filter_schedules(scheds, now) #end end + # Places schedule in storage. Returns the id of the 'schedule' document. + # If the schedule got triggered immediately, nil is returned. + # def put_schedule (flavour, owner_fei, s, msg) - if doc = prepare_schedule_doc(flavour, owner_fei, s, msg) - put(doc) - return doc['_id'] - end + doc = prepare_schedule_doc(flavour, owner_fei, s, msg) - nil + return nil unless doc + + r = put(doc) + + raise "put_schedule failed" if r != nil + + doc['_id'] end def delete_schedule (schedule_id) s = get('schedules', schedule_id) @@ -192,12 +219,14 @@ %w[ configurations errors expressions msgs schedules variables workitems ].each do |type| - get_many(type).each do |item| + ids(type).each do |id| + item = get(type, id) + item.delete('_rev') target.put(item) counter += 1 puts(" #{type}/#{item['_id']}") if opts[:verbose] @@ -205,10 +234,22 @@ end counter end + # Used when doing integration tests, removes all + # msgs, schedules, errors, expressions and workitems. + # + # NOTE that it doesn't remove engine variables (danger) + # + def clear + + %w[ msgs schedules errors expressions workitems ].each do |type| + purge_type!(type) + end + end + protected # Used by put_msg # def prepare_msg_doc (action, options) @@ -270,14 +311,38 @@ 'type' => 'variables', '_id' => 'variables', 'variables' => {} } end # Returns all the ats whose due date arrived (now or earlier) # - def filter_schedules (scheds, now) + def filter_schedules (schedules, now) now = Ruote.time_to_utc_s(now) - scheds.select { |sched| sched['at'] <= now } + schedules.select { |sch| sch['at'] <= now } + end + + ## Returns true if the doc wfid is included in the wfids passed. + ## + #def wfid_match? (doc, wfids) + # wfids.find { |wfid| doc['_id'].index(wfid) } != nil + #end + + # Used by #get_many. Returns true whenever one of the keys matches the + # doc['_id']. Works with strings (_id ends with key) or regexes (_id matches + # key). + # + # It's a class method meant to be used by the various storage + # implementations. + # + def self.key_match? (keys, doc) + + _id = doc.is_a?(Hash) ? doc['_id'] : doc + + if keys.first.is_a?(String) + keys.find { |key| _id[-key.length..-1] == key } + else # Regexp + keys.find { |key| key.match(_id) } + end end end end