diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 10ca02082..95fb2e7dd 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -68,7 +68,7 @@ type DataStorage interface { GetRatingProfile(string) (*RatingProfile, error) SetRatingProfile(*RatingProfile) error GetDestination(string) (*Destination, error) - DestinationContainsPrefix(string, string) (int, error) + // DestinationContainsPrefix(string, string) (int, error) SetDestination(*Destination) error GetActions(string) (Actions, error) SetActions(string, Actions) error diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 916f40c28..8d339aada 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -103,12 +103,11 @@ func (rs *RedisStorage) PreCache() error { } func (rs *RedisStorage) GetRatingPlan(key string) (rp *RatingPlan, err error) { - var values string if x, err := cache2go.GetCached(key); err == nil { return x.(*RatingPlan), nil } + var values string if values, err = rs.db.Get(RATING_PLAN_PREFIX + key); err == nil { - rp = new(RatingPlan) b := bytes.NewBufferString(values) r, err := zlib.NewReader(b) if err != nil { @@ -119,6 +118,7 @@ func (rs *RedisStorage) GetRatingPlan(key string) (rp *RatingPlan, err error) { return nil, err } r.Close() + rp = new(RatingPlan) err = rs.ms.Unmarshal(out, rp) cache2go.Cache(key, rp) } @@ -162,15 +162,26 @@ func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error if x, err := cache2go.GetCached(key); err == nil { return x.(*Destination), nil } - var values []string - if values, err = rs.db.SMembers(DESTINATION_PREFIX + key); len(values) > 0 && err == nil { - dest = &Destination{Id: key, Prefixes: values} + var values string + if values, err = rs.db.Get(DESTINATION_PREFIX + key); len(values) > 0 && err == nil { + b := bytes.NewBufferString(values) + r, err := zlib.NewReader(b) + if err != nil { + return nil, err + } + out, err := ioutil.ReadAll(r) + if err != nil { + return nil, err + } + r.Close() + dest = new(Destination) + err = rs.ms.Unmarshal(out, dest) cache2go.Cache(key, dest) } return } -func (rs *RedisStorage) DestinationContainsPrefix(key string, prefix string) (precision int, err error) { +/*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 } @@ -186,14 +197,15 @@ func (rs *RedisStorage) DestinationContainsPrefix(key string, prefix string) (pr Logger.Err("Error removing temp ") } return -} +}*/ func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { - var prefixes []interface{} - for _, p := range dest.Prefixes { - prefixes = append(prefixes, p) - } - _, err = rs.db.SAdd(DESTINATION_PREFIX+dest.Id, prefixes...) + result, err := rs.ms.Marshal(dest) + var b bytes.Buffer + w := zlib.NewWriter(&b) + w.Write(result) + w.Close() + _, err = rs.db.Set(DESTINATION_PREFIX+dest.Id, b.Bytes()) 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 4cf4b3b1f..2717c6c0c 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -72,21 +72,24 @@ func TestMsgpackTime(t *testing.T) { } func TestStorageDestinationContainsPrefixShort(t *testing.T) { - precision, err := storageGetter.DestinationContainsPrefix("NAT", "0723") + dest, err := storageGetter.GetDestination("NAT") + precision := dest.containsPrefix("0723") if err != nil || precision != 4 { t.Error("Error finding prefix: ", err, precision) } } func TestStorageDestinationContainsPrefixLong(t *testing.T) { - precision, err := storageGetter.DestinationContainsPrefix("NAT", "0723045326") + dest, err := storageGetter.GetDestination("NAT") + precision := dest.containsPrefix("0723045326") if err != nil || precision != 4 { t.Error("Error finding prefix: ", err, precision) } } func TestStorageDestinationContainsPrefixNotExisting(t *testing.T) { - precision, err := storageGetter.DestinationContainsPrefix("NAT", "072") + dest, err := storageGetter.GetDestination("NAT") + precision := dest.containsPrefix("072") if err != nil || precision != 0 { t.Error("Error finding prefix: ", err, precision) } diff --git a/engine/units_counter.go b/engine/units_counter.go index deed2fdc7..b1a83a83f 100644 --- a/engine/units_counter.go +++ b/engine/units_counter.go @@ -52,11 +52,12 @@ func (uc *UnitsCounter) initMinuteBalances(ats []*ActionTrigger) { // is the same or ads the minute balance to the list if none matches. func (uc *UnitsCounter) addMinutes(amount float64, prefix string) { for _, mb := range uc.MinuteBalances { - precision, err := storageGetter.DestinationContainsPrefix(mb.DestinationId, prefix) + dest, err := storageGetter.GetDestination(mb.DestinationId) if err != nil { Logger.Err(fmt.Sprintf("Minutes counter: unknown destination: %v", mb.DestinationId)) continue } + precision := dest.containsPrefix(prefix) if precision > 0 { mb.Value += amount break diff --git a/engine/userbalance.go b/engine/userbalance.go index e3b010ccb..4b26d1d22 100644 --- a/engine/userbalance.go +++ b/engine/userbalance.go @@ -131,10 +131,11 @@ func (ub *UserBalance) getBalancesForPrefix(prefix string, balances BalanceChain continue } if b.DestinationId != "" { - precision, err := storageGetter.DestinationContainsPrefix(b.DestinationId, prefix) + dest, err := storageGetter.GetDestination(b.DestinationId) if err != nil { continue } + precision := dest.containsPrefix(prefix) if precision > 0 { b.precision = precision usefulBalances = append(usefulBalances, b)