Class: OllamaChat::Utils::OrderedQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_chat/utils/ordered_queue.rb

Overview

A thread-safe priority queue backed by a Min-Heap.

This class ensures that items are retrieved in the order of their IDs, which can be tuples like [thread_id, chunk_id].

Instance Method Summary collapse

Constructor Details

#initializeOrderedQueue

Initializes a new OrderedQueue.



7
8
9
10
11
# File 'lib/ollama_chat/utils/ordered_queue.rb', line 7

def initialize
  @heap = []
  @mutex = Mutex.new
  @cv = ConditionVariable.new
end

Instance Method Details

#empty?Boolean

Checks if the queue is empty.

Returns:

  • (Boolean)

    true if empty



43
44
45
# File 'lib/ollama_chat/utils/ordered_queue.rb', line 43

def empty?
  @mutex.synchronize { @heap.empty? }
end

#peekArray?

Peeks at the item with the lowest ID without removing it.

Returns:

  • (Array, nil)

    the [id, payload] pair or nil if empty



29
30
31
# File 'lib/ollama_chat/utils/ordered_queue.rb', line 29

def peek
  @mutex.synchronize { @heap[0] }
end

#popArray?

Removes and returns the item with the lowest ID.

Returns:

  • (Array, nil)

    the [id, payload] pair or nil if empty



36
37
38
# File 'lib/ollama_chat/utils/ordered_queue.rb', line 36

def pop
  @mutex.synchronize { extract_min }
end

#push(id, payload) ⇒ Object

Pushes an item into the queue.

Parameters:

  • id (Array<Integer>, Integer)

    the ID for ordering (e.g., [thread_id, chunk_id])

  • payload (Object)

    the data associated with the ID



17
18
19
20
21
22
23
24
# File 'lib/ollama_chat/utils/ordered_queue.rb', line 17

def push(id, payload)
  @mutex.synchronize do
    id.extend(Comparable) rescue nil
    @heap << [id, payload]
    heapify_up(@heap.size - 1)
    @cv.signal
  end
end

#waitObject

Waits for a signal from a producer thread.



48
49
50
# File 'lib/ollama_chat/utils/ordered_queue.rb', line 48

def wait
  @mutex.synchronize { @cv.wait(@mutex) }
end