mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed? Add new Counter implementations: - count-min sketch for high cardinality of keys - hybrid to switch from map to sketch when number of keys gets high enough ## Why? Support high cardinality of fairness keys. ## How did you test it? - [ ] built - [ ] run locally and tested manually - [x] covered by existing tests - [x] added new unit test(s) - [ ] added new functional test(s)
24 lines
480 B
Go
24 lines
480 B
Go
package counter
|
|
|
|
// mapCounter is a Counter that stores counts in a map.
|
|
// mapCounter is not safe for concurrent use.
|
|
type mapCounter struct {
|
|
m map[string]int64
|
|
}
|
|
|
|
var _ Counter = (*mapCounter)(nil)
|
|
|
|
func NewMapCounter() *mapCounter {
|
|
return &mapCounter{m: make(map[string]int64)}
|
|
}
|
|
|
|
func (m *mapCounter) GetPass(key string, base, inc int64) int64 {
|
|
c := max(base, m.m[key]+inc)
|
|
m.m[key] = c
|
|
return c
|
|
}
|
|
|
|
func (m *mapCounter) EstimateDistinctKeys() int {
|
|
return len(m.m)
|
|
}
|