From f0293fbc4c8eff15b3d251f9f69e4c2175360ff6 Mon Sep 17 00:00:00 2001 From: hexxa Date: Sat, 30 Jul 2022 20:44:22 +0800 Subject: [PATCH] fix(server): restore and persist file index --- src/db/boltstore/bolt_store.go | 11 +++++------ src/handlers/fileshdr/handlers.go | 9 +-------- src/server/config.go | 2 ++ src/server/server.go | 24 ++++++++++++++++++++++-- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/db/boltstore/bolt_store.go b/src/db/boltstore/bolt_store.go index 9b23094..9087318 100644 --- a/src/db/boltstore/bolt_store.go +++ b/src/db/boltstore/bolt_store.go @@ -310,7 +310,7 @@ func (bs *BoltStore) delShareID(tx *bolt.Tx, itemPath string) error { return nil } -func (bs *BoltStore) DelInfos(userID uint64, itemPath string, isDir bool) error { +func (bs *BoltStore) DelInfos(userID uint64, itemPath string) error { return bs.boltdb.Update(func(tx *bolt.Tx) error { var err error @@ -336,11 +336,10 @@ func (bs *BoltStore) DelInfos(userID uint64, itemPath string, isDir bool) error if err != nil { return err } - if fileInfo.IsDir { - err = bs.delShareID(tx, childPath) - if err != nil { - return err - } + + err = bs.delShareID(tx, childPath) + if err != nil { + return err } } diff --git a/src/handlers/fileshdr/handlers.go b/src/handlers/fileshdr/handlers.go index ad8f86d..96a47c3 100644 --- a/src/handlers/fileshdr/handlers.go +++ b/src/handlers/fileshdr/handlers.go @@ -282,19 +282,13 @@ func (h *FileHandlers) Delete(c *gin.Context) { var txErr error locker := h.NewAutoLocker(c, lockName(filePath)) locker.Exec(func() { - info, err := h.deps.FS().Stat(filePath) - if err != nil { - txErr = err - return - } - err = h.deps.FS().Remove(filePath) if err != nil { txErr = err return } - err = h.deps.BoltStore().DelInfos(userIDInt, filePath, info.IsDir()) + err = h.deps.BoltStore().DelInfos(userIDInt, filePath) if err != nil { txErr = err return @@ -1173,7 +1167,6 @@ func (h *FileHandlers) SearchItems(c *gin.Context) { } for _, searchResult := range searchResults { - fmt.Println(keyword, searchResult) if _, ok := resultsMap[searchResult]; !ok { resultsMap[searchResult] = 0 } diff --git a/src/server/config.go b/src/server/config.go index ad54f8a..b840baf 100644 --- a/src/server/config.go +++ b/src/server/config.go @@ -6,6 +6,8 @@ import ( "github.com/ihexxa/quickshare/src/db" ) +const fileIndexPath = "/fileindex.jsonl" + type DbConfig struct { DbPath string `json:"dbPath" yaml:"dbPath"` } diff --git a/src/server/server.go b/src/server/server.go index b2fa5ae..4891a9d 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -144,10 +144,26 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps { searchResultLimit := cfg.GrabInt("Server.SearchResultLimit") fileIndex := fileindex.NewFileTreeIndex(filesystem, "/", searchResultLimit) + indexInfo, err := filesystem.Stat(fileIndexPath) + if err != nil { + if !os.IsNotExist(err) { + panic(fmt.Sprintf("failed to detect file index: %s", err)) + } else { + logger.Info("warning: no file index found") + } + } else if indexInfo.IsDir() { + panic(fmt.Sprintf("file index is folder, not file: %s", fileIndexPath)) + } else { + err = fileIndex.ReadFrom(fileIndexPath) + if err != nil { + panic(fmt.Sprintf("failed to load file index: %s", err)) + } + } + dbPath := cfg.GrabString("Db.DbPath") dbDir := filepath.Dir(dbPath) - if err := filesystem.MkdirAll(dbDir); err != nil { - panic(fmt.Sprintf("fail to create path for db: %s", err)) + if err = filesystem.MkdirAll(dbDir); err != nil { + panic(fmt.Sprintf("failed to create path for db: %s", err)) } kv := boltdbpvd.New(dbPath, 1024) @@ -393,6 +409,10 @@ func (s *Server) Start() error { func (s *Server) Shutdown() error { // TODO: add timeout + err := s.deps.FileIndex().WriteTo(fileIndexPath) + if err != nil { + s.deps.Log().Errorf("failed to persist file index: %s", err) + } s.deps.Workers().Stop() s.deps.FS().Close() s.deps.Log().Sync()