Added bool parameter for searching datadb for cached values or not

This commit is contained in:
Radu Ioan Fericean
2013-11-28 18:09:24 +02:00
parent 8e92c51da2
commit b669d1d326
12 changed files with 63 additions and 44 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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