Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xindoo
redis
提交
5660ff1c
R
redis
项目概览
xindoo
/
redis
通知
2
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5660ff1c
编写于
3月 28, 2014
作者:
A
antirez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
hllAdd() low level HyperLogLog "add" implemented.
上级
e3234116
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
45 addition
and
0 deletion
+45
-0
src/hyperloglog.c
src/hyperloglog.c
+45
-0
未找到文件。
src/hyperloglog.c
浏览文件 @
5660ff1c
...
...
@@ -30,6 +30,7 @@
*/
#include "redis.h"
#include "crc64.h"
/* The Redis HyperLogLog implementation is based on the following ideas:
*
...
...
@@ -53,6 +54,7 @@
#define REDIS_HLL_P 14
/* The greater is P, the smaller the error. */
#define REDIS_HLL_REGISTERS (1<<REDIS_HLL_P)
/* With P=14, 16384 registers. */
#define REDIS_HLL_P_MASK (REDIS_HLL_REGISTERS-1)
/* Mask to index register. */
#define REDIS_HLL_BITS 6
/* Enough to count up to 63 leading zeroes. */
#define REDIS_HLL_REGISTER_MAX ((1<<REDIS_HLL_BITS)-1)
#define REDIS_HLL_SIZE ((REDIS_HLL_REGISTERS*REDIS_HLL_BITS+7)/8)
...
...
@@ -180,6 +182,49 @@
/* ========================= HyperLogLog algorithm ========================= */
/* "Add" the element in the hyperloglog data structure.
* Actually nothing is added, but the max 0 pattern counter of the subset
* the element belongs to is incremented if needed.
*
* 'registers' is expected to have room for REDIS_HLL_REGISTERS plus an
* additional byte on the right. This requirement is met by sds strings
* automatically since they are implicitly null terminated.
*
* The function always succeed, however if as a result of the operation
* the approximated cardinality changed, 1 is returned. Otherwise 0
* is returned. */
int
hllAdd
(
uint8_t
*
registers
,
uint8_t
*
ele
,
size_t
elesize
)
{
uint64_t
hash
,
bit
,
index
;
uint8_t
oldcount
,
count
;
/* Count the number of zeroes starting from bit REDIS_HLL_REGISTERS
* (that is a power of two corresponding to the first bit we don't use
* as index). The max run can be 64-P bits.
*
* This may sound like inefficient, but actually in the average case
* there are high probabilities to find a 1 after a few iterations. */
hash
=
crc64
(
0
,
ele
,
elesize
);
bit
=
REDIS_HLL_REGISTERS
;
count
=
0
;
while
((
hash
&
bit
)
==
0
)
{
count
++
;
/* Test the next bit. Note that if we run out of bits in the 64
* bit integer, bit will be set to 0, and the while test will fail,
* so we can save the explicit check and yet the algorithm will
* terminate. */
bit
<<=
1
;
}
index
=
hash
&
REDIS_HLL_P_MASK
;
/* Index a register inside registers. */
HLL_GET_REGISTER
(
oldcount
,
registers
,
index
);
if
(
count
>
oldcount
)
{
HLL_SET_REGISTER
(
registers
,
index
,
count
);
return
1
;
}
else
{
return
0
;
}
}
/* ========================== HyperLogLog commands ========================== */
/* This command performs a self-test of the HLL registers implementation.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录