Skip to content

Commit

Permalink
error process during init, bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfm committed May 19, 2022
1 parent c56a67e commit 5ff4df8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
4 changes: 3 additions & 1 deletion core/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func NewContainer(tokenizer *words.Tokenizer) *searcher.Container {
Shard: global.CONFIG.Shard,
Timeout: global.CONFIG.Timeout,
}
go container.Init()
if err := container.Init(); err != nil {
panic(err)
}

return container
}
Expand Down
15 changes: 1 addition & 14 deletions searcher/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (
"gofound/searcher/storage"
"gofound/searcher/utils"
"gofound/searcher/words"
"io/ioutil"
"log"
"os"
"path"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -326,8 +324,6 @@ func (e *Engine) MultiSearch(request *model.SearchRequest) *model.SearchResult {
//分词搜索
words := e.Tokenizer.Cut(request.Query)

totalTime := float64(0)

fastSort := &sorts.FastSort{
IsDebug: e.IsDebug,
Order: request.Order,
Expand All @@ -345,7 +341,6 @@ func (e *Engine) MultiSearch(request *model.SearchRequest) *model.SearchResult {
wg.Wait()
})
if e.IsDebug {
log.Println("数组查找耗时:", totalTime, "ms")
log.Println("搜索时间:", _time, "ms")
}
// 处理分页
Expand Down Expand Up @@ -539,17 +534,9 @@ func (e *Engine) Drop() error {
e.Lock()
defer e.Unlock()
//删除文件
dir, err := ioutil.ReadDir(e.IndexPath)
if err != nil {
if err := os.RemoveAll(e.IndexPath); err != nil {
return err
}
for _, d := range dir {
err := os.RemoveAll(path.Join([]string{d.Name()}...))
if err != nil {
return err
}
os.Remove(e.IndexPath)
}

//清空内存
for i := 0; i < e.Shard; i++ {
Expand Down
22 changes: 15 additions & 7 deletions searcher/storage/leveldb_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import (
"github.com/syndtr/goleveldb/leveldb/filter"
"github.com/syndtr/goleveldb/leveldb/opt"
"log"
"sync"
"time"
)

// LeveldbStorage TODO 要支持事务
type LeveldbStorage struct {
db *leveldb.DB
path string
mu sync.RWMutex //加锁
closed bool
timeout int64
lastTime int64
count int64
}

func (s *LeveldbStorage) autoOpenDB() {
if s.closed {
if s.isClosed() {
s.ReOpen()
}
s.lastTime = time.Now().Unix()
Expand Down Expand Up @@ -47,7 +49,7 @@ func (s *LeveldbStorage) task() {
}
for {

if !s.closed && time.Now().Unix()-s.lastTime > s.timeout {
if !s.isClosed() && time.Now().Unix()-s.lastTime > s.timeout {
s.Close()
//log.Println("leveldb storage timeout", s.path)
}
Expand All @@ -68,17 +70,18 @@ func openDB(path string) (*leveldb.DB, error) {
return db, err
}
func (s *LeveldbStorage) ReOpen() {
if !s.closed {
if !s.isClosed() {
log.Println("db is not closed")
return
}
s.mu.Lock()
db, err := openDB(s.path)
if err != nil {
return
panic(err)
}
s.db = db
s.closed = false

s.mu.Unlock()
//计算总条数
go s.compute()
}
Expand Down Expand Up @@ -117,17 +120,22 @@ func (s *LeveldbStorage) Delete(key []byte) error {

// Close 关闭
func (s *LeveldbStorage) Close() error {
if s.closed {
if s.isClosed() {
return nil
}
s.mu.Lock()
err := s.db.Close()
if err != nil {
return err
}
s.closed = true
s.mu.Unlock()
return nil
}

func (s *LeveldbStorage) isClosed() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.closed
}

Expand All @@ -142,7 +150,7 @@ func (s *LeveldbStorage) compute() {
}

func (s *LeveldbStorage) GetCount() int64 {
if s.count == 0 && s.closed {
if s.count == 0 && s.isClosed() {
s.ReOpen()
s.compute()
}
Expand Down
2 changes: 1 addition & 1 deletion web/service/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewIndex() *Index {

// AddIndex 添加索引
func (i *Index) AddIndex(dbName string, request *model.IndexDoc) {
go i.Container.GetDataBase(dbName).IndexDocument(request)
i.Container.GetDataBase(dbName).IndexDocument(request)
}

// BatchAddIndex 批次添加索引
Expand Down

0 comments on commit 5ff4df8

Please sign in to comment.