This commit is contained in:
Liang Ding 2023-01-08 22:47:43 +08:00
parent dc4b5accfb
commit 88f716b5d6
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
7 changed files with 90 additions and 48 deletions

View file

@ -523,30 +523,6 @@ func genTreeID(tree *parse.Tree) {
return
}
func ReindexTree(path string) (err error) {
if !strings.HasPrefix(path, "/data/") {
return errors.New("path must start with /data/")
}
part := strings.TrimPrefix(path, "/data/")
idx := strings.Index(part, "/")
if 0 > idx {
return errors.New("parse box failed")
}
box := part[:idx]
p := strings.TrimPrefix(path, "/data/"+box)
tree, err := LoadTree(box, p)
if nil != err {
return
}
treenode.ReindexBlockTree(tree)
sql.UpsertTreeQueue(tree)
sql.WaitForWritingDatabase()
return
}
func FullReindex() {
util.PushEndlessProgress(Conf.Language(35))
WaitForWritingFiles()

View file

@ -1217,3 +1217,57 @@ func updateRefText(refNode *ast.Node, changedDefNodes map[string]*ast.Node) (cha
})
return
}
func AutoFixIndex() {
for {
autoFixIndex()
time.Sleep(30 * time.Second)
}
}
func autoFixIndex() {
rootUpdated := treenode.GetRootUpdated()
for rootID, updated := range rootUpdated {
root := sql.GetBlock(rootID)
if nil == root {
reindexTree(rootID)
continue
}
if "" == updated {
reindexTree(rootID)
continue
}
btUpdated, _ := time.Parse("20060102150405", updated)
dbUpdated, _ := time.Parse("20060102150405", root.Updated)
if dbUpdated.Before(btUpdated.Add(-1 * time.Minute)) {
reindexTree(rootID)
continue
}
roots := sql.GetBlockRedundant(rootID)
if 1 < len(roots) {
reindexTree(rootID)
continue
}
}
}
func reindexTree(rootID string) {
root := treenode.GetBlockTree(rootID)
if nil == root {
logging.LogWarnf("root block not found", rootID)
return
}
tree, err := LoadTree(root.BoxID, root.Path)
if nil != err {
return
}
treenode.ReindexBlockTree(tree)
sql.UpsertTreeQueue(tree)
logging.LogInfof("reindex tree [%s]", tree.ID)
}