fix(server): restore and persist file index
This commit is contained in:
parent
591b50c51f
commit
f0293fbc4c
4 changed files with 30 additions and 16 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
"github.com/ihexxa/quickshare/src/db"
|
||||
)
|
||||
|
||||
const fileIndexPath = "/fileindex.jsonl"
|
||||
|
||||
type DbConfig struct {
|
||||
DbPath string `json:"dbPath" yaml:"dbPath"`
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue