diff --git a/engine/actions_test.go b/engine/actions_test.go index abd0aac35..f62d1017e 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -779,7 +779,7 @@ func TestActionResetAllCounters(t *testing.T) { len(ub.UnitCounters[0].Balances) != 2 || len(ub.BalanceMap[MINUTES]) != 2 || ub.ActionTriggers[0].Executed != true { - t.Errorf("Reset counters action failed: %+v", ub.UnitCounters[0]) + t.Errorf("Reset counters action failed: %+v", ub.UnitCounters) } if len(ub.UnitCounters) < 1 { t.FailNow() diff --git a/engine/callcost_test.go b/engine/callcost_test.go index 1181ccdb6..f2c3e1200 100644 --- a/engine/callcost_test.go +++ b/engine/callcost_test.go @@ -19,7 +19,6 @@ along with this program. If not, see package engine import ( - // "log" "testing" "time" ) diff --git a/engine/calldesc.go b/engine/calldesc.go index 726ef26aa..5ef5000f6 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -43,7 +43,6 @@ func init() { //storageGetter, _ = NewMongoStorage(db_server, "27017", "cgrates_test", "", "") storageGetter, _ = NewRedisStorage("127.0.0.1:6379", 11, "", utils.MSGPACK) } - storageLogger = storageGetter.(LogStorage) } diff --git a/engine/destinations.go b/engine/destinations.go index 5835347c9..68941e28b 100644 --- a/engine/destinations.go +++ b/engine/destinations.go @@ -38,7 +38,7 @@ type Destination struct { } // returns prefix precision -func (d *Destination) containsPrefix1(prefix string) int { +func (d *Destination) containsPrefix(prefix string) int { if d == nil { return 0 } diff --git a/engine/ratingprofile.go b/engine/ratingprofile.go index d49d7c26a..9aeec2131 100644 --- a/engine/ratingprofile.go +++ b/engine/ratingprofile.go @@ -23,6 +23,9 @@ import ( "fmt" "sort" "time" + + "github.com/cgrates/cgrates/cache2go" + "github.com/cgrates/cgrates/utils" ) type RatingProfile struct { @@ -110,17 +113,19 @@ func (rp *RatingProfile) GetRatingPlansForPrefix(cd *CallDescriptor) (err error) } bestPrecision := 0 var rps RateIntervalList - for dId, _ := range rpl.DestinationRates { - //precision, err := storageGetter.DestinationContainsPrefix(dId, cd.Destination) - d, err := storageGetter.GetDestination(dId, false) - if err != nil { - Logger.Err(fmt.Sprintf("Error checking destination: %v", err)) - continue + for _, p := range utils.SplitPrefix(cd.Destination) { + if x, err := cache2go.GetCached(p); err == nil { + destIds := x.([]string) + for _, dId := range destIds { + if _, ok := rpl.DestinationRates[dId]; ok { + rps = rpl.RateIntervalList(dId) + bestPrecision = len(p) + break + } + } } - precision := d.containsPrefix(cd.Destination) - if precision > bestPrecision { - bestPrecision = precision - rps = rpl.RateIntervalList(dId) + if rps != nil { + break } } // check if it's the first ri and add a blank one for the initial part not covered diff --git a/engine/storage_map.go b/engine/storage_map.go index bb8726426..6e25cbd1a 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -52,6 +52,7 @@ func (ms *MapStorage) PreCache(dKeys, rpKeys, rpfKeys, actKeys []string) error { for k, _ := range ms.dict { if strings.HasPrefix(k, DESTINATION_PREFIX) { cache2go.RemKey(k) + // TODO: here I must delete all optimized prefixes if _, err := ms.GetDestination(k[len(DESTINATION_PREFIX):], true); err != nil { return err } @@ -115,7 +116,6 @@ func (ms *MapStorage) SetRatingPlan(rp *RatingPlan) (err error) { ms.dict[RATING_PLAN_PREFIX+rp.Id] = result response := 0 go historyScribe.Record(&history.Record{RATING_PLAN_PREFIX + rp.Id, rp}, &response) - cache2go.Cache(RATING_PLAN_PREFIX+rp.Id, rp) return } @@ -143,7 +143,6 @@ func (ms *MapStorage) SetRatingProfile(rpf *RatingProfile) (err error) { ms.dict[RATING_PROFILE_PREFIX+rpf.Id] = result response := 0 go historyScribe.Record(&history.Record{RATING_PROFILE_PREFIX + rpf.Id, rpf}, &response) - cache2go.Cache(RATING_PROFILE_PREFIX+rpf.Id, rpf) return } @@ -158,6 +157,15 @@ func (ms *MapStorage) GetDestination(key string, checkDb bool) (dest *Destinatio if values, ok := ms.dict[key]; ok { dest = &Destination{Id: key} err = ms.ms.Unmarshal(values, dest) + // create optimized structure + for _, p := range dest.Prefixes { + var ids []string + if x, err := cache2go.GetCached(p); err == nil { + ids = x.([]string) + } + ids = append(ids, dest.Id) + cache2go.Cache(p, ids) + } dest.OptimizePrefixes() cache2go.Cache(key, dest) } else { @@ -171,7 +179,6 @@ func (ms *MapStorage) SetDestination(dest *Destination) (err error) { ms.dict[DESTINATION_PREFIX+dest.Id] = result response := 0 go historyScribe.Record(&history.Record{DESTINATION_PREFIX + dest.Id, dest}, &response) - cache2go.Cache(DESTINATION_PREFIX+dest.Id, dest) return } @@ -202,7 +209,6 @@ func (ms *MapStorage) GetActions(key string, checkDb bool) (as Actions, err erro func (ms *MapStorage) SetActions(key string, as Actions) (err error) { result, err := ms.ms.Marshal(&as) ms.dict[ACTION_PREFIX+key] = result - cache2go.Cache(DESTINATION_PREFIX+key, as) return } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index c3dac36b6..edc05d198 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -190,7 +190,6 @@ func (rs *RedisStorage) SetRatingPlan(rp *RatingPlan) (err error) { response := 0 go historyScribe.Record(&history.Record{RATING_PLAN_PREFIX + rp.Id, rp}, &response) } - cache2go.Cache(RATING_PLAN_PREFIX+rp.Id, rp) return } @@ -218,7 +217,6 @@ func (rs *RedisStorage) SetRatingProfile(rpf *RatingProfile) (err error) { response := 0 go historyScribe.Record(&history.Record{RATING_PROFILE_PREFIX + rpf.Id, rpf}, &response) } - cache2go.Cache(RATING_PROFILE_PREFIX+rpf.Id, rpf) return } @@ -244,6 +242,15 @@ func (rs *RedisStorage) GetDestination(key string, checkDb bool) (dest *Destinat r.Close() dest = new(Destination) err = rs.ms.Unmarshal(out, dest) + // create optimized structure + for _, p := range dest.Prefixes { + var ids []string + if x, err := cache2go.GetCached(p); err == nil { + ids = x.([]string) + } + ids = append(ids, dest.Id) + cache2go.Cache(p, ids) + } dest.OptimizePrefixes() cache2go.Cache(key, dest) } @@ -264,7 +271,6 @@ func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { response := 0 go historyScribe.Record(&history.Record{DESTINATION_PREFIX + dest.Id, dest}, &response) } - cache2go.Cache(DESTINATION_PREFIX+dest.Id, dest) return } @@ -287,7 +293,6 @@ func (rs *RedisStorage) GetActions(key string, checkDb bool) (as Actions, err er func (rs *RedisStorage) SetActions(key string, as Actions) (err error) { result, err := rs.ms.Marshal(&as) err = rs.db.Set(ACTION_PREFIX+key, result) - cache2go.Cache(ACTION_PREFIX+key, as) return }