Class: N::SafeHashDelegator
- Inherits:
-
Hash
- Object
- Hash
- N::SafeHashDelegator
- Defined in:
- lib/n/utils/hash.rb
Overview
SafeHashDelegator
A thread-safe hash. We use a sync object instead of a mutex, because it is re-entrant. An exclusive lock is needed when writing, a shared lock IS NEEDED when reading
Design: This class uses the delegator pattern. However we dont use rubys delegation facilities, they are more general and powerfull than we need here (and slower). Instead a custom (but simple) solution is used.
Example:
hash = SafeHashDelegator.new(Hash.new) hash = SafeHashDelegator.new(Hash.new)
Instance Attribute Summary collapse
-
#delegate ⇒ Object
readonly
Returns the value of attribute delegate.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(delegate) ⇒ SafeHashDelegator
constructor
A new instance of SafeHashDelegator.
- #keys ⇒ Object
- #size ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(delegate) ⇒ SafeHashDelegator
Returns a new instance of SafeHashDelegator.
99 100 101 102 |
# File 'lib/n/utils/hash.rb', line 99 def initialize(delegate) @delegate = delegate @sync = ::Sync.new end |
Instance Attribute Details
#delegate ⇒ Object (readonly)
Returns the value of attribute delegate.
97 98 99 |
# File 'lib/n/utils/hash.rb', line 97 def delegate @delegate end |
Instance Method Details
#[](key) ⇒ Object
104 105 106 107 108 |
# File 'lib/n/utils/hash.rb', line 104 def [](key) return @sync.synchronize(::Sync::SH) { @delegate[key] } end |
#[]=(key, value) ⇒ Object
110 111 112 113 114 |
# File 'lib/n/utils/hash.rb', line 110 def []=(key, value) return @sync.synchronize(::Sync::EX) { @delegate[key] = value } end |
#clear ⇒ Object
122 123 124 125 126 |
# File 'lib/n/utils/hash.rb', line 122 def clear @sync.synchronize(::Sync::EX) { @delegate.clear } end |
#delete(key) ⇒ Object
116 117 118 119 120 |
# File 'lib/n/utils/hash.rb', line 116 def delete(key) return @sync.synchronize(::Sync::EX) { @delegate.delete(key) } end |
#keys ⇒ Object
140 141 142 143 144 |
# File 'lib/n/utils/hash.rb', line 140 def keys return @sync.synchronize(::Sync::SH) { @delegate.keys() } end |
#size ⇒ Object
128 129 130 131 132 |
# File 'lib/n/utils/hash.rb', line 128 def size return @sync.synchronize(::Sync::SH) { @delegate.size() } end |
#values ⇒ Object
134 135 136 137 138 |
# File 'lib/n/utils/hash.rb', line 134 def values return @sync.synchronize(::Sync::SH) { @delegate.values() } end |