我是靠谱客的博主 明理钻石,这篇文章主要介绍Go-过滤器的简单使用(bloom),现在分享给大家,希望可以做个参考。

之前在进行项目的时候使用到了Go的过滤,所以总结了常用的几种过滤器,现贴代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package mygo import ( "fmt" "strings" boom "github.com/BoomFilters" "github.com/bloom" cuckoo "github.com/goCuckoo" ) func macuckooin() { // speicify capacity //cuckoo filter := cuckoo.NewFilter(10000) filter.Insert([]byte("zheng-ji,stupid")) //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")) fmt.Println(filter.Size()) } func wilifiBloomFilterTest() { //wilifi/bloom n := uint(1000) filter := bloom.New(20*n, 5) // load of 20, 5 keys // filter.Add([]byte("Love")) // filter.Add([]byte("swxctx")) filter.AddString("love") if filter.Test([]byte("love")) { fmt.Println("exist") } else { fmt.Println("no exists") } } func bloomFiterTest2() { sbf := boom.NewDefaultStableBloomFilter(10000, 0.01) sbf.Add([]byte(`a`)) if sbf.Test([]byte(`a`)) { fmt.Println("contains a") } if !sbf.TestAndAdd([]byte(`b`)) { fmt.Println("doesn't contain b") } if sbf.Test([]byte(`b`)) { fmt.Println("now it contains b!") } // Restore to initial state. sbf.Reset() } // 加载禁用词 func bloomFilterTest() { var BanWords string = "helloword" Title := "word" var adsTitle []string titleRune := []rune(Title) for m := 0; m < len(titleRune)-1; m++ { fmt.Println(string(titleRune[m : m+2])) adsTitle = append(adsTitle, string(titleRune[m:m+2])) } fmt.Println(len(adsTitle)) s := strings.Split(BanWords, ",") fmt.Println(s) //cuckoo //filter := cuckoo.NewFilter(100000) n := uint(1000) filter := bloom.New(20*n, uint(len(s))) // load of 20, 5 keys for i := 0; i < len(s); i++ { filter.Add([]byte(s[i])) } for titleLen := 0; titleLen < len(adsTitle); titleLen++ { if filter.Test([]byte(adsTitle[titleLen])) { fmt.Println("exist") } else { fmt.Println("Not exist") } } }


最后

以上就是明理钻石最近收集整理的关于Go-过滤器的简单使用(bloom)的全部内容,更多相关Go-过滤器内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部