MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / hllCount

Function hllCount

src/hyperloglog.cpp:1021–1056  ·  view source on GitHub ↗

Return the approximated cardinality of the set based on the harmonic * mean of the registers values. 'hdr' points to the start of the SDS * representing the String object holding the HLL representation. * * If the sparse representation of the HLL object is not valid, the integer * pointed by 'invalid' is set to non-zero, otherwise it is left untouched. * * hllCount() supports a special inte

Source from the content-addressed store, hash-verified

1019 * This is useful in order to speedup PFCOUNT when called against multiple
1020 * keys (no need to work with 6-bit integers encoding). */
1021uint64_t hllCount(struct hllhdr *hdr, int *invalid) {
1022 double m = HLL_REGISTERS;
1023 double E;
1024 int j;
1025 /* Note that reghisto size could be just HLL_Q+2, because HLL_Q+1 is
1026 * the maximum frequency of the "000...1" sequence the hash function is
1027 * able to return. However it is slow to check for sanity of the
1028 * input: instead we history array at a safe size: overflows will
1029 * just write data to wrong, but correctly allocated, places. */
1030 int reghisto[64] = {0};
1031
1032 /* Compute register histogram */
1033 if (hdr->encoding == HLL_DENSE) {
1034 hllDenseRegHisto(hdr->registers(),reghisto);
1035 } else if (hdr->encoding == HLL_SPARSE) {
1036 hllSparseRegHisto(hdr->registers(),
1037 sdslen((sds)hdr)-HLL_HDR_SIZE,invalid,reghisto);
1038 } else if (hdr->encoding == HLL_RAW) {
1039 hllRawRegHisto(hdr->registers(),reghisto);
1040 } else {
1041 serverPanic("Unknown HyperLogLog encoding in hllCount()");
1042 }
1043
1044 /* Estimate cardinality form register histogram. See:
1045 * "New cardinality estimation algorithms for HyperLogLog sketches"
1046 * Otmar Ertl, arXiv:1702.01284 */
1047 double z = m * hllTau((m-reghisto[HLL_Q+1])/(double)m);
1048 for (j = HLL_Q; j >= 1; --j) {
1049 z += reghisto[j];
1050 z *= 0.5;
1051 }
1052 z += m * hllSigma(reghisto[0]/(double)m);
1053 E = llroundl(HLL_ALPHA_INF*m*m/z);
1054
1055 return (uint64_t) E;
1056}
1057
1058/* Call hllDenseAdd() or hllSparseAdd() according to the HLL encoding. */
1059int hllAdd(robj *o, unsigned char *ele, size_t elesize) {

Callers 2

pfcountCommandFunction · 0.85
pfselftestCommandFunction · 0.85

Calls 7

hllDenseRegHistoFunction · 0.85
hllSparseRegHistoFunction · 0.85
sdslenFunction · 0.85
hllRawRegHistoFunction · 0.85
hllTauFunction · 0.85
hllSigmaFunction · 0.85
registersMethod · 0.80

Tested by

no test coverage detected