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.



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

#callbackObject (readonly)

Returns the value of attribute callback.



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

def callback
  @callback
end

#sizeObject (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)

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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

#keysObject

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