2020-05-30 07:00:35 +00:00
|
|
|
package etl
|
2020-05-26 13:37:25 +00:00
|
|
|
|
2020-09-10 12:35:58 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
)
|
2020-05-26 13:37:25 +00:00
|
|
|
|
|
|
|
type HeapElem struct {
|
2020-05-31 06:57:47 +00:00
|
|
|
Key []byte
|
|
|
|
TimeIdx int
|
|
|
|
Value []byte
|
2020-05-26 13:37:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 12:35:58 +00:00
|
|
|
type Heap struct {
|
|
|
|
comparator dbutils.CmpFunc
|
|
|
|
elems []HeapElem
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Heap) SetComparator(cmp dbutils.CmpFunc) {
|
|
|
|
h.comparator = cmp
|
|
|
|
}
|
2020-05-26 13:37:25 +00:00
|
|
|
|
|
|
|
func (h Heap) Len() int {
|
2020-09-10 12:35:58 +00:00
|
|
|
return len(h.elems)
|
2020-05-26 13:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h Heap) Less(i, j int) bool {
|
2020-09-10 12:35:58 +00:00
|
|
|
if h.comparator != nil {
|
|
|
|
if c := h.comparator(h.elems[i].Key, h.elems[j].Key, h.elems[i].Value, h.elems[j].Value); c != 0 {
|
|
|
|
return c < 0
|
|
|
|
}
|
|
|
|
return h.elems[i].TimeIdx < h.elems[j].TimeIdx
|
|
|
|
}
|
|
|
|
|
|
|
|
if c := bytes.Compare(h.elems[i].Key, h.elems[j].Key); c != 0 {
|
2020-05-26 13:37:25 +00:00
|
|
|
return c < 0
|
|
|
|
}
|
2020-09-10 12:35:58 +00:00
|
|
|
return h.elems[i].TimeIdx < h.elems[j].TimeIdx
|
2020-05-26 13:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h Heap) Swap(i, j int) {
|
2020-09-10 12:35:58 +00:00
|
|
|
h.elems[i], h.elems[j] = h.elems[j], h.elems[i]
|
2020-05-26 13:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) Push(x interface{}) {
|
|
|
|
// Push and Pop use pointer receivers because they modify the slice's length,
|
|
|
|
// not just its contents.
|
2020-09-10 12:35:58 +00:00
|
|
|
h.elems = append(h.elems, x.(HeapElem))
|
2020-05-26 13:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heap) Pop() interface{} {
|
2020-09-10 12:35:58 +00:00
|
|
|
old := h.elems
|
2020-05-26 13:37:25 +00:00
|
|
|
n := len(old)
|
|
|
|
x := old[n-1]
|
2020-09-10 12:35:58 +00:00
|
|
|
h.elems = old[0 : n-1]
|
2020-05-26 13:37:25 +00:00
|
|
|
return x
|
|
|
|
}
|