lib/ruote/storage/hash_storage.rb in ruote-2.2.0 vs lib/ruote/storage/hash_storage.rb in ruote-2.3.0

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2005-2012, John Mettraux, jmettraux@gmail.com # # 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 @@ -28,12 +28,15 @@ require 'monitor' module Ruote + # # An in-memory storage. # + # Useful for testing or for transient engines. + # class HashStorage include StorageBase include MonitorMixin @@ -45,18 +48,17 @@ # since were including MonitorMixin, this super() is necessary @options = options purge! + # which initializes @h - put(options.merge('type' => 'configurations', '_id' => 'engine')) + replace_engine_configuration(options) end def put(doc, opts={}) - i = @h.size - synchronize do pre = get(doc['type'], doc['_id']) if pre && pre['_rev'] != doc['_rev'] @@ -66,12 +68,11 @@ if pre.nil? && doc['_rev'] return true end doc = if opts[:update_rev] - doc['_rev'] = pre ? pre['_rev'] : -1 - doc + doc.merge!('_rev' => pre ? pre['_rev'] : -1) else doc.merge('_rev' => doc['_rev'] || -1) end doc['put_at'] = Ruote.now_to_utc_s @@ -80,16 +81,16 @@ @h[doc['type']][doc['_id']] = Rufus::Json.dup(doc) nil end - rescue => e - puts "=" * 80 - File.open('doc.json', 'wb') do |f| - f.puts Rufus::Json.pretty_encode(doc) - end - raise e + #rescue => e + # puts "=" * 80 + # File.open('doc.json', 'wb') do |f| + # f.puts Rufus::Json.pretty_encode(doc) + # end + # raise e end def get(type, key) synchronize do @@ -123,23 +124,20 @@ # NOTE : no dup here for now synchronize do - keys = key ? - Array(key).map { |k| k.is_a?(String) ? "!#{k}" : k } : nil - - docs = keys ? - @h[type].values.select { |doc| - Ruote::StorageBase.key_match?(keys, doc) - } : + docs = if key + keys = Array(key).map { |k| k.is_a?(String) ? "!#{k}" : k } + @h[type].values.select { |doc| key_match?(type, keys, doc) } + else @h[type].values + end - docs = docs.sort_by { |d| d['_id'] } - return docs.size if opts[:count] + docs = docs.sort_by { |d| d['_id'] } docs = docs.reverse if opts[:descending] skip = opts[:skip] || 0 limit = opts[:limit] || docs.size @@ -152,10 +150,28 @@ def ids(type) @h[type].keys.sort end + #-- + # keeping it commented out... using it for documentation efforts + #class NoisyHash < Hash + # def initialize(type) + # @type = type + # super() + # end + # def []=(k, v) + # puts " + #{@type}.put #{k} #{v['_rev']}" + # super + # end + # def delete(k) + # puts " - #{@type}.del #{k} " + # super + # end + #end + #++ + # Purges the storage completely. # def purge! @h = %w[ @@ -167,13 +183,12 @@ errors schedules configurations workitems - ].inject({}) { |h, k| + ].each_with_object({}) { |k, h| h[k] = {} - h } @h['configurations']['engine'] = @options end @@ -183,22 +198,9 @@ end def purge_type!(type) @h[type] = {} - end - - def dump(type) - - s = "=== #{type} ===\n" - - @h[type].inject(s) do |s1, (k, v)| - s1 << "\n" - s1 << "#{k} :\n" - v.keys.sort.inject(s1) do |s2, k1| - s2 << " #{k1} => #{v[k1].inspect}\n" - end - end end # Shuts this storage down. # def shutdown