mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Fixes for db driver to avoid returning new values in case of errors
This commit is contained in:
@@ -653,7 +653,7 @@ func (ms *MongoStorage) GetRatingPlan(key string, skipCache bool, transactionID
|
||||
cache.Set(cacheKey, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
b := bytes.NewBuffer(kv.Value)
|
||||
r, err := zlib.NewReader(b)
|
||||
@@ -712,7 +712,7 @@ func (ms *MongoStorage) GetRatingProfile(key string, skipCache bool, transaction
|
||||
cache.Set(cacheKey, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
return nil, err // Make sure we don't return new object on error
|
||||
}
|
||||
cache.Set(cacheKey, rp, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
@@ -772,7 +772,7 @@ func (ms *MongoStorage) GetLCR(key string, skipCache bool, transactionID string)
|
||||
cache.Set(cacheKey, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
cache.Set(cacheKey, result.Value, cCommit, transactionID)
|
||||
return
|
||||
@@ -1003,7 +1003,7 @@ func (ms *MongoStorage) GetActions(key string, skipCache bool, transactionID str
|
||||
cache.Set(cacheKey, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
as = result.Value
|
||||
cache.Set(utils.ACTION_PREFIX+key, as, cacheCommit(transactionID), transactionID)
|
||||
@@ -1040,13 +1040,13 @@ func (ms *MongoStorage) GetSharedGroup(key string, skipCache bool, transactionID
|
||||
}
|
||||
session, col := ms.conn(colShg)
|
||||
defer session.Close()
|
||||
sg = &SharedGroup{}
|
||||
sg = new(SharedGroup)
|
||||
if err = col.Find(bson.M{"id": key}).One(sg); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
cache.Set(cacheKey, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
cache.Set(cacheKey, sg, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
@@ -1105,10 +1105,13 @@ func (ms *MongoStorage) GetCdrStatsQueue(key string) (sq *StatsQueue, err error)
|
||||
}
|
||||
session, col := ms.conn(colStq)
|
||||
defer session.Close()
|
||||
err = col.Find(bson.M{"key": key}).One(&result)
|
||||
if err == nil {
|
||||
sq = result.Value
|
||||
if err = col.Find(bson.M{"key": key}).One(&result); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
sq = result.Value
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1171,10 +1174,13 @@ func (ms *MongoStorage) GetUser(key string) (up *UserProfile, err error) {
|
||||
}
|
||||
session, col := ms.conn(colUsr)
|
||||
defer session.Close()
|
||||
err = col.Find(bson.M{"key": key}).One(&kv)
|
||||
if err == nil {
|
||||
up = kv.Value
|
||||
if err = col.Find(bson.M{"key": key}).One(&kv); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
up = kv.Value
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1493,7 +1499,7 @@ func (ms *MongoStorage) GetActionPlan(key string, skipCache bool, transactionID
|
||||
cache.Set(cacheKey, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
b := bytes.NewBuffer(kv.Value)
|
||||
r, err := zlib.NewReader(b)
|
||||
@@ -1655,10 +1661,15 @@ func (ms *MongoStorage) SetCdrStats(cs *CdrStats) error {
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetCdrStats(key string) (cs *CdrStats, err error) {
|
||||
cs = &CdrStats{}
|
||||
cs = new(CdrStats)
|
||||
session, col := ms.conn(colCrs)
|
||||
defer session.Close()
|
||||
err = col.Find(bson.M{"id": key}).One(cs)
|
||||
if err = col.Find(bson.M{"id": key}).One(cs); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1692,9 +1703,11 @@ func (ms *MongoStorage) GetStructVersion() (rsv *StructVersion, err error) {
|
||||
}
|
||||
session, col := ms.conn(colVer)
|
||||
defer session.Close()
|
||||
err = col.Find(bson.M{"key": utils.VERSION_PREFIX + "struct"}).One(&result)
|
||||
if err == mgo.ErrNotFound {
|
||||
rsv = nil
|
||||
if err = col.Find(bson.M{"key": utils.VERSION_PREFIX + "struct"}).One(&result); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
rsv = &result.Value
|
||||
return
|
||||
@@ -1718,7 +1731,7 @@ func (ms *MongoStorage) GetResourceLimit(id string, skipCache bool, transactionI
|
||||
err = utils.ErrNotFound
|
||||
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
||||
}
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
for _, fltr := range rl.Filters {
|
||||
if err = fltr.CompileValues(); err != nil {
|
||||
|
||||
@@ -755,17 +755,25 @@ func (rs *RedisStorage) RemoveAccount(key string) (err error) {
|
||||
|
||||
func (rs *RedisStorage) GetCdrStatsQueue(key string) (sq *StatsQueue, err error) {
|
||||
var values []byte
|
||||
if values, err = rs.Cmd("GET", utils.CDR_STATS_QUEUE_PREFIX+key).Bytes(); err == nil {
|
||||
sq = &StatsQueue{}
|
||||
err = rs.ms.Unmarshal(values, &sq)
|
||||
if values, err = rs.Cmd("GET", utils.CDR_STATS_QUEUE_PREFIX+key).Bytes(); err != nil {
|
||||
if err.Error() == "wrong type" { // did not find the destination
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
}
|
||||
sq = new(StatsQueue)
|
||||
if err = rs.ms.Unmarshal(values, &sq); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) SetCdrStatsQueue(sq *StatsQueue) (err error) {
|
||||
result, err := rs.ms.Marshal(sq)
|
||||
err = rs.Cmd("SET", utils.CDR_STATS_QUEUE_PREFIX+sq.GetId(), result).Err
|
||||
return
|
||||
var result []byte
|
||||
if result, err = rs.ms.Marshal(sq); err != nil {
|
||||
return
|
||||
}
|
||||
return rs.Cmd("SET", utils.CDR_STATS_QUEUE_PREFIX+sq.GetId(), result).Err
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetSubscribers() (result map[string]*SubscriberData, err error) {
|
||||
@@ -775,13 +783,18 @@ func (rs *RedisStorage) GetSubscribers() (result map[string]*SubscriberData, err
|
||||
}
|
||||
result = make(map[string]*SubscriberData)
|
||||
for _, key := range keys {
|
||||
if values, err := rs.Cmd("GET", key).Bytes(); err == nil {
|
||||
sub := &SubscriberData{}
|
||||
err = rs.ms.Unmarshal(values, sub)
|
||||
result[key[len(utils.PUBSUB_SUBSCRIBERS_PREFIX):]] = sub
|
||||
} else {
|
||||
return nil, utils.ErrNotFound
|
||||
var values []byte
|
||||
if values, err = rs.Cmd("GET", key).Bytes(); err != nil {
|
||||
if err.Error() == "wrong type" { // did not find the destination
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
}
|
||||
sub := new(SubscriberData)
|
||||
if err = rs.ms.Unmarshal(values, sub); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[key[len(utils.PUBSUB_SUBSCRIBERS_PREFIX):]] = sub
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -800,18 +813,24 @@ func (rs *RedisStorage) RemoveSubscriber(key string) (err error) {
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) SetUser(up *UserProfile) (err error) {
|
||||
result, err := rs.ms.Marshal(up)
|
||||
if err != nil {
|
||||
return err
|
||||
var result []byte
|
||||
if result, err = rs.ms.Marshal(up); err != nil {
|
||||
return
|
||||
}
|
||||
return rs.Cmd("SET", utils.USERS_PREFIX+up.GetId(), result).Err
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetUser(key string) (up *UserProfile, err error) {
|
||||
var values []byte
|
||||
if values, err = rs.Cmd("GET", utils.USERS_PREFIX+key).Bytes(); err == nil {
|
||||
up = &UserProfile{}
|
||||
err = rs.ms.Unmarshal(values, &up)
|
||||
if values, err = rs.Cmd("GET", utils.USERS_PREFIX+key).Bytes(); err != nil {
|
||||
if err.Error() == "wrong type" { // did not find the destination
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
}
|
||||
up = new(UserProfile)
|
||||
if err = rs.ms.Unmarshal(values, &up); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -833,7 +852,7 @@ func (rs *RedisStorage) GetUsers() (result []*UserProfile, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) RemoveUser(key string) (err error) {
|
||||
func (rs *RedisStorage) RemoveUser(key string) error {
|
||||
return rs.Cmd("DEL", utils.USERS_PREFIX+key).Err
|
||||
}
|
||||
|
||||
@@ -860,7 +879,7 @@ func (rs *RedisStorage) GetAlias(key string, skipCache bool, transactionID strin
|
||||
al = &Alias{Values: make(AliasValues, 0)}
|
||||
al.SetId(key)
|
||||
if err = rs.ms.Unmarshal(values, &al.Values); err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
cache.Set(cacheKey, al, cCommit, transactionID)
|
||||
return
|
||||
@@ -1138,18 +1157,15 @@ func (rs *RedisStorage) SetActionPlan(key string, ats *ActionPlan, overwrite boo
|
||||
}
|
||||
}
|
||||
}
|
||||
result, err := rs.ms.Marshal(ats)
|
||||
if err != nil {
|
||||
return err
|
||||
var result []byte
|
||||
if result, err = rs.ms.Marshal(ats); err != nil {
|
||||
return
|
||||
}
|
||||
var b bytes.Buffer
|
||||
w := zlib.NewWriter(&b)
|
||||
w.Write(result)
|
||||
w.Close()
|
||||
if err = rs.Cmd("SET", dbKey, b.Bytes()).Err; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
return rs.Cmd("SET", dbKey, b.Bytes()).Err
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetAllActionPlans() (ats map[string]*ActionPlan, err error) {
|
||||
@@ -1243,8 +1259,14 @@ func (rs *RedisStorage) SetCdrStats(cs *CdrStats) error {
|
||||
|
||||
func (rs *RedisStorage) GetCdrStats(key string) (cs *CdrStats, err error) {
|
||||
var values []byte
|
||||
if values, err = rs.Cmd("GET", utils.CDR_STATS_PREFIX+key).Bytes(); err == nil {
|
||||
err = rs.ms.Unmarshal(values, &cs)
|
||||
if values, err = rs.Cmd("GET", utils.CDR_STATS_PREFIX+key).Bytes(); err != nil {
|
||||
if err.Error() == "wrong type" { // did not find the destination
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
}
|
||||
if err = rs.ms.Unmarshal(values, &cs); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1277,10 +1299,13 @@ func (rs *RedisStorage) SetStructVersion(v *StructVersion) (err error) {
|
||||
|
||||
func (rs *RedisStorage) GetStructVersion() (rsv *StructVersion, err error) {
|
||||
var values []byte
|
||||
rsv = &StructVersion{}
|
||||
if values, err = rs.Cmd("GET", utils.VERSION_PREFIX+"struct").Bytes(); err == nil {
|
||||
err = rs.ms.Unmarshal(values, &rsv)
|
||||
if values, err = rs.Cmd("GET", utils.VERSION_PREFIX+"struct").Bytes(); err != nil {
|
||||
if err.Error() == "wrong type" { // did not find the destination
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
}
|
||||
err = rs.ms.Unmarshal(values, &rsv)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user