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

Function hllMerge

src/hyperloglog.cpp:1076–1118  ·  view source on GitHub ↗

Merge by computing MAX(registers[i],hll[i]) the HyperLogLog 'hll' * with an array of uint8_t HLL_REGISTERS registers pointed by 'max'. * * The hll object must be already validated via isHLLObjectOrReply() * or in some other way. * * If the HyperLogLog is sparse and is found to be invalid, C_ERR * is returned, otherwise the function always succeeds. */

Source from the content-addressed store, hash-verified

1074 * If the HyperLogLog is sparse and is found to be invalid, C_ERR
1075 * is returned, otherwise the function always succeeds. */
1076int hllMerge(uint8_t *max, size_t cmax, robj_roptr hll) {
1077 struct hllhdr *hdr = (hllhdr*)ptrFromObj(hll);
1078 int i;
1079
1080 if (hdr->encoding == HLL_DENSE) {
1081 uint8_t val;
1082
1083 for (i = 0; i < HLL_REGISTERS; i++) {
1084 HLL_DENSE_GET_REGISTER(val,hdr->registers(),i);
1085 if (val > max[i]) max[i] = val;
1086 }
1087 } else {
1088 const uint8_t *p = (const uint8_t*)ptrFromObj(hll), *end = p + sdslen(szFromObj(hll));
1089 long runlen, regval;
1090
1091 p += HLL_HDR_SIZE;
1092 i = 0;
1093 while(p < end) {
1094 if (HLL_SPARSE_IS_ZERO(p)) {
1095 runlen = HLL_SPARSE_ZERO_LEN(p);
1096 i += runlen;
1097 p++;
1098 } else if (HLL_SPARSE_IS_XZERO(p)) {
1099 runlen = HLL_SPARSE_XZERO_LEN(p);
1100 i += runlen;
1101 p += 2;
1102 } else {
1103 runlen = HLL_SPARSE_VAL_LEN(p);
1104 regval = HLL_SPARSE_VAL_VALUE(p);
1105 if ((runlen + i) > HLL_REGISTERS) break; /* Overflow. */
1106 while(runlen--) {
1107 if (i < 0 || (size_t)i >= cmax)
1108 return C_ERR;
1109 if (regval > max[i]) max[i] = regval;
1110 i++;
1111 }
1112 p++;
1113 }
1114 }
1115 if (i != HLL_REGISTERS) return C_ERR;
1116 }
1117 return C_OK;
1118}
1119
1120/* ========================== HyperLogLog commands ========================== */
1121

Callers 2

pfcountCommandFunction · 0.85
pfmergeCommandFunction · 0.85

Calls 4

ptrFromObjFunction · 0.85
sdslenFunction · 0.85
szFromObjFunction · 0.85
registersMethod · 0.80

Tested by

no test coverage detected