From b669d1d326b2cdd678326be9c16044444fb9769d Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 28 Nov 2013 18:09:24 +0200 Subject: [PATCH] Added bool parameter for searching datadb for cached values or not --- engine/calldesc.go | 10 ++++++++++ engine/destinations_test.go | 14 ++++++++------ engine/ratingplan_test.go | 4 +++- engine/ratingprofile.go | 4 ++-- engine/storage_interface.go | 6 ++++-- engine/storage_map.go | 29 ++++++++++++----------------- engine/storage_redis.go | 18 ++++++++++++++---- engine/storage_test.go | 10 +++++----- engine/units_counter.go | 2 +- engine/userbalance.go | 4 +++- hard_update_external_libs.py | 5 +---- update_external_libs.sh | 1 - 12 files changed, 63 insertions(+), 44 deletions(-) diff --git a/engine/calldesc.go b/engine/calldesc.go index 5ec275d38..d7e85c88d 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -108,6 +108,16 @@ type CallDescriptor struct { userBalance *UserBalance } +func (cd *CallDescriptor) ValidateCallData() error { + if cd.TimeStart.After(cd.TimeEnd) || cd.TimeStart.Equal(cd.TimeEnd) { + return errors.New("TimeStart must be strctly before TimeEnd") + } + if cd.TimeEnd.Sub(cd.TimeStart) < cd.CallDuration { + return errors.New("CallDuration must be equal or grater than TimeEnd - TimeStart") + } + return nil +} + // Adds a rating plan that applyes to current call descriptor. func (cd *CallDescriptor) AddRatingInfo(ris ...*RatingInfo) { cd.RatingInfos = append(cd.RatingInfos, ris...) diff --git a/engine/destinations_test.go b/engine/destinations_test.go index 09979939e..55004ead0 100644 --- a/engine/destinations_test.go +++ b/engine/destinations_test.go @@ -20,8 +20,10 @@ package engine import ( "encoding/json" + "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/utils" + "testing" ) @@ -42,7 +44,7 @@ func TestDestinationStorageStore(t *testing.T) { if err != nil { t.Error("Error storing destination: ", err) } - result, err := storageGetter.GetDestination(nationale.Id) + result, err := storageGetter.GetDestination(nationale.Id, false) var ss utils.StringSlice = result.Prefixes if !ss.Contains("0257") || !ss.Contains("0256") || !ss.Contains("0723") { t.Errorf("Expected %q was %q", nationale, result) @@ -74,28 +76,28 @@ func TestDestinationContainsPrefixWrong(t *testing.T) { } func TestDestinationGetExists(t *testing.T) { - d, err := storageGetter.GetDestination("NAT") + d, err := storageGetter.GetDestination("NAT", false) if err != nil || d == nil { t.Error("Could not get destination: ", d) } } func TestDestinationGetExistsCache(t *testing.T) { - storageGetter.GetDestination("NAT") + storageGetter.GetDestination("NAT", false) if _, err := cache2go.GetCached("NAT"); err != nil { t.Error("Destination not cached:", err) } } func TestDestinationGetNotExists(t *testing.T) { - d, err := storageGetter.GetDestination("not existing") + d, err := storageGetter.GetDestination("not existing", false) if d != nil { t.Error("Got false destination: ", d, err) } } func TestDestinationGetNotExistsCache(t *testing.T) { - storageGetter.GetDestination("not existing") + storageGetter.GetDestination("not existing", false) if d, err := cache2go.GetCached("not existing"); err == nil { t.Error("Bad destination cached: ", d) } @@ -156,6 +158,6 @@ func BenchmarkDestinationStorageStoreRestore(b *testing.B) { nationale := &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} for i := 0; i < b.N; i++ { storageGetter.SetDestination(nationale) - storageGetter.GetDestination(nationale.Id) + storageGetter.GetDestination(nationale.Id, true) } } diff --git a/engine/ratingplan_test.go b/engine/ratingplan_test.go index 5be37e5b1..4c4b179b9 100644 --- a/engine/ratingplan_test.go +++ b/engine/ratingplan_test.go @@ -20,7 +20,9 @@ package engine import ( "encoding/json" + "github.com/cgrates/cgrates/utils" + "reflect" "testing" "time" @@ -270,6 +272,6 @@ func BenchmarkRatingPlanRestore(b *testing.B) { storageGetter.SetRatingPlan(rp) b.ResetTimer() for i := 0; i < b.N; i++ { - storageGetter.GetRatingPlan(rp.Id) + storageGetter.GetRatingPlan(rp.Id, true) } } diff --git a/engine/ratingprofile.go b/engine/ratingprofile.go index d5e6b6122..d49d7c26a 100644 --- a/engine/ratingprofile.go +++ b/engine/ratingprofile.go @@ -103,7 +103,7 @@ func (ris RatingInfos) Sort() { func (rp *RatingProfile) GetRatingPlansForPrefix(cd *CallDescriptor) (err error) { var ris RatingInfos for index, rpa := range rp.RatingPlanActivations.GetActiveForCall(cd) { - rpl, err := storageGetter.GetRatingPlan(rpa.RatingPlanId) + rpl, err := storageGetter.GetRatingPlan(rpa.RatingPlanId, false) if err != nil || rpl == nil { Logger.Err(fmt.Sprintf("Error checking destination: %v", err)) continue @@ -112,7 +112,7 @@ func (rp *RatingProfile) GetRatingPlansForPrefix(cd *CallDescriptor) (err error) var rps RateIntervalList for dId, _ := range rpl.DestinationRates { //precision, err := storageGetter.DestinationContainsPrefix(dId, cd.Destination) - d, err := storageGetter.GetDestination(dId) + d, err := storageGetter.GetDestination(dId, false) if err != nil { Logger.Err(fmt.Sprintf("Error checking destination: %v", err)) continue diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 63b453b0a..7c3c038e0 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -24,9 +24,11 @@ import ( "encoding/gob" "encoding/json" "fmt" + "github.com/cgrates/cgrates/utils" "github.com/ugorji/go/codec" "labix.org/v2/mgo/bson" + "reflect" "time" ) @@ -70,11 +72,11 @@ type DataStorage interface { Storage PreCache([]string, []string) error ExistsData(string, string) (bool, error) - GetRatingPlan(string) (*RatingPlan, error) + GetRatingPlan(string, bool) (*RatingPlan, error) SetRatingPlan(*RatingPlan) error GetRatingProfile(string) (*RatingProfile, error) SetRatingProfile(*RatingProfile) error - GetDestination(string) (*Destination, error) + GetDestination(string, bool) (*Destination, error) // DestinationContainsPrefix(string, string) (int, error) SetDestination(*Destination) error GetActions(string) (Actions, error) diff --git a/engine/storage_map.go b/engine/storage_map.go index a2d61a727..ae0eb134f 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -51,13 +51,13 @@ func (ms *MapStorage) PreCache(dKeys, rppKeys []string) error { for k, _ := range ms.dict { if strings.HasPrefix(k, DESTINATION_PREFIX) { cache2go.RemKey(k[prefixLen:]) - if _, err := ms.GetDestination(k[prefixLen:]); err != nil { + if _, err := ms.GetDestination(k[prefixLen:], true); err != nil { return err } } if strings.HasPrefix(k, RATING_PLAN_PREFIX) { cache2go.RemKey(k[prefixLen1:]) - if _, err := ms.GetRatingPlan(k[prefixLen1:]); err != nil { + if _, err := ms.GetRatingPlan(k[prefixLen1:], true); err != nil { return err } } @@ -78,10 +78,13 @@ func (ms *MapStorage) ExistsData(categ, subject string) (bool, error) { return false, errors.New("Unsupported category") } -func (ms *MapStorage) GetRatingPlan(key string) (rp *RatingPlan, err error) { +func (ms *MapStorage) GetRatingPlan(key string, checkDb bool) (rp *RatingPlan, err error) { if x, err := cache2go.GetCached(key); err == nil { return x.(*RatingPlan), nil } + if !checkDb { + return nil, errors.New(utils.ERR_NOT_FOUND) + } if values, ok := ms.dict[RATING_PLAN_PREFIX+key]; ok { rp = new(RatingPlan) @@ -98,6 +101,7 @@ 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(rp.Id, rp) return } @@ -120,10 +124,13 @@ func (ms *MapStorage) SetRatingProfile(rp *RatingProfile) (err error) { return } -func (ms *MapStorage) GetDestination(key string) (dest *Destination, err error) { +func (ms *MapStorage) GetDestination(key string, checkDb bool) (dest *Destination, err error) { if x, err := cache2go.GetCached(key); err == nil { return x.(*Destination), nil } + if !checkDb { + return nil, errors.New(utils.ERR_NOT_FOUND) + } if values, ok := ms.dict[DESTINATION_PREFIX+key]; ok { dest = &Destination{Id: key} err = ms.ms.Unmarshal(values, dest) @@ -134,24 +141,12 @@ func (ms *MapStorage) GetDestination(key string) (dest *Destination, err error) return } -func (ms *MapStorage) DestinationContainsPrefix(key string, prefix string) (precision int, err error) { - if d, err := ms.GetDestination(key); err != nil { - return 0, err - } else { - for _, p := range utils.SplitPrefixInterface(prefix) { - if precision := d.containsPrefix(p.(string)); precision > 0 { - return precision, nil - } - } - return precision, nil - } -} - func (ms *MapStorage) SetDestination(dest *Destination) (err error) { result, err := ms.ms.Marshal(dest) ms.dict[DESTINATION_PREFIX+dest.Id] = result response := 0 go historyScribe.Record(&history.Record{DESTINATION_PREFIX + dest.Id, dest}, &response) + cache2go.Cache(dest.Id, dest) return } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index f0b89bf6f..48b747ec0 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -23,10 +23,12 @@ import ( "compress/zlib" "errors" "fmt" + "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/history" "github.com/cgrates/cgrates/utils" "github.com/hoisie/redis" + "io/ioutil" "time" ) @@ -75,7 +77,7 @@ func (rs *RedisStorage) PreCache(dKeys, rpKeys []string) (err error) { prefixLen := len(DESTINATION_PREFIX) for _, key := range dKeys { cache2go.RemKey(key[prefixLen:]) - if _, err = rs.GetDestination(key[prefixLen:]); err != nil { + if _, err = rs.GetDestination(key[prefixLen:], true); err != nil { return err } } @@ -87,7 +89,7 @@ func (rs *RedisStorage) PreCache(dKeys, rpKeys []string) (err error) { prefixLen = len(RATING_PLAN_PREFIX) for _, key := range rpKeys { cache2go.RemKey(key[prefixLen:]) - if _, err = rs.GetRatingPlan(key[prefixLen:]); err != nil { + if _, err = rs.GetRatingPlan(key[prefixLen:], true); err != nil { return err } } @@ -103,10 +105,13 @@ func (rs *RedisStorage) ExistsData(category, subject string) (bool, error) { return false, errors.New("Unsupported category in ExistsData") } -func (rs *RedisStorage) GetRatingPlan(key string) (rp *RatingPlan, err error) { +func (rs *RedisStorage) GetRatingPlan(key string, checkDb bool) (rp *RatingPlan, err error) { if x, err := cache2go.GetCached(key); err == nil { return x.(*RatingPlan), nil } + if !checkDb { + return nil, errors.New(utils.ERR_NOT_FOUND) + } var values []byte if values, err = rs.db.Get(RATING_PLAN_PREFIX + key); err == nil { b := bytes.NewBuffer(values) @@ -137,6 +142,7 @@ func (rs *RedisStorage) SetRatingPlan(rp *RatingPlan) (err error) { response := 0 historyScribe.Record(&history.Record{RATING_PLAN_PREFIX + rp.Id, rp}, &response) } + cache2go.Cache(rp.Id, rp) return } @@ -159,10 +165,13 @@ func (rs *RedisStorage) SetRatingProfile(rp *RatingProfile) (err error) { return } -func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error) { +func (rs *RedisStorage) GetDestination(key string, checkDb bool) (dest *Destination, err error) { if x, err := cache2go.GetCached(key); err == nil { return x.(*Destination), nil } + if !checkDb { + return nil, errors.New(utils.ERR_NOT_FOUND) + } var values []byte if values, err = rs.db.Get(DESTINATION_PREFIX + key); len(values) > 0 && err == nil { b := bytes.NewBuffer(values) @@ -196,6 +205,7 @@ func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { response := 0 historyScribe.Record(&history.Record{DESTINATION_PREFIX + dest.Id, dest}, &response) } + cache2go.Cache(dest.Id, dest) return } diff --git a/engine/storage_test.go b/engine/storage_test.go index 477419808..b51707d3f 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -72,7 +72,7 @@ func TestMsgpackTime(t *testing.T) { } func TestStorageDestinationContainsPrefixShort(t *testing.T) { - dest, err := storageGetter.GetDestination("NAT") + dest, err := storageGetter.GetDestination("NAT", false) precision := dest.containsPrefix("0723") if err != nil || precision != 4 { t.Error("Error finding prefix: ", err, precision) @@ -80,7 +80,7 @@ func TestStorageDestinationContainsPrefixShort(t *testing.T) { } func TestStorageDestinationContainsPrefixLong(t *testing.T) { - dest, err := storageGetter.GetDestination("NAT") + dest, err := storageGetter.GetDestination("NAT", false) precision := dest.containsPrefix("0723045326") if err != nil || precision != 4 { t.Error("Error finding prefix: ", err, precision) @@ -88,7 +88,7 @@ func TestStorageDestinationContainsPrefixLong(t *testing.T) { } func TestStorageDestinationContainsPrefixNotExisting(t *testing.T) { - dest, err := storageGetter.GetDestination("NAT") + dest, err := storageGetter.GetDestination("NAT", false) precision := dest.containsPrefix("072") if err != nil || precision != 0 { t.Error("Error finding prefix: ", err, precision) @@ -97,10 +97,10 @@ func TestStorageDestinationContainsPrefixNotExisting(t *testing.T) { func TestPreCacheRefresh(t *testing.T) { storageGetter.SetDestination(&Destination{"T11", []string{"0"}}) - storageGetter.GetDestination("T11") + storageGetter.GetDestination("T11", false) storageGetter.SetDestination(&Destination{"T11", []string{"1"}}) storageGetter.PreCache(nil, nil) - if d, err := storageGetter.GetDestination("T11"); err != nil || d.Prefixes[0] != "1" { + if d, err := storageGetter.GetDestination("T11", false); err != nil || d.Prefixes[0] != "1" { t.Error("Error refreshing cache:", d) } } diff --git a/engine/units_counter.go b/engine/units_counter.go index b1a83a83f..36f3b9ecd 100644 --- a/engine/units_counter.go +++ b/engine/units_counter.go @@ -52,7 +52,7 @@ 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 { - dest, err := storageGetter.GetDestination(mb.DestinationId) + dest, err := storageGetter.GetDestination(mb.DestinationId, false) if err != nil { Logger.Err(fmt.Sprintf("Minutes counter: unknown destination: %v", mb.DestinationId)) continue diff --git a/engine/userbalance.go b/engine/userbalance.go index 4b26d1d22..b209cf6df 100644 --- a/engine/userbalance.go +++ b/engine/userbalance.go @@ -21,7 +21,9 @@ package engine import ( "errors" "fmt" + "github.com/cgrates/cgrates/utils" + "strings" "time" ) @@ -131,7 +133,7 @@ func (ub *UserBalance) getBalancesForPrefix(prefix string, balances BalanceChain continue } if b.DestinationId != "" { - dest, err := storageGetter.GetDestination(b.DestinationId) + dest, err := storageGetter.GetDestination(b.DestinationId, false) if err != nil { continue } diff --git a/hard_update_external_libs.py b/hard_update_external_libs.py index 8e5d75915..4affa8562 100755 --- a/hard_update_external_libs.py +++ b/hard_update_external_libs.py @@ -4,15 +4,12 @@ import os import os.path from subprocess import call -libs = ('github.com/fzzy/radix/redis', - 'code.google.com/p/goconf/conf', +libs = ('code.google.com/p/goconf/conf', 'github.com/bmizerany/pq', 'github.com/ugorji/go/codec', 'labix.org/v2/mgo', 'github.com/cgrates/fsock', 'github.com/go-sql-driver/mysql', - 'github.com/garyburd/redigo/redis', - 'menteslibres.net/gosexy/redis', 'github.com/hoisie/redis' 'github.com/howeyc/fsnotify', ) diff --git a/update_external_libs.sh b/update_external_libs.sh index dc6687464..60624e358 100755 --- a/update_external_libs.sh +++ b/update_external_libs.sh @@ -5,7 +5,6 @@ go get -v -u github.com/ugorji/go/codec go get -v -u labix.org/v2/mgo go get -v -u github.com/cgrates/fsock go get -u -v github.com/go-sql-driver/mysql -go get -u -v menteslibres.net/gosexy/redis go get -u -v github.com/hoisie/redis go get -u -v github.com/howeyc/fsnotify