Class: SizedThreadSafeHash
- Inherits:
-
Object
- Object
- SizedThreadSafeHash
- Defined in:
- lib/mqrpc/sizedhash.rb
Overview
Thread-safe sized hash similar to SizedQueue. The only time we will block execution is in setting new items. That is, SizedThreadSafeHash#[]=
Instance Attribute Summary collapse
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#size ⇒ Object
(also: #length)
readonly
return the size (total number of entries) in this hash.
Instance Method Summary collapse
-
#[](key) ⇒ Object
get an value by key.
-
#[]=(key, value) ⇒ Object
set a key and value.
-
#delete(key) ⇒ Object
delete a key.
-
#full? ⇒ Boolean
boolean, indicates true when the hash is full (has size == initialized size).
-
#has_key?(key) ⇒ Boolean
(also: #include?)
boolean, does the hash have a given key?.
-
#initialize(size, &callback) ⇒ SizedThreadSafeHash
constructor
A new instance of SizedThreadSafeHash.
-
#keys ⇒ Object
Return an array of keys for this hash.
Constructor Details
#initialize(size, &callback) ⇒ SizedThreadSafeHash
Returns a new instance of SizedThreadSafeHash.
20 21 22 23 24 25 26 27 |
# File 'lib/mqrpc/sizedhash.rb', line 20 def initialize(size, &callback) @lock = Mutex.new @size = size @condvar = ConditionVariable.new @data = Hash.new @callback = callback @state = nil end |
Instance Attribute Details
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
17 18 19 |
# File 'lib/mqrpc/sizedhash.rb', line 17 def callback @callback end |
#size ⇒ Object (readonly) Also known as: length
return the size (total number of entries) in this hash.
92 93 94 |
# File 'lib/mqrpc/sizedhash.rb', line 92 def size @size end |
Instance Method Details
#[](key) ⇒ Object
get an value by key
57 58 59 60 61 |
# File 'lib/mqrpc/sizedhash.rb', line 57 def [](key) @lock.synchronize do return @data[key] end end |
#[]=(key, value) ⇒ Object
set a key and value
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mqrpc/sizedhash.rb', line 30 def []=(key, value) @lock.synchronize do # If adding a new item, wait if the hash is full if !@data.has_key?(key) and _withlock_full? MQRPC::logger.info "#{self}: Waiting to add key #{key.inspect}, hash is full (thread #{Thread.current})" if @state != :blocked @state = :blocked @callback.call(@state) if @callback else puts "State is already #{@state} (want :blocked), skipping event call" end @condvar.wait(@lock) if @state != :ready @state = :ready MQRPC::logger.info "#{self}: state => :ready" @callback.call(@state) if @callback else puts "State is already #{@state} (want :ready), skipping event call" end end @data[key] = value end end |
#delete(key) ⇒ Object
delete a key
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mqrpc/sizedhash.rb', line 73 def delete(key) @lock.synchronize do was_full = _withlock_full? @data.delete(key) if was_full MQRPC::logger.info "#{self}: signalling non-fullness" @condvar.signal end end end |
#full? ⇒ Boolean
boolean, indicates true when the hash is full (has size == initialized size)
85 86 87 88 89 |
# File 'lib/mqrpc/sizedhash.rb', line 85 def full? @lock.synchronize do return _withlock_full? end end |
#has_key?(key) ⇒ Boolean Also known as: include?
boolean, does the hash have a given key?
64 65 66 67 68 |
# File 'lib/mqrpc/sizedhash.rb', line 64 def has_key?(key) @lock.synchronize do return @data.has_key?(key) end end |
#keys ⇒ Object
Return an array of keys for this hash.
99 100 101 102 103 |
# File 'lib/mqrpc/sizedhash.rb', line 99 def keys @lock.synchronize do return @data.keys end end |