This commit is contained in:
Liang Ding 2023-01-09 10:25:50 +08:00
parent d7090839e7
commit c631b64d00
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 36 additions and 16 deletions

View file

@ -1237,18 +1237,21 @@ func autoFixIndex() {
autoFixLock.Lock() autoFixLock.Lock()
defer autoFixLock.Unlock() defer autoFixLock.Unlock()
rootUpdated := treenode.GetRootUpdated() rootUpdatedMap := treenode.GetRootUpdated()
i := -1 i := -1
size := len(rootUpdated) size := len(rootUpdatedMap)
for rootID, updated := range rootUpdated { for rootID, updated := range rootUpdatedMap {
if isFullReindexing { if isFullReindexing {
break break
} }
i++ i++
root := sql.GetBlock(rootID) rootUpdated, err := sql.GetRootUpdated(rootID)
if nil == root { if nil != err {
continue
}
if "" == rootUpdated {
logging.LogWarnf("not found tree [%s] in database, reindex it", rootID) logging.LogWarnf("not found tree [%s] in database, reindex it", rootID)
reindexTree(rootID, i, size) reindexTree(rootID, i, size)
continue continue
@ -1261,20 +1264,24 @@ func autoFixIndex() {
} }
btUpdated, _ := time.Parse("20060102150405", updated) btUpdated, _ := time.Parse("20060102150405", updated)
dbUpdated, _ := time.Parse("20060102150405", root.Updated) dbUpdated, _ := time.Parse("20060102150405", rootUpdated)
if dbUpdated.Before(btUpdated.Add(-1 * time.Minute)) { if dbUpdated.Before(btUpdated.Add(-1 * time.Minute)) {
logging.LogWarnf("tree [%s] is not up to date, reindex it", rootID) logging.LogWarnf("tree [%s] is not up to date, reindex it", rootID)
reindexTree(rootID, i, size) reindexTree(rootID, i, size)
continue continue
} }
}
duplicatedRootIDs := sql.GetDuplicatedRootIDs()
for _, rootID := range duplicatedRootIDs {
root := sql.GetBlock(rootID)
if nil == root {
continue
}
roots := sql.GetBlockRedundant(rootID)
if 1 < len(roots) {
logging.LogWarnf("exist more than one tree [%s], reindex it", rootID) logging.LogWarnf("exist more than one tree [%s], reindex it", rootID)
sql.RemoveTreeQueue(root.Box, rootID) sql.RemoveTreeQueue(root.Box, rootID)
reindexTree(rootID, i, size) reindexTree(rootID, i, size)
continue
}
} }
} }

View file

@ -581,17 +581,30 @@ func GetBlock(id string) (ret *Block) {
return return
} }
func GetBlockRedundant(id string) (ret []*Block) { func GetRootUpdated(rootID string) (ret string, err error) {
rows, err := query("SELECT * FROM blocks WHERE id = ?", id) rows, err := query("SELECT updated FROM blocks WHERE root_id = ? AND type = 'd'", rootID)
if nil != err { if nil != err {
logging.LogErrorf("sql query failed: %s", err) logging.LogErrorf("sql query failed: %s", err)
return return
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
if block := scanBlockRows(rows); nil != block { rows.Scan(&ret)
ret = append(ret, block)
} }
return
}
func GetDuplicatedRootIDs() (ret []string) {
rows, err := query("SELECT DISTINCT root_id FROM blocks GROUP BY id HAVING COUNT(*) > 1")
if nil != err {
logging.LogErrorf("sql query failed: %s", err)
return
}
defer rows.Close()
for rows.Next() {
var id string
rows.Scan(&id)
ret = append(ret, id)
} }
return return
} }