From cb014c39a41fb184e610d17e2b9e511446e3adf6 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 3 Feb 2021 16:15:04 +0200 Subject: [PATCH] Optimized the automated index fields matching --- packages/debian/changelog | 1 + utils/mapstorage.go | 55 ++++++++------------------------------- utils/mapstorage_test.go | 8 ------ 3 files changed, 12 insertions(+), 52 deletions(-) diff --git a/packages/debian/changelog b/packages/debian/changelog index 777788edd..394175857 100644 --- a/packages/debian/changelog +++ b/packages/debian/changelog @@ -8,6 +8,7 @@ cgrates (0.10.3~dev) UNRELEASED; urgency=medium * [ApierS] Correct handle error in case of APIerSv1.GetActionTriggers * [SessionS] Added extra condition to determine if the increment is considered the roundIncrement * [SessionS] Cloned the charging interval added on EventCost merge + * [FilterS] Optimized the automated index fields matching -- DanB Thu, 08 Oct 2020 16:23:58 +0300 diff --git a/utils/mapstorage.go b/utils/mapstorage.go index 4819cca3d..963c40ab7 100644 --- a/utils/mapstorage.go +++ b/utils/mapstorage.go @@ -187,7 +187,7 @@ func (ms MapStorage) GetKeys(nesteed bool) (keys []string) { return } for k, v := range ms { - keys = append(keys, k) + // keys = append(keys, k) switch rv := v.(type) { case dataStorage: for _, dsKey := range rv.GetKeys(nesteed) { @@ -200,7 +200,7 @@ func (ms MapStorage) GetKeys(nesteed bool) (keys []string) { case []MapStorage: for i, dp := range rv { pref := k + fmt.Sprintf("[%v]", i) - keys = append(keys, pref) + // keys = append(keys, pref) for _, dsKey := range dp.GetKeys(nesteed) { keys = append(keys, pref+NestingSep+dsKey) } @@ -208,7 +208,7 @@ func (ms MapStorage) GetKeys(nesteed bool) (keys []string) { case []dataStorage: for i, dp := range rv { pref := k + fmt.Sprintf("[%v]", i) - keys = append(keys, pref) + // keys = append(keys, pref) for _, dsKey := range dp.GetKeys(nesteed) { keys = append(keys, pref+NestingSep+dsKey) } @@ -216,7 +216,7 @@ func (ms MapStorage) GetKeys(nesteed bool) (keys []string) { case []map[string]interface{}: for i, dp := range rv { pref := k + fmt.Sprintf("[%v]", i) - keys = append(keys, pref) + // keys = append(keys, pref) for _, dsKey := range MapStorage(dp).GetKeys(nesteed) { keys = append(keys, pref+NestingSep+dsKey) } @@ -229,9 +229,11 @@ func (ms MapStorage) GetKeys(nesteed bool) (keys []string) { for i := range rv { keys = append(keys, k+fmt.Sprintf("[%v]", i)) } + case nil, int, int32, int64, uint32, uint64, bool, float32, float64, []uint8, time.Duration, time.Time, string: //no path + keys = append(keys, k) default: // ToDo:should not be called - keys = append(keys, getPathFromInterface(v, k+NestingSep)...) + keys = append(keys, getPathFromValue(reflect.ValueOf(v), k+NestingSep)...) } } @@ -280,61 +282,26 @@ func getPathFromValue(in reflect.Value, prefix string) (out []string) { prefix = strings.TrimSuffix(prefix, NestingSep) for i := 0; i < in.Len(); i++ { pref := fmt.Sprintf("%s[%v]", prefix, i) - out = append(out, pref) + // out = append(out, pref) out = append(out, getPathFromValue(in.Index(i), pref+NestingSep)...) } case reflect.Map: iter := reflect.ValueOf(in).MapRange() for iter.Next() { pref := prefix + iter.Key().String() - out = append(out, pref) + // out = append(out, pref) out = append(out, getPathFromValue(iter.Value(), pref+NestingSep)...) } case reflect.Struct: inType := in.Type() for i := 0; i < in.NumField(); i++ { pref := prefix + inType.Field(i).Name - out = append(out, pref) + // out = append(out, pref) out = append(out, getPathFromValue(in.Field(i), pref+NestingSep)...) } case reflect.Invalid, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.String, reflect.Chan, reflect.Func, reflect.UnsafePointer, reflect.Interface: + out = append(out, strings.TrimSuffix(prefix, NestingSep)) default: } return } - -// used by MapStorage2 GetKeys to return all values -func getPathFromInterface(in interface{}, prefix string) (out []string) { - switch vin := in.(type) { - case map[string]interface{}: - for k, val := range vin { - pref := prefix + k - out = append(out, pref) - out = append(out, getPathFromInterface(val, pref+NestingSep)...) - } - case []map[string]interface{}: - prefix = strings.TrimSuffix(prefix, NestingSep) - for i, val := range vin { - pref := fmt.Sprintf("%s[%v]", prefix, i) - out = append(out, pref) - out = append(out, getPathFromInterface(val, pref+NestingSep)...) - } - case []interface{}: - prefix = strings.TrimSuffix(prefix, NestingSep) - for i, val := range vin { - pref := fmt.Sprintf("%s[%v]", prefix, i) - out = append(out, pref) - out = append(out, getPathFromInterface(val, pref+NestingSep)...) - } - case []string: - prefix = strings.TrimSuffix(prefix, NestingSep) - for i := range vin { - pref := fmt.Sprintf("%s[%v]", prefix, i) - out = append(out, pref) - } - case nil, int, int32, int64, uint32, uint64, bool, float32, float64, []uint8, time.Duration, time.Time, string: //no path - default: //reflect based - out = getPathFromValue(reflect.ValueOf(vin), prefix) - } - return -} diff --git a/utils/mapstorage_test.go b/utils/mapstorage_test.go index a462f354f..bca365c80 100644 --- a/utils/mapstorage_test.go +++ b/utils/mapstorage_test.go @@ -321,21 +321,13 @@ func TestNavMapGetKeys(t *testing.T) { "Field6": []string{"1", "2"}, } expKeys := []string{ - "FirstLevel", - "FirstLevel.SecondLevel", - "FirstLevel.SecondLevel.ThirdLevel", "FirstLevel.SecondLevel.ThirdLevel.Fld1", - "FistLever2", - "FistLever2.SecondLevel2", "FistLever2.SecondLevel2.Field2", "FistLever2.Field3", - "FistLever2.Field4", "FistLever2.Field4.Item1", "FistLever2.Field4.Item2", - "Field5", "Field5.Item1", "Field5.Item2", - "Field6", "Field6[0]", "Field6[1]", }