Class: Dnsruby::DeadlineQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsruby/deadline_queue.rb

Overview

Indexed minimum heap. Updating or cancelling a deadline removes its old entry immediately, so storage never exceeds the number of live owners. Callers provide synchronization alongside their corresponding query tables.

Instance Method Summary collapse

Constructor Details

#initialize ⇒ DeadlineQueue

:nodoc: all



6
7
8
9
# File 'lib/dnsruby/deadline_queue.rb', line 6

def initialize
  @heap = []
  @positions = {}
end

Instance Method Details

#[]=(key, deadline) ⇒ Object



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

def []=(key, deadline)
  index = @positions[key]
  if index
    @heap[index][1] = deadline
    move_down(move_up(index))
  else
    @positions[key] = @heap.length
    @heap << [key, deadline]
    move_up(@heap.length - 1)
  end
  deadline
end

#delete(key) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dnsruby/deadline_queue.rb', line 32

def delete(key)
  index = @positions.delete(key)
  return unless index
  removed = @heap[index]
  last = @heap.pop
  if index < @heap.length
    @heap[index] = last
    @positions[last[0]] = index
    move_down(move_up(index))
  end
  removed[1]
end

#first ⇒ Object



11
12
13
# File 'lib/dnsruby/deadline_queue.rb', line 11

def first
  @heap.first
end

#first_deadline ⇒ Object



15
16
17
# File 'lib/dnsruby/deadline_queue.rb', line 15

def first_deadline
  @heap.first&.last
end