sbdb 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/VERSION +1 -1
  2. data/lib/sbdb/weakhash.rb +59 -0
  3. metadata +38 -58
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.11
1
+ 0.0.12
@@ -0,0 +1,59 @@
1
+ module SBDB
2
+ # See http://eigenclass.org/hiki/deferred-finalizers-in-Ruby
3
+ # Not threadsafe.
4
+ class WeakHash
5
+ attr_reader :cache
6
+ def initialize cache = ::Hash.new
7
+ @cache = cache
8
+ @key_map = {}
9
+ @rev_cache = ::Hash.new { |h,k| h[k] = {} }
10
+ @reclaim_value = lambda do |value_id|
11
+ if @rev_cache.has_key? value_id
12
+ @rev_cache[value_id].each_key{ |key| @cache.delete key }
13
+ @rev_cache.delete value_id
14
+ end
15
+ end
16
+ @reclaim_key = lambda do |key_id|
17
+ @cache.delete @key_map.delete(key_id) if @key_map.has_key? key_id
18
+ end
19
+ end
20
+
21
+ def []=(key, value)
22
+ case key
23
+ when Fixnum, Symbol, true, false
24
+ key2 = key
25
+ else
26
+ key2 = key.dup
27
+ end
28
+ @rev_cache[value.object_id][key2] = true
29
+ @cache[key2] = value.object_id
30
+ @key_map[key.object_id] = key2
31
+
32
+ ObjectSpace.define_finalizer(value, @reclaim_value)
33
+ ObjectSpace.define_finalizer(key, @reclaim_key)
34
+ value
35
+ end
36
+
37
+ def [](key)
38
+ value_id = @cache[key]
39
+ return ObjectSpace._id2ref(value_id) unless value_id.nil?
40
+ nil
41
+ rescue RangeError
42
+ nil
43
+ end
44
+
45
+ def each(&e)
46
+ @cache.each do |k, vid|
47
+ unless vid.nil?
48
+ obj =
49
+ begin
50
+ ObjectSpace._id2ref vid
51
+ rescue RangeError
52
+ next
53
+ end
54
+ yield k, obj
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
metadata CHANGED
@@ -1,58 +1,46 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sbdb
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 11
9
- version: 0.0.11
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.12
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Denis Knauf
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-08-25 00:00:00 +02:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-08-25 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: bdb
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70289576905920 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.2.6.4
31
22
  type: :runtime
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: ref
35
23
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70289576905920
25
+ - !ruby/object:Gem::Dependency
26
+ name: ref
27
+ requirement: &70289576905240 !ruby/object:Gem::Requirement
37
28
  none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
44
33
  type: :runtime
45
- version_requirements: *id002
34
+ prerelease: false
35
+ version_requirements: *70289576905240
46
36
  description: Simple Ruby Berkeley DB wrapper library for bdb.
47
37
  email: Denis.Knauf@gmail.com
48
38
  executables: []
49
-
50
39
  extensions: []
51
-
52
- extra_rdoc_files:
40
+ extra_rdoc_files:
53
41
  - LICENSE
54
42
  - README.md
55
- files:
43
+ files:
56
44
  - AUTHORS
57
45
  - README.md
58
46
  - VERSION
@@ -61,38 +49,30 @@ files:
61
49
  - lib/sbdb/db.rb
62
50
  - lib/sbdb/environment.rb
63
51
  - lib/sbdb/transaction.rb
52
+ - lib/sbdb/weakhash.rb
64
53
  - LICENSE
65
- has_rdoc: true
66
54
  homepage: http://github.com/ruby-bdb/sbdb
67
55
  licenses: []
68
-
69
56
  post_install_message:
70
57
  rdoc_options: []
71
-
72
- require_paths:
58
+ require_paths:
73
59
  - lib
74
- required_ruby_version: !ruby/object:Gem::Requirement
60
+ required_ruby_version: !ruby/object:Gem::Requirement
75
61
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- segments:
80
- - 0
81
- version: "0"
82
- required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
67
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- segments:
88
- - 0
89
- version: "0"
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
90
72
  requirements: []
91
-
92
73
  rubyforge_project:
93
- rubygems_version: 1.3.7
74
+ rubygems_version: 1.8.9
94
75
  signing_key:
95
76
  specification_version: 3
96
77
  summary: Simple Ruby Berkeley DB
97
78
  test_files: []
98
-