github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/util/hyperloglog/hyperloglog.go (about)

     1  // Package hyperloglog wraps github.com/clarkduvall/hyperloglog with mutexes
     2  package hyperloglog
     3  
     4  import (
     5  	"sync"
     6  
     7  	original "github.com/clarkduvall/hyperloglog"
     8  )
     9  
    10  type Hash64 original.Hash64
    11  
    12  type HyperLogLogPlus struct {
    13  	hMutex sync.Mutex
    14  	h      *original.HyperLogLogPlus
    15  }
    16  
    17  func NewPlus(n uint8) (*HyperLogLogPlus, error) {
    18  	h, err := original.NewPlus(n)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	return &HyperLogLogPlus{
    24  		h: h,
    25  	}, nil
    26  }
    27  
    28  func (h *HyperLogLogPlus) Count() uint64 {
    29  	h.hMutex.Lock()
    30  	defer h.hMutex.Unlock()
    31  
    32  	return h.h.Count()
    33  }
    34  
    35  func (h *HyperLogLogPlus) Add(s Hash64) {
    36  	h.hMutex.Lock()
    37  	defer h.hMutex.Unlock()
    38  
    39  	h.h.Add(s)
    40  }