erigon-pulse/etl/heap.go
Håvard Anda Estensen 5a1726b107
Avoid leaking the popped item (#760)
Set the deleted item to its zero val so it can be garbage collected.
2022-12-04 16:19:09 +07:00

72 lines
1.6 KiB
Go

/*
Copyright 2021 Erigon contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package etl
import (
"bytes"
"github.com/ledgerwatch/erigon-lib/kv"
)
type HeapElem struct {
Key []byte
Value []byte
TimeIdx int
}
type Heap struct {
comparator kv.CmpFunc
elems []HeapElem
}
func (h Heap) Len() int {
return len(h.elems)
}
func (h Heap) Less(i, j int) bool {
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 {
return c < 0
}
return h.elems[i].TimeIdx < h.elems[j].TimeIdx
}
func (h Heap) Swap(i, j int) {
h.elems[i], h.elems[j] = h.elems[j], h.elems[i]
}
func (h *Heap) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
h.elems = append(h.elems, x.(HeapElem))
}
func (h *Heap) Pop() interface{} {
old := h.elems
n := len(old)
x := old[n-1]
old[n-1] = HeapElem{}
h.elems = old[0 : n-1]
return x
}