Skip to content

Commit

Permalink
优化计数器
Browse files Browse the repository at this point in the history
  • Loading branch information
newpanjing committed Jun 13, 2022
1 parent 2fb5dbb commit 64c029b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
47 changes: 41 additions & 6 deletions searcher/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Engine struct {

Shard int //分片数
Timeout int64 //超时时间,单位秒

documentCount int64 //文档总数量
}

type Option struct {
Expand All @@ -55,6 +57,8 @@ func (e *Engine) Init() {
if e.Timeout == 0 {
e.Timeout = 10 * 3 //默认10分钟
}
//-1代表没有初始化
e.documentCount = -1
//log.Println("数据存储目录:", e.IndexPath)

e.addDocumentWorkerChan = make([]chan *model.IndexDoc, e.Shard)
Expand Down Expand Up @@ -104,6 +108,8 @@ func (e *Engine) automaticGC() {

func (e *Engine) IndexDocument(doc *model.IndexDoc) {
//根据ID来判断,使用多线程,提速
//数量增加
e.documentCount++
e.addDocumentWorkerChan[e.getShard(doc.Id)] <- doc
}

Expand Down Expand Up @@ -187,6 +193,7 @@ func (e *Engine) AddDocument(index *model.IndexDoc) {

//添加id索引
e.addPositiveIndex(index, splitWords)

}

// 添加倒排索引
Expand Down Expand Up @@ -316,11 +323,24 @@ func (e *Engine) addPositiveIndex(index *model.IndexDoc, keys []string) {
//设置到id和key的映射中
positiveIndexStorage.Set(key, utils.Encoder(keys))
}
func (e *Engine) SearchAll(request *model.SearchRequest) *model.SearchResult {

//获取所有的数据库,然后便利
count := e.GetDocumentCount()
fmt.Println(count)
return nil
}

// MultiSearch 多线程搜索
func (e *Engine) MultiSearch(request *model.SearchRequest) *model.SearchResult {
//等待搜索初始化完成
e.Wait()

//如果搜索词为空,就返回全部数据,不处理排序
if request.Query == "" {
return e.SearchAll(request)
}

//分词搜索
words := e.Tokenizer.Cut(request.Query)

Expand All @@ -336,7 +356,7 @@ func (e *Engine) MultiSearch(request *model.SearchRequest) *model.SearchResult {
wg.Add(base)

for _, word := range words {
go e.processKeySearch(word, fastSort, wg, base)
go e.processKeySearch(word, fastSort, wg)
}
wg.Wait()
})
Expand Down Expand Up @@ -431,7 +451,7 @@ func (e *Engine) getDocument(item model.SliceItem, doc *model.ResponseDoc, reque

}

func (e *Engine) processKeySearch(word string, fastSort *sorts.FastSort, wg *sync.WaitGroup, base int) {
func (e *Engine) processKeySearch(word string, fastSort *sorts.FastSort, wg *sync.WaitGroup) {
defer wg.Done()

shard := e.getShardByWord(word)
Expand Down Expand Up @@ -460,11 +480,23 @@ func (e *Engine) GetIndexCount() int64 {

// GetDocumentCount 获取文档数量
func (e *Engine) GetDocumentCount() int64 {
var count int64
for i := 0; i < e.Shard; i++ {
count += e.docStorages[i].GetCount()
if e.documentCount == -1 {
var count int64
//使用多线程加速统计
wg := sync.WaitGroup{}
wg.Add(e.Shard)
//这里的统计可能会出现数据错误,因为没加锁
for i := 0; i < e.Shard; i++ {
go func(i int) {
count += e.docStorages[i].GetCount()
wg.Done()
}(i)
}
wg.Wait()
e.documentCount = count
}
return count

return e.documentCount
}

// GetDocById 通过id获取文档
Expand Down Expand Up @@ -516,6 +548,9 @@ func (e *Engine) RemoveIndex(id uint32) error {
if err != nil {
return err
}
//减少数量
e.documentCount--

return nil
}

Expand Down
15 changes: 0 additions & 15 deletions searcher/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"bytes"
"compress/flate"
"encoding/binary"
"encoding/gob"
"io/fs"
Expand Down Expand Up @@ -45,20 +44,6 @@ func Decoder(data []byte, v interface{}) {
}
}

//Decompression 解压缩数据
func Decompression(data []byte) []byte {
return DecompressionBuffer(data).Bytes()
}

func DecompressionBuffer(data []byte) *bytes.Buffer {
buf := new(bytes.Buffer)
read := flate.NewReader(bytes.NewReader(data))
defer read.Close()

buf.ReadFrom(read)
return buf
}

const (
c1 = 0xcc9e2d51
c2 = 0x1b873593
Expand Down
12 changes: 2 additions & 10 deletions web/admin/assets/web/src/components/Runtime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,8 @@
</el-row>
<el-divider content-position="left">数据库</el-divider>
<el-row>
<el-col :span="6">数据库数量</el-col>
<el-col :span="18"><el-tag v-text="system.dbs"></el-tag></el-col>

<!-- <el-col :span="6">索引数量</el-col>-->
<!-- <el-col :span="18"><el-tag v-text="system.indexCount"></el-tag></el-col>-->

<!-- <el-col :span="6">文档数量</el-col>-->
<!-- <el-col :span="18"><el-tag v-text="system.documentCount"></el-tag></el-col>-->


<el-col :span="10">数据库数量</el-col>
<el-col :span="14"><el-tag v-text="system.dbs"></el-tag></el-col>
</el-row>

</el-row>
Expand Down

0 comments on commit 64c029b

Please sign in to comment.