diff options
author | Valery Piashchynski <[email protected]> | 2021-07-06 17:30:31 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-07-06 17:30:31 +0300 |
commit | 2c78e93222cc9d3b88456175348e42f7f40c449b (patch) | |
tree | be4fc671db33ceb8700019a5ede900c8d900d7c0 /pkg/priority_queue/binary_heap.go | |
parent | 207739f7346c98e16087547bc510e1f909671260 (diff) |
Rework ephemeral and binary heaps
Implemented a sync.Cond for binary heap algo to save processor from
spinning in the for loop and receiving nil Items until the Queue will be
filled.
Add num_pollers option to the configuration to specify number of
pollers from the queue.
Add Resume, ResumeAll, Stop, StopAll, PushBatch methods to the ephemeral.
Remove map and use sync.Map in the ephemeral broker.
Add protobuf schema.
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'pkg/priority_queue/binary_heap.go')
-rw-r--r-- | pkg/priority_queue/binary_heap.go | 75 |
1 files changed, 0 insertions, 75 deletions
diff --git a/pkg/priority_queue/binary_heap.go b/pkg/priority_queue/binary_heap.go deleted file mode 100644 index c7c148da..00000000 --- a/pkg/priority_queue/binary_heap.go +++ /dev/null @@ -1,75 +0,0 @@ -/* -binary heap (min-heap) algorithm used as a core for the priority queue -*/ - -package priorityqueue - -import priorityqueue "github.com/spiral/roadrunner/v2/common/priority_queue" - -type BinHeap []priorityqueue.Item - -func NewBinHeap() *BinHeap { - return &BinHeap{} -} - -func (bh *BinHeap) fixUp() { - k := len(*bh) - 1 - p := (k - 1) >> 1 // k-1 / 2 - - for k > 0 { - cur, par := (*bh)[k], (*bh)[p] - - if cur.Priority() < par.Priority() { - bh.swap(k, p) - k = p - p = (k - 1) >> 1 - } else { - return - } - } -} - -func (bh *BinHeap) swap(i, j int) { - (*bh)[i], (*bh)[j] = (*bh)[j], (*bh)[i] -} - -func (bh *BinHeap) fixDown(curr, end int) { - cOneIdx := curr*2 + 1 - for cOneIdx <= end { - cTwoIdx := -1 - if curr*2+2 <= end { - cTwoIdx = curr*2 + 2 - } - - idxToSwap := cOneIdx - if cTwoIdx > -1 && (*bh)[cTwoIdx].Priority() < (*bh)[cOneIdx].Priority() { - idxToSwap = cTwoIdx - } - if (*bh)[idxToSwap].Priority() < (*bh)[curr].Priority() { - bh.swap(curr, idxToSwap) - curr = idxToSwap - cOneIdx = curr*2 + 1 - } else { - return - } - } -} - -func (bh *BinHeap) Insert(item priorityqueue.Item) { - *bh = append(*bh, item) - bh.fixUp() -} - -func (bh *BinHeap) GetMax() priorityqueue.Item { - l := len(*bh) - if l == 0 { - return nil - } - - bh.swap(0, l-1) - - item := (*bh)[l-1] - *bh = (*bh)[0 : l-1] - bh.fixDown(0, l-2) - return item -} |