Browse Source

:art: 改进索引订正,考虑大小写库

Liang Ding 2 years ago
parent
commit
f84efb9efa
2 changed files with 51 additions and 31 deletions
  1. 47 27
      kernel/model/transaction.go
  2. 4 4
      kernel/sql/block_query.go

+ 47 - 27
kernel/model/transaction.go

@@ -1307,38 +1307,29 @@ func autoFixIndex() {
 
 	// 对比块树和数据库并订正数据库
 	rootUpdatedMap := treenode.GetRootUpdated()
-	dbRootUpdatedMap, err := sql.GetRootUpdated()
+	dbRootUpdatedMap, err := sql.GetRootUpdated("blocks")
 	if nil == err {
-		i := -1
-		size := len(rootUpdatedMap)
-		for rootID, updated := range rootUpdatedMap {
-			i++
-
-			rootUpdated := dbRootUpdatedMap[rootID]
-			if "" == rootUpdated {
-				logging.LogWarnf("not found tree [%s] in database, reindex it", rootID)
-				reindexTree(rootID, i, size)
-				continue
-			}
-
-			if "" == updated {
-				// BlockTree 迁移,v2.6.3 之前没有 updated 字段
-				reindexTree(rootID, i, size)
-				continue
-			}
-
-			btUpdated, _ := time.Parse("20060102150405", updated)
-			dbUpdated, _ := time.Parse("20060102150405", rootUpdated)
-			if dbUpdated.Before(btUpdated.Add(-10 * time.Minute)) {
-				logging.LogWarnf("tree [%s] is not up to date, reindex it", rootID)
-				reindexTree(rootID, i, size)
-				continue
-			}
+		reindexTreeByUpdated(rootUpdatedMap, dbRootUpdatedMap)
+	}
+	dbFtsRootUpdatedMap, err := sql.GetRootUpdated("blocks_fts")
+	if nil == err {
+		reindexTreeByUpdated(rootUpdatedMap, dbFtsRootUpdatedMap)
+	}
+	if !Conf.Search.CaseSensitive {
+		dbFtsRootUpdatedMap, err := sql.GetRootUpdated("blocks_fts_case_insensitive")
+		if nil == err {
+			reindexTreeByUpdated(rootUpdatedMap, dbFtsRootUpdatedMap)
 		}
 	}
 
 	// 去除重复的数据库块记录
-	duplicatedRootIDs := sql.GetDuplicatedRootIDs()
+	duplicatedRootIDs := sql.GetDuplicatedRootIDs("blocks")
+	if 1 > len(duplicatedRootIDs) {
+		duplicatedRootIDs = sql.GetDuplicatedRootIDs("blocks_fts")
+		if 1 > len(duplicatedRootIDs) && !Conf.Search.CaseSensitive {
+			duplicatedRootIDs = sql.GetDuplicatedRootIDs("blocks_fts_case_insensitive")
+		}
+	}
 	size := len(duplicatedRootIDs)
 	for i, rootID := range duplicatedRootIDs {
 		root := sql.GetBlock(rootID)
@@ -1354,6 +1345,35 @@ func autoFixIndex() {
 	util.PushStatusBar(Conf.Language(185))
 }
 
+func reindexTreeByUpdated(rootUpdatedMap, dbRootUpdatedMap map[string]string) {
+	i := -1
+	size := len(rootUpdatedMap)
+	for rootID, updated := range rootUpdatedMap {
+		i++
+
+		rootUpdated := dbRootUpdatedMap[rootID]
+		if "" == rootUpdated {
+			logging.LogWarnf("not found tree [%s] in database, reindex it", rootID)
+			reindexTree(rootID, i, size)
+			continue
+		}
+
+		if "" == updated {
+			// BlockTree 迁移,v2.6.3 之前没有 updated 字段
+			reindexTree(rootID, i, size)
+			continue
+		}
+
+		btUpdated, _ := time.Parse("20060102150405", updated)
+		dbUpdated, _ := time.Parse("20060102150405", rootUpdated)
+		if dbUpdated.Before(btUpdated.Add(-10 * time.Minute)) {
+			logging.LogWarnf("tree [%s] is not up to date, reindex it", rootID)
+			reindexTree(rootID, i, size)
+			continue
+		}
+	}
+}
+
 func reindexTreeByPath(box, p string, i, size int) {
 	tree, err := LoadTree(box, p)
 	if nil != err {

+ 4 - 4
kernel/sql/block_query.go

@@ -597,8 +597,8 @@ func GetBlock(id string) (ret *Block) {
 	return
 }
 
-func GetRootUpdated() (ret map[string]string, err error) {
-	rows, err := query("SELECT root_id, updated FROM blocks WHERE type = 'd'")
+func GetRootUpdated(blocksTable string) (ret map[string]string, err error) {
+	rows, err := query("SELECT root_id, updated FROM `" + blocksTable + "` WHERE type = 'd'")
 	if nil != err {
 		logging.LogErrorf("sql query failed: %s", err)
 		return
@@ -614,8 +614,8 @@ func GetRootUpdated() (ret map[string]string, err error) {
 	return
 }
 
-func GetDuplicatedRootIDs() (ret []string) {
-	rows, err := query("SELECT DISTINCT root_id FROM blocks GROUP BY id HAVING COUNT(*) > 1")
+func GetDuplicatedRootIDs(blocksTable string) (ret []string) {
+	rows, err := query("SELECT DISTINCT root_id FROM `" + blocksTable + "` GROUP BY id HAVING COUNT(*) > 1")
 	if nil != err {
 		logging.LogErrorf("sql query failed: %s", err)
 		return