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