From bfe13fcce8eff93808e43c03d482e21728718ec9 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 25 Aug 2011 13:23:38 +0200 Subject: [PATCH] weakhash entfernt. ref-gem --- .gitignore | 1 + lib/sbdb/db.rb | 6 +++- lib/sbdb/environment.rb | 2 +- lib/sbdb/weakhash.rb | 61 ----------------------------------------- 4 files changed, 7 insertions(+), 63 deletions(-) delete mode 100644 lib/sbdb/weakhash.rb diff --git a/.gitignore b/.gitignore index 486603e..d964a1f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ pkg *.gem *.gemspec rdoc +*.rbc diff --git a/lib/sbdb/db.rb b/lib/sbdb/db.rb index 2da1a94..2a0d04a 100644 --- a/lib/sbdb/db.rb +++ b/lib/sbdb/db.rb @@ -60,7 +60,11 @@ module SBDB def _txn txn txn ||= @txn - txn && t.bdb_object + txn && txn.bdb_object + end + + def transaction flg = nil, &exe + block_given? ? home.transaction( flg, &exe) : home.transaction( flg) end # Arguments: diff --git a/lib/sbdb/environment.rb b/lib/sbdb/environment.rb index 9a6479e..0e2011b 100644 --- a/lib/sbdb/environment.rb +++ b/lib/sbdb/environment.rb @@ -61,7 +61,7 @@ module SBDB def initialize *args opts = ::Hash === args.last ? args.pop : {} opts = {:dir => args[0], :flags => args[1], :mode => args[2]}.update opts - @dbs, @env = WeakHash.new, Bdb::Env.new( 0) + @dbs, @env = Ref::WeakValueMap.new, Bdb::Env.new( 0) @env.log_config opts[:log_config], 1 if opts[:log_config] @env.lg_bsize = opts[:lg_bsize] if opts[:lg_bsize] @env.lg_max = opts[:lg_max] if opts[:lg_max] diff --git a/lib/sbdb/weakhash.rb b/lib/sbdb/weakhash.rb deleted file mode 100644 index f6e9d9c..0000000 --- a/lib/sbdb/weakhash.rb +++ /dev/null @@ -1,61 +0,0 @@ -module SBDB - -# See http://eigenclass.org/hiki/deferred-finalizers-in-Ruby -# Not threadsafe. - -class WeakHash - attr_reader :cache - def initialize cache = ::Hash.new - @cache = cache - @key_map = {} - @rev_cache = ::Hash.new{|h,k| h[k] = {}} - @reclaim_value = lambda do |value_id| - if @rev_cache.has_key? value_id - @rev_cache[value_id].each_key{|key| @cache.delete key} - @rev_cache.delete value_id - end - end - @reclaim_key = lambda do |key_id| - @cache.delete @key_map.delete(key_id) if @key_map.has_key? key_id - end - end - - def []= key, value - case key - when Fixnum, Symbol, true, false - key2 = key - else - key2 = key.dup - end - @rev_cache[value.object_id][key2] = true - @cache[key2] = value.object_id - @key_map[key.object_id] = key2 - - ObjectSpace.define_finalizer(value, @reclaim_value) - ObjectSpace.define_finalizer(key, @reclaim_key) - value - end - - def [] key - value_id = @cache[key] - return ObjectSpace._id2ref( value_id) unless value_id.nil? - nil - rescue RangeError - nil - end - - def each &e - @cache.each do |k, vid| - unless vid.nil? - obj = begin - ObjectSpace._id2ref vid - rescue RangeError - next - end - yield k, obj - end - end - end -end - -end