summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-04-12 23:42:48 +0200
committerantirez <antirez@gmail.com>2014-04-16 15:26:27 +0200
commiteaa73ef73a812abfb4acce4d9f56d7041d47e382 (patch)
treeb8fdac999b65943ca2a2fcf8de2e2873affb37f2
parent95de3eaea0ebeb554b71b0370eef63cc8ad0598f (diff)
downloadredis-eaa73ef73a812abfb4acce4d9f56d7041d47e382.tar.gz
Abstract hllSparseAdd() / hllDenseAdd() via hllAdd().
-rw-r--r--src/hyperloglog.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/hyperloglog.c b/src/hyperloglog.c
index 7dfc34c19..8e0cba7ae 100644
--- a/src/hyperloglog.c
+++ b/src/hyperloglog.c
@@ -909,6 +909,16 @@ uint64_t hllCount(struct hllhdr *hdr) {
return (uint64_t) E;
}
+/* Call hllDenseAdd() or hllSparseAdd() according to the HLL encoding. */
+int hllAdd(robj *o, unsigned char *ele, size_t elesize) {
+ struct hllhdr *hdr = o->ptr;
+ switch(hdr->encoding) {
+ case HLL_DENSE: return hllDenseAdd(hdr->registers,ele,elesize);
+ case HLL_SPARSE: return hllSparseAdd(o,ele,elesize);
+ default: return -1; /* Invalid representation. */
+ }
+}
+
/* ========================== HyperLogLog commands ========================== */
/* Create an HLL object. We always create the HLL using sparse encoding.
@@ -996,14 +1006,19 @@ void pfaddCommand(redisClient *c) {
o = dbUnshareStringValue(c->db,c->argv[1],o);
}
/* Perform the low level ADD operation for every element. */
- hdr = o->ptr;
for (j = 2; j < c->argc; j++) {
- if (hllDenseAdd(hdr->registers, (unsigned char*)c->argv[j]->ptr,
- sdslen(c->argv[j]->ptr)))
- {
+ int retval = hllAdd(o, (unsigned char*)c->argv[j]->ptr,
+ sdslen(c->argv[j]->ptr));
+ switch(retval) {
+ case 1:
updated++;
+ break;
+ case -1:
+ addReplyError(c,"Invalid HyperLogLog representation");
+ return;
}
}
+ hdr = o->ptr;
if (updated) {
signalModifiedKey(c->db,c->argv[1]);
notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"pfadd",c->argv[1],c->db->id);