mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-12-24 01:17:56 +08:00
33b11f92bc
commit 465b47b8f72668dfc12eaa72befb232513c9e1db
Merge: f39272b54 9c440d472
Author: chrislu <chris.lu@gmail.com>
Date: Wed Jul 10 23:56:51 2024 -0700
Merge branch 'master' into merged-persisted-logs
commit f39272b5416bdd4d1714ce3e99aded0fe62e7d89
Author: chrislu <chris.lu@gmail.com>
Date: Wed Jul 10 23:49:26 2024 -0700
merge meta logs from multiple files
75 lines
844 B
Go
75 lines
844 B
Go
package util
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type node[T any]struct {
|
|
data T
|
|
next *node[T]
|
|
}
|
|
|
|
type Queue[T any] struct {
|
|
head *node[T]
|
|
tail *node[T]
|
|
count int
|
|
sync.RWMutex
|
|
}
|
|
|
|
func NewQueue[T any]() *Queue[T] {
|
|
q := &Queue[T]{}
|
|
return q
|
|
}
|
|
|
|
func (q *Queue[T]) Len() int {
|
|
q.RLock()
|
|
defer q.RUnlock()
|
|
return q.count
|
|
}
|
|
|
|
func (q *Queue[T]) Enqueue(item T) {
|
|
q.Lock()
|
|
defer q.Unlock()
|
|
|
|
n := &node[T]{data: item}
|
|
|
|
if q.tail == nil {
|
|
q.tail = n
|
|
q.head = n
|
|
} else {
|
|
q.tail.next = n
|
|
q.tail = n
|
|
}
|
|
q.count++
|
|
}
|
|
|
|
func (q *Queue[T]) Dequeue() (result T) {
|
|
q.Lock()
|
|
defer q.Unlock()
|
|
|
|
if q.head == nil {
|
|
return
|
|
}
|
|
|
|
n := q.head
|
|
q.head = n.next
|
|
|
|
if q.head == nil {
|
|
q.tail = nil
|
|
}
|
|
q.count--
|
|
|
|
return n.data
|
|
}
|
|
|
|
func (q *Queue[T]) Peek() (result T) {
|
|
q.RLock()
|
|
defer q.RUnlock()
|
|
|
|
if q.head == nil {
|
|
return
|
|
}
|
|
|
|
return q.head.data
|
|
}
|