lib/openwfe/storage/yamlfilestorage.rb in openwferu-0.9.4 vs lib/openwfe/storage/yamlfilestorage.rb in openwferu-0.9.5
- old
+ new
@@ -40,10 +40,11 @@
# John Mettraux at openwfe.org
#
require 'find'
require 'yaml'
+require 'monitor'
require 'fileutils'
require 'openwfe/utils'
require 'openwfe/service'
@@ -56,51 +57,61 @@
# a definition of the class...)
module OpenWFE
-
#
# Stores OpenWFEru related objects into yaml encoded files.
# This storage is meant to look and feel like a Hash.
#
class YamlFileStorage
- include ServiceMixin
+ include MonitorMixin, ServiceMixin
attr_accessor :basepath
def initialize (service_name, application_context, path)
+
+ super()
+
service_init(service_name, application_context)
+
@basepath = path
+ #@basepath = OpenWFE::clean_path(@basepath)
+ @basepath += "/" unless @basepath[-1, 1] == "/"
+
FileUtils.makedirs @basepath
end
#
# Stores an object with its FlowExpressionId instance as its key.
#
def []= (fei, object)
-
- 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)
+ synchronize do
+ fei_path = compute_file_path(fei)
+
+ fei_parent_path = File.dirname(fei_path)
- fd = IO.sysopen(fei_path , "w+")
- io = IO.open(fd , "w+")
-
- data = YAML.dump(object)
-
- io.write(data)
- io.close
+ FileUtils.makedirs(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
end
#
# Deletes the whole storage directory... beware...
#
def purge
- FileUtils.remove_dir @basepath
+ synchronize do
+ FileUtils.remove_dir @basepath
+ end
end
#
# Checks whether there is an object (expression, workitem) stored
# for the given FlowExpressionId instance.
@@ -112,17 +123,19 @@
#
# Removes the object (file) stored for the given FlowExpressionId
# instance.
#
def delete (fei)
+ synchronize do
- fei_path = compute_file_path(fei)
-
- if File.exist?(fei_path)
+ fei_path = compute_file_path(fei)
+
+ #ldebug do
+ # "delete()\n for #{fei.to_debug_s}\n at #{fei_path}"
+ #end
+
File.delete(fei_path)
- else
- raise "Object not found at #{fei_path}"
end
end
#
# Actually loads and returns the object for the given
@@ -139,11 +152,12 @@
#
# Returns the count of objects currently stored in this instance.
#
def length
- return count_objects(0, @basepath)
+ #return count_objects(0, @basepath)
+ count_objects()
end
alias :size :length
protected
@@ -157,41 +171,54 @@
if object.respond_to? :application_context=
return object
end
- 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_objects(count, i)
- end
- d.close()
+ def count_objects
+ count = 0
+ Find.find(@basepath) do |path|
+ next if File.stat(path).directory?
+ count += 1 if OpenWFE::ends_with(path, ".yaml")
end
-
return count
end
+ #
+ # Passes each object path to the given block
+ #
def each_object_path (&block)
return unless block
- Find.find(@basepath) do |path|
- block.call path \
- unless File.stat(path).directory?
+ synchronize do
+ Find.find(@basepath) do |path|
+ next if File.stat(path).directory?
+ next unless OpenWFE::ends_with(path, ".yaml")
+ ldebug { "each_object_path() considering #{path}" }
+ block.call path
+ end
end
end
-
- def compute_file_path (fei)
- raise NotImplementedError.new
+
+ #
+ # Passes each object to the given block
+ #
+ def each_object (&block)
+ return unless block
+ each_object_path do |path|
+ block.call load_object(path)
+ end
end
+
+ #
+ # each_value() is a method from Hash, by providing it here
+ # it's easier to disguise a YamlFileStorage as a hash.
+ #
+ alias :each_value :each_object
+
+ protected
+
+ def compute_file_path (object)
+ raise NotImplementedError.new
+ end
end
end