Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Alloc = func(n int) []byte { return make([]byte, n) }
Alloc allocates the memory blob. It is a variable, so one can change it to use, say, sync.Pool.
var Free = func(blob []byte) {
}
Free returns the blob back. It is a variable, so one can change it to use, say, sync.Pool.
Functions ¶
func DenseSizeByError ¶
DenseSizeByError returns a byte size of a dense HLL for a given errorRate. The error must be between 0.0253% and 26% (inclusive).
func DenseSizeByP ¶
DenseSizeByP returns a byte size of a dense HLL for a given precision. Precision (p) must be between 4 and 25 (inclusive).
func SizeByError ¶
SizeByError returns a byte size of an HLL for a given errorRate. The error must be between 0.0253% and 26% (inclusive).
Types ¶
type Dense ¶
type Dense []byte
Dense implements hyper-log log from http://research.google.com/pubs/pub40671.html With bias correction and linear counting, but not the HLL++ part (not sparse). HLL uses constant memory (0.75 * 2^p bytes.) All operations are non-allocating.
func (Dense) EstimateCardinality ¶
EstimateCardinality returns a cardinality estimate.
type HLL ¶
type HLL []byte
HLL is a hybrid hyper-loglog: either sparse or dense, switching from sparse to dense when needed. Note, both sparse and dense representation take exactly same space. Dense representation performs no allocations, sparse might need some when switching to dense.
Sparse mode estimate is exact. HLL is byte buffer friendly (no need to serialize/deserialize).
Layout: First 8 bit header. Leftmost bit: dirty. Second left most: mode (1: dense, 0: sparse).
sparse:
Next 30 bits: number of elements (big endian), 32 bits unused, followed by elements, 8 bytes each (uint64 little endian). Note, the header is a part of sparse HLL.
dense:
Next 62 bits: previous cardinality esimate (big endian). Valid if !dirty. Followed by dense HLL.
full: 8 byte header, hll[0]&(1<<6) != 0, followed by dense HLL.
All operations are in place. Add/Merge might allocate a temporary buffer when switching from sparse to dense representation. Note, this is not HLL++ - it uses a different sparse representation.
Creating an HLL:
s, err := SizeByP(p) // Or SizeByError.
if err != nil { log.Panicln(err) }
h := make(HLL, s)
h is empty and sparse at this point.
func (HLL) Add ¶
Add a hash to an HLL. Might allocate a block (with Alloc) if HLL is sparse and it gets full. Make sure to use a good hash function.
func (HLL) EstimateCardinality ¶
EstimateCardinality returns a cardinality estimate. Note, EstimateCardinality might (will) modify the HLL iff HLL is dirty.
func (HLL) IsSparse ¶
IsSparse returns true iff the underlying HLL is sparse (and thus the cardinality estimate is exact).