@ -7,10 +7,10 @@ import (
type QueuePath [ ] string //nolint:revive
// Leaf Queue is an hierarchical queue implementation where each sub-queue
// Tree Queue is an hierarchical queue implementation where each sub-queue
// has the same guarantees to be chosen from.
// Each queue has also a local queue, which gets chosen with equal preference as the sub-queues.
type Leaf Queue struct {
type Tree Queue struct {
// local queue
ch RequestChannel
// index of where this item is located in the mapping
@ -18,18 +18,18 @@ type LeafQueue struct {
// index of the sub-queues
current QueueIndex
// mapping for sub-queues
mapping * Mapping [ * Leaf Queue]
mapping * Mapping [ * Tree Queue]
// name of the queue
name string
// maximum queue size of the local queue
size int
}
// newLeafQueue creates a new Leaf Queue instance
func newLeaf Queue ( size int , name string ) * Leaf Queue {
m := & Mapping [ * Leaf Queue] { }
// newTreeQueue creates a new Tree Queue instance
func newTree Queue ( size int , name string ) * Tree Queue {
m := & Mapping [ * Tree Queue] { }
m . Init ( 64 ) // TODO(chaudum): What is a good initial value?
return & Leaf Queue{
return & Tree Queue{
ch : make ( RequestChannel , size ) ,
pos : StartIndexWithLocalQueue ,
current : StartIndexWithLocalQueue ,
@ -40,7 +40,7 @@ func newLeafQueue(size int, name string) *LeafQueue {
}
// add recursively adds queues based on given path
func ( q * Leaf Queue) add ( path QueuePath ) * Leaf Queue {
func ( q * Tree Queue) add ( path QueuePath ) * Tree Queue {
if len ( path ) == 0 {
return q
}
@ -52,22 +52,22 @@ func (q *LeafQueue) add(path QueuePath) *LeafQueue {
return queue . add ( remaining )
}
func ( q * Leaf Queue) getOrCreate ( name string ) ( subq * Leaf Queue, created bool ) {
func ( q * Tree Queue) getOrCreate ( name string ) ( subq * Tree Queue, created bool ) {
subq = q . mapping . GetByKey ( name )
if subq == nil {
subq = newLeaf Queue ( q . size , name )
subq = newTree Queue ( q . size , name )
created = true
}
return subq , created
}
// Chan implements Queue
func ( q * Leaf Queue) Chan ( ) RequestChannel {
func ( q * Tree Queue) Chan ( ) RequestChannel {
return q . ch
}
// Dequeue implements Queue
func ( q * Leaf Queue) Dequeue ( ) Request {
func ( q * Tree Queue) Dequeue ( ) Request {
var item Request
// shortcut of there are not sub-queues
@ -111,14 +111,14 @@ func (q *LeafQueue) Dequeue() Request {
}
// Name implements Queue
func ( q * Leaf Queue) Name ( ) string {
func ( q * Tree Queue) Name ( ) string {
return q . name
}
// Len implements Queue
// It returns the length of the local queue and all sub-queues.
// This may be expensive depending on the size of the queue tree.
func ( q * Leaf Queue) Len ( ) int {
func ( q * Tree Queue) Len ( ) int {
count := len ( q . ch )
for _ , subq := range q . mapping . Values ( ) {
count += subq . Len ( )
@ -127,20 +127,20 @@ func (q *LeafQueue) Len() int {
}
// Index implements Mapable
func ( q * Leaf Queue) Pos ( ) QueueIndex {
func ( q * Tree Queue) Pos ( ) QueueIndex {
return q . pos
}
// Index implements Mapable
func ( q * Leaf Queue) SetPos ( index QueueIndex ) {
func ( q * Tree Queue) SetPos ( index QueueIndex ) {
q . pos = index
}
// String makes the queue printable
func ( q * Leaf Queue) String ( ) string {
func ( q * Tree Queue) String ( ) string {
sb := & strings . Builder { }
sb . WriteString ( "{" )
fmt . Fprintf ( sb , "name=%s, len=%d/%d, leafs =[" , q . Name ( ) , q . Len ( ) , cap ( q . ch ) )
fmt . Fprintf ( sb , "name=%s, len=%d/%d, children =[" , q . Name ( ) , q . Len ( ) , cap ( q . ch ) )
subqs := q . mapping . Values ( )
for i , m := range subqs {
sb . WriteString ( m . String ( ) )