mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
made map storage behave more like redis one
This commit is contained in:
@@ -280,11 +280,18 @@ func (ms *MapStorage) SetRpAlias(key, alias string) (err error) {
|
||||
}
|
||||
|
||||
func (ms *MapStorage) RemoveRpAliases(tenantRtSubjects []*TenantRatingSubject) (err error) {
|
||||
for key, value := range ms.dict {
|
||||
for key, _ := range ms.dict {
|
||||
for _, tntRtSubj := range tenantRtSubjects {
|
||||
tenantPrfx := RP_ALIAS_PREFIX + tntRtSubj.Tenant + utils.CONCATENATED_KEY_SEP
|
||||
if strings.HasPrefix(key, RP_ALIAS_PREFIX) && len(key) >= len(tenantPrfx) && key[:len(tenantPrfx)] == tenantPrfx && tntRtSubj.Subject == string(value) {
|
||||
delete(ms.dict, key)
|
||||
if strings.HasPrefix(key, RP_ALIAS_PREFIX) {
|
||||
alsSubj, err := ms.GetRpAlias(key[len(RP_ALIAS_PREFIX):], true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(key) >= len(tenantPrfx) && key[:len(tenantPrfx)] == tenantPrfx && tntRtSubj.Subject == alsSubj {
|
||||
cache2go.RemKey(key)
|
||||
delete(ms.dict, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -292,10 +299,21 @@ func (ms *MapStorage) RemoveRpAliases(tenantRtSubjects []*TenantRatingSubject) (
|
||||
}
|
||||
|
||||
func (ms *MapStorage) GetRPAliases(tenant, subject string, checkDb bool) (aliases []string, err error) {
|
||||
for key, value := range ms.dict {
|
||||
tenantPrfx := RP_ALIAS_PREFIX + tenant + utils.CONCATENATED_KEY_SEP
|
||||
if strings.HasPrefix(key, RP_ALIAS_PREFIX) && len(key) >= len(tenantPrfx) && key[:len(tenantPrfx)] == tenantPrfx && subject == string(value) {
|
||||
aliases = append(aliases, key[len(tenantPrfx):])
|
||||
tenantPrfx := RP_ALIAS_PREFIX + tenant + utils.CONCATENATED_KEY_SEP
|
||||
alsKeys := cache2go.GetEntriesKeys(tenantPrfx)
|
||||
for _, key := range alsKeys {
|
||||
if alsSubj, err := ms.GetRpAlias(key[len(RP_ALIAS_PREFIX):], checkDb); err != nil {
|
||||
return nil, err
|
||||
} else if alsSubj == subject {
|
||||
alsFromKey := key[len(tenantPrfx):] // take out the alias out of key+tenant
|
||||
aliases = append(aliases, alsFromKey)
|
||||
}
|
||||
}
|
||||
if len(alsKeys) == 0 && checkDb {
|
||||
for key, value := range ms.dict {
|
||||
if strings.HasPrefix(key, RP_ALIAS_PREFIX) && len(key) >= len(tenantPrfx) && key[:len(tenantPrfx)] == tenantPrfx && subject == string(value) {
|
||||
aliases = append(aliases, key[len(tenantPrfx):])
|
||||
}
|
||||
}
|
||||
}
|
||||
return aliases, nil
|
||||
|
||||
@@ -362,6 +362,7 @@ func (rs *RedisStorage) RemoveRpAliases(tenantRtSubjects []*TenantRatingSubject)
|
||||
if tntRSubj.Subject != alias {
|
||||
continue
|
||||
}
|
||||
cache2go.RemKey(key)
|
||||
if _, err = rs.db.Del(key); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ func TestCacheRefresh(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCacheAliases(t *testing.T) {
|
||||
if subj, err := cache2go.GetCached(RP_ALIAS_PREFIX + utils.RatingSubjectAliasKey("vdf", "a3")); err != nil || subj != "minu" {
|
||||
if subj, err := cache2go.GetCached(RP_ALIAS_PREFIX + utils.RatingSubjectAliasKey("vdf", "a3")); err == nil && subj != "minu" {
|
||||
t.Error("Error caching alias: ", subj, err)
|
||||
}
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func TestGetRPAliases(t *testing.T) {
|
||||
}
|
||||
expectAliases := sort.StringSlice([]string{"2001", "2002"})
|
||||
expectAliases.Sort()
|
||||
if aliases, err := dataStorage.GetRPAliases("cgrates.org", "1001", false); err != nil {
|
||||
if aliases, err := dataStorage.GetRPAliases("cgrates.org", "1001", true); err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
aliases := sort.StringSlice(aliases)
|
||||
@@ -167,7 +167,7 @@ func TestRemRSubjAliases(t *testing.T) {
|
||||
if cgrAliases, err := dataStorage.GetRPAliases("cgrates.org", "1001", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(cgrAliases) != 0 {
|
||||
t.Error("Subject aliases not removed")
|
||||
t.Error("Subject aliases not removed: ", cgrAliases)
|
||||
}
|
||||
if iscAliases, err := dataStorage.GetRPAliases("itsyscom.com", "1001", false); err != nil { // Make sure the aliases were removed at tenant level
|
||||
t.Error(err)
|
||||
|
||||
Reference in New Issue
Block a user