From eb182825ed4de4fafd4db1ec07ff44546635dc47 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 4 Sep 2015 17:34:51 +0300 Subject: [PATCH] using destination id in revers aliases --- engine/storage_map.go | 12 +++++++++--- engine/storage_redis.go | 13 ++++++++++--- engine/storage_test.go | 6 +++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/engine/storage_map.go b/engine/storage_map.go index d1b1c933a..f9c7e117c 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -562,6 +562,7 @@ func (ms *MapStorage) SetAlias(al *Alias) error { } func (ms *MapStorage) GetAlias(key string, skipCache bool) (al *Alias, err error) { + origKey := key key = utils.ALIASES_PREFIX + key if !skipCache { if x, err := cache2go.GetCached(key); err == nil { @@ -586,7 +587,7 @@ func (ms *MapStorage) GetAlias(key string, skipCache bool) (al *Alias, err error } else { existingKeys = make(map[string]bool) } - existingKeys[key] = true + existingKeys[utils.ConcatenatedKey(origKey, v.DestinationId)] = true cache2go.Cache(rKey, existingKeys) } } @@ -599,6 +600,7 @@ func (ms *MapStorage) GetAlias(key string, skipCache bool) (al *Alias, err error func (ms *MapStorage) RemoveAlias(key string) error { al := &Alias{} al.SetId(key) + origKey := key key = utils.ALIASES_PREFIX + key aliasValues := make(AliasValues, 0) if values, ok := ms.dict[key]; ok { @@ -611,10 +613,14 @@ func (ms *MapStorage) RemoveAlias(key string) error { if x, err := cache2go.GetCached(rKey); err == nil { existingKeys = x.(map[string]bool) } - if len(existingKeys) == 1 { + for eKey := range existingKeys { + if strings.HasPrefix(eKey, origKey) { + delete(existingKeys, eKey) + } + } + if len(existingKeys) == 0 { cache2go.RemKey(rKey) } else { - delete(existingKeys, key) cache2go.Cache(rKey, existingKeys) } } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 263d3da0e..7cc5ba45a 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -23,6 +23,7 @@ import ( "compress/zlib" "errors" "fmt" + "strings" "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/utils" @@ -665,6 +666,7 @@ func (rs *RedisStorage) SetAlias(al *Alias) (err error) { } func (rs *RedisStorage) GetAlias(key string, skipCache bool) (al *Alias, err error) { + origKey := key key = utils.ALIASES_PREFIX + key if !skipCache { if x, err := cache2go.GetCached(key); err == nil { @@ -691,7 +693,7 @@ func (rs *RedisStorage) GetAlias(key string, skipCache bool) (al *Alias, err err } else { existingKeys = make(map[string]bool) } - existingKeys[key] = true + existingKeys[utils.ConcatenatedKey(origKey, v.DestinationId)] = true cache2go.Cache(rKey, existingKeys) } } @@ -702,6 +704,7 @@ func (rs *RedisStorage) GetAlias(key string, skipCache bool) (al *Alias, err err func (rs *RedisStorage) RemoveAlias(key string) (err error) { al := &Alias{} al.SetId(key) + origKey := key key = utils.ALIASES_PREFIX + key aliasValues := make(AliasValues, 0) if values, err := rs.db.Get(key); err == nil { @@ -715,10 +718,14 @@ func (rs *RedisStorage) RemoveAlias(key string) (err error) { if x, err := cache2go.GetCached(rKey); err == nil { existingKeys = x.(map[string]bool) } - if len(existingKeys) == 1 { + for eKey := range existingKeys { + if strings.HasPrefix(eKey, origKey) { + delete(existingKeys, eKey) + } + } + if len(existingKeys) == 0 { cache2go.RemKey(rKey) } else { - delete(existingKeys, key) cache2go.Cache(rKey, existingKeys) } } diff --git a/engine/storage_test.go b/engine/storage_test.go index 8ed787a31..8f4f80c8a 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -181,16 +181,16 @@ func TestStorageCacheGetReverseAliases(t *testing.T) { } if x, err := cache2go.GetCached(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_RP); err == nil { aliasKeys := x.(map[string]bool) - _, found := aliasKeys[utils.ALIASES_PREFIX+ala.GetId()] + _, found := aliasKeys[utils.ConcatenatedKey(ala.GetId(), utils.ANY)] if !found { - t.Error("Error getting reverse alias: ", aliasKeys) + t.Error("Error getting reverse alias: ", aliasKeys, ala.GetId()+utils.ANY) } } else { t.Error("Error getting reverse alias: ", err) } if x, err := cache2go.GetCached(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_ACC); err == nil { aliasKeys := x.(map[string]bool) - _, found := aliasKeys[utils.ALIASES_PREFIX+alb.GetId()] + _, found := aliasKeys[utils.ConcatenatedKey(alb.GetId(), utils.ANY)] if !found { t.Error("Error getting reverse alias: ", aliasKeys) }