Class: SizedThreadSafeHash

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(size, &callback) ⇒ SizedThreadSafeHash

Returns a new instance of SizedThreadSafeHash.



19
20
21
22
23
24
25
26
# File 'lib/mqrpc/sizedhash.rb', line 19

def initialize(size, &callback)
  @lock = TrackingMutex.new
  @size = size
  @condvar = ConditionVariable.new
  @data = Hash.new
  @callback = callback
  @state = nil
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



16
17
18
# File 'lib/mqrpc/sizedhash.rb', line 16

def callback
  @callback
end

#sizeObject (readonly) Also known as: length

return the size (total number of entries) in this hash.



91
92
93
# File 'lib/mqrpc/sizedhash.rb', line 91

def size
  @size
end

Instance Method Details

#[](key) ⇒ Object

get an value by key



56
57
58
59
60
# File 'lib/mqrpc/sizedhash.rb', line 56

def [](key)
  @lock.synchronize do
    return @data[key]
  end
end

#[]=(key, value) ⇒ Object

set a key and value



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mqrpc/sizedhash.rb', line 29

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 (thraed #{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



72
73
74
75
76
77
78
79
80
81
# File 'lib/mqrpc/sizedhash.rb', line 72

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)

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/mqrpc/sizedhash.rb', line 84

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?

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/mqrpc/sizedhash.rb', line 63

def has_key?(key)
  @lock.synchronize do
    return @data.has_key?(key)
  end
end

#keysObject

Return an array of keys for this hash.



98
99
100
101
102
# File 'lib/mqrpc/sizedhash.rb', line 98

def keys
  @lock.synchronize do
    return @data.keys
  end
end