mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add GetRatingPlan in DataManager
This commit is contained in:
committed by
Dan Christian Bogos
parent
35ad39685e
commit
7ff3f9b39a
@@ -169,7 +169,7 @@ func (self *ApierV1) SetDestination(attrs utils.AttrSetDestination, reply *strin
|
||||
}
|
||||
|
||||
func (self *ApierV1) GetRatingPlan(rplnId string, reply *engine.RatingPlan) error {
|
||||
if rpln, err := self.DataManager.DataDB().GetRatingPlan(rplnId, false, utils.NonTransactional); err != nil {
|
||||
if rpln, err := self.DataManager.GetRatingPlan(rplnId, false, utils.NonTransactional); err != nil {
|
||||
return utils.ErrNotFound
|
||||
} else {
|
||||
*reply = *rpln
|
||||
|
||||
@@ -93,7 +93,7 @@ func (dm *DataManager) PreloadCacheForPrefix(prefix string) error {
|
||||
switch prefix {
|
||||
case utils.RATING_PLAN_PREFIX:
|
||||
for _, key := range keyList {
|
||||
_, err := dm.DataDB().GetRatingPlan(key[len(utils.RATING_PLAN_PREFIX):], true, transID)
|
||||
_, err := dm.GetRatingPlan(key[len(utils.RATING_PLAN_PREFIX):], true, transID)
|
||||
if err != nil {
|
||||
cache.RollbackTransaction(transID)
|
||||
return err
|
||||
@@ -165,7 +165,7 @@ func (dm *DataManager) CacheDataFromDB(prfx string, ids []string, mustBeCached b
|
||||
case utils.REVERSE_DESTINATION_PREFIX:
|
||||
_, err = dm.DataDB().GetReverseDestination(dataID, true, utils.NonTransactional)
|
||||
case utils.RATING_PLAN_PREFIX:
|
||||
_, err = dm.DataDB().GetRatingPlan(dataID, true, utils.NonTransactional)
|
||||
_, err = dm.GetRatingPlan(dataID, true, utils.NonTransactional)
|
||||
case utils.RATING_PROFILE_PREFIX:
|
||||
_, err = dm.DataDB().GetRatingProfile(dataID, true, utils.NonTransactional)
|
||||
case utils.ACTION_PREFIX:
|
||||
@@ -664,3 +664,25 @@ func (dm *DataManager) RemoveActions(key, transactionID string) (err error) {
|
||||
cache.RemKey(utils.ACTION_PREFIX+key, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *DataManager) GetRatingPlan(key string, skipCache bool, transactionID string) (rp *RatingPlan, err error) {
|
||||
cachekey := utils.RATING_PLAN_PREFIX + key
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(cachekey); ok {
|
||||
if x != nil {
|
||||
return x.(*RatingPlan), nil
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
}
|
||||
rp, err = dm.DataDB().GetRatingPlanDrv(key)
|
||||
if err != nil {
|
||||
if err == utils.ErrNotFound {
|
||||
cache.Set(cachekey, nil, cacheCommit(transactionID), transactionID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
cache.Set(cachekey, rp, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
@@ -231,7 +231,7 @@ func TestLoaderITWriteToDatabase(t *testing.T) {
|
||||
}
|
||||
|
||||
for k, rp := range loader.ratingPlans {
|
||||
rcv, err := loader.dataStorage.GetRatingPlan(k, true, utils.NonTransactional)
|
||||
rcv, err := loader.dm.GetRatingPlan(k, true, utils.NonTransactional)
|
||||
if err != nil {
|
||||
t.Error("Failed GetRatingPlan: ", err.Error())
|
||||
}
|
||||
|
||||
@@ -1021,7 +1021,7 @@ func testOnStorITCRUDRatingPlan(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, rcvErr := onStor.DataDB().GetRatingPlan(rp.Id, true, utils.NonTransactional); rcvErr != utils.ErrNotFound {
|
||||
if _, rcvErr := onStor.GetRatingPlan(rp.Id, true, utils.NonTransactional); rcvErr != utils.ErrNotFound {
|
||||
t.Error(rcvErr)
|
||||
}
|
||||
if err := onStor.DataDB().SetRatingPlan(rp, utils.NonTransactional); err != nil {
|
||||
@@ -1035,7 +1035,7 @@ func testOnStorITCRUDRatingPlan(t *testing.T) {
|
||||
t.Errorf("Expected : %+v, but received %+v", len(expectedRP), len(itm))
|
||||
}
|
||||
|
||||
if rcv, err := onStor.DataDB().GetRatingPlan(rp.Id, true, utils.NonTransactional); err != nil {
|
||||
if rcv, err := onStor.GetRatingPlan(rp.Id, true, utils.NonTransactional); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(rp, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", rp, rcv)
|
||||
@@ -1048,7 +1048,7 @@ func testOnStorITCRUDRatingPlan(t *testing.T) {
|
||||
// t.Error(rcvErr)
|
||||
// }
|
||||
|
||||
if rcv, err := onStor.DataDB().GetRatingPlan(rp.Id, false, utils.NonTransactional); err != nil {
|
||||
if rcv, err := onStor.GetRatingPlan(rp.Id, false, utils.NonTransactional); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(rp, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", rp, rcv)
|
||||
|
||||
@@ -451,6 +451,6 @@ func BenchmarkRatingPlanRestore(b *testing.B) {
|
||||
dm.DataDB().SetRatingPlan(rp, utils.NonTransactional)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
dm.DataDB().GetRatingPlan(rp.Id, true, utils.NonTransactional)
|
||||
dm.GetRatingPlan(rp.Id, true, utils.NonTransactional)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ func (ris RatingInfos) String() string {
|
||||
func (rpf *RatingProfile) GetRatingPlansForPrefix(cd *CallDescriptor) (err error) {
|
||||
var ris RatingInfos
|
||||
for index, rpa := range rpf.RatingPlanActivations.GetActiveForCall(cd) {
|
||||
rpl, err := dm.DataDB().GetRatingPlan(rpa.RatingPlanId, false, utils.NonTransactional)
|
||||
rpl, err := dm.GetRatingPlan(rpa.RatingPlanId, false, utils.NonTransactional)
|
||||
if err != nil || rpl == nil {
|
||||
utils.Logger.Err(fmt.Sprintf("Error checking destination: %v", err))
|
||||
continue
|
||||
|
||||
@@ -48,7 +48,7 @@ type DataDB interface {
|
||||
Storage
|
||||
Marshaler() Marshaler
|
||||
HasData(string, string) (bool, error)
|
||||
GetRatingPlan(string, bool, string) (*RatingPlan, error)
|
||||
GetRatingPlanDrv(string) (*RatingPlan, error)
|
||||
SetRatingPlan(*RatingPlan, string) error
|
||||
GetRatingProfile(string, bool, string) (*RatingProfile, error)
|
||||
SetRatingProfile(*RatingProfile, string) error
|
||||
|
||||
@@ -178,19 +178,10 @@ func (ms *MapStorage) HasData(categ, subject string) (bool, error) {
|
||||
return false, errors.New("Unsupported HasData category")
|
||||
}
|
||||
|
||||
func (ms *MapStorage) GetRatingPlan(key string, skipCache bool, transactionID string) (rp *RatingPlan, err error) {
|
||||
func (ms *MapStorage) GetRatingPlanDrv(key string) (rp *RatingPlan, err error) {
|
||||
ms.mu.RLock()
|
||||
defer ms.mu.RUnlock()
|
||||
key = utils.RATING_PLAN_PREFIX + key
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(key); ok {
|
||||
if x != nil {
|
||||
return x.(*RatingPlan), nil
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
}
|
||||
cCommit := cacheCommit(transactionID)
|
||||
if values, ok := ms.dict[key]; ok {
|
||||
b := bytes.NewBuffer(values)
|
||||
r, err := zlib.NewReader(b)
|
||||
@@ -202,13 +193,10 @@ func (ms *MapStorage) GetRatingPlan(key string, skipCache bool, transactionID st
|
||||
return nil, err
|
||||
}
|
||||
r.Close()
|
||||
rp = new(RatingPlan)
|
||||
err = ms.ms.Unmarshal(out, rp)
|
||||
err = ms.ms.Unmarshal(out, &rp)
|
||||
} else {
|
||||
cache.Set(key, nil, cCommit, transactionID)
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
cache.Set(key, rp, cCommit, transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -616,17 +616,7 @@ func (ms *MongoStorage) HasData(category, subject string) (has bool, err error)
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetRatingPlan(key string, skipCache bool, transactionID string) (rp *RatingPlan, err error) {
|
||||
cacheKey := utils.RATING_PLAN_PREFIX + key
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(cacheKey); ok {
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return x.(*RatingPlan), nil
|
||||
}
|
||||
}
|
||||
rp = new(RatingPlan)
|
||||
func (ms *MongoStorage) GetRatingPlanDrv(key string) (rp *RatingPlan, err error) {
|
||||
var kv struct {
|
||||
Key string
|
||||
Value []byte
|
||||
@@ -635,7 +625,6 @@ func (ms *MongoStorage) GetRatingPlan(key string, skipCache bool, transactionID
|
||||
defer session.Close()
|
||||
if err = col.Find(bson.M{"key": key}).One(&kv); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
cache.Set(cacheKey, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
@@ -653,7 +642,6 @@ func (ms *MongoStorage) GetRatingPlan(key string, skipCache bool, transactionID
|
||||
if err = ms.ms.Unmarshal(out, &rp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cache.Set(cacheKey, rp, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -215,20 +215,11 @@ func (rs *RedisStorage) HasData(category, subject string) (bool, error) {
|
||||
return false, errors.New("unsupported HasData category")
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetRatingPlan(key string, skipCache bool, transactionID string) (rp *RatingPlan, err error) {
|
||||
func (rs *RedisStorage) GetRatingPlanDrv(key string) (rp *RatingPlan, err error) {
|
||||
key = utils.RATING_PLAN_PREFIX + key
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(key); ok {
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return x.(*RatingPlan), nil
|
||||
}
|
||||
}
|
||||
var values []byte
|
||||
if values, err = rs.Cmd("GET", key).Bytes(); err != nil {
|
||||
if err == redis.ErrRespNil { // did not find the destination
|
||||
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
@@ -243,12 +234,10 @@ func (rs *RedisStorage) GetRatingPlan(key string, skipCache bool, transactionID
|
||||
return nil, err
|
||||
}
|
||||
r.Close()
|
||||
rp = new(RatingPlan)
|
||||
err = rs.ms.Unmarshal(out, rp)
|
||||
err = rs.ms.Unmarshal(out, &rp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cache.Set(key, rp, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user