Class: Shoryuken::Helpers::AtomicHash
- Inherits:
-
Object
- Object
- Shoryuken::Helpers::AtomicHash
- Defined in:
- lib/shoryuken/helpers/atomic_hash.rb
Overview
This implementation uses mutex synchronization for all operations, ensuring complete thread safety with minimal performance impact.
All operations are atomic and will never see partial effects from concurrent operations.
A thread-safe hash implementation using Ruby’s Mutex for all operations.
This class provides a hash-like interface with thread-safe operations, serving as a drop-in replacement for Concurrent::Hash without requiring external dependencies. The implementation uses a single mutex to protect both read and write operations, ensuring complete thread safety across all Ruby implementations including JRuby.
Since hash operations (lookup, assignment) are very fast, the mutex overhead is minimal while providing guaranteed safety and simplicity. This approach avoids the complexity of copy-on-write while maintaining excellent performance for typical usage patterns.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Returns the value associated with the given key.
-
#[]=(key, value) ⇒ Object
Sets the value for the given key.
-
#clear ⇒ Hash
Removes all key-value pairs from the hash.
-
#fetch(key, default = nil) ⇒ Object
Returns the value for the given key, or a default value if the key is not found.
-
#initialize ⇒ AtomicHash
constructor
Creates a new empty atomic hash.
-
#keys ⇒ Array
Returns an array of all keys in the hash.
Constructor Details
#initialize ⇒ AtomicHash
Creates a new empty atomic hash.
The hash starts empty and ready to accept key-value pairs through thread-safe operations.
63 64 65 66 |
# File 'lib/shoryuken/helpers/atomic_hash.rb', line 63 def initialize @mutex = Mutex.new @hash = {} end |
Instance Method Details
#[](key) ⇒ Object?
Returns the value associated with the given key.
This operation is thread-safe and will return a consistent value even when called concurrently with write operations.
88 89 90 |
# File 'lib/shoryuken/helpers/atomic_hash.rb', line 88 def [](key) @mutex.synchronize { @hash[key] } end |
#[]=(key, value) ⇒ Object
Sets the value for the given key.
This is a thread-safe write operation that ensures data integrity when called concurrently with other read or write operations.
112 113 114 |
# File 'lib/shoryuken/helpers/atomic_hash.rb', line 112 def []=(key, value) @mutex.synchronize { @hash[key] = value } end |
#clear ⇒ Hash
Removes all key-value pairs from the hash.
This is a thread-safe write operation that ensures atomicity when called concurrently with other operations.
131 132 133 |
# File 'lib/shoryuken/helpers/atomic_hash.rb', line 131 def clear @mutex.synchronize { @hash.clear } end |
#fetch(key, default = nil) ⇒ Object
Returns the value for the given key, or a default value if the key is not found.
This operation is thread-safe and will return a consistent value even when called concurrently with write operations.
177 178 179 |
# File 'lib/shoryuken/helpers/atomic_hash.rb', line 177 def fetch(key, default = nil) @mutex.synchronize { @hash.fetch(key, default) } end |
#keys ⇒ Array
Returns an array of all keys in the hash.
This operation is thread-safe and will return a consistent snapshot of keys even when called concurrently with write operations.
151 152 153 |
# File 'lib/shoryuken/helpers/atomic_hash.rb', line 151 def keys @mutex.synchronize { @hash.keys } end |