destinations are now stored in sets

This commit is contained in:
Radu Ioan Fericean
2013-09-18 17:55:07 +03:00
parent 1ff176cf47
commit f90b9e7aa3
6 changed files with 60 additions and 14 deletions

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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_"

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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)