queue_asset_content.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package sql
  17. import (
  18. "database/sql"
  19. "errors"
  20. "fmt"
  21. "runtime/debug"
  22. "sync"
  23. "time"
  24. "github.com/siyuan-note/eventbus"
  25. "github.com/siyuan-note/logging"
  26. "github.com/siyuan-note/siyuan/kernel/task"
  27. "github.com/siyuan-note/siyuan/kernel/util"
  28. )
  29. var (
  30. assetContentOperationQueue []*assetContentDBQueueOperation
  31. assetContentDBQueueLock = sync.Mutex{}
  32. assetContentTxLock = sync.Mutex{}
  33. )
  34. type assetContentDBQueueOperation struct {
  35. inQueueTime time.Time
  36. action string // index/deletePath
  37. assetContents []*AssetContent // index
  38. path string // deletePath
  39. }
  40. func FlushAssetContentTxJob() {
  41. task.AppendTask(task.AssetContentDatabaseIndexCommit, FlushAssetContentQueue)
  42. }
  43. func FlushAssetContentQueue() {
  44. ops := getAssetContentOperations()
  45. total := len(ops)
  46. if 1 > total {
  47. return
  48. }
  49. assetContentTxLock.Lock()
  50. defer assetContentTxLock.Unlock()
  51. start := time.Now()
  52. groupOpsTotal := map[string]int{}
  53. for _, op := range ops {
  54. groupOpsTotal[op.action]++
  55. }
  56. context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}
  57. groupOpsCurrent := map[string]int{}
  58. for i, op := range ops {
  59. if util.IsExiting.Load() {
  60. return
  61. }
  62. tx, err := beginAssetContentTx()
  63. if nil != err {
  64. return
  65. }
  66. groupOpsCurrent[op.action]++
  67. context["current"] = groupOpsCurrent[op.action]
  68. context["total"] = groupOpsTotal[op.action]
  69. if err = execAssetContentOp(op, tx, context); nil != err {
  70. tx.Rollback()
  71. logging.LogErrorf("queue operation failed: %s", err)
  72. eventbus.Publish(util.EvtSQLAssetContentRebuild)
  73. return
  74. }
  75. if err = commitAssetContentTx(tx); nil != err {
  76. logging.LogErrorf("commit tx failed: %s", err)
  77. return
  78. }
  79. if 16 < i && 0 == i%128 {
  80. debug.FreeOSMemory()
  81. }
  82. }
  83. if 128 < total {
  84. debug.FreeOSMemory()
  85. }
  86. elapsed := time.Now().Sub(start).Milliseconds()
  87. if 7000 < elapsed {
  88. logging.LogInfof("database asset content op tx [%dms]", elapsed)
  89. }
  90. }
  91. func execAssetContentOp(op *assetContentDBQueueOperation, tx *sql.Tx, context map[string]interface{}) (err error) {
  92. switch op.action {
  93. case "index":
  94. err = insertAssetContents(tx, op.assetContents, context)
  95. case "deletePath":
  96. err = deleteAssetContentsByPath(tx, op.path, context)
  97. default:
  98. msg := fmt.Sprintf("unknown asset content operation [%s]", op.action)
  99. logging.LogErrorf(msg)
  100. err = errors.New(msg)
  101. }
  102. return
  103. }
  104. func DeleteAssetContentsByPathQueue(path string) {
  105. assetContentDBQueueLock.Lock()
  106. defer assetContentDBQueueLock.Unlock()
  107. newOp := &assetContentDBQueueOperation{inQueueTime: time.Now(), action: "deletePath", path: path}
  108. assetContentOperationQueue = append(assetContentOperationQueue, newOp)
  109. }
  110. func IndexAssetContentsQueue(assetContents []*AssetContent) {
  111. assetContentDBQueueLock.Lock()
  112. defer assetContentDBQueueLock.Unlock()
  113. newOp := &assetContentDBQueueOperation{inQueueTime: time.Now(), action: "index", assetContents: assetContents}
  114. assetContentOperationQueue = append(assetContentOperationQueue, newOp)
  115. }
  116. func getAssetContentOperations() (ops []*assetContentDBQueueOperation) {
  117. assetContentDBQueueLock.Lock()
  118. defer assetContentDBQueueLock.Unlock()
  119. ops = assetContentOperationQueue
  120. assetContentOperationQueue = nil
  121. return
  122. }