Skip to content

Commit 30e2289

Browse files
committed
fixed:bloom filter doesn't work on Chinese
1 parent dd2aec6 commit 30e2289

6 files changed

Lines changed: 105 additions & 20 deletions

File tree

‎.gitignore‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66
/static/server.crt
77
/plugin/*.vsix
88
/.vscode/
9-
/config.yaml
9+
/config.yaml
10+
/mkblog.pid
11+
/nohup.out
12+
/test_add_article.sh

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ copy:
1616
@echo "Copying frontend build to static/ and static/assets/..."
1717
mkdir -p static static/assets
1818
@# detect build output dir (common: dist or build)
19-
if [ -d frontend/dist ]; then DIST=frontend/dist; elif [ -d frontend/build ]; then DIST=frontend/build; else echo "No frontend build dir (frontend/dist or frontend/build). Run 'make frontend-build' first." && exit 1; fi; \
19+
if [ -d frontend/dist ]; then DIST=frontend/dist; elif [ -d frontend/build ]; then DIST=frontend/build; else echo "No frontend build dir (frontend/dist or frontend/build). Run 'make fbuild' first." && exit 1; fi; \
2020
cp -f "$$DIST/index.html" static/; \
2121
find "$$DIST" -type f \( -name '*.css' -o -name '*.js' \) -exec cp {} static/assets/ \;
2222

‎config/config.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ func init() {
126126
if !Cfg.TLS.Enabled {
127127
slog.Info("loaded configuration successfully", "mysql", Cfg.MySQL, "tls", Cfg.TLS, "auth_enabled", Cfg.Auth.Enabled)
128128
slog.Warn("TLS is disabled, consider enabling it in production environments")
129-
panic("TLS is disabled, please enable it for better security")
130129
}
131130

132131
if cert := os.Getenv("TLS_CERT"); cert != "" {

‎pkg/bloom/bloom.go‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
type BloomFilter struct {
99
bitset []byte
1010
bitcount []uint8
11-
hashs []func(data []byte) uint8
11+
hashs []func(data []rune) uint8
1212
}
1313

1414
const (
@@ -19,30 +19,26 @@ const (
1919

2020
var bf *BloomFilter
2121

22-
func GetBloomFilter() *BloomFilter {
23-
return bf
24-
}
25-
2622
func init() {
2723
bf = &BloomFilter{
2824
bitset: make([]byte, byteSize),
2925
bitcount: make([]uint8, byteSize),
30-
hashs: []func(data []byte) uint8{
31-
func(data []byte) uint8 {
26+
hashs: []func(data []rune) uint8{
27+
func(data []rune) uint8 {
3228
var hash uint32 = 0
3329
for _, b := range data {
3430
hash += uint32(b)
3531
}
3632
return uint8(hash % uint32(bitSize))
3733
},
38-
func(data []byte) uint8 {
34+
func(data []rune) uint8 {
3935
var hash uint32 = 0
4036
for i, b := range data {
4137
hash += uint32(b) * uint32(i+1)
4238
}
4339
return uint8(hash % uint32(byteSize))
4440
},
45-
func(data []byte) uint8 {
41+
func(data []rune) uint8 {
4642
var hash uint32 = 0
4743
for i, b := range data {
4844
hash += uint32(b) * uint32((i+1)*(i+1))
@@ -54,11 +50,15 @@ func init() {
5450
var titles []string
5551
database.GetDatabase().Model(models.ArticleDetail{}).Select("title").Find(&titles)
5652
for _, title := range titles {
57-
bf.Add([]byte(title))
53+
bf.Add([]rune(title))
5854
}
5955
}
6056

61-
func (bf *BloomFilter) Add(data []byte) {
57+
func GetBloomFilter() *BloomFilter {
58+
return bf
59+
}
60+
61+
func (bf *BloomFilter) Add(data []rune) {
6262
for _, h := range bf.hashs {
6363
pos := h(data)
6464
bytePos := pos / 8
@@ -70,7 +70,7 @@ func (bf *BloomFilter) Add(data []byte) {
7070
}
7171
}
7272

73-
func (bf *BloomFilter) Exists(data []byte) bool {
73+
func (bf *BloomFilter) Exists(data []rune) bool {
7474
for _, h := range bf.hashs {
7575
pos := h(data)
7676
bytePos := pos / 8
@@ -82,7 +82,7 @@ func (bf *BloomFilter) Exists(data []byte) bool {
8282
return true
8383
}
8484

85-
func (bf *BloomFilter) Remove(data []byte) {
85+
func (bf *BloomFilter) Remove(data []rune) {
8686
for _, h := range bf.hashs {
8787
pos := h(data)
8888
bytePos := pos / 8

‎pkg/bloom/bloom_test.go‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package bloom
2+
3+
import "testing"
4+
5+
func TestBloomFilter(t *testing.T) {
6+
bf := GetBloomFilter()
7+
test_data := [][]rune{
8+
[]rune("test1"),
9+
[]rune("test2"),
10+
[]rune("test3"),
11+
}
12+
for _, data := range test_data {
13+
bf.Add(data)
14+
if !bf.Exists(data) {
15+
t.Errorf("Expected %s to be in the bloom filter", string(data))
16+
}
17+
if bf.Exists([]rune("not_in_filter")) {
18+
t.Error("Expected 'not_in_filter' to not be in the bloom filter")
19+
}
20+
if bf.Exists([]rune("1test")) {
21+
t.Error("Expected '1test' to not be in the bloom filter")
22+
}
23+
if bf.Exists([]rune("2test")) {
24+
t.Error("Expected '2test' to not be in the bloom filter")
25+
}
26+
if bf.Exists([]rune("3test")) {
27+
t.Error("Expected '3test' to not be in the bloom filter")
28+
}
29+
}
30+
for _, data := range test_data {
31+
bf.Remove(data)
32+
if bf.Exists(data) {
33+
t.Errorf("Expected %s to be removed from the bloom filter", string(data))
34+
}
35+
}
36+
if bf.Exists([]rune("test1")) || bf.Exists([]rune("test2")) || bf.Exists([]rune("test3")) {
37+
t.Error("Expected all test data to be removed from the bloom filter")
38+
}
39+
if bf.Exists([]rune("not_in_filter")) {
40+
t.Error("Expected 'not_in_filter' to not be in the bloom filter after removal")
41+
}
42+
if bf.Exists([]rune("1test")) {
43+
t.Error("Expected '1test' to not be in the bloom filter after removal")
44+
}
45+
if bf.Exists([]rune("2test")) {
46+
t.Error("Expected '2test' to not be in the bloom filter after removal")
47+
}
48+
if bf.Exists([]rune("3test")) {
49+
t.Error("Expected '3test' to not be in the bloom filter after removal")
50+
}
51+
t.Log("Bloom filter test passed")
52+
}
53+
54+
func TestBloomFilterChinese(t *testing.T) {
55+
bf := GetBloomFilter()
56+
test_data := [][]rune{
57+
[]rune("测试1"),
58+
[]rune("测试2"),
59+
[]rune("测试3"),
60+
}
61+
for _, data := range test_data {
62+
bf.Add(data)
63+
if !bf.Exists(data) {
64+
t.Errorf("Expected %s to be in the bloom filter", string(data))
65+
}
66+
if bf.Exists([]rune("不在过滤器中")) {
67+
t.Error("Expected '不在过滤器中' to not be in the bloom filter")
68+
}
69+
}
70+
for _, data := range test_data {
71+
bf.Remove(data)
72+
if bf.Exists(data) {
73+
t.Errorf("Expected %s to be removed from the bloom filter", string(data))
74+
}
75+
}
76+
if bf.Exists([]rune("测试1")) || bf.Exists([]rune("测试2")) || bf.Exists([]rune("测试3")) {
77+
t.Error("Expected all test data to be removed from the bloom filter")
78+
}
79+
if bf.Exists([]rune("不在过滤器中")) {
80+
t.Error("Expected '不在过滤器中' to not be in the bloom filter after removal")
81+
}
82+
t.Log("Bloom filter Chinese test passed")
83+
}

‎service/api/article.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func UploadArticle(c *gin.Context) {
8080
slog.Error("insert article summary failed ", "error: ", err)
8181
}
8282

83-
bloom.GetBloomFilter().Add([]byte(article.Title))
83+
bloom.GetBloomFilter().Add([]rune(article.Title))
8484
c.JSON(200, gin.H{"msg": "successfully added article"})
8585
}
8686

@@ -93,7 +93,7 @@ func GetArticleDetail(c *gin.Context) {
9393
}
9494

9595
// 先检查布隆过滤器,快速判断文章是否存在,减少数据库压力
96-
if !bloom.GetBloomFilter().Exists([]byte(title)) {
96+
if !bloom.GetBloomFilter().Exists([]rune(title)) {
9797
c.JSON(404, gin.H{"msg": "article not found"})
9898
return
9999
}
@@ -272,7 +272,7 @@ func DeleteArticle(c *gin.Context) {
272272
}
273273

274274
// 先检查布隆过滤器,快速判断文章是否存在,减少数据库压力
275-
if !bloom.GetBloomFilter().Exists([]byte(title)) {
275+
if !bloom.GetBloomFilter().Exists([]rune(title)) {
276276
c.JSON(404, gin.H{"msg": "article not found"})
277277
return
278278
}
@@ -289,6 +289,6 @@ func DeleteArticle(c *gin.Context) {
289289
}
290290
time.Sleep(10 << i * time.Millisecond)
291291
}
292-
bloom.GetBloomFilter().Remove([]byte(title))
292+
bloom.GetBloomFilter().Remove([]rune(title))
293293
c.JSON(200, gin.H{"msg": "successfully deleted article"})
294294
}

0 commit comments

Comments
 (0)