V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
zhengji
V2EX  ›  分享创造

根据 CMU 论文实现 Cuckoo Filter 的 Golang 库

  •  1
     
  •   zhengji ·
    zheng-ji · 2016-07-25 21:55:53 +08:00 · 2631 次点击
    这是一个创建于 2825 天前的主题,其中的信息可能已经有所发展或是发生改变。

    goCuckoo

    Build Status GoDoc

    A Cuckoo hashing, substituting for bloom filter. written by Go

    一个 Cuckoo Filter 的 Go 库

    goCuckoo

    Description

    面对海量数据,我们需要一个索引数据结构,用来帮助查询,快速判断数据记录是否存在,这类数据结构叫过滤器,常用的选择是 Bloom Filter. 而 Cuckoo Filter 是它的优化变种。

    Bloom Filter 的位图模式有两个问题:

    • 误报,它能判断元素一定不存在,但只能判断可能存在,因为存在其它元素被映射到部分相同位上,导致该位置 1 ,那么一个不存在的元素可能会被误报成存在;
    • 漏报,如果删除了某个元素,导致该映射位被置 0 ,那么本来存在的元素会被漏报成不存在。

    Cuckoo Filter,可以确保该元素存在的必然性,又可以在不违背此前提下删除任意元素,仅仅比 Bloom Filter 牺牲了微量空间效率。 它的的数据模型:

    • 每个元素对应两个哈希算法,在哈希碰撞时会启用备用哈希算法。
    • 每一个桶是有 4 路的,每个槽对应一个指纹。

    model

    Feature

    • Deletion Support
    • FastLoopUp O(1)
    • High Space Utilization,4-way set-associative table: > 95% entries occupied
    • Subsituting for Bloom Filters

    Installation

    go get github.com/zheng-ji/goCuckoo
    

    Example

    import (
    	"fmt"
    	"github.com/zheng-ji/goCuckoo"
    )
    
    func main() {
        // speicify capacity 
    	filter := cuckoo.NewCuckooFilter(10000)
    
    	filter.Insert([]byte("zheng-ji"))
    	filter.Insert([]byte("stupid"))
    	filter.Insert([]byte("coder"))
    
    	if filter.Find([]byte("stupid")) {
    		fmt.Println("exist")
    	} else {
    		fmt.Println("Not exist")
    	}
    
    	filter.Del([]byte("stupid"))
    	filter.Println(filter.Size())
    }
    

    Documentation

    License

    Copyright (c) 2016 by zheng-ji released under MIT License.

    3 条回复    2016-07-26 13:00:30 +08:00
    dgkae
        1
    dgkae  
       2016-07-25 22:00:34 +08:00   ❤️ 1
    Comdex
        2
    Comdex  
       2016-07-25 22:08:36 +08:00   ❤️ 1
    趁机自我推荐一下新手之作: https://github.com/Comdex/octopus
    Charles0429
        3
    Charles0429  
       2016-07-26 13:00:30 +08:00   ❤️ 1
    赞一个,正在学 golang
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1037 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 23:01 · PVG 07:01 · LAX 16:01 · JFK 19:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.