diff --git a/engine/calldesc.go b/engine/calldesc.go index ca1cd976d..35fbe3161 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -36,11 +36,11 @@ func init() { Logger = new(utils.StdLogger) Logger.Err(fmt.Sprintf("Could not connect to syslog: %v", err)) } - //db_server := "127.0.0.1" + db_server := "127.0.0.1" //db_server := "192.168.0.17" - m, _ := NewMapStorage() + //m, _ := NewMapStorage() //m, _ := NewMongoStorage(db_server, "27017", "cgrates_test", "", "") - //m, _ := NewRedisStorage(db_server+":6379", 11, "") + m, _ := NewRedisStorage(db_server+":6379", 11, "") //m, _ := NewRedigoStorage(db_server+":6379", 11, "") //m, _ := NewRadixStorage(db_server+":6379", 11, "") storageGetter, _ = m.(DataStorage) diff --git a/engine/destinations_test.go b/engine/destinations_test.go index c5ccc3ca9..56e875d49 100644 --- a/engine/destinations_test.go +++ b/engine/destinations_test.go @@ -21,7 +21,7 @@ package engine import ( "encoding/json" "github.com/cgrates/cgrates/cache2go" - "reflect" + "github.com/cgrates/cgrates/utils" "testing" ) @@ -43,7 +43,8 @@ func TestDestinationStorageStore(t *testing.T) { t.Error("Error storing destination: ", err) } result, err := storageGetter.GetDestination(nationale.Id) - if !reflect.DeepEqual(nationale, result) { + var ss utils.StringSlice = result.Prefixes + if !ss.Contains("0257") || !ss.Contains("0256") || !ss.Contains("0723") { t.Errorf("Expected %q was %q", nationale, result) } } @@ -54,7 +55,6 @@ func TestDestinationContainsPrefix(t *testing.T) { if !ok || precision != len("0256") { t.Error("Should contain prefix: ", nationale) } - } func TestDestinationContainsPrefixLong(t *testing.T) { @@ -63,7 +63,14 @@ func TestDestinationContainsPrefixLong(t *testing.T) { if !ok || precision != len("0256") { t.Error("Should contain prefix: ", nationale) } +} +func TestDestinationContainsPrefixWrong(t *testing.T) { + nationale := &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} + precision, ok := nationale.containsPrefix("01234567") + if ok || precision != 0 { + t.Error("Should not contain prefix: ", nationale) + } } func TestDestinationGetExists(t *testing.T) { diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 161c5c930..0fe0b567a 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -36,6 +36,7 @@ const ( ACTION_PREFIX = "act_" USER_BALANCE_PREFIX = "ubl_" DESTINATION_PREFIX = "dst_" + TEMP_DESTINATION_PREFIX = "tmp_" LOG_CALL_COST_PREFIX = "cco_" LOG_ACTION_TIMMING_PREFIX = "ltm_" LOG_ACTION_TRIGGER_PREFIX = "ltr_" diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 4addfb9c5..d0d0ac95c 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -89,30 +89,36 @@ func (rs *RedisStorage) SetRatingProfile(rp *RatingProfile) (err error) { func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error) { var values []string - if values, err = rs.db.HKeys(DESTINATION_PREFIX + key); len(values) > 0 && err == nil { + if values, err = rs.db.SMembers(DESTINATION_PREFIX + key); len(values) > 0 && err == nil { dest = &Destination{Id: key, Prefixes: values} } return } func (rs *RedisStorage) DestinationContainsPrefix(key string, prefix string) (precision int, err error) { + if _, err := rs.db.SAdd(TEMP_DESTINATION_PREFIX+prefix, utils.SplitPrefixInterface(prefix)...); err != nil { + return 0, err + } var values []string - if values, err = rs.db.HMGet(DESTINATION_PREFIX+key, utils.SplitPrefix(prefix)...); err == nil { - for i, p := range values { - if p != "" { - return len(prefix) - i, nil + if values, err = rs.db.SInter(DESTINATION_PREFIX+key, TEMP_DESTINATION_PREFIX+prefix); err == nil { + for _, p := range values { + if len(p) > precision { + precision = len(p) } } } + if _, err := rs.db.Del(TEMP_DESTINATION_PREFIX + prefix); err != nil { + Logger.Err("Error removing temp ") + } return } func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { - var newPrefixes []interface{} + var prefixes []interface{} for _, p := range dest.Prefixes { - newPrefixes = append(newPrefixes, p, "*") + prefixes = append(prefixes, p) } - _, err = rs.db.HMSet(DESTINATION_PREFIX+dest.Id, newPrefixes...) + _, err = rs.db.SAdd(DESTINATION_PREFIX+dest.Id, prefixes...) if err == nil && historyScribe != nil { response := 0 historyScribe.Record(&history.Record{DESTINATION_PREFIX + dest.Id, dest}, &response) diff --git a/engine/storage_test.go b/engine/storage_test.go index 4a297a4b4..7b450c21c 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -71,6 +71,27 @@ func TestMsgpackTime(t *testing.T) { } } +func TestStorageDestinationContainsPrefixShort(t *testing.T) { + precision, err := storageGetter.DestinationContainsPrefix("NAT", "0723") + if err != nil || precision != 4 { + t.Error("Error finding prefix: ", err, precision) + } +} + +func TestStorageDestinationContainsPrefixLong(t *testing.T) { + precision, err := storageGetter.DestinationContainsPrefix("NAT", "0723045326") + if err != nil || precision != 4 { + t.Error("Error finding prefix: ", err, precision) + } +} + +func TestStorageDestinationContainsPrefixNotExisting(t *testing.T) { + precision, err := storageGetter.DestinationContainsPrefix("NAT", "072") + if err != nil || precision != 0 { + t.Error("Error finding prefix: ", err, precision) + } +} + /************************** Benchmarks *****************************/ func GetUB() *UserBalance { diff --git a/utils/coreutils.go b/utils/coreutils.go index 73b047373..68e26a294 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -131,6 +131,17 @@ func RoundTo(whole, amount time.Duration) time.Duration { return time.Duration((w - math.Mod(a, w)) + a) } +type StringSlice []string + +func (ss StringSlice) Contains(needle string) bool { + for _, hay := range ss { + if hay == needle { + return true + } + } + return false +} + func SplitPrefix(prefix string) []string { var subs []string max := len(prefix)