Class: HashQueue::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_queue/hash.rb

Instance Method Summary collapse

Constructor Details

#initializeHash

Returns a new instance of Hash.



4
5
6
7
# File 'lib/hash_queue/hash.rb', line 4

def initialize
  @hash_queue = {}
  @mutex = Mutex.new
end

Instance Method Details

#[](key) ⇒ Object



9
10
11
# File 'lib/hash_queue/hash.rb', line 9

def [](key)
  QueueProxy.new(self, key)
end

#cleanObject



53
54
55
56
57
58
59
# File 'lib/hash_queue/hash.rb', line 53

def clean
  @mutex.synchronize do 
    @hash_queue.reject! do |key, queue|
      queue.empty?
    end
  end
end

#clearObject



61
62
63
64
65
# File 'lib/hash_queue/hash.rb', line 61

def clear
  @mutex.synchronize do 
    @hash_queue.clear
  end
end

#empty?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/hash_queue/hash.rb', line 43

def empty?
  size.zero?
end

#get_queue(key) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/hash_queue/hash.rb', line 67

def get_queue(key)
  @mutex.synchronize do 
    @hash_queue.fetch(key) do
      @hash_queue[key] = Queue.new
    end
  end
end

#keysObject



47
48
49
50
51
# File 'lib/hash_queue/hash.rb', line 47

def keys
  @mutex.synchronize do 
    @hash_queue.keys
  end
end

#pop(options = {}) ⇒ Object Also known as: shift



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hash_queue/hash.rb', line 19

def pop(options = {})
  loop do
    results = pop_from_queues(options)
    
    if options[:blocking] 
      return results unless results.empty?
      sleep 0.01
    else
      return results
    end
  end
end

#queue(key, obj) ⇒ Object Also known as: enqueue, push



13
14
15
# File 'lib/hash_queue/hash.rb', line 13

def queue(key, obj)
  get_queue(key).queue obj      
end

#sizeObject Also known as: count, length



33
34
35
36
37
38
39
# File 'lib/hash_queue/hash.rb', line 33

def size
  @mutex.synchronize do
    @hash_queue.each_value.inject(0) do |sum, queue|
      sum += queue.size
    end 
  end
end