This commit is contained in:
aozhiwei 2024-06-16 14:17:30 +08:00
parent e64899e3e8
commit 550a64387e

View File

@ -31,12 +31,12 @@ type RawMetaTable[T any] struct {
type IdMetaTable[T any] struct { type IdMetaTable[T any] struct {
RawMetaTable[T] RawMetaTable[T]
idHash map[int64]*T idHash *q5.ConcurrentMap[int64, *T]
} }
type NameMetaTable[T any] struct { type NameMetaTable[T any] struct {
RawMetaTable[T] RawMetaTable[T]
nameHash map[string]*T nameHash *q5.ConcurrentMap[string, *T]
} }
func (this *RawMetaTable[T]) Traverse(cb func(*T) bool) { func (this *RawMetaTable[T]) Traverse(cb func(*T) bool) {
@ -95,8 +95,8 @@ func (this *RawMetaTable[T]) ElementsInit(round int) {
} }
func (this *IdMetaTable[T]) GetById(id int64) *T { func (this *IdMetaTable[T]) GetById(id int64) *T {
if v, ok := this.idHash[id]; ok { if v, ok := this.idHash.Load(id); ok {
return v return *v
} else { } else {
return nil return nil
} }
@ -146,18 +146,18 @@ func (this *RawMetaTable[T]) Load() {
func (this *IdMetaTable[T]) Load() { func (this *IdMetaTable[T]) Load() {
this.RawMetaTable.Load() this.RawMetaTable.Load()
this.idHash = make(map[int64]*T) this.idHash = new(q5.ConcurrentMap[int64, *T])
i := int64(0) i := int64(0)
getFuncName := "Get" + q5.ConvertUpperCamelCase(this.PrimKey) getFuncName := "Get" + q5.ConvertUpperCamelCase(this.PrimKey)
this.Traverse(func(obj *T) bool { this.Traverse(func(obj *T) bool {
if this.PrimKey == "" { if this.PrimKey == "" {
this.idHash[i] = obj this.idHash.Store(i, obj)
} else { } else {
in := []reflect.Value{} in := []reflect.Value{}
method := reflect.ValueOf(obj).MethodByName(getFuncName) method := reflect.ValueOf(obj).MethodByName(getFuncName)
out := method.Call(in) out := method.Call(in)
if key, err := q5.ToInt64Ex(out[0].Interface()); err == nil { if key, err := q5.ToInt64Ex(out[0].Interface()); err == nil {
this.idHash[key] = obj this.idHash.Store(key, obj)
} else { } else {
panic("IdMetaTable load PrimKey error") panic("IdMetaTable load PrimKey error")
} }
@ -168,8 +168,8 @@ func (this *IdMetaTable[T]) Load() {
} }
func (this *NameMetaTable[T]) GetByName(name string) *T { func (this *NameMetaTable[T]) GetByName(name string) *T {
if v, ok := this.nameHash[name]; ok { if v, ok := this.nameHash.Load(name); ok {
return v return *v
} else { } else {
return nil return nil
} }
@ -177,7 +177,7 @@ func (this *NameMetaTable[T]) GetByName(name string) *T {
func (this *NameMetaTable[T]) Load() { func (this *NameMetaTable[T]) Load() {
this.RawMetaTable.Load() this.RawMetaTable.Load()
this.nameHash = make(map[string]*T) this.nameHash = new(q5.ConcurrentMap[string, *T])
i := int64(0) i := int64(0)
getFuncName := "Get" + q5.ConvertUpperCamelCase(this.PrimKey) getFuncName := "Get" + q5.ConvertUpperCamelCase(this.PrimKey)
this.Traverse(func(obj *T) bool { this.Traverse(func(obj *T) bool {
@ -185,7 +185,7 @@ func (this *NameMetaTable[T]) Load() {
method := reflect.ValueOf(obj).MethodByName(getFuncName) method := reflect.ValueOf(obj).MethodByName(getFuncName)
out := method.Call(in) out := method.Call(in)
if key, err := q5.ToStringEx(out[0].Interface()); err == nil { if key, err := q5.ToStringEx(out[0].Interface()); err == nil {
this.nameHash[key] = obj this.nameHash.Store(key, obj)
} else { } else {
panic("NameMetaTable load PrimKey error") panic("NameMetaTable load PrimKey error")
} }