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 int64 // deleteOutdated
  39. }
  40. func FlushHistoryTxJob() {
  41. task.AppendTask(task.HistoryDatabaseIndexCommit, FlushHistoryQueue)
  42. }
  43. func FlushHistoryQueue() {
  44. ops := getHistoryOperations()
  45. total := len(ops)
  46. if 1 > total {
  47. return
  48. }
  49. historyTxLock.Lock()
  50. defer historyTxLock.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 := beginHistoryTx()
  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 = execHistoryOp(op, tx, context); nil != err {
  70. tx.Rollback()
  71. logging.LogErrorf("queue operation failed: %s", err)
  72. eventbus.Publish(util.EvtSQLHistoryRebuild)
  73. return
  74. }
  75. if err = commitHistoryTx(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 history op tx [%dms]", elapsed)
  89. }
  90. }
  91. func execHistoryOp(op *historyDBQueueOperation, tx *sql.Tx, context map[string]interface{}) (err error) {
  92. switch op.action {
  93. case "index":
  94. err = insertHistories(tx, op.histories, context)
  95. case "deleteOutdated":
  96. err = deleteOutdatedHistories(tx, op.before, context)
  97. default:
  98. msg := fmt.Sprintf("unknown history operation [%s]", op.action)
  99. logging.LogErrorf(msg)
  100. err = errors.New(msg)
  101. }
  102. return
  103. }
  104. func DeleteOutdatedHistories(before int64) {
  105. historyDBQueueLock.Lock()
  106. defer historyDBQueueLock.Unlock()
  107. newOp := &historyDBQueueOperation{inQueueTime: time.Now(), action: "deleteOutdated", before: before}
  108. historyOperationQueue = append(historyOperationQueue, newOp)
  109. }
  110. func IndexHistoriesQueue(histories []*History) {
  111. historyDBQueueLock.Lock()
  112. defer historyDBQueueLock.Unlock()
  113. newOp := &historyDBQueueOperation{inQueueTime: time.Now(), action: "index", histories: histories}
  114. historyOperationQueue = append(historyOperationQueue, newOp)
  115. }
  116. func getHistoryOperations() (ops []*historyDBQueueOperation) {
  117. historyDBQueueLock.Lock()
  118. defer historyDBQueueLock.Unlock()
  119. ops = historyOperationQueue
  120. historyOperationQueue = nil
  121. return
  122. }