queue_history.go 3.7 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. historyOperationQueue []*historyDBQueueOperation
  31. historyDBQueueLock = sync.Mutex{}
  32. historyTxLock = sync.Mutex{}
  33. )
  34. type historyDBQueueOperation struct {
  35. inQueueTime time.Time
  36. action string // index/deleteOutdated
  37. histories []*History // index
  38. before string // deleteOutdated
  39. }
  40. func FlushHistoryTxJob() {
  41. task.AppendTask(task.HistoryDatabaseIndexCommit, FlushHistoryQueue)
  42. }
  43. func FlushHistoryQueue() {
  44. ops := getHistoryOperations()
  45. if 1 > len(ops) {
  46. return
  47. }
  48. historyTxLock.Lock()
  49. defer historyTxLock.Unlock()
  50. start := time.Now()
  51. groupOpsTotal := map[string]int{}
  52. for _, op := range ops {
  53. groupOpsTotal[op.action]++
  54. }
  55. context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}
  56. groupOpsCurrent := map[string]int{}
  57. for i, op := range ops {
  58. if util.IsExiting.Load() {
  59. return
  60. }
  61. tx, err := beginHistoryTx()
  62. if nil != err {
  63. return
  64. }
  65. groupOpsCurrent[op.action]++
  66. context["current"] = groupOpsCurrent[op.action]
  67. context["total"] = groupOpsTotal[op.action]
  68. if err = execHistoryOp(op, tx, context); nil != err {
  69. tx.Rollback()
  70. logging.LogErrorf("queue operation failed: %s", err)
  71. eventbus.Publish(util.EvtSQLHistoryRebuild)
  72. return
  73. }
  74. if err = commitHistoryTx(tx); nil != err {
  75. logging.LogErrorf("commit tx failed: %s", err)
  76. return
  77. }
  78. if 16 < i && 0 == i%128 {
  79. debug.FreeOSMemory()
  80. }
  81. }
  82. if 128 < len(ops) {
  83. debug.FreeOSMemory()
  84. }
  85. elapsed := time.Now().Sub(start).Milliseconds()
  86. if 7000 < elapsed {
  87. logging.LogInfof("database history op tx [%dms]", elapsed)
  88. }
  89. }
  90. func execHistoryOp(op *historyDBQueueOperation, tx *sql.Tx, context map[string]interface{}) (err error) {
  91. switch op.action {
  92. case "index":
  93. err = insertHistories(tx, op.histories, context)
  94. case "deleteOutdated":
  95. err = deleteOutdatedHistories(tx, op.before, context)
  96. default:
  97. msg := fmt.Sprintf("unknown history operation [%s]", op.action)
  98. logging.LogErrorf(msg)
  99. err = errors.New(msg)
  100. }
  101. return
  102. }
  103. func DeleteOutdatedHistories(before string) {
  104. historyDBQueueLock.Lock()
  105. defer historyDBQueueLock.Unlock()
  106. newOp := &historyDBQueueOperation{inQueueTime: time.Now(), action: "deleteOutdated", before: before}
  107. historyOperationQueue = append(historyOperationQueue, newOp)
  108. }
  109. func IndexHistoriesQueue(histories []*History) {
  110. historyDBQueueLock.Lock()
  111. defer historyDBQueueLock.Unlock()
  112. newOp := &historyDBQueueOperation{inQueueTime: time.Now(), action: "index", histories: histories}
  113. historyOperationQueue = append(historyOperationQueue, newOp)
  114. }
  115. func getHistoryOperations() (ops []*historyDBQueueOperation) {
  116. historyDBQueueLock.Lock()
  117. defer historyDBQueueLock.Unlock()
  118. ops = historyOperationQueue
  119. historyOperationQueue = nil
  120. return
  121. }