mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01:00
🎨 改进同步后全量重建索引判断 Fix https://github.com/siyuan-note/siyuan/issues/5764
This commit is contained in:
parent
955bc042e6
commit
5139e51165
4 changed files with 39 additions and 8 deletions
|
|
@ -480,6 +480,7 @@ func RefreshFileTree() {
|
||||||
util.PushErrMsg(fmt.Sprintf(Conf.Language(85), err), 5000)
|
util.PushErrMsg(fmt.Sprintf(Conf.Language(85), err), 5000)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
treenode.InitBlockTree(true)
|
||||||
|
|
||||||
util.PushEndlessProgress(Conf.Language(35))
|
util.PushEndlessProgress(Conf.Language(35))
|
||||||
openedBoxes := Conf.GetOpenedBoxes()
|
openedBoxes := Conf.GetOpenedBoxes()
|
||||||
|
|
|
||||||
|
|
@ -502,7 +502,7 @@ func InitBoxes() {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
treenode.InitBlockTree()
|
treenode.InitBlockTree(false)
|
||||||
initialized = true
|
initialized = true
|
||||||
}
|
}
|
||||||
} else { // 大于 1 的话说明在同步阶段已经加载过了
|
} else { // 大于 1 的话说明在同步阶段已经加载过了
|
||||||
|
|
|
||||||
|
|
@ -497,7 +497,7 @@ func syncRepo(boot, exit, byHand bool) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
indexBeforeSync, err := indexRepoBeforeCloudSync(repo)
|
err = indexRepoBeforeCloudSync(repo)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
syncDownloadErrCount++
|
syncDownloadErrCount++
|
||||||
planSyncAfter(fixSyncInterval)
|
planSyncAfter(fixSyncInterval)
|
||||||
|
|
@ -592,21 +592,24 @@ func syncRepo(boot, exit, byHand bool) (err error) {
|
||||||
|
|
||||||
// 有数据变更,需要重建索引
|
// 有数据变更,需要重建索引
|
||||||
var upserts, removes []string
|
var upserts, removes []string
|
||||||
|
var upsertTrees int
|
||||||
for _, file := range mergeResult.Upserts {
|
for _, file := range mergeResult.Upserts {
|
||||||
upserts = append(upserts, file.Path)
|
upserts = append(upserts, file.Path)
|
||||||
|
if strings.HasSuffix(file.Path, ".sy") {
|
||||||
|
upsertTrees++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for _, file := range mergeResult.Removes {
|
for _, file := range mergeResult.Removes {
|
||||||
removes = append(removes, file.Path)
|
removes = append(removes, file.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
if boot && gulu.File.IsExist(util.BlockTreePath) {
|
if boot && gulu.File.IsExist(util.BlockTreePath) {
|
||||||
treenode.InitBlockTree()
|
treenode.InitBlockTree(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.ClearDocsIAL() // 同步后文档树文档图标没有更新 https://github.com/siyuan-note/siyuan/issues/4939
|
cache.ClearDocsIAL() // 同步后文档树文档图标没有更新 https://github.com/siyuan-note/siyuan/issues/4939
|
||||||
|
|
||||||
fullReindex := 0.2 < float64(len(upserts))/float64(len(indexBeforeSync.Files))
|
if needFullReindex(upsertTrees) { // 改进同步后全量重建索引判断 https://github.com/siyuan-note/siyuan/issues/5764
|
||||||
if fullReindex { // 如果更新的文件比较多则全量重建索引
|
|
||||||
RefreshFileTree()
|
RefreshFileTree()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -626,10 +629,14 @@ func syncRepo(boot, exit, byHand bool) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func indexRepoBeforeCloudSync(repo *dejavu.Repo) (index *entity.Index, err error) {
|
func needFullReindex(upsertTrees int) bool {
|
||||||
|
return 0.2 < float64(upsertTrees)/float64(treenode.CountTrees())
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexRepoBeforeCloudSync(repo *dejavu.Repo) (err error) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
latest, _ := repo.Latest()
|
latest, _ := repo.Latest()
|
||||||
index, err = repo.Index("[Sync] Cloud sync", map[string]interface{}{
|
index, err := repo.Index("[Sync] Cloud sync", map[string]interface{}{
|
||||||
dejavu.CtxPushMsg: dejavu.CtxPushMsgToStatusBar,
|
dejavu.CtxPushMsg: dejavu.CtxPushMsgToStatusBar,
|
||||||
})
|
})
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,15 @@ type BlockTree struct {
|
||||||
HPath string // 文档逻辑路径
|
HPath string // 文档逻辑路径
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CountTrees() (ret int) {
|
||||||
|
roots := map[string]bool{}
|
||||||
|
for _, b := range blockTrees {
|
||||||
|
roots[b.RootID] = true
|
||||||
|
}
|
||||||
|
ret = len(roots)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func GetBlockTrees() map[string]*BlockTree {
|
func GetBlockTrees() map[string]*BlockTree {
|
||||||
return blockTrees
|
return blockTrees
|
||||||
}
|
}
|
||||||
|
|
@ -218,11 +227,24 @@ func AutoFlushBlockTree() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitBlockTree() {
|
func InitBlockTree(force bool) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
if nil == blocktreeFileLock {
|
if nil == blocktreeFileLock {
|
||||||
blocktreeFileLock = flock.New(util.BlockTreePath)
|
blocktreeFileLock = flock.New(util.BlockTreePath)
|
||||||
|
} else {
|
||||||
|
if force {
|
||||||
|
err := blocktreeFileLock.Unlock()
|
||||||
|
if nil != err {
|
||||||
|
logging.LogErrorf("unlock blocktree file failed: %s", err)
|
||||||
|
}
|
||||||
|
err = os.RemoveAll(util.BlockTreePath)
|
||||||
|
if nil != err {
|
||||||
|
logging.LogErrorf("remove blocktree file failed: %s", err)
|
||||||
|
}
|
||||||
|
blocktreeFileLock = flock.New(util.BlockTreePath)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
@ -247,6 +269,7 @@ func InitBlockTree() {
|
||||||
blockTreesLock.Lock()
|
blockTreesLock.Lock()
|
||||||
if err = msgpack.Unmarshal(data, &blockTrees); nil != err {
|
if err = msgpack.Unmarshal(data, &blockTrees); nil != err {
|
||||||
logging.LogErrorf("unmarshal block tree failed: %s", err)
|
logging.LogErrorf("unmarshal block tree failed: %s", err)
|
||||||
|
blocktreeFileLock.Unlock()
|
||||||
if err = os.RemoveAll(util.BlockTreePath); nil != err {
|
if err = os.RemoveAll(util.BlockTreePath); nil != err {
|
||||||
logging.LogErrorf("removed corrupted block tree failed: %s", err)
|
logging.LogErrorf("removed corrupted block tree failed: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue