Browse Source

:art: Improve boot index checking https://github.com/siyuan-note/siyuan/issues/11842

Daniel 1 year ago
parent
commit
ed5c4738f9
5 changed files with 38 additions and 22 deletions
  1. 1 1
      kernel/go.mod
  2. 2 2
      kernel/go.sum
  3. 4 5
      kernel/model/conf.go
  4. 10 0
      kernel/model/index.go
  5. 21 14
      kernel/sql/queue.go

+ 1 - 1
kernel/go.mod

@@ -54,7 +54,7 @@ require (
 	github.com/shirou/gopsutil/v3 v3.24.5
 	github.com/shirou/gopsutil/v3 v3.24.5
 	github.com/siyuan-note/dejavu v0.0.0-20240626145351-e4f9b0821aaf
 	github.com/siyuan-note/dejavu v0.0.0-20240626145351-e4f9b0821aaf
 	github.com/siyuan-note/encryption v0.0.0-20231219001248-1e028a4d13b4
 	github.com/siyuan-note/encryption v0.0.0-20231219001248-1e028a4d13b4
-	github.com/siyuan-note/eventbus v0.0.0-20240318125243-801c98e8f025
+	github.com/siyuan-note/eventbus v0.0.0-20240627125516-396fdb0f0f97
 	github.com/siyuan-note/filelock v0.0.0-20240505150405-3de3a406d257
 	github.com/siyuan-note/filelock v0.0.0-20240505150405-3de3a406d257
 	github.com/siyuan-note/httpclient v0.0.0-20240626145026-29585d45a51c
 	github.com/siyuan-note/httpclient v0.0.0-20240626145026-29585d45a51c
 	github.com/siyuan-note/logging v0.0.0-20240505035402-6430d57006a2
 	github.com/siyuan-note/logging v0.0.0-20240505035402-6430d57006a2

+ 2 - 2
kernel/go.sum

@@ -357,8 +357,8 @@ github.com/siyuan-note/dejavu v0.0.0-20240626145351-e4f9b0821aaf h1:rul3+iFekIhj
 github.com/siyuan-note/dejavu v0.0.0-20240626145351-e4f9b0821aaf/go.mod h1:fEgyP8xD6Do5ogMn9hFCz0e4IQQVamwM2uX96bOCpPQ=
 github.com/siyuan-note/dejavu v0.0.0-20240626145351-e4f9b0821aaf/go.mod h1:fEgyP8xD6Do5ogMn9hFCz0e4IQQVamwM2uX96bOCpPQ=
 github.com/siyuan-note/encryption v0.0.0-20231219001248-1e028a4d13b4 h1:kJaw5L/evyW6LcB9IQT8PR4ppx8JVqOFP9Ix3rfwSrc=
 github.com/siyuan-note/encryption v0.0.0-20231219001248-1e028a4d13b4 h1:kJaw5L/evyW6LcB9IQT8PR4ppx8JVqOFP9Ix3rfwSrc=
 github.com/siyuan-note/encryption v0.0.0-20231219001248-1e028a4d13b4/go.mod h1:UYcCCY+0wh+GmUoDOaO63j1sV5lgy7laLAk1XhEiUis=
 github.com/siyuan-note/encryption v0.0.0-20231219001248-1e028a4d13b4/go.mod h1:UYcCCY+0wh+GmUoDOaO63j1sV5lgy7laLAk1XhEiUis=
-github.com/siyuan-note/eventbus v0.0.0-20240318125243-801c98e8f025 h1:crQmKw5vZz3MljAP/xKMWY/rksMX634jJK9kcsOIbkw=
-github.com/siyuan-note/eventbus v0.0.0-20240318125243-801c98e8f025/go.mod h1:1/nGgthl89FPA7GzAcEWKl6zRRnfgyTjzLZj9bW7kuw=
+github.com/siyuan-note/eventbus v0.0.0-20240627125516-396fdb0f0f97 h1:lM5v8BfNtbOL5jYwhCdMYBcYtr06IYBKjjSLAPMKTM8=
+github.com/siyuan-note/eventbus v0.0.0-20240627125516-396fdb0f0f97/go.mod h1:1/nGgthl89FPA7GzAcEWKl6zRRnfgyTjzLZj9bW7kuw=
 github.com/siyuan-note/filelock v0.0.0-20240505150405-3de3a406d257 h1:lA6qBehxciYKye8LpP59LHGC8DQkkJkg7AYA4VoF+g8=
 github.com/siyuan-note/filelock v0.0.0-20240505150405-3de3a406d257 h1:lA6qBehxciYKye8LpP59LHGC8DQkkJkg7AYA4VoF+g8=
 github.com/siyuan-note/filelock v0.0.0-20240505150405-3de3a406d257/go.mod h1:vUXGLmTVIg5trcnNWGkMCwc3EuIJaXXx3hQzgno1Hn4=
 github.com/siyuan-note/filelock v0.0.0-20240505150405-3de3a406d257/go.mod h1:vUXGLmTVIg5trcnNWGkMCwc3EuIJaXXx3hQzgno1Hn4=
 github.com/siyuan-note/httpclient v0.0.0-20240626145026-29585d45a51c h1:E6W4x2GL+7jHsfkPEldvosWNl7hxtRHJjgGVhHHywn4=
 github.com/siyuan-note/httpclient v0.0.0-20240626145026-29585d45a51c h1:E6W4x2GL+7jHsfkPEldvosWNl7hxtRHJjgGVhHHywn4=

+ 4 - 5
kernel/model/conf.go

@@ -81,7 +81,7 @@ type AppConf struct {
 	ShowChangelog  bool             `json:"showChangelog"`  // 是否显示版本更新日志
 	ShowChangelog  bool             `json:"showChangelog"`  // 是否显示版本更新日志
 	CloudRegion    int              `json:"cloudRegion"`    // 云端区域,0:中国大陆,1:北美
 	CloudRegion    int              `json:"cloudRegion"`    // 云端区域,0:中国大陆,1:北美
 	Snippet        *conf.Snpt       `json:"snippet"`        // 代码片段
 	Snippet        *conf.Snpt       `json:"snippet"`        // 代码片段
-	State          int              `json:"state"`          // 运行状态,0:已经正常退出,1:运行中
+	DataIndexState int              `json:"dataIndexState"` // 数据索引状态,0:已索引,1:未索引
 
 
 	m *sync.Mutex
 	m *sync.Mutex
 }
 }
@@ -464,8 +464,8 @@ func InitConf() {
 
 
 	Conf.LocalIPs = util.GetLocalIPs()
 	Conf.LocalIPs = util.GetLocalIPs()
 
 
-	if 1 == Conf.State {
-		// 上次未正常退出
+	if 1 == Conf.DataIndexState {
+		// 上次未正常完成数据索引
 		go func() {
 		go func() {
 			util.WaitForUILoaded()
 			util.WaitForUILoaded()
 			time.Sleep(2 * time.Second)
 			time.Sleep(2 * time.Second)
@@ -477,7 +477,7 @@ func InitConf() {
 		}()
 		}()
 	}
 	}
 
 
-	Conf.State = 1 // 运行中
+	Conf.DataIndexState = 0
 
 
 	Conf.Save()
 	Conf.Save()
 	logging.SetLogLevel(Conf.LogLevel)
 	logging.SetLogLevel(Conf.LogLevel)
@@ -716,7 +716,6 @@ func (conf *AppConf) save0(data []byte) {
 }
 }
 
 
 func (conf *AppConf) Close() {
 func (conf *AppConf) Close() {
-	conf.State = 0 // 已经正常退出
 	conf.Save()
 	conf.Save()
 }
 }
 
 

+ 10 - 0
kernel/model/index.go

@@ -429,4 +429,14 @@ func subscribeSQLEvents() {
 		util.SetBootDetails(msg)
 		util.SetBootDetails(msg)
 		util.ContextPushMsg(context, msg)
 		util.ContextPushMsg(context, msg)
 	})
 	})
+
+	eventbus.Subscribe(eventbus.EvtSQLIndexChanged, func() {
+		Conf.DataIndexState = 1
+		Conf.Save()
+	})
+
+	eventbus.Subscribe(eventbus.EvtSQLIndexFlushed, func() {
+		Conf.DataIndexState = 0
+		Conf.Save()
+	})
 }
 }

+ 21 - 14
kernel/sql/queue.go

@@ -164,6 +164,8 @@ func FlushQueue() {
 
 
 	// Push database index commit event https://github.com/siyuan-note/siyuan/issues/8814
 	// Push database index commit event https://github.com/siyuan-note/siyuan/issues/8814
 	util.BroadcastByType("main", "databaseIndexCommit", 0, "", nil)
 	util.BroadcastByType("main", "databaseIndexCommit", 0, "", nil)
+
+	eventbus.Publish(eventbus.EvtSQLIndexFlushed)
 }
 }
 
 
 func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]interface{}) (err error) {
 func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]interface{}) (err error) {
@@ -219,7 +221,7 @@ func IndexNodeQueue(id string) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func BatchRemoveAssetsQueue(hashes []string) {
 func BatchRemoveAssetsQueue(hashes []string) {
@@ -231,7 +233,7 @@ func BatchRemoveAssetsQueue(hashes []string) {
 	defer dbQueueLock.Unlock()
 	defer dbQueueLock.Unlock()
 
 
 	newOp := &dbQueueOperation{removeAssetHashes: hashes, inQueueTime: time.Now(), action: "delete_assets"}
 	newOp := &dbQueueOperation{removeAssetHashes: hashes, inQueueTime: time.Now(), action: "delete_assets"}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func UpdateBlockContentQueue(block *Block) {
 func UpdateBlockContentQueue(block *Block) {
@@ -245,7 +247,7 @@ func UpdateBlockContentQueue(block *Block) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func DeleteRefsTreeQueue(tree *parse.Tree) {
 func DeleteRefsTreeQueue(tree *parse.Tree) {
@@ -259,7 +261,7 @@ func DeleteRefsTreeQueue(tree *parse.Tree) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func UpdateRefsTreeQueue(tree *parse.Tree) {
 func UpdateRefsTreeQueue(tree *parse.Tree) {
@@ -273,7 +275,7 @@ func UpdateRefsTreeQueue(tree *parse.Tree) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func DeleteBoxRefsQueue(boxID string) {
 func DeleteBoxRefsQueue(boxID string) {
@@ -287,7 +289,7 @@ func DeleteBoxRefsQueue(boxID string) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func DeleteBoxQueue(boxID string) {
 func DeleteBoxQueue(boxID string) {
@@ -301,7 +303,7 @@ func DeleteBoxQueue(boxID string) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func IndexTreeQueue(tree *parse.Tree) {
 func IndexTreeQueue(tree *parse.Tree) {
@@ -315,7 +317,7 @@ func IndexTreeQueue(tree *parse.Tree) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func UpsertTreeQueue(tree *parse.Tree) {
 func UpsertTreeQueue(tree *parse.Tree) {
@@ -329,7 +331,7 @@ func UpsertTreeQueue(tree *parse.Tree) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func RenameTreeQueue(tree *parse.Tree) {
 func RenameTreeQueue(tree *parse.Tree) {
@@ -347,7 +349,7 @@ func RenameTreeQueue(tree *parse.Tree) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func RenameSubTreeQueue(tree *parse.Tree) {
 func RenameSubTreeQueue(tree *parse.Tree) {
@@ -365,7 +367,7 @@ func RenameSubTreeQueue(tree *parse.Tree) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func RemoveTreeQueue(rootID string) {
 func RemoveTreeQueue(rootID string) {
@@ -379,7 +381,7 @@ func RemoveTreeQueue(rootID string) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func BatchRemoveTreeQueue(rootIDs []string) {
 func BatchRemoveTreeQueue(rootIDs []string) {
@@ -391,7 +393,7 @@ func BatchRemoveTreeQueue(rootIDs []string) {
 	defer dbQueueLock.Unlock()
 	defer dbQueueLock.Unlock()
 
 
 	newOp := &dbQueueOperation{removeTreeIDs: rootIDs, inQueueTime: time.Now(), action: "delete_ids"}
 	newOp := &dbQueueOperation{removeTreeIDs: rootIDs, inQueueTime: time.Now(), action: "delete_ids"}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func RemoveTreePathQueue(treeBox, treePathPrefix string) {
 func RemoveTreePathQueue(treeBox, treePathPrefix string) {
@@ -405,7 +407,7 @@ func RemoveTreePathQueue(treeBox, treePathPrefix string) {
 			return
 			return
 		}
 		}
 	}
 	}
-	operationQueue = append(operationQueue, newOp)
+	appendOperation(newOp)
 }
 }
 
 
 func getOperations() (ops []*dbQueueOperation) {
 func getOperations() (ops []*dbQueueOperation) {
@@ -416,3 +418,8 @@ func getOperations() (ops []*dbQueueOperation) {
 	operationQueue = nil
 	operationQueue = nil
 	return
 	return
 }
 }
+
+func appendOperation(op *dbQueueOperation) {
+	operationQueue = append(operationQueue, op)
+	eventbus.Publish(eventbus.EvtSQLIndexChanged)
+}