erigon-pulse/cl/pool/operation_pool.go

36 lines
928 B
Go
Raw Normal View History

2023-09-27 17:09:36 +00:00
package pool
import (
"github.com/ledgerwatch/erigon/cl/phase1/core/state/lru"
)
var operationsMultiplier = 20 // Cap the amount of cached element to max_operations_per_block * operations_multiplier
2023-09-27 17:09:36 +00:00
type OperationPool[K comparable, T any] struct {
pool *lru.Cache[K, T] // Map the Signature to the underlying object
2023-09-27 17:09:36 +00:00
}
func NewOperationPool[K comparable, T any](maxOperationsPerBlock int, matricName string) *OperationPool[K, T] {
pool, err := lru.New[K, T](matricName, maxOperationsPerBlock*operationsMultiplier)
2023-09-27 17:09:36 +00:00
if err != nil {
panic(err)
}
return &OperationPool[K, T]{pool: pool}
2023-09-27 17:09:36 +00:00
}
func (o *OperationPool[K, T]) Insert(k K, operation T) {
o.pool.Add(k, operation)
2023-09-27 17:09:36 +00:00
}
func (o *OperationPool[K, T]) DeleteIfExist(k K) (removed bool) {
return o.pool.Remove(k)
2023-09-27 17:09:36 +00:00
}
func (o *OperationPool[K, T]) Has(k K) (hash bool) {
return o.pool.Contains(k)
}
func (o *OperationPool[K, T]) Raw() []T {
2023-09-27 17:09:36 +00:00
return o.pool.Values()
}