github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/lib/queue/queue_intf.go (about)

     1  // Reference:
     2  // https://github.com/nsqio/nsq/blob/master/internal/pqueue/pqueue.go
     3  
     4  package queue
     5  
     6  import (
     7  	"github.com/benz9527/xboot/lib/infra"
     8  )
     9  
    10  type PriorityQueue[E comparable] interface {
    11  	Len() int64
    12  	Push(item PQItem[E])
    13  	Pop() ReadOnlyPQItem[E]
    14  	Peek() ReadOnlyPQItem[E]
    15  }
    16  
    17  type ReadOnlyPQItem[E comparable] interface {
    18  	Index() int64
    19  	Value() E
    20  	Priority() int64
    21  }
    22  
    23  type CmpEnum int64
    24  
    25  const (
    26  	iLTj CmpEnum = -1 + iota
    27  	iEQj
    28  	iGTj
    29  )
    30  
    31  // PQItemLessThenComparator
    32  // Priority queue item comparator
    33  // if return 1, i > j
    34  // if return 0, i == j
    35  // if return -1, i < j
    36  type PQItemLessThenComparator[E comparable] func(i, j ReadOnlyPQItem[E]) CmpEnum
    37  
    38  type PQItem[E comparable] interface {
    39  	ReadOnlyPQItem[E]
    40  	SetIndex(idx int64)
    41  	SetPriority(pri int64)
    42  }
    43  
    44  type DelayQueue[E comparable] interface {
    45  	Offer(item E, expiration int64)
    46  	// PollToChan Asynchronous function
    47  	PollToChan(nowFn func() int64, C infra.SendOnlyChannel[E])
    48  	Len() int64
    49  }
    50  
    51  type DQItem[E comparable] interface {
    52  	Expiration() int64
    53  	PQItem[E]
    54  }
    55  
    56  type RingBufferEntry[T any] interface {
    57  	GetValue() T
    58  	GetCursor() uint64
    59  	Store(cursor uint64, value T)
    60  }
    61  
    62  type RingBuffer[T any] interface {
    63  	Capacity() uint64
    64  	LoadEntryByCursor(cursor uint64) RingBufferEntry[T]
    65  }
    66  
    67  type RingBufferCursor interface {
    68  	Next() uint64
    69  	NextN(n uint64) uint64
    70  	Load() uint64
    71  	CompareAndSwap(old, new uint64) bool
    72  }