78 lines
1.8 KiB
Plaintext
78 lines
1.8 KiB
Plaintext
package sqlAsyncMgr
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// SqlAsyncIdentityStatistics sql标识统计类
|
|
type SqlAsyncIdentityStatistics struct {
|
|
// 锁对象
|
|
m sync.Mutex
|
|
|
|
// 当前正在同步的sql数量
|
|
syncCount int32
|
|
|
|
// 待同步的统计
|
|
statisticData map[string]*StatisticModel
|
|
}
|
|
|
|
// newSqlAsyncIdentityStatistics 创建新StatisticModel对象
|
|
func newSqlAsyncIdentityStatistics() *SqlAsyncIdentityStatistics {
|
|
return &SqlAsyncIdentityStatistics{
|
|
statisticData: make(map[string]*StatisticModel),
|
|
}
|
|
}
|
|
|
|
// getItem 获取指定统计对象
|
|
func (this *SqlAsyncIdentityStatistics) getItem(_identityId string, isCreate bool) *StatisticModel {
|
|
this.m.Lock()
|
|
defer this.m.Unlock()
|
|
|
|
if item, exists := this.statisticData[_identityId]; exists {
|
|
return item
|
|
}
|
|
|
|
if isCreate == false {
|
|
return nil
|
|
}
|
|
|
|
newItem := newStatisticModel(_identityId)
|
|
this.statisticData[_identityId] = newItem
|
|
|
|
return newItem
|
|
}
|
|
|
|
// AddCount 指定标识添加数量
|
|
func (this *SqlAsyncIdentityStatistics) AddCount(_identityId string, count int32) {
|
|
model := this.getItem(_identityId, true)
|
|
model.AddCount(count)
|
|
|
|
// 原子操作相加
|
|
atomic.AddInt32(&this.syncCount, count)
|
|
}
|
|
|
|
// Reduce 指定标识减少数量
|
|
func (this *SqlAsyncIdentityStatistics) Reduce(_identityId string, count int32) {
|
|
if model := this.getItem(_identityId, false); model != nil {
|
|
model.ReduceCount(count)
|
|
}
|
|
|
|
// 原子操作相减
|
|
atomic.AddInt32(&this.syncCount, -count)
|
|
}
|
|
|
|
// GetCount 获取指定标识的数量
|
|
func (this *SqlAsyncIdentityStatistics) GetCount(_identityId string) int32 {
|
|
if model := this.getItem(_identityId, false); model != nil {
|
|
return model.Count
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
// GetAllCount 获取总的待写入数据
|
|
func (this *SqlAsyncIdentityStatistics) GetAllCount() int32 {
|
|
return this.syncCount
|
|
}
|