mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-25 17:18:44 +05:00
prepared new getter and setter from db
This commit is contained in:
@@ -99,12 +99,16 @@ func (ap *ActivationPeriod) restore(input string) {
|
||||
}
|
||||
|
||||
type RatingProfile struct {
|
||||
Id string `bson:"_id,omitempty"`
|
||||
DestinationInfo string
|
||||
FallbackKey string
|
||||
ActivationPeriods []*ActivationPeriod
|
||||
}
|
||||
|
||||
func (rp *RatingProfile) store() (result string) {
|
||||
result += rp.Id + ">"
|
||||
result += rp.DestinationInfo + ">"
|
||||
result += rp.FallbackKey + ">"
|
||||
for _, ap := range rp.ActivationPeriods {
|
||||
result += ap.store() + "<"
|
||||
}
|
||||
@@ -114,8 +118,10 @@ func (rp *RatingProfile) store() (result string) {
|
||||
|
||||
func (rp *RatingProfile) restore(input string) {
|
||||
elements := strings.Split(input, ">")
|
||||
rp.DestinationInfo = elements[0]
|
||||
apsList := strings.Split(elements[1], "<")
|
||||
rp.Id = elements[0]
|
||||
rp.DestinationInfo = elements[1]
|
||||
rp.FallbackKey = elements[2]
|
||||
apsList := strings.Split(elements[3], "<")
|
||||
for _, aps := range apsList {
|
||||
ap := new(ActivationPeriod)
|
||||
ap.restore(aps)
|
||||
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -38,8 +37,8 @@ Interface for storage providers.
|
||||
type StorageGetter interface {
|
||||
Close()
|
||||
Flush() error
|
||||
GetActivationPeriodsOrFallback(string) ([]*ActivationPeriod, string, error)
|
||||
SetActivationPeriodsOrFallback(string, []*ActivationPeriod, string) error
|
||||
GetRatingProfile(string) (*RatingProfile, error)
|
||||
SetRatingProfile(*RatingProfile) error
|
||||
GetDestination(string) (*Destination, error)
|
||||
SetDestination(*Destination) error
|
||||
GetActions(string) ([]*Action, error)
|
||||
@@ -117,12 +116,6 @@ type MyMarshaler struct {
|
||||
|
||||
func (mm *MyMarshaler) Marshal(v interface{}) (data []byte, err error) {
|
||||
switch v.(type) {
|
||||
case []*ActivationPeriod:
|
||||
result := ""
|
||||
for _, ap := range v.([]*ActivationPeriod) {
|
||||
result += ap.store() + "\n"
|
||||
}
|
||||
return []byte(result), nil
|
||||
case []*Action:
|
||||
result := ""
|
||||
for _, a := range v.([]*Action) {
|
||||
@@ -148,20 +141,6 @@ func (mm *MyMarshaler) Marshal(v interface{}) (data []byte, err error) {
|
||||
|
||||
func (mm *MyMarshaler) Unmarshal(data []byte, v interface{}) (err error) {
|
||||
switch v.(type) {
|
||||
case *[]*ActivationPeriod:
|
||||
aps := v.(*[]*ActivationPeriod)
|
||||
splits := strings.Split(string(data), "\n")
|
||||
if len(splits) == 0 {
|
||||
return errors.New("")
|
||||
}
|
||||
for _, ap_string := range splits {
|
||||
if len(ap_string) > 0 {
|
||||
ap := &ActivationPeriod{}
|
||||
ap.restore(ap_string)
|
||||
*aps = append(*aps, ap)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
case *[]*Action:
|
||||
as := v.(*[]*Action)
|
||||
for _, a_string := range strings.Split(string(data), "\n") {
|
||||
|
||||
@@ -41,27 +41,19 @@ func (ms *MapStorage) Flush() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *MapStorage) GetActivationPeriodsOrFallback(key string) (aps []*ActivationPeriod, fallbackKey string, err error) {
|
||||
elem, ok := ms.dict[key]
|
||||
if !ok {
|
||||
err = errors.New(fmt.Sprintf("%s not found!", key))
|
||||
return
|
||||
}
|
||||
err = ms.ms.Unmarshal(elem, &aps)
|
||||
if err != nil {
|
||||
err = ms.ms.Unmarshal(elem, &fallbackKey)
|
||||
func (ms *MapStorage) GetRatingProfile(key string) (rp *RatingProfile, err error) {
|
||||
if values, ok := ms.dict[key]; ok {
|
||||
rp = new(RatingProfile)
|
||||
err = ms.ms.Unmarshal(values, rp)
|
||||
} else {
|
||||
return nil, errors.New("not found")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MapStorage) SetActivationPeriodsOrFallback(key string, aps []*ActivationPeriod, fallbackKey string) (err error) {
|
||||
var result []byte
|
||||
if len(aps) > 0 {
|
||||
result, err = ms.ms.Marshal(aps)
|
||||
} else {
|
||||
result, err = ms.ms.Marshal(fallbackKey)
|
||||
}
|
||||
ms.dict[key] = result
|
||||
func (ms *MapStorage) SetRatingProfile(rp *RatingProfile) (err error) {
|
||||
result, err := ms.ms.Marshal(rp.Id)
|
||||
ms.dict[rp.Id] = result
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -46,12 +46,11 @@ func NewMongoStorage(host, port, db, user, pass string) (StorageGetter, error) {
|
||||
ndb := session.DB(db)
|
||||
session.SetMode(mgo.Monotonic, true)
|
||||
index := mgo.Index{Key: []string{"key"}, Background: true}
|
||||
err = ndb.C("activationperiods").EnsureIndex(index)
|
||||
err = ndb.C("destinations").EnsureIndex(index)
|
||||
err = ndb.C("actions").EnsureIndex(index)
|
||||
index = mgo.Index{Key: []string{"id"}, Background: true}
|
||||
err = ndb.C("userbalances").EnsureIndex(index)
|
||||
err = ndb.C("actiontimings").EnsureIndex(index)
|
||||
index = mgo.Index{Key: []string{"id"}, Background: true}
|
||||
err = ndb.C("destinations").EnsureIndex(index)
|
||||
err = ndb.C("userbalances").EnsureIndex(index)
|
||||
|
||||
return &MongoStorage{db: ndb, session: session}, nil
|
||||
}
|
||||
@@ -61,7 +60,7 @@ func (ms *MongoStorage) Close() {
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) Flush() (err error) {
|
||||
err = ms.db.C("activationperiods").DropCollection()
|
||||
err = ms.db.C("ratingprofiles").DropCollection()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -84,15 +83,6 @@ func (ms *MongoStorage) Flush() (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
Helper type for activation periods storage.
|
||||
*/
|
||||
type ApKeyValue struct {
|
||||
Key string
|
||||
FallbackKey string `omitempty`
|
||||
Value []*ActivationPeriod
|
||||
}
|
||||
|
||||
type AcKeyValue struct {
|
||||
Key string
|
||||
Value []*Action
|
||||
@@ -121,14 +111,14 @@ type LogTriggerEntry struct {
|
||||
LogTime time.Time
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetActivationPeriodsOrFallback(key string) ([]*ActivationPeriod, string, error) {
|
||||
result := new(ApKeyValue)
|
||||
err := ms.db.C("activationperiods").Find(bson.M{"key": key}).One(&result)
|
||||
return result.Value, result.FallbackKey, err
|
||||
func (ms *MongoStorage) GetRatingProfile(key string) (rp *RatingProfile, err error) {
|
||||
rp = new(RatingProfile)
|
||||
err = ms.db.C("ratingprofiles").Find(bson.M{"_id": key}).One(&rp)
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetActivationPeriodsOrFallback(key string, aps []*ActivationPeriod, fallbackKey string) error {
|
||||
return ms.db.C("activationperiods").Insert(&ApKeyValue{Key: key, FallbackKey: fallbackKey, Value: aps})
|
||||
func (ms *MongoStorage) SetRatingProfile(rp *RatingProfile) error {
|
||||
return ms.db.C("ratingprofiles").Insert(rp)
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetDestination(key string) (result *Destination, err error) {
|
||||
|
||||
@@ -42,11 +42,11 @@ func (psl *PostgresStorage) Flush() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (psl *PostgresStorage) GetActivationPeriodsOrFallback(string) (aps []*ActivationPeriod, fallback string, err error) {
|
||||
func (psl *PostgresStorage) GetRatingProfile(string) (rp *RatingProfile, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (psl *PostgresStorage) SetActivationPeriodsOrFallback(key string, aps []*ActivationPeriod, fallback string) (err error) {
|
||||
func (psl *PostgresStorage) SetRatingProfile(rp *RatingProfile) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -47,26 +47,19 @@ func (rs *RedisStorage) Flush() error {
|
||||
return rs.db.Flushdb()
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetActivationPeriodsOrFallback(key string) (aps []*ActivationPeriod, fallbackKey string, err error) {
|
||||
elem, err := rs.db.Get(key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = rs.ms.Unmarshal(elem, &aps)
|
||||
if err != nil {
|
||||
err = rs.ms.Unmarshal(elem, &fallbackKey)
|
||||
func (rs *RedisStorage) GetRatingProfile(key string) (rp *RatingProfile, err error) {
|
||||
if values, err := rs.db.Get(key); err == nil {
|
||||
rp = new(RatingProfile)
|
||||
err = rs.ms.Unmarshal(values, rp)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) SetActivationPeriodsOrFallback(key string, aps []*ActivationPeriod, fallbackKey string) (err error) {
|
||||
var result []byte
|
||||
if len(aps) > 0 {
|
||||
result, err = rs.ms.Marshal(aps)
|
||||
} else {
|
||||
result, err = rs.ms.Marshal(fallbackKey)
|
||||
}
|
||||
return rs.db.Set(key, result)
|
||||
func (rs *RedisStorage) SetRatingProfile(rp *RatingProfile) (err error) {
|
||||
result, err := rs.ms.Marshal(rp)
|
||||
return rs.db.Set(rp.Id, result)
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error) {
|
||||
|
||||
Reference in New Issue
Block a user