diff --git a/core/initialize.go b/core/initialize.go index 06140fa..0f2a19a 100644 --- a/core/initialize.go +++ b/core/initialize.go @@ -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 } diff --git a/searcher/engine.go b/searcher/engine.go index 2adebf4..bbd42e9 100644 --- a/searcher/engine.go +++ b/searcher/engine.go @@ -9,10 +9,8 @@ import ( "gofound/searcher/storage" "gofound/searcher/utils" "gofound/searcher/words" - "io/ioutil" "log" "os" - "path" "runtime" "strings" "sync" @@ -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, @@ -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") } // 处理分页 @@ -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++ { diff --git a/searcher/storage/leveldb_storage.go b/searcher/storage/leveldb_storage.go index b176f94..e5ebd09 100644 --- a/searcher/storage/leveldb_storage.go +++ b/searcher/storage/leveldb_storage.go @@ -5,6 +5,7 @@ import ( "github.com/syndtr/goleveldb/leveldb/filter" "github.com/syndtr/goleveldb/leveldb/opt" "log" + "sync" "time" ) @@ -12,6 +13,7 @@ import ( type LeveldbStorage struct { db *leveldb.DB path string + mu sync.RWMutex //加锁 closed bool timeout int64 lastTime int64 @@ -19,7 +21,7 @@ type LeveldbStorage struct { } func (s *LeveldbStorage) autoOpenDB() { - if s.closed { + if s.isClosed() { s.ReOpen() } s.lastTime = time.Now().Unix() @@ -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) } @@ -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() } @@ -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 } @@ -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() } diff --git a/web/service/index.go b/web/service/index.go index 19e36d1..ead7ff8 100644 --- a/web/service/index.go +++ b/web/service/index.go @@ -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 批次添加索引