This commit is contained in:
Daniel 2024-04-12 20:33:53 +08:00
parent f822e4d69e
commit ee473289d2
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 32 additions and 23 deletions

View file

@ -35,6 +35,7 @@ import (
var (
operationQueue []*dbQueueOperation
dbQueueLock = sync.Mutex{}
txLock = sync.Mutex{}
)
type dbQueueOperation struct {
@ -95,29 +96,30 @@ func ClearQueue() {
}
func FlushQueue() {
dbQueueLock.Lock()
defer dbQueueLock.Unlock()
total := len(operationQueue)
ops := getOperations()
total := len(ops)
if 1 > total {
return
}
txLock.Lock()
defer txLock.Unlock()
start := time.Now()
context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}
if 512 < total {
if 512 < len(ops) {
disableCache()
defer enableCache()
}
groupOpsTotal := map[string]int{}
for _, op := range operationQueue {
for _, op := range ops {
groupOpsTotal[op.action]++
}
groupOpsCurrent := map[string]int{}
for i, op := range operationQueue {
for i, op := range ops {
if util.IsExiting.Load() {
return
}
@ -150,8 +152,6 @@ func FlushQueue() {
debug.FreeOSMemory()
}
operationQueue = nil
elapsed := time.Now().Sub(start).Milliseconds()
if 7000 < elapsed {
logging.LogInfof("database op tx [%dms]", elapsed)
@ -418,3 +418,12 @@ func RemoveTreePathQueue(treeBox, treePathPrefix string) {
}
operationQueue = append(operationQueue, newOp)
}
func getOperations() (ops []*dbQueueOperation) {
dbQueueLock.Lock()
defer dbQueueLock.Unlock()
ops = operationQueue
operationQueue = nil
return
}