destinations using string and compression

This commit is contained in:
Radu Ioan Fericean
2013-10-29 16:57:55 +02:00
parent 4f7501a24c
commit fb80c6c2a8
5 changed files with 35 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

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