lib/openwfe/storage/yamlfilestorage.rb in openwferu-0.9.2 vs lib/openwfe/storage/yamlfilestorage.rb in openwferu-0.9.3

- old
+ new

@@ -38,10 +38,11 @@ # # Nicolas Modrzyk at openwfe.org # John Mettraux at openwfe.org # +require 'find' require 'yaml' require 'fileutils' require 'openwfe/utils' require 'openwfe/service' @@ -57,11 +58,12 @@ module OpenWFE # - # yaml expression storage + # Stores OpenWFEru related objects into yaml encoded files. + # This storage is meant to look and feel like a Hash. # class YamlFileStorage include ServiceMixin attr_accessor :basepath @@ -70,85 +72,121 @@ service_init(service_name, application_context) @basepath = path FileUtils.makedirs @basepath end - def []= (id, object) + # + # Stores an object with its FlowExpressionId instance as its key. + # + def []= (fei, object) - fei_path = compute_file_path(id) + fei_path = compute_file_path(fei) fei_parent_path = File.dirname(fei_path) FileUtils.makedirs(fei_parent_path) \ - if not File.exist?(fei_parent_path) + if not File.exist?(fei_parent_path) - fd = IO.sysopen(fei_path , "w+") - io = IO.open(fd , "w+") - - data = YAML.dump(object) - - io.write(data) - io.close - end + fd = IO.sysopen(fei_path , "w+") + io = IO.open(fd , "w+") - # - # deletes the whole storage directory... beware... - # - def purge - FileUtils.remove_dir @basepath - end + data = YAML.dump(object) - # - # check whether the key has a file in the file storage - def has_key? (fei) - File.exist?(compute_file_path(fei)) - end + io.write(data) + io.close + end - def remove (fei, workitem) - - fei_path = compute_file_path(fei) - - if File.exist?(fei_path) - File.delete(fei_path) - else - raise "Object not found at #{fei_path}" - end + # + # Deletes the whole storage directory... beware... + # + def purge + FileUtils.remove_dir @basepath + end + + # + # Checks whether there is an object (expression, workitem) stored + # for the given FlowExpressionId instance. + # + def has_key? (fei) + File.exist?(compute_file_path(fei)) + end + + # + # Removes the object (file) stored for the given FlowExpressionId + # instance. + # + def delete (fei) + + fei_path = compute_file_path(fei) + + if File.exist?(fei_path) + File.delete(fei_path) + else + raise "Object not found at #{fei_path}" end + end + + # + # Actually loads and returns the object for the given + # FlowExpressionId instance. + # + def [] (fei) + + fei_path = compute_file_path(fei) - def [] (fei) - fei_path = compute_file_path(fei) - - return nil if not File.exist?(fei_path) - - data = IO.read(compute_file_path(fei)) + return nil if not File.exist?(fei_path) + + load_object(fei_path) + end + + # + # Returns the count of objects currently stored in this instance. + # + def length + return count_objects(0, @basepath) + end + + alias :size :length + + protected + + def load_object (path) + + data = IO.read(path) object = YAML.load(data) - object.application_context = @application_context + object.application_context = @application_context \ + if object.respond_to? :application_context= return object end - def length - return count_expression(0, @basepath) - end - - protected - - def count_expression (count, item) + def count_objects (count, item) + + # TODO #8346 : use "find" to do that job + # measure perf before and after change ! return count + 1 if OpenWFE::ends_with(item, ".yaml") if File.stat(item).directory? d = Dir.new(item) d.each do |i| next if i == "." or i == ".." i = item + "/" + i - count = count_expression(count, i) + count = count_objects(count, i) end d.close() end return count + end + + def each_object_path (&block) + return unless block + Find.find(@basepath) do |path| + block.call path \ + unless File.stat(path).directory? + end end def compute_file_path (fei) raise "this should be implemented in a subclass" end