Cuckoo Filter(布谷过滤器)
布谷过滤器是布隆过滤器的改进。
- 布隆过滤器:适用于高效地测试成员集且空间利用率高,但存在小概率错误正向的输出(即对象不在某个集合里面但布隆过滤器却输出存在),且不适用于集合中元素被删除时的情况。
- 布谷过滤器解决布隆过滤器不适用于元素被删除的情况。
Cuckoo filter(布谷过滤器)的优点:
- 支持动态添加和删除元素.
- 比布隆过滤器具有更高的性能.
1. Cuckoo Hash Table(布谷哈希表)
对于布谷过滤器而言,采用的不是位数组,而是桶数组,每一行即为一个bucket,且每个bucket都有4个空间。每个元素item都有两个hash function决定候选bucket.
1.1.插入元素过程:
假设:bucket1:=h1(x),bucket2:=h2(x)
如果bucket1和bucket2中有一个候选桶是empty,则选择空的候选桶插入。
relocate: 如果bucket1和bucket2中没有一个候选桶是empty,则选择其中一个候选桶 b,然后先将该桶的原元素插入到它对应的另外一个候选桶中,如果另外一个候选桶不是空的则递归执行该过程,然后再将x插入到候选桶b中。(注:until a maximum number of displacements is reached (e.g.500 times in our implementation ). If no vacant bucket is found, this hash table is considered too full to insert.)
其平均时间复杂度为O(1)
practical implementation: 会在上述的基础上,使得每个bucket可以存放多个item,如图1©所示。
1.2.查找过程:
检查h1(x)和h2(x)所对应的桶中是否包含这个元素(注:只要其中一个桶包含这个元素即可)
2. Cuckoo Filter Algorithm(布谷过滤器算法)
在Cuckoo Hash Table的基础上设计相关算法使得适用于集合成员测试(Using Cuckoo Hashing for Set-membership:)
2.1 Challenge and Proposal
- 空间利用率,查找速度=>多路联合布谷哈希表(mult-way associative cuckoo hash table).
- 哈希表空间=>每个元素先被哈希成固定长度的指纹(fingerprint)然后插入哈希表中.
=> 需要重新设计布谷哈希表
Challenge1:由于布谷哈希表中存在relocate的过程中,如果布谷哈希表中只存放item的哈希值,那么当item需要relocate时不知道另一个候选桶的位置.
Proposal1: cuckoo fifilters use partial-key cuckoo hashing to find an item’s alternate location based on only its fingerprint .
2.2 Concept:
entry:在布谷过滤器中布谷哈希表的基本单元。每一个entry存放一个fingerprint
cuckoo hash table: bucket的数组.一个bucket可以存放多个entries。
partial-key cuckoo hashing: 基于fingerprint来确定item的可替代位置.
- 对于item x,计算候选桶的哈希函数如下:
=>这样存在一个非常重要的属性:
已
知
h
a
s
h
(
x
′
s
f
i
n
g
e
r
p
r
i
n
t
)
,
h
1
(
x
)
时
,
h
2
(
x
)
=
h
1
(
x
)
⨁
h
a
s
h
(
x
′
s
f
i
n
g
e
r
p
r
i
n
t
)
已
知
h
a
s
h
(
x
′
s
f
i
n
g
e
r
p
r
i
n
t
)
,
h
2
(
x
)
时
,
h
1
(
x
)
=
h
2
(
x
)
⨁
h
a
s
h
(
x
′
s
f
i
n
g
e
r
p
r
i
n
t
)
每
个
f
i
n
g
e
r
p
r
i
n
t
生
成
的
哈
希
算
法
可
以
不
同
,
但
是
生
成
h
1
、
h
2
的
h
a
s
h
算
法
是
相
同
的
已知hash(x's\quad fingerprint),h_1(x)时,h_2(x)=h_1(x)\bigoplus hash(x's\quad fingerprint)\\已知hash(x's \quad fingerprint),h_2(x)时,h_1(x)=h_2(x)\bigoplus hash(x's\quad fingerprint)\\每个fingerprint生成的哈希算法可以不同,但是生成h1、h2的hash算法是相同的
已知hash(x′sfingerprint),h1(x)时,h2(x)=h1(x)⨁hash(x′sfingerprint)已知hash(x′sfingerprint),h2(x)时,h1(x)=h2(x)⨁hash(x′sfingerprint)每个fingerprint生成的哈希算法可以不同,但是生成h1、h2的hash算法是相同的
换句话说,alternative’s position=this position xor hash(x’s fingerprint),还要注意的是每个item的fingerprint都会事先被hash,可以确保item在relocate到完全不同部分的bucket中,从而减少哈希冲突并提高表的利用率。
2.3 Insert Operation
上面的Insert 算法表示当n>=MaxNumKicks时会出现桶溢出现象(bucket overflow).
2.4 Loop Up Operation
注释上面的查找过程不会出现 false negative question,只有当出现桶溢出时才会出现。
2.5 Delete Operation
布隆过滤器是不支持删除操作的,因为Bloom filter是位数组即元素即为0/1不能指向集合中的某个元素,而布谷过滤器中entry存放的是item的fingerprint可以唯一标识某个item.
所以Cuckoo Filter支持删除操作.
Bloom filter “delete operation”: 需要重建过滤器.
Cuckoo Filter delete operation: 在特定entry中删除其fingerprint.
删除操作的分析:
-
即使item x和item y同时存放在bucket i1中且fingerprint都相同(我的理解对于fingerprint的哈希计算不同item有不同生成算法),也不会出现false negative,但是会出现false positve。
当删除item x后,y依然可以被认为是集合成员(不会出现false negative)。但是item x也依然会认为是集合成员(true positive)。
very serious problem: 当item x 和y有相同的fingerprint时,但item x已经插入到bucket且item y没有插入bucket,如果此时执行delete(y),则会出现删除真实存在的item,出现false negative。
=> 因此需要事先要要求删除的元素一定是存在的元素.(This requirement also holds true for all other deletion-supporting fifilters.)
参考文献:
Cuckoo Filter: Practically Better Than Bloom