Class: OllamaChat::Utils::OrderedQueue
- Inherits:
-
Object
- Object
- OllamaChat::Utils::OrderedQueue
- 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
-
#empty? ⇒ Boolean
Checks if the queue is empty.
-
#initialize ⇒ OrderedQueue
constructor
Initializes a new OrderedQueue.
-
#peek ⇒ Array?
Peeks at the item with the lowest ID without removing it.
-
#pop ⇒ Array?
Removes and returns the item with the lowest ID.
-
#push(id, payload) ⇒ Object
Pushes an item into the queue.
-
#wait ⇒ Object
Waits for a signal from a producer thread.
Constructor Details
#initialize ⇒ OrderedQueue
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.
43 44 45 |
# File 'lib/ollama_chat/utils/ordered_queue.rb', line 43 def empty? @mutex.synchronize { @heap.empty? } end |
#peek ⇒ Array?
Peeks at the item with the lowest ID without removing it.
29 30 31 |
# File 'lib/ollama_chat/utils/ordered_queue.rb', line 29 def peek @mutex.synchronize { @heap[0] } end |
#pop ⇒ Array?
Removes and returns the item with the lowest ID.
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.
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 |
#wait ⇒ Object
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 |