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
|
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 {
|
return bs.boltdb.Update(func(tx *bolt.Tx) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -336,11 +336,10 @@ func (bs *BoltStore) DelInfos(userID uint64, itemPath string, isDir bool) error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if fileInfo.IsDir {
|
|
||||||
err = bs.delShareID(tx, childPath)
|
err = bs.delShareID(tx, childPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -282,19 +282,13 @@ func (h *FileHandlers) Delete(c *gin.Context) {
|
||||||
var txErr error
|
var txErr error
|
||||||
locker := h.NewAutoLocker(c, lockName(filePath))
|
locker := h.NewAutoLocker(c, lockName(filePath))
|
||||||
locker.Exec(func() {
|
locker.Exec(func() {
|
||||||
info, err := h.deps.FS().Stat(filePath)
|
|
||||||
if err != nil {
|
|
||||||
txErr = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = h.deps.FS().Remove(filePath)
|
err = h.deps.FS().Remove(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
txErr = err
|
txErr = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.deps.BoltStore().DelInfos(userIDInt, filePath, info.IsDir())
|
err = h.deps.BoltStore().DelInfos(userIDInt, filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
txErr = err
|
txErr = err
|
||||||
return
|
return
|
||||||
|
@ -1173,7 +1167,6 @@ func (h *FileHandlers) SearchItems(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, searchResult := range searchResults {
|
for _, searchResult := range searchResults {
|
||||||
fmt.Println(keyword, searchResult)
|
|
||||||
if _, ok := resultsMap[searchResult]; !ok {
|
if _, ok := resultsMap[searchResult]; !ok {
|
||||||
resultsMap[searchResult] = 0
|
resultsMap[searchResult] = 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"github.com/ihexxa/quickshare/src/db"
|
"github.com/ihexxa/quickshare/src/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const fileIndexPath = "/fileindex.jsonl"
|
||||||
|
|
||||||
type DbConfig struct {
|
type DbConfig struct {
|
||||||
DbPath string `json:"dbPath" yaml:"dbPath"`
|
DbPath string `json:"dbPath" yaml:"dbPath"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,10 +144,26 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
|
||||||
searchResultLimit := cfg.GrabInt("Server.SearchResultLimit")
|
searchResultLimit := cfg.GrabInt("Server.SearchResultLimit")
|
||||||
fileIndex := fileindex.NewFileTreeIndex(filesystem, "/", 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")
|
dbPath := cfg.GrabString("Db.DbPath")
|
||||||
dbDir := filepath.Dir(dbPath)
|
dbDir := filepath.Dir(dbPath)
|
||||||
if err := filesystem.MkdirAll(dbDir); err != nil {
|
if err = filesystem.MkdirAll(dbDir); err != nil {
|
||||||
panic(fmt.Sprintf("fail to create path for db: %s", err))
|
panic(fmt.Sprintf("failed to create path for db: %s", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
kv := boltdbpvd.New(dbPath, 1024)
|
kv := boltdbpvd.New(dbPath, 1024)
|
||||||
|
@ -393,6 +409,10 @@ func (s *Server) Start() error {
|
||||||
|
|
||||||
func (s *Server) Shutdown() error {
|
func (s *Server) Shutdown() error {
|
||||||
// TODO: add timeout
|
// 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.Workers().Stop()
|
||||||
s.deps.FS().Close()
|
s.deps.FS().Close()
|
||||||
s.deps.Log().Sync()
|
s.deps.Log().Sync()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue