lib/openwfe/expool/expstorage.rb in ruote-0.9.19 vs lib/openwfe/expool/expstorage.rb in ruote-0.9.20

- old
+ new

@@ -1,47 +1,30 @@ -# #-- -# Copyright (c) 2006-2008, Nicolas Modryzk and John Mettraux, OpenWFE.org -# All rights reserved. +# Copyright (c) 2006-2009, Nicolas Modryzk and John Mettraux, OpenWFE.org # -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: # -# . Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. # -# . Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. # -# . Neither the name of the "OpenWFE" nor the names of its contributors may be -# used to endorse or promote products derived from this software without -# specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE -# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. +# Made in Japan. #++ -# -# -# "made in Japan" -# -# Nicolas Modrzyk at openwfe.org -# John Mettraux at openwfe.org -# -require 'fileutils' - require 'rufus/lru' require 'openwfe/service' require 'openwfe/flowexpressionid' require 'openwfe/rudefinitions' @@ -57,17 +40,17 @@ # module ExpressionStorageBase def observe_expool + return unless get_expression_pool + get_expression_pool.add_observer(:update) do |channel, fei, fe| - ldebug { ":update for #{fei}" } self[fei] = fe end get_expression_pool.add_observer(:remove) do |channel, fei| - ldebug { ":remove for #{fei}" } - self.delete fei + self.delete(fei) end end # # a human readable representation of the content of the expression @@ -136,11 +119,11 @@ if wfname and fei.wfname != wfname return false \ if wfrevision and fei.wfrevision != wfrevision return false \ - if cs and fei.sub_instance_id != "" + if cs and fei.sub_instance_id != '' return false \ if wfid and fei.wfid != wfid return false \ if wfid_prefix and not fei.wfid.match("^#{wfid_prefix}") return false \ @@ -200,21 +183,19 @@ linfo { "new() size is #{size}" } @cache = LruHash.new(size) - @real_storage = nil - observe_expool end def [] (fei) #ldebug { "[] size is #{@cache.size}" } #ldebug { "[] (sz #{@cache.size}) for #{fei.to_debug_s}" } - fe = @cache[fei.hash] + fe = @cache[fei.short_hash] return fe if fe #ldebug { "[] (reload) for #{fei.to_debug_s}" } fe = get_real_storage[fei] @@ -222,27 +203,27 @@ unless fe #ldebug { "[] (reload) miss for #{fei.to_debug_s}" } return nil end - @cache[fei.hash] = fe - - fe + @cache[fei.short_hash] = fe end def []= (fei, fe) - ldebug { "[]= caching #{fei}" } - - @cache[fei.hash] = fe + #ldebug { "[]= caching #{fei}" } + @cache[fei.short_hash] = fe end def delete (fei) - @cache.delete fei.hash + @cache.delete(fei.short_hash) end + # + # returns the count of expressions currently cached here + # def length @cache.length end @@ -259,61 +240,80 @@ # This implementations of find_expressions() immediately passes # the call to the underlying real storage. # def find_expressions (options={}) - get_real_storage.find_expressions options + options[:cache] = self + + get_real_storage.find_expressions(options) end # # Attempts at fetching the root expression of a given process # instance. # def fetch_root (wfid) - # - # at first, look in the cache + @cache.values.find { |fexp| + fexp.fei.wfid == wfid and fexp.is_a?(DefineExpression) + } || get_real_storage.fetch_root(wfid) + end - @cache.each do |hashed_fei, fexp| + # + # Returns the expression corresponding to the fei if cached. + # Does not lookup in the underlying "real" storage (will therefore + # return nil if the expression is not cached. + # + def fetch (fei) - return fexp \ - if fexp.fei.wfid == wfid and fexp.is_a?(DefineExpression) - end - - get_real_storage.fetch_root wfid + @cache[fei.short_hash] end protected - # - # Returns the "real storage" i.e. the storage that does the real - # persistence behind this "cache storage". - # - def get_real_storage + # + # Returns the "real storage" i.e. the storage that does the real + # persistence behind this "cache storage". + # + def get_real_storage - @application_context[:s_expression_storage__1] - end + @application_context[:s_expression_storage__1] + end end # - # Memory consuming in-memory storage. + # [memory consuming] in-memory storage. # No memory limit, puts everything in a Hash # + # USE ONLY FOR TESTS + # class InMemoryExpressionStorage < Hash include ServiceMixin include OwfeServiceLocator include ExpressionStorageBase def initialize (service_name, application_context) - service_init service_name, application_context + service_init(service_name, application_context) observe_expool end alias :purge :clear + #-- + #def [] (k) + # super(k.short_hash) + #end + #def []= (k, v) + # super(k.short_hash, v) + #end + #def delete (k) + # super(k.short_hash) + #end + #++ + # # Finds expressions matching the given criteria (returns a list # of expressions). # # This methods is called by the expression pool, it's thus not @@ -351,23 +351,19 @@ # if this option is set to true, will only return the expressions # that have been applied (exp.apply_time != nil). # def find_expressions (options={}) - values.find_all do |fexp| - does_match? options, fexp - end + values.find_all { |fexp| does_match?(options, fexp) } end # # Attempts at fetching the root expression of a given process # instance. # def fetch_root (wfid) - find_expressions( - :wfid => wfid, - :include_classes => DefineExpression)[0] + find_expressions(:wfid => wfid, :include_classes => DefineExpression)[0] end end end