diff --git a/data/conf/samples/tutsentinel/cgrates.json b/data/conf/samples/tutsentinel/cgrates.json index 075d26d00..e17b11f7c 100755 --- a/data/conf/samples/tutsentinel/cgrates.json +++ b/data/conf/samples/tutsentinel/cgrates.json @@ -33,4 +33,9 @@ }, +"apiers": { + "enabled": true, +}, + + } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 7e88b264e..4905e80c1 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -22,195 +22,118 @@ import ( "bytes" "compress/zlib" "errors" - "fmt" + "io" "io/ioutil" "strconv" - "sync" "time" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" "github.com/cgrates/ltcache" - "github.com/mediocregopher/radix.v2/pool" - "github.com/mediocregopher/radix.v2/redis" - "github.com/mediocregopher/radix.v2/sentinel" + "github.com/mediocregopher/radix/v3" ) type RedisStorage struct { - dbPool *pool.Pool - maxConns int - ms Marshaler - sentinelName string - sentinelInsts []*sentinelInst - db int //database number used when recconect sentinel - pass string //password used when recconect sentinel - sentinelMux sync.RWMutex + client radix.Client + ms Marshaler } -type sentinelInst struct { - addr string - conn *sentinel.Client +// Redis commands +const ( + redis_AUTH = "AUTH" + redis_SELECT = "SELECT" + redis_FLUSHDB = "FLUSHDB" + redis_DEL = "DEL" + redis_HGETALL = "HGETALL" + redis_KEYS = "KEYS" + redis_SADD = "SADD" + redis_SMEMBERS = "SMEMBERS" + redis_SREM = "SREM" + redis_EXISTS = "EXISTS" + redis_GET = "GET" + redis_SET = "SET" + redis_LRANGE = "LRANGE" + redis_LLEN = "LLEN" + redis_RPOP = "RPOP" + redis_LPUSH = "LPUSH" + redis_RPUSH = "RPUSH" + redis_LPOP = "LPOP" + redis_HMGET = "HMGET" + redis_HDEL = "HDEL" + redis_HGET = "HGET" + redis_RENAME = "RENAME" + redis_HMSET = "HMSET" +) + +func NewRedisStorage(address string, db int, user, pass, mrshlerStr string, + maxConns int, sentinelName string) (rs *RedisStorage, err error) { + + rs = new(RedisStorage) + + if rs.ms, err = NewMarshaler(mrshlerStr); err != nil { + rs = nil + return + } + + dialOpts := []radix.DialOpt{ + radix.DialSelectDB(db), + } + if pass != utils.EmptyString { + if user == utils.EmptyString { + dialOpts = append(dialOpts, radix.DialAuthPass(pass)) + } else { + dialOpts = append(dialOpts, radix.DialAuthUser(user, pass)) + } + } + + dialFunc := func(network, addr string) (radix.Conn, error) { + return radix.Dial(network, addr, dialOpts...) + } + + switch { + case sentinelName != utils.EmptyString: + dialFuncAuthOnly := func(network, addr string) (radix.Conn, error) { + return radix.Dial(network, addr, dialOpts[1:]...) + } + if rs.client, err = radix.NewSentinel(sentinelName, utils.InfieldSplit(address), + radix.SentinelConnFunc(dialFuncAuthOnly), + radix.SentinelPoolFunc(func(network, addr string) (radix.Client, error) { + return radix.NewPool(network, addr, maxConns, radix.PoolConnFunc(dialFunc)) + })); err != nil { + rs = nil + return + } + default: + if rs.client, err = radix.NewPool(utils.TCP, address, maxConns, radix.PoolConnFunc(dialFunc)); err != nil { + rs = nil + return + } + } + + return } -func NewRedisStorage(address string, db int, pass, mrshlerStr string, - maxConns int, sentinelName string) (*RedisStorage, error) { - - df := func(network, addr string) (*redis.Client, error) { - client, err := redis.Dial(network, addr) - if err != nil { - return nil, err - } - if len(pass) != 0 { - if err = client.Cmd(redis_AUTH, pass).Err; err != nil { - client.Close() - return nil, err - } - } - if db != 0 { - if err = client.Cmd(redis_SELECT, db).Err; err != nil { - client.Close() - return nil, err - } - } - return client, nil - } - - ms, err := NewMarshaler(mrshlerStr) - if err != nil { - return nil, err - } - - if sentinelName != "" { - var err error - addrs := utils.InfieldSplit(address) - sentinelInsts := make([]*sentinelInst, len(addrs)) - for i, addr := range addrs { - sentinelInsts[i] = &sentinelInst{addr: addr} - if sentinelInsts[i].conn, err = sentinel.NewClientCustom(utils.TCP, - addr, maxConns, df, sentinelName); err != nil { - sentinelInsts[i].conn = nil - utils.Logger.Warning(fmt.Sprintf(" could not connenct to sentinel at address: %s because error: %s ", - sentinelInsts[i].addr, err.Error())) - // return nil, err - } - } - if err != nil { - return nil, err - } - return &RedisStorage{ - maxConns: maxConns, - ms: ms, - sentinelName: sentinelName, - sentinelInsts: sentinelInsts, - db: db, - pass: pass}, nil - } - p, err := pool.NewCustom(utils.TCP, address, maxConns, df) - if err != nil { - return nil, err - } - return &RedisStorage{ - dbPool: p, - maxConns: maxConns, - ms: ms, - }, nil -} - -func reconnectSentinel(addr, sentinelName string, db int, pass string, maxConns int) (*sentinel.Client, error) { - df := func(network, addr string) (*redis.Client, error) { - client, err := redis.Dial(network, addr) - if err != nil { - return nil, err - } - if len(pass) != 0 { - if err = client.Cmd(redis_AUTH, pass).Err; err != nil { - client.Close() - return nil, err - } - } - if db != 0 { - if err = client.Cmd(redis_SELECT, db).Err; err != nil { - client.Close() - return nil, err - } - } - return client, nil - } - return sentinel.NewClientCustom(utils.TCP, addr, maxConns, df, sentinelName) -} - -// This CMD function get a connection from the pool. +// Cmd function get a connection from the pool. // Handles automatic failover in case of network disconnects -func (rs *RedisStorage) Cmd(cmd string, args ...interface{}) *redis.Resp { - if rs.sentinelName != "" { - var err error - for i := range rs.sentinelInsts { - rs.sentinelMux.Lock() +func (rs *RedisStorage) Cmd(rcv interface{}, cmd string, args ...string) error { + return rs.client.Do(radix.Cmd(rcv, cmd, args...)) +} - if rs.sentinelInsts[i].conn == nil { - rs.sentinelInsts[i].conn, err = reconnectSentinel(rs.sentinelInsts[i].addr, - rs.sentinelName, rs.db, rs.pass, rs.maxConns) - if err != nil { - if i == len(rs.sentinelInsts)-1 { - rs.sentinelMux.Unlock() - return redis.NewResp(fmt.Errorf("No sentinels active")) - } - rs.sentinelMux.Unlock() - continue - } - } - sConn := rs.sentinelInsts[i].conn - rs.sentinelMux.Unlock() - - conn, err := sConn.GetMaster(rs.sentinelName) - if err != nil { - if i == len(rs.sentinelInsts)-1 { - return redis.NewResp(fmt.Errorf("No sentinels active")) - } - rs.sentinelMux.Lock() - rs.sentinelInsts[i].conn = nil - rs.sentinelMux.Unlock() - utils.Logger.Warning(fmt.Sprintf(" sentinel at address: %s became nil error: %s ", - rs.sentinelInsts[i].addr, err.Error())) - continue - } - result := conn.Cmd(cmd, args...) - sConn.PutMaster(rs.sentinelName, conn) - return result - } - } - - c1, err := rs.dbPool.Get() - if err != nil { - return redis.NewResp(err) - } - result := c1.Cmd(cmd, args...) - if result.IsType(redis.IOErr) { // Failover mecanism - utils.Logger.Warning(fmt.Sprintf(" error <%s>, attempting failover.", result.Err.Error())) - for i := 0; i < rs.maxConns; i++ { // Two attempts, one on connection of original pool, one on new pool - c2, err := rs.dbPool.Get() - if err == nil { - if result2 := c2.Cmd(cmd, args...); !result2.IsType(redis.IOErr) { - rs.dbPool.Put(c2) - return result2 - } - } - } - } else { - rs.dbPool.Put(c1) - } - return result +// FlatCmd function get a connection from the pool. +// Handles automatic failover in case of network disconnects +func (rs *RedisStorage) FlatCmd(rcv interface{}, cmd, key string, args ...interface{}) error { + return rs.client.Do(radix.FlatCmd(rcv, cmd, key, args...)) } func (rs *RedisStorage) Close() { - if rs.dbPool != nil { - rs.dbPool.Empty() + if rs.client != nil { + rs.client.Close() } } func (rs *RedisStorage) Flush(ignore string) error { - return rs.Cmd(redis_FLUSHDB).Err + return rs.Cmd(nil, redis_FLUSHDB) } func (rs *RedisStorage) Marshaler() Marshaler { @@ -218,7 +141,7 @@ func (rs *RedisStorage) Marshaler() Marshaler { } func (rs *RedisStorage) SelectDatabase(dbName string) (err error) { - return rs.Cmd(redis_SELECT, dbName).Err + return rs.Cmd(nil, redis_SELECT, dbName) } func (rs *RedisStorage) IsDBEmpty() (resp bool, err error) { @@ -243,7 +166,7 @@ func (rs *RedisStorage) RebuildReverseForPrefix(prefix string) (err error) { return } for _, key := range keys { - if err = rs.Cmd(redis_DEL, key).Err; err != nil { + if err = rs.Cmd(nil, redis_DEL, key); err != nil { return } } @@ -290,7 +213,7 @@ func (rs *RedisStorage) RemoveReverseForPrefix(prefix string) (err error) { return } for _, key := range keys { - if err = rs.Cmd(redis_DEL, key).Err; err != nil { + if err = rs.Cmd(nil, redis_DEL, key); err != nil { return } } @@ -330,8 +253,7 @@ func (rs *RedisStorage) RemoveReverseForPrefix(prefix string) (err error) { func (rs *RedisStorage) getKeysForFilterIndexesKeys(fkeys []string) (keys []string, err error) { for _, itemIDPrefix := range fkeys { mp := make(map[string]string) - mp, err = rs.Cmd(redis_HGETALL, itemIDPrefix).Map() - if err != nil { + if err = rs.Cmd(&mp, redis_HGETALL, itemIDPrefix); err != nil { return } else if len(mp) == 0 { return nil, utils.ErrNotFound @@ -343,52 +265,51 @@ func (rs *RedisStorage) getKeysForFilterIndexesKeys(fkeys []string) (keys []stri return } -func (rs *RedisStorage) RebbuildActionPlanKeys() error { - r := rs.Cmd(redis_KEYS, utils.ACTION_PLAN_PREFIX+"*") - if r.Err != nil { - return r.Err +func (rs *RedisStorage) RebbuildActionPlanKeys() (err error) { + var keys []string + if err = rs.Cmd(&keys, redis_KEYS, utils.ACTION_PLAN_PREFIX+"*"); err != nil { + return } - keys, _ := r.List() for _, key := range keys { - if err := rs.Cmd(redis_SADD, utils.ActionPlanIndexes, key).Err; err != nil { - return err + if err = rs.Cmd(nil, redis_SADD, utils.ActionPlanIndexes, key); err != nil { + return } } - return nil + return } -func (rs *RedisStorage) GetKeysForPrefix(prefix string) ([]string, error) { - var r *redis.Resp +func (rs *RedisStorage) GetKeysForPrefix(prefix string) (keys []string, err error) { if prefix == utils.ACTION_PLAN_PREFIX { // so we can avoid the full scan on scheduler reloads - r = rs.Cmd(redis_SMEMBERS, utils.ActionPlanIndexes) + err = rs.Cmd(&keys, redis_SMEMBERS, utils.ActionPlanIndexes) } else { - r = rs.Cmd(redis_KEYS, prefix+"*") + err = rs.Cmd(&keys, redis_KEYS, prefix+"*") } - if r.Err != nil { - return nil, r.Err + if err != nil { + return } - if keys, _ := r.List(); len(keys) != 0 { + if len(keys) != 0 { if filterIndexesPrefixMap.Has(prefix) { return rs.getKeysForFilterIndexesKeys(keys) } - return keys, nil + return } return nil, nil } // Used to check if specific subject is stored using prefix key attached to entity -func (rs *RedisStorage) HasDataDrv(category, subject, tenant string) (bool, error) { +func (rs *RedisStorage) HasDataDrv(category, subject, tenant string) (exists bool, err error) { + var i int switch category { case utils.DESTINATION_PREFIX, utils.RATING_PLAN_PREFIX, utils.RATING_PROFILE_PREFIX, utils.ACTION_PREFIX, utils.ACTION_PLAN_PREFIX, utils.ACCOUNT_PREFIX: - i, err := rs.Cmd(redis_EXISTS, category+subject).Int() + err = rs.Cmd(&i, redis_EXISTS, category+subject) return i == 1, err case utils.ResourcesPrefix, utils.ResourceProfilesPrefix, utils.StatQueuePrefix, utils.StatQueueProfilePrefix, utils.ThresholdPrefix, utils.ThresholdProfilePrefix, utils.FilterPrefix, utils.RouteProfilePrefix, utils.AttributeProfilePrefix, utils.ChargerProfilePrefix, utils.DispatcherProfilePrefix, utils.DispatcherHostPrefix, utils.RateProfilePrefix: - i, err := rs.Cmd(redis_EXISTS, category+utils.ConcatenatedKey(tenant, subject)).Int() + err := rs.Cmd(&i, redis_EXISTS, category+utils.ConcatenatedKey(tenant, subject)) return i == 1, err } return false, errors.New("unsupported HasData category") @@ -397,26 +318,23 @@ func (rs *RedisStorage) HasDataDrv(category, subject, tenant string) (bool, erro func (rs *RedisStorage) GetRatingPlanDrv(key string) (rp *RatingPlan, err error) { key = utils.RATING_PLAN_PREFIX + key var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return nil, err + if err = rs.Cmd(&values, redis_GET, key); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound + return } b := bytes.NewBuffer(values) - r, err := zlib.NewReader(b) - if err != nil { - return nil, err + var r io.ReadCloser + if r, err = zlib.NewReader(b); err != nil { + return } - out, err := ioutil.ReadAll(r) - if err != nil { - return nil, err + var out []byte + if out, err = ioutil.ReadAll(r); err != nil { + return } r.Close() err = rs.ms.Unmarshal(out, &rp) - if err != nil { - return nil, err - } return } @@ -426,61 +344,57 @@ func (rs *RedisStorage) SetRatingPlanDrv(rp *RatingPlan) (err error) { w := zlib.NewWriter(&b) w.Write(result) w.Close() - err = rs.Cmd(redis_SET, utils.RATING_PLAN_PREFIX+rp.Id, b.Bytes()).Err + err = rs.Cmd(nil, redis_SET, utils.RATING_PLAN_PREFIX+rp.Id, b.String()) return } -func (rs *RedisStorage) RemoveRatingPlanDrv(key string) error { - keys, err := rs.Cmd(redis_KEYS, utils.RATING_PLAN_PREFIX+key+"*").List() - if err != nil { - return err +func (rs *RedisStorage) RemoveRatingPlanDrv(key string) (err error) { + var keys []string + if err = rs.Cmd(&keys, redis_KEYS, utils.RATING_PLAN_PREFIX+key+"*"); err != nil { + return } for _, key := range keys { - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return err + if err = rs.Cmd(nil, redis_DEL, key); err != nil { + return } } - return nil + return } func (rs *RedisStorage) GetRatingProfileDrv(key string) (rpf *RatingProfile, err error) { key = utils.RATING_PROFILE_PREFIX + key var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &rpf); err != nil { + if err = rs.Cmd(&values, redis_GET, key); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &rpf) return } func (rs *RedisStorage) SetRatingProfileDrv(rpf *RatingProfile) (err error) { - result, err := rs.ms.Marshal(rpf) - if err != nil { - return err - } - key := utils.RATING_PROFILE_PREFIX + rpf.Id - if err = rs.Cmd(redis_SET, key, result).Err; err != nil { + var result []byte + if result, err = rs.ms.Marshal(rpf); err != nil { return } + key := utils.RATING_PROFILE_PREFIX + rpf.Id + err = rs.Cmd(nil, redis_SET, key, string(result)) return } -func (rs *RedisStorage) RemoveRatingProfileDrv(key string) error { - keys, err := rs.Cmd(redis_KEYS, utils.RATING_PROFILE_PREFIX+key+"*").List() - if err != nil { - return err +func (rs *RedisStorage) RemoveRatingProfileDrv(key string) (err error) { + var keys []string + if err = rs.Cmd(&keys, redis_KEYS, utils.RATING_PROFILE_PREFIX+key+"*"); err != nil { + return } for _, key := range keys { - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return err + if err = rs.Cmd(nil, redis_DEL, key); err != nil { + return } } - return nil + return } // GetDestination retrieves a destination with id from tp_db @@ -495,50 +409,44 @@ func (rs *RedisStorage) GetDestinationDrv(key string, skipCache bool, } } var values []byte - if values, err = rs.Cmd(redis_GET, utils.DESTINATION_PREFIX+key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - if errCh := Cache.Set(utils.CacheDestinations, key, nil, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh - } - err = utils.ErrNotFound + if err = rs.Cmd(&values, redis_GET, utils.DESTINATION_PREFIX+key); err != nil { + return + } else if len(values) == 0 { + if errCh := Cache.Set(utils.CacheDestinations, key, nil, nil, + cacheCommit(transactionID), transactionID); errCh != nil { + return nil, errCh } + err = utils.ErrNotFound return } b := bytes.NewBuffer(values) - r, err := zlib.NewReader(b) - if err != nil { - return nil, err + var r io.ReadCloser + if r, err = zlib.NewReader(b); err != nil { + return } - out, err := ioutil.ReadAll(r) - if err != nil { - return nil, err + var out []byte + if out, err = ioutil.ReadAll(r); err != nil { + return } r.Close() - err = rs.ms.Unmarshal(out, &dest) - if err != nil { - return nil, err - } - if errCh := Cache.Set(utils.CacheDestinations, key, dest, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh + if err = rs.ms.Unmarshal(out, &dest); err != nil { + return } + err = Cache.Set(utils.CacheDestinations, key, dest, nil, + cacheCommit(transactionID), transactionID) return } func (rs *RedisStorage) SetDestinationDrv(dest *Destination, transactionID string) (err error) { - result, err := rs.ms.Marshal(dest) - if err != nil { - return err + var result []byte + if result, err = rs.ms.Marshal(dest); err != nil { + return } var b bytes.Buffer w := zlib.NewWriter(&b) w.Write(result) w.Close() - key := utils.DESTINATION_PREFIX + dest.Id - if err = rs.Cmd(redis_SET, key, b.Bytes()).Err; err != nil { - return err - } + err = rs.Cmd(nil, redis_SET, utils.DESTINATION_PREFIX+dest.Id, b.String()) return } @@ -552,28 +460,26 @@ func (rs *RedisStorage) GetReverseDestinationDrv(key string, return x.([]string), nil } } - if ids, err = rs.Cmd(redis_SMEMBERS, utils.REVERSE_DESTINATION_PREFIX+key).List(); err != nil { + if err = rs.Cmd(&ids, redis_SMEMBERS, utils.REVERSE_DESTINATION_PREFIX+key); err != nil { return - } else if len(ids) == 0 { - if errCh := Cache.Set(utils.CacheReverseDestinations, key, nil, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh + } + if len(ids) == 0 { + if err = Cache.Set(utils.CacheReverseDestinations, key, nil, nil, + cacheCommit(transactionID), transactionID); err != nil { + return } err = utils.ErrNotFound return } - if errCh := Cache.Set(utils.CacheReverseDestinations, key, ids, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh - } + err = Cache.Set(utils.CacheReverseDestinations, key, ids, nil, + cacheCommit(transactionID), transactionID) return } func (rs *RedisStorage) SetReverseDestinationDrv(dest *Destination, transactionID string) (err error) { for _, p := range dest.Prefixes { - key := utils.REVERSE_DESTINATION_PREFIX + p - if err = rs.Cmd(redis_SADD, key, dest.Id).Err; err != nil { - break + if err = rs.Cmd(nil, redis_SADD, utils.REVERSE_DESTINATION_PREFIX+p, dest.Id); err != nil { + return } } return @@ -581,32 +487,30 @@ func (rs *RedisStorage) SetReverseDestinationDrv(dest *Destination, transactionI func (rs *RedisStorage) RemoveDestinationDrv(destID, transactionID string) (err error) { // get destination for prefix list - d, err := rs.GetDestinationDrv(destID, false, transactionID) - if err != nil { + var d *Destination + if d, err = rs.GetDestinationDrv(destID, false, transactionID); err != nil { return } - err = rs.Cmd(redis_DEL, utils.DESTINATION_PREFIX+destID).Err - if err != nil { - return err + if err = rs.Cmd(nil, redis_DEL, utils.DESTINATION_PREFIX+destID); err != nil { + return } - if errCh := Cache.Remove(utils.CacheDestinations, destID, - cacheCommit(transactionID), transactionID); errCh != nil { - return errCh + if err = Cache.Remove(utils.CacheDestinations, destID, + cacheCommit(transactionID), transactionID); err != nil { + return } if d == nil { return utils.ErrNotFound } for _, prefix := range d.Prefixes { - err = rs.Cmd(redis_SREM, utils.REVERSE_DESTINATION_PREFIX+prefix, destID).Err - if err != nil { - return err + if err = rs.Cmd(nil, redis_SREM, utils.REVERSE_DESTINATION_PREFIX+prefix, destID); err != nil { + return } rs.GetReverseDestinationDrv(prefix, true, transactionID) // it will recache the destination } return } -func (rs *RedisStorage) UpdateReverseDestinationDrv(oldDest, newDest *Destination, transactionID string) error { +func (rs *RedisStorage) UpdateReverseDestinationDrv(oldDest, newDest *Destination, transactionID string) (err error) { //log.Printf("Old: %+v, New: %+v", oldDest, newDest) var obsoletePrefixes []string var addedPrefixes []string @@ -640,67 +544,59 @@ func (rs *RedisStorage) UpdateReverseDestinationDrv(oldDest, newDest *Destinatio } // remove id for all obsolete prefixes cCommit := cacheCommit(transactionID) - var err error for _, obsoletePrefix := range obsoletePrefixes { - err = rs.Cmd(redis_SREM, - utils.REVERSE_DESTINATION_PREFIX+obsoletePrefix, oldDest.Id).Err - if err != nil { - return err + if err = rs.Cmd(nil, redis_SREM, + utils.REVERSE_DESTINATION_PREFIX+obsoletePrefix, oldDest.Id); err != nil { + return } - if errCh := Cache.Remove(utils.CacheReverseDestinations, obsoletePrefix, - cCommit, transactionID); errCh != nil { - return errCh + if err = Cache.Remove(utils.CacheReverseDestinations, obsoletePrefix, + cCommit, transactionID); err != nil { + return } } // add the id to all new prefixes for _, addedPrefix := range addedPrefixes { - err = rs.Cmd(redis_SADD, utils.REVERSE_DESTINATION_PREFIX+addedPrefix, newDest.Id).Err - if err != nil { - return err + if err = rs.Cmd(nil, redis_SADD, utils.REVERSE_DESTINATION_PREFIX+addedPrefix, newDest.Id); err != nil { + return } } - return nil + return } func (rs *RedisStorage) GetActionsDrv(key string) (as Actions, err error) { - key = utils.ACTION_PREFIX + key var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &as); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.ACTION_PREFIX+key); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &as) return } func (rs *RedisStorage) SetActionsDrv(key string, as Actions) (err error) { - result, err := rs.ms.Marshal(&as) - err = rs.Cmd(redis_SET, utils.ACTION_PREFIX+key, result).Err - return + var result []byte + if result, err = rs.ms.Marshal(&as); err != nil { + return + } + return rs.Cmd(nil, redis_SET, utils.ACTION_PREFIX+key, string(result)) } func (rs *RedisStorage) RemoveActionsDrv(key string) (err error) { - err = rs.Cmd(redis_DEL, utils.ACTION_PREFIX+key).Err - return + return rs.Cmd(nil, redis_DEL, utils.ACTION_PREFIX+key) } func (rs *RedisStorage) GetSharedGroupDrv(key string) (sg *SharedGroup, err error) { - key = utils.SHARED_GROUP_PREFIX + key var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &sg); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.SHARED_GROUP_PREFIX+key); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &sg) return } @@ -709,26 +605,22 @@ func (rs *RedisStorage) SetSharedGroupDrv(sg *SharedGroup) (err error) { if result, err = rs.ms.Marshal(sg); err != nil { return } - err = rs.Cmd(redis_SET, utils.SHARED_GROUP_PREFIX+sg.Id, result).Err - return + return rs.Cmd(nil, redis_SET, utils.SHARED_GROUP_PREFIX+sg.Id, string(result)) } func (rs *RedisStorage) RemoveSharedGroupDrv(id string) (err error) { - return rs.Cmd(redis_DEL, utils.SHARED_GROUP_PREFIX+id).Err + return rs.Cmd(nil, redis_DEL, utils.SHARED_GROUP_PREFIX+id) } -func (rs *RedisStorage) GetAccountDrv(key string) (*Account, error) { - rpl := rs.Cmd(redis_GET, utils.ACCOUNT_PREFIX+key) - if rpl.Err != nil { - return nil, rpl.Err - } else if rpl.IsType(redis.Nil) { - return nil, utils.ErrNotFound +func (rs *RedisStorage) GetAccountDrv(key string) (ub *Account, err error) { + var values []byte + if err = rs.Cmd(&values, redis_GET, utils.ACCOUNT_PREFIX+key); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound + return } - values, err := rpl.Bytes() - if err != nil { - return nil, err - } - ub := &Account{ID: key} + ub = &Account{ID: key} if err = rs.ms.Unmarshal(values, ub); err != nil { return nil, err } @@ -740,7 +632,8 @@ func (rs *RedisStorage) SetAccountDrv(acc *Account) (err error) { // UPDATE: if all balances expired and were cleaned it makes // sense to write empty balance map if len(acc.BalanceMap) == 0 { - if ac, err := rs.GetAccountDrv(acc.ID); err == nil && !ac.allBalancesExpired() { + var ac *Account + if ac, err = rs.GetAccountDrv(acc.ID); err == nil && !ac.allBalancesExpired() { ac.ActionTriggers = acc.ActionTriggers ac.UnitCounters = acc.UnitCounters ac.AllowNegative = acc.AllowNegative @@ -749,22 +642,20 @@ func (rs *RedisStorage) SetAccountDrv(acc *Account) (err error) { } } acc.UpdateTime = time.Now() - result, err := rs.ms.Marshal(acc) - err = rs.Cmd(redis_SET, utils.ACCOUNT_PREFIX+acc.ID, result).Err - return + var result []byte + if result, err = rs.ms.Marshal(acc); err != nil { + return + } + return rs.Cmd(nil, redis_SET, utils.ACCOUNT_PREFIX+acc.ID, string(result)) } func (rs *RedisStorage) RemoveAccountDrv(key string) (err error) { - err = rs.Cmd(redis_DEL, utils.ACCOUNT_PREFIX+key).Err - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } - return + return rs.Cmd(nil, redis_DEL, utils.ACCOUNT_PREFIX+key) } // Limit will only retrieve the last n items out of history, newest first func (rs *RedisStorage) GetLoadHistory(limit int, skipCache bool, - transactionID string) ([]*utils.LoadInstance, error) { + transactionID string) (loadInsts []*utils.LoadInstance, err error) { if limit == 0 { return nil, nil } @@ -784,31 +675,28 @@ func (rs *RedisStorage) GetLoadHistory(limit int, skipCache bool, if limit != -1 { limit -= -1 // Decrease limit to match redis approach on lrange } - marshaleds, err := rs.Cmd(redis_LRANGE, - utils.LOADINST_KEY, 0, limit).ListBytes() cCommit := cacheCommit(transactionID) - if err != nil { + var marshaleds [][]byte + if err = rs.Cmd(&marshaleds, redis_LRANGE, + utils.LOADINST_KEY, "0", strconv.Itoa(limit)); err != nil { if errCh := Cache.Set(utils.LOADINST_KEY, "", nil, nil, cCommit, transactionID); errCh != nil { return nil, errCh } - return nil, err + return } - loadInsts := make([]*utils.LoadInstance, len(marshaleds)) + loadInsts = make([]*utils.LoadInstance, len(marshaleds)) for idx, marshaled := range marshaleds { - var lInst utils.LoadInstance - err = rs.ms.Unmarshal(marshaled, &lInst) - if err != nil { + if err = rs.ms.Unmarshal(marshaled, loadInsts[idx]); err != nil { return nil, err } - loadInsts[idx] = &lInst } - if errCh := Cache.Remove(utils.LOADINST_KEY, "", cCommit, transactionID); errCh != nil { - return nil, errCh + if err = Cache.Remove(utils.LOADINST_KEY, "", cCommit, transactionID); err != nil { + return nil, err } - if errCh := Cache.Set(utils.LOADINST_KEY, "", loadInsts, nil, - cCommit, transactionID); errCh != nil { - return nil, errCh + if err := Cache.Set(utils.LOADINST_KEY, "", loadInsts, nil, + cCommit, transactionID); err != nil { + return nil, err } if len(loadInsts) < limit || limit == -1 { return loadInsts, nil @@ -817,68 +705,63 @@ func (rs *RedisStorage) GetLoadHistory(limit int, skipCache bool, } // Adds a single load instance to load history -func (rs *RedisStorage) AddLoadHistory(ldInst *utils.LoadInstance, loadHistSize int, transactionID string) error { +func (rs *RedisStorage) AddLoadHistory(ldInst *utils.LoadInstance, loadHistSize int, transactionID string) (err error) { if loadHistSize == 0 { // Load history disabled - return nil + return } - marshaled, err := rs.ms.Marshal(&ldInst) - if err != nil { - return err + var marshaled []byte + if marshaled, err = rs.ms.Marshal(&ldInst); err != nil { + return } _, err = guardian.Guardian.Guard(func() (interface{}, error) { // Make sure we do it locked since other instance can modify history while we read it - histLen, err := rs.Cmd(redis_LLEN, utils.LOADINST_KEY).Int() - if err != nil { + var histLen int + if err := rs.Cmd(&histLen, redis_LLEN, utils.LOADINST_KEY); err != nil { return nil, err } if histLen >= loadHistSize { // Have hit maximum history allowed, remove oldest element in order to add new one - if err := rs.Cmd(redis_RPOP, utils.LOADINST_KEY).Err; err != nil { + if err = rs.Cmd(nil, redis_RPOP, utils.LOADINST_KEY); err != nil { return nil, err } } - return nil, rs.Cmd(redis_LPUSH, utils.LOADINST_KEY, marshaled).Err + return nil, rs.Cmd(nil, redis_LPUSH, utils.LOADINST_KEY, string(marshaled)) }, config.CgrConfig().GeneralCfg().LockingTimeout, utils.LOADINST_KEY) if errCh := Cache.Remove(utils.LOADINST_KEY, "", cacheCommit(transactionID), transactionID); errCh != nil { return errCh } - return err + return } func (rs *RedisStorage) GetActionTriggersDrv(key string) (atrs ActionTriggers, err error) { - key = utils.ACTION_TRIGGER_PREFIX + key var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &atrs); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.ACTION_TRIGGER_PREFIX+key); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &atrs) return } func (rs *RedisStorage) SetActionTriggersDrv(key string, atrs ActionTriggers) (err error) { if len(atrs) == 0 { // delete the key - return rs.Cmd(redis_DEL, utils.ACTION_TRIGGER_PREFIX+key).Err + return rs.Cmd(nil, redis_DEL, utils.ACTION_TRIGGER_PREFIX+key) } var result []byte if result, err = rs.ms.Marshal(atrs); err != nil { - return err + return } - if err = rs.Cmd(redis_SET, utils.ACTION_TRIGGER_PREFIX+key, result).Err; err != nil { + if err = rs.Cmd(nil, redis_SET, utils.ACTION_TRIGGER_PREFIX+key, string(result)); err != nil { return } return } func (rs *RedisStorage) RemoveActionTriggersDrv(key string) (err error) { - key = utils.ACTION_TRIGGER_PREFIX + key - err = rs.Cmd(redis_DEL, key).Err - return + return rs.Cmd(nil, redis_DEL, utils.ACTION_TRIGGER_PREFIX+key) } func (rs *RedisStorage) GetActionPlanDrv(key string, skipCache bool, @@ -895,47 +778,45 @@ func (rs *RedisStorage) GetActionPlanDrv(key string, skipCache bool, } } var values []byte - if values, err = rs.Cmd(redis_GET, utils.ACTION_PLAN_PREFIX+key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - if errCh := Cache.Set(utils.CacheActionPlans, key, nil, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh - } - err = utils.ErrNotFound + if err = rs.Cmd(&values, redis_GET, utils.ACTION_PLAN_PREFIX+key); err != nil { + return + } else if len(values) == 0 { + if errCh := Cache.Set(utils.CacheActionPlans, key, nil, nil, + cacheCommit(transactionID), transactionID); errCh != nil { + return nil, errCh } + err = utils.ErrNotFound return } b := bytes.NewBuffer(values) - r, err := zlib.NewReader(b) - if err != nil { - return nil, err + var r io.ReadCloser + if r, err = zlib.NewReader(b); err != nil { + return } - out, err := ioutil.ReadAll(r) - if err != nil { - return nil, err + var out []byte + if out, err = ioutil.ReadAll(r); err != nil { + return } r.Close() if err = rs.ms.Unmarshal(out, &ats); err != nil { return } - if errCh := Cache.Set(utils.CacheActionPlans, key, ats, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh - } + err = Cache.Set(utils.CacheActionPlans, key, ats, nil, + cacheCommit(transactionID), transactionID) return } func (rs *RedisStorage) RemoveActionPlanDrv(key string, - transactionID string) error { + transactionID string) (err error) { cCommit := cacheCommit(transactionID) - if err := rs.Cmd(redis_SREM, utils.ActionPlanIndexes, utils.ACTION_PLAN_PREFIX+key).Err; err != nil { - return err + if err = rs.Cmd(nil, redis_SREM, utils.ActionPlanIndexes, utils.ACTION_PLAN_PREFIX+key); err != nil { + return } - err := rs.Cmd(redis_DEL, utils.ACTION_PLAN_PREFIX+key).Err + err = rs.Cmd(nil, redis_DEL, utils.ACTION_PLAN_PREFIX+key) if errCh := Cache.Remove(utils.CacheActionPlans, key, cCommit, transactionID); errCh != nil { return errCh } - return err + return } func (rs *RedisStorage) SetActionPlanDrv(key string, ats *ActionPlan, @@ -943,10 +824,10 @@ func (rs *RedisStorage) SetActionPlanDrv(key string, ats *ActionPlan, cCommit := cacheCommit(transactionID) if len(ats.ActionTimings) == 0 { // delete the key - if err := rs.Cmd(redis_SREM, utils.ActionPlanIndexes, utils.ACTION_PLAN_PREFIX+key).Err; err != nil { - return err + if err = rs.Cmd(nil, redis_SREM, utils.ActionPlanIndexes, utils.ACTION_PLAN_PREFIX+key); err != nil { + return } - err = rs.Cmd(redis_DEL, utils.ACTION_PLAN_PREFIX+key).Err + err = rs.Cmd(nil, redis_DEL, utils.ACTION_PLAN_PREFIX+key) if errCh := Cache.Remove(utils.CacheActionPlans, key, cCommit, transactionID); errCh != nil { return errCh @@ -972,28 +853,27 @@ func (rs *RedisStorage) SetActionPlanDrv(key string, ats *ActionPlan, w := zlib.NewWriter(&b) w.Write(result) w.Close() - if err := rs.Cmd(redis_SADD, utils.ActionPlanIndexes, utils.ACTION_PLAN_PREFIX+key).Err; err != nil { - return err + if err = rs.Cmd(nil, redis_SADD, utils.ActionPlanIndexes, utils.ACTION_PLAN_PREFIX+key); err != nil { + return } - return rs.Cmd(redis_SET, utils.ACTION_PLAN_PREFIX+key, b.Bytes()).Err + return rs.Cmd(nil, redis_SET, utils.ACTION_PLAN_PREFIX+key, b.String()) } func (rs *RedisStorage) GetAllActionPlansDrv() (ats map[string]*ActionPlan, err error) { - keys, err := rs.GetKeysForPrefix(utils.ACTION_PLAN_PREFIX) - if err != nil { - return nil, err + var keys []string + if keys, err = rs.GetKeysForPrefix(utils.ACTION_PLAN_PREFIX); err != nil { + return } if len(keys) == 0 { - return nil, utils.ErrNotFound + err = utils.ErrNotFound + return } ats = make(map[string]*ActionPlan, len(keys)) for _, key := range keys { - ap, err := rs.GetActionPlanDrv(key[len(utils.ACTION_PLAN_PREFIX):], - false, utils.NonTransactional) - if err != nil { + if ats[key[len(utils.ACTION_PLAN_PREFIX):]], err = rs.GetActionPlanDrv(key[len(utils.ACTION_PLAN_PREFIX):], + false, utils.NonTransactional); err != nil { return nil, err } - ats[key[len(utils.ACTION_PLAN_PREFIX):]] = ap } return } @@ -1009,54 +889,52 @@ func (rs *RedisStorage) GetAccountActionPlansDrv(acntID string, skipCache bool, } } var values []byte - if values, err = rs.Cmd(redis_GET, - utils.AccountActionPlansPrefix+acntID).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - if errCh := Cache.Set(utils.CacheAccountActionPlans, acntID, nil, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh - } - err = utils.ErrNotFound + if err = rs.Cmd(&values, redis_GET, + utils.AccountActionPlansPrefix+acntID); err != nil { + return + } else if len(values) == 0 { + if errCh := Cache.Set(utils.CacheAccountActionPlans, acntID, nil, nil, + cacheCommit(transactionID), transactionID); errCh != nil { + return nil, errCh } + err = utils.ErrNotFound return } if err = rs.ms.Unmarshal(values, &aPlIDs); err != nil { return } - if errCh := Cache.Set(utils.CacheAccountActionPlans, acntID, aPlIDs, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh - } + err = Cache.Set(utils.CacheAccountActionPlans, acntID, aPlIDs, nil, + cacheCommit(transactionID), transactionID) return } func (rs *RedisStorage) SetAccountActionPlansDrv(acntID string, aPlIDs []string, overwrite bool) (err error) { if !overwrite { - if oldaPlIDs, err := rs.GetAccountActionPlansDrv(acntID, true, utils.NonTransactional); err != nil && err != utils.ErrNotFound { - return err - } else { - for _, oldAPid := range oldaPlIDs { - if !utils.IsSliceMember(aPlIDs, oldAPid) { - aPlIDs = append(aPlIDs, oldAPid) - } + var oldaPlIDs []string + if oldaPlIDs, err = rs.GetAccountActionPlansDrv(acntID, true, utils.NonTransactional); err != nil && err != utils.ErrNotFound { + return + } + for _, oldAPid := range oldaPlIDs { + if !utils.IsSliceMember(aPlIDs, oldAPid) { + aPlIDs = append(aPlIDs, oldAPid) } } } var result []byte if result, err = rs.ms.Marshal(aPlIDs); err != nil { - return err + return } - return rs.Cmd(redis_SET, utils.AccountActionPlansPrefix+acntID, result).Err + return rs.Cmd(nil, redis_SET, utils.AccountActionPlansPrefix+acntID, string(result)) } func (rs *RedisStorage) RemAccountActionPlansDrv(acntID string, aPlIDs []string) (err error) { key := utils.AccountActionPlansPrefix + acntID if len(aPlIDs) == 0 { - return rs.Cmd(redis_DEL, key).Err + return rs.Cmd(nil, redis_DEL, key) } - oldaPlIDs, err := rs.GetAccountActionPlansDrv(acntID, true, utils.NonTransactional) - if err != nil { - return err + var oldaPlIDs []string + if oldaPlIDs, err = rs.GetAccountActionPlansDrv(acntID, true, utils.NonTransactional); err != nil { + return } for i := 0; i < len(oldaPlIDs); { if utils.IsSliceMember(aPlIDs, oldaPlIDs[i]) { @@ -1066,151 +944,127 @@ func (rs *RedisStorage) RemAccountActionPlansDrv(acntID string, aPlIDs []string) i++ } if len(oldaPlIDs) == 0 { // no more elements, remove the reference - return rs.Cmd(redis_DEL, key).Err + return rs.Cmd(nil, redis_DEL, key) } var result []byte if result, err = rs.ms.Marshal(oldaPlIDs); err != nil { - return err + return } - return rs.Cmd(redis_SET, key, result).Err + return rs.Cmd(nil, redis_SET, key, string(result)) } -func (rs *RedisStorage) PushTask(t *Task) error { - result, err := rs.ms.Marshal(t) - if err != nil { - return err +func (rs *RedisStorage) PushTask(t *Task) (err error) { + var result []byte + if result, err = rs.ms.Marshal(t); err != nil { + return } - return rs.Cmd(redis_RPUSH, utils.TASKS_KEY, result).Err + return rs.Cmd(nil, redis_RPUSH, utils.TASKS_KEY, string(result)) } func (rs *RedisStorage) PopTask() (t *Task, err error) { var values []byte - if values, err = rs.Cmd(redis_LPOP, utils.TASKS_KEY).Bytes(); err == nil { - t = &Task{} - err = rs.ms.Unmarshal(values, t) + if err = rs.Cmd(&values, redis_LPOP, utils.TASKS_KEY); err != nil { + return } + t = &Task{} + err = rs.ms.Unmarshal(values, t) return } func (rs *RedisStorage) GetResourceProfileDrv(tenant, id string) (rsp *ResourceProfile, err error) { - key := utils.ResourceProfilesPrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &rsp); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.ResourceProfilesPrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &rsp) return } -func (rs *RedisStorage) SetResourceProfileDrv(rsp *ResourceProfile) error { - result, err := rs.ms.Marshal(rsp) - if err != nil { - return err +func (rs *RedisStorage) SetResourceProfileDrv(rsp *ResourceProfile) (err error) { + var result []byte + if result, err = rs.ms.Marshal(rsp); err != nil { + return } - return rs.Cmd(redis_SET, utils.ResourceProfilesPrefix+rsp.TenantID(), result).Err + return rs.Cmd(nil, redis_SET, utils.ResourceProfilesPrefix+rsp.TenantID(), string(result)) } func (rs *RedisStorage) RemoveResourceProfileDrv(tenant, id string) (err error) { - key := utils.ResourceProfilesPrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.ResourceProfilesPrefix+utils.ConcatenatedKey(tenant, id)) } func (rs *RedisStorage) GetResourceDrv(tenant, id string) (r *Resource, err error) { - key := utils.ResourcesPrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.ResourcesPrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &r) return } func (rs *RedisStorage) SetResourceDrv(r *Resource) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err + var result []byte + if result, err = rs.ms.Marshal(r); err != nil { + return } - return rs.Cmd(redis_SET, utils.ResourcesPrefix+r.TenantID(), result).Err + return rs.Cmd(nil, redis_SET, utils.ResourcesPrefix+r.TenantID(), string(result)) } func (rs *RedisStorage) RemoveResourceDrv(tenant, id string) (err error) { - key := utils.ResourcesPrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.ResourcesPrefix+utils.ConcatenatedKey(tenant, id)) } func (rs *RedisStorage) GetTimingDrv(id string) (t *utils.TPTiming, err error) { - key := utils.TimingsPrefix + id var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &t); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.TimingsPrefix+id); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &t) return } -func (rs *RedisStorage) SetTimingDrv(t *utils.TPTiming) error { - result, err := rs.ms.Marshal(t) - if err != nil { - return err +func (rs *RedisStorage) SetTimingDrv(t *utils.TPTiming) (err error) { + var result []byte + if result, err = rs.ms.Marshal(t); err != nil { + return } - return rs.Cmd(redis_SET, utils.TimingsPrefix+t.ID, result).Err + return rs.Cmd(nil, redis_SET, utils.TimingsPrefix+t.ID, string(result)) } func (rs *RedisStorage) RemoveTimingDrv(id string) (err error) { - key := utils.TimingsPrefix + id - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.TimingsPrefix+id) } func (rs *RedisStorage) GetVersions(itm string) (vrs Versions, err error) { if itm != "" { - fldVal, err := rs.Cmd(redis_HGET, utils.TBLVersions, itm).Str() - if err != nil { - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } + var fldVal int64 + mn := radix.MaybeNil{Rcv: &fldVal} + if err = rs.Cmd(&mn, redis_HGET, utils.TBLVersions, itm); err != nil { return nil, err + } else if mn.Nil { + err = utils.ErrNotFound + return } - intVal, err := strconv.ParseInt(fldVal, 10, 64) - if err != nil { - return nil, err - } - return Versions{itm: intVal}, nil + return Versions{itm: fldVal}, nil } - mp, err := rs.Cmd(redis_HGETALL, utils.TBLVersions).Map() - if err != nil { + var mp map[string]string + if err = rs.Cmd(&mp, redis_HGETALL, utils.TBLVersions); err != nil { return nil, err } - vrs, err = utils.MapStringToInt64(mp) - if err != nil { - return nil, err - } - if len(vrs) == 0 { + if len(mp) == 0 { return nil, utils.ErrNotFound } + if vrs, err = utils.MapStringToInt64(mp); err != nil { + return nil, err + } return } @@ -1220,62 +1074,55 @@ func (rs *RedisStorage) SetVersions(vrs Versions, overwrite bool) (err error) { return } } - return rs.Cmd(redis_HMSET, utils.TBLVersions, vrs).Err + return rs.FlatCmd(nil, redis_HMSET, utils.TBLVersions, vrs) } func (rs *RedisStorage) RemoveVersions(vrs Versions) (err error) { if len(vrs) != 0 { for key := range vrs { - err = rs.Cmd(redis_HDEL, utils.TBLVersions, key).Err - if err != nil { - return err + if err = rs.Cmd(nil, redis_HDEL, utils.TBLVersions, key); err != nil { + return } } return } - return rs.Cmd(redis_DEL, utils.TBLVersions).Err + return rs.Cmd(nil, redis_DEL, utils.TBLVersions) } // GetStatQueueProfileDrv retrieves a StatQueueProfile from dataDB func (rs *RedisStorage) GetStatQueueProfileDrv(tenant string, id string) (sq *StatQueueProfile, err error) { - key := utils.StatQueueProfilePrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &sq); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.StatQueueProfilePrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &sq) return } -// SetStatsQueueDrv stores a StatsQueue into DataDB +// SetStatQueueProfileDrv stores a StatsQueue into DataDB func (rs *RedisStorage) SetStatQueueProfileDrv(sq *StatQueueProfile) (err error) { - result, err := rs.ms.Marshal(sq) - if err != nil { + var result []byte + if result, err = rs.ms.Marshal(sq); err != nil { return } - return rs.Cmd(redis_SET, utils.StatQueueProfilePrefix+utils.ConcatenatedKey(sq.Tenant, sq.ID), result).Err + return rs.Cmd(nil, redis_SET, utils.StatQueueProfilePrefix+utils.ConcatenatedKey(sq.Tenant, sq.ID), string(result)) } -// RemStatsQueueDrv removes a StatsQueue from dataDB +// RemStatQueueProfileDrv removes a StatsQueue from dataDB func (rs *RedisStorage) RemStatQueueProfileDrv(tenant, id string) (err error) { - key := utils.StatQueueProfilePrefix + utils.ConcatenatedKey(tenant, id) - err = rs.Cmd(redis_DEL, key).Err - return + return rs.Cmd(nil, redis_DEL, utils.StatQueueProfilePrefix+utils.ConcatenatedKey(tenant, id)) } -// GetStoredStatQueue retrieves the stored metrics for a StatsQueue +// GetStatQueueDrv retrieves the stored metrics for a StatsQueue func (rs *RedisStorage) GetStatQueueDrv(tenant, id string) (sq *StatQueue, err error) { - key := utils.StatQueuePrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } + if err = rs.Cmd(&values, redis_GET, utils.StatQueuePrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } var ssq StoredStatQueue @@ -1286,273 +1133,213 @@ func (rs *RedisStorage) GetStatQueueDrv(tenant, id string) (sq *StatQueue, err e return } -// SetStoredStatQueue stores the metrics for a StatsQueue +// SetStatQueueDrv stores the metrics for a StatsQueue func (rs *RedisStorage) SetStatQueueDrv(ssq *StoredStatQueue, sq *StatQueue) (err error) { var result []byte - result, err = rs.ms.Marshal(ssq) - if err != nil { + if result, err = rs.ms.Marshal(ssq); err != nil { return } - return rs.Cmd(redis_SET, utils.StatQueuePrefix+ssq.SqID(), result).Err + return rs.Cmd(nil, redis_SET, utils.StatQueuePrefix+ssq.SqID(), string(result)) } -// RemoveStatQueue removes a StatsQueue +// RemStatQueueDrv removes a StatsQueue func (rs *RedisStorage) RemStatQueueDrv(tenant, id string) (err error) { - key := utils.StatQueuePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.StatQueuePrefix+utils.ConcatenatedKey(tenant, id)) } // GetThresholdProfileDrv retrieves a ThresholdProfile from dataDB func (rs *RedisStorage) GetThresholdProfileDrv(tenant, ID string) (tp *ThresholdProfile, err error) { - key := utils.ThresholdProfilePrefix + utils.ConcatenatedKey(tenant, ID) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &tp); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.ThresholdProfilePrefix+utils.ConcatenatedKey(tenant, ID)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &tp) return } // SetThresholdProfileDrv stores a ThresholdProfile into DataDB func (rs *RedisStorage) SetThresholdProfileDrv(tp *ThresholdProfile) (err error) { var result []byte - result, err = rs.ms.Marshal(tp) - if err != nil { + if result, err = rs.ms.Marshal(tp); err != nil { return } - return rs.Cmd(redis_SET, utils.ThresholdProfilePrefix+tp.TenantID(), result).Err + return rs.Cmd(nil, redis_SET, utils.ThresholdProfilePrefix+tp.TenantID(), string(result)) } -// RemoveThresholdProfile removes a ThresholdProfile from dataDB/cache +// RemThresholdProfileDrv removes a ThresholdProfile from dataDB/cache func (rs *RedisStorage) RemThresholdProfileDrv(tenant, id string) (err error) { - key := utils.ThresholdProfilePrefix + utils.ConcatenatedKey(tenant, id) - err = rs.Cmd(redis_DEL, key).Err - return + return rs.Cmd(nil, redis_DEL, utils.ThresholdProfilePrefix+utils.ConcatenatedKey(tenant, id)) } func (rs *RedisStorage) GetThresholdDrv(tenant, id string) (r *Threshold, err error) { - key := utils.ThresholdPrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.ThresholdPrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &r) return } func (rs *RedisStorage) SetThresholdDrv(r *Threshold) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err + var result []byte + if result, err = rs.ms.Marshal(r); err != nil { + return } - return rs.Cmd(redis_SET, utils.ThresholdPrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result).Err + return rs.Cmd(nil, redis_SET, utils.ThresholdPrefix+utils.ConcatenatedKey(r.Tenant, r.ID), string(result)) } func (rs *RedisStorage) RemoveThresholdDrv(tenant, id string) (err error) { - key := utils.ThresholdPrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.ThresholdPrefix+utils.ConcatenatedKey(tenant, id)) } func (rs *RedisStorage) GetFilterDrv(tenant, id string) (r *Filter, err error) { - key := utils.FilterPrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } + if err = rs.Cmd(&values, redis_GET, utils.FilterPrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } - if err = rs.ms.Unmarshal(values, &r); err != nil { - return nil, err - } + err = rs.ms.Unmarshal(values, &r) return } func (rs *RedisStorage) SetFilterDrv(r *Filter) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err + var result []byte + if result, err = rs.ms.Marshal(r); err != nil { + return } - return rs.Cmd(redis_SET, utils.FilterPrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result).Err + return rs.Cmd(nil, redis_SET, utils.FilterPrefix+utils.ConcatenatedKey(r.Tenant, r.ID), string(result)) } func (rs *RedisStorage) RemoveFilterDrv(tenant, id string) (err error) { - key := utils.FilterPrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.FilterPrefix+utils.ConcatenatedKey(tenant, id)) } func (rs *RedisStorage) GetRouteProfileDrv(tenant, id string) (r *RouteProfile, err error) { - key := utils.RouteProfilePrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.RouteProfilePrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &r) return } func (rs *RedisStorage) SetRouteProfileDrv(r *RouteProfile) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err + var result []byte + if result, err = rs.ms.Marshal(r); err != nil { + return } - return rs.Cmd(redis_SET, utils.RouteProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result).Err + return rs.Cmd(nil, redis_SET, utils.RouteProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), string(result)) } func (rs *RedisStorage) RemoveRouteProfileDrv(tenant, id string) (err error) { - key := utils.RouteProfilePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.RouteProfilePrefix+utils.ConcatenatedKey(tenant, id)) } func (rs *RedisStorage) GetAttributeProfileDrv(tenant, id string) (r *AttributeProfile, err error) { - key := utils.AttributeProfilePrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.AttributeProfilePrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &r) return } func (rs *RedisStorage) SetAttributeProfileDrv(r *AttributeProfile) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err + var result []byte + if result, err = rs.ms.Marshal(r); err != nil { + return } - return rs.Cmd(redis_SET, utils.AttributeProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result).Err + return rs.Cmd(nil, redis_SET, utils.AttributeProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), string(result)) } func (rs *RedisStorage) RemoveAttributeProfileDrv(tenant, id string) (err error) { - key := utils.AttributeProfilePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.AttributeProfilePrefix+utils.ConcatenatedKey(tenant, id)) } func (rs *RedisStorage) GetChargerProfileDrv(tenant, id string) (r *ChargerProfile, err error) { - key := utils.ChargerProfilePrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.ChargerProfilePrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &r) return } func (rs *RedisStorage) SetChargerProfileDrv(r *ChargerProfile) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err + var result []byte + if result, err = rs.ms.Marshal(r); err != nil { + return } - return rs.Cmd(redis_SET, utils.ChargerProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result).Err + return rs.Cmd(nil, redis_SET, utils.ChargerProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), string(result)) } func (rs *RedisStorage) RemoveChargerProfileDrv(tenant, id string) (err error) { - key := utils.ChargerProfilePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.ChargerProfilePrefix+utils.ConcatenatedKey(tenant, id)) } func (rs *RedisStorage) GetDispatcherProfileDrv(tenant, id string) (r *DispatcherProfile, err error) { - key := utils.DispatcherProfilePrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.DispatcherProfilePrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &r) return } func (rs *RedisStorage) SetDispatcherProfileDrv(r *DispatcherProfile) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err + var result []byte + if result, err = rs.ms.Marshal(r); err != nil { + return } - return rs.Cmd(redis_SET, utils.DispatcherProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result).Err + return rs.Cmd(nil, redis_SET, utils.DispatcherProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), string(result)) } func (rs *RedisStorage) RemoveDispatcherProfileDrv(tenant, id string) (err error) { - key := utils.DispatcherProfilePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.DispatcherProfilePrefix+utils.ConcatenatedKey(tenant, id)) } func (rs *RedisStorage) GetDispatcherHostDrv(tenant, id string) (r *DispatcherHost, err error) { - key := utils.DispatcherHostPrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.DispatcherHostPrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &r) return } func (rs *RedisStorage) SetDispatcherHostDrv(r *DispatcherHost) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err + var result []byte + if result, err = rs.ms.Marshal(r); err != nil { + return } - return rs.Cmd(redis_SET, utils.DispatcherHostPrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result).Err + return rs.Cmd(nil, redis_SET, utils.DispatcherHostPrefix+utils.ConcatenatedKey(r.Tenant, r.ID), string(result)) } func (rs *RedisStorage) RemoveDispatcherHostDrv(tenant, id string) (err error) { - key := utils.DispatcherHostPrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.DispatcherHostPrefix+utils.ConcatenatedKey(tenant, id)) } func (rs *RedisStorage) GetStorageType() string { @@ -1561,70 +1348,62 @@ func (rs *RedisStorage) GetStorageType() string { func (rs *RedisStorage) GetItemLoadIDsDrv(itemIDPrefix string) (loadIDs map[string]int64, err error) { if itemIDPrefix != "" { - fldVal, err := rs.Cmd(redis_HGET, utils.LoadIDs, itemIDPrefix).Int64() - if err != nil { - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } - return nil, err + var fldVal int64 + mn := radix.MaybeNil{Rcv: &fldVal} + if err = rs.Cmd(&mn, redis_HGET, utils.LoadIDs, itemIDPrefix); err != nil { + return + } else if mn.Nil { + err = utils.ErrNotFound + return } return map[string]int64{itemIDPrefix: fldVal}, nil } - mpLoadIDs, err := rs.Cmd(redis_HGETALL, utils.LoadIDs).Map() - if err != nil { - return nil, err + mpLoadIDs := make(map[string]string) + if err = rs.Cmd(&mpLoadIDs, redis_HGETALL, utils.LoadIDs); err != nil { + return + } + if len(mpLoadIDs) == 0 { + return nil, utils.ErrNotFound } loadIDs = make(map[string]int64) for key, val := range mpLoadIDs { - intVal, err := strconv.ParseInt(val, 10, 64) - if err != nil { + if loadIDs[key], err = strconv.ParseInt(val, 10, 64); err != nil { return nil, err } - loadIDs[key] = intVal - } - if len(loadIDs) == 0 { - return nil, utils.ErrNotFound } return } func (rs *RedisStorage) SetLoadIDsDrv(loadIDs map[string]int64) error { - return rs.Cmd(redis_HMSET, utils.LoadIDs, loadIDs).Err + return rs.FlatCmd(nil, redis_HMSET, utils.LoadIDs, loadIDs) } func (rs *RedisStorage) RemoveLoadIDsDrv() (err error) { - return rs.Cmd(redis_DEL, utils.LoadIDs).Err + return rs.Cmd(nil, redis_DEL, utils.LoadIDs) } func (rs *RedisStorage) GetRateProfileDrv(tenant, id string) (rpp *RateProfile, err error) { - key := utils.RateProfilePrefix + utils.ConcatenatedKey(tenant, id) var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &rpp); err != nil { + if err = rs.Cmd(&values, redis_GET, utils.RateProfilePrefix+utils.ConcatenatedKey(tenant, id)); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } + err = rs.ms.Unmarshal(values, &rpp) return } func (rs *RedisStorage) SetRateProfileDrv(rpp *RateProfile) (err error) { - result, err := rs.ms.Marshal(rpp) - if err != nil { - return err + var result []byte + if result, err = rs.ms.Marshal(rpp); err != nil { + return } - return rs.Cmd(redis_SET, utils.RateProfilePrefix+utils.ConcatenatedKey(rpp.Tenant, rpp.ID), result).Err + return rs.Cmd(nil, redis_SET, utils.RateProfilePrefix+utils.ConcatenatedKey(rpp.Tenant, rpp.ID), string(result)) } func (rs *RedisStorage) RemoveRateProfileDrv(tenant, id string) (err error) { - key := utils.RateProfilePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(redis_DEL, key).Err; err != nil { - return - } - return + return rs.Cmd(nil, redis_DEL, utils.RateProfilePrefix+utils.ConcatenatedKey(tenant, id)) } // GetIndexesDrv retrieves Indexes from dataDB @@ -1632,16 +1411,14 @@ func (rs *RedisStorage) GetIndexesDrv(idxItmType, tntCtx, idxKey string) (indexe mp := make(map[string]string) dbKey := utils.CacheInstanceToPrefix[idxItmType] + tntCtx if len(idxKey) == 0 { - mp, err = rs.Cmd(redis_HGETALL, dbKey).Map() - if err != nil { + if err = rs.Cmd(&mp, redis_HGETALL, dbKey); err != nil { return } else if len(mp) == 0 { return nil, utils.ErrNotFound } } else { var itmMpStrLst []string - itmMpStrLst, err = rs.Cmd(redis_HMGET, dbKey, idxKey).List() - if err != nil { + if err = rs.Cmd(&itmMpStrLst, redis_HMGET, dbKey, idxKey); err != nil { return } else if itmMpStrLst[0] == utils.EmptyString { return nil, utils.ErrNotFound @@ -1668,10 +1445,10 @@ func (rs *RedisStorage) SetIndexesDrv(idxItmType, tntCtx string, dbKey = "tmp_" + utils.ConcatenatedKey(dbKey, transactionID) } if commit && transactionID != utils.EmptyString { - return rs.Cmd(redis_RENAME, dbKey, originKey).Err + return rs.Cmd(nil, redis_RENAME, dbKey, originKey) } mp := make(map[string]string) - deleteArgs := []interface{}{dbKey} // the dbkey is necesary for the HDEL command + deleteArgs := []string{dbKey} // the dbkey is necesary for the HDEL command for key, strMp := range indexes { if len(strMp) == 0 { // remove with no more elements inside deleteArgs = append(deleteArgs, key) @@ -1684,19 +1461,19 @@ func (rs *RedisStorage) SetIndexesDrv(idxItmType, tntCtx string, mp[key] = string(encodedMp) } if len(deleteArgs) != 1 { - if err = rs.Cmd(redis_HDEL, deleteArgs...).Err; err != nil { + if err = rs.Cmd(nil, redis_HDEL, deleteArgs...); err != nil { return } } if len(mp) != 0 { - return rs.Cmd(redis_HMSET, dbKey, mp).Err + return rs.FlatCmd(nil, redis_HMSET, dbKey, mp) } return } func (rs *RedisStorage) RemoveIndexesDrv(idxItmType, tntCtx, idxKey string) (err error) { if idxKey == utils.EmptyString { - return rs.Cmd(redis_DEL, utils.CacheInstanceToPrefix[idxItmType]+tntCtx).Err + return rs.Cmd(nil, redis_DEL, utils.CacheInstanceToPrefix[idxItmType]+tntCtx) } - return rs.Cmd(redis_HDEL, utils.CacheInstanceToPrefix[idxItmType]+tntCtx, idxKey).Err + return rs.Cmd(nil, redis_HDEL, utils.CacheInstanceToPrefix[idxItmType]+tntCtx, idxKey) } diff --git a/engine/storage_redisv3.go b/engine/storage_redisv3.go deleted file mode 100644 index 577ae3887..000000000 --- a/engine/storage_redisv3.go +++ /dev/null @@ -1,1557 +0,0 @@ -/* -Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments -Copyright (C) ITsysCOM GmbH - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see -*/ - -package engine - -import ( - "bytes" - "compress/zlib" - "errors" - "io" - "io/ioutil" - "strconv" - "time" - - "github.com/cgrates/cgrates/config" - "github.com/cgrates/cgrates/guardian" - "github.com/cgrates/cgrates/utils" - "github.com/cgrates/ltcache" - "github.com/mediocregopher/radix.v2/redis" - "github.com/mediocregopher/radix/v3" -) - -type RedisStoragev3 struct { - client radix.Client - ms Marshaler -} - -// Redis commands -const ( - redis_AUTH = "AUTH" - redis_SELECT = "SELECT" - redis_FLUSHDB = "FLUSHDB" - redis_DEL = "DEL" - redis_HGETALL = "HGETALL" - redis_KEYS = "KEYS" - redis_SADD = "SADD" - redis_SMEMBERS = "SMEMBERS" - redis_SREM = "SREM" - redis_EXISTS = "EXISTS" - redis_GET = "GET" - redis_SET = "SET" - redis_LRANGE = "LRANGE" - redis_LLEN = "LLEN" - redis_RPOP = "RPOP" - redis_LPUSH = "LPUSH" - redis_RPUSH = "RPUSH" - redis_LPOP = "LPOP" - redis_HMGET = "HMGET" - redis_HDEL = "HDEL" - redis_HGET = "HGET" - redis_RENAME = "RENAME" - redis_HMSET = "HMSET" -) - -func NewRedisStoragev3(address string, db int, user, pass, mrshlerStr string, - maxConns int, sentinelName string) (rs *RedisStoragev3, err error) { - - rs = new(RedisStoragev3) - - if rs.ms, err = NewMarshaler(mrshlerStr); err != nil { - rs = nil - return - } - - dialOpts := []radix.DialOpt{ - radix.DialSelectDB(db), - } - if user == utils.EmptyString { - dialOpts = append(dialOpts, radix.DialAuthPass(pass)) - } else { - dialOpts = append(dialOpts, radix.DialAuthUser(user, pass)) - } - - dialFunc := func(network, addr string) (radix.Conn, error) { - return radix.Dial(network, addr, dialOpts...) - } - - switch { - case sentinelName != utils.EmptyString: - if rs.client, err = radix.NewSentinel(sentinelName, utils.InfieldSplit(address), - radix.SentinelConnFunc(dialFunc), - radix.SentinelPoolFunc(func(network, addr string) (radix.Client, error) { - return radix.NewPool(network, addr, maxConns, radix.PoolConnFunc(dialFunc)) - })); err != nil { - rs = nil - return - } - default: - if rs.client, err = radix.NewPool(utils.TCP, address, maxConns, radix.PoolConnFunc(dialFunc)); err != nil { - rs = nil - return - } - } - - return -} - -// Cmd function get a connection from the pool. -// Handles automatic failover in case of network disconnects -func (rs *RedisStoragev3) Cmd(rcv interface{}, cmd string, args ...string) error { - return rs.client.Do(radix.Cmd(rcv, cmd, args...)) -} - -// Cmd function get a connection from the pool. -// Handles automatic failover in case of network disconnects -func (rs *RedisStoragev3) FlatCmd(rcv interface{}, cmd, key string, args ...interface{}) error { - return rs.client.Do(radix.FlatCmd(rcv, cmd, key, args...)) -} - -func (rs *RedisStoragev3) Close() { - if rs.client != nil { - rs.client.Close() - } -} - -func (rs *RedisStoragev3) Flush(ignore string) error { - return rs.Cmd(nil, redis_FLUSHDB) -} - -func (rs *RedisStoragev3) Marshaler() Marshaler { - return rs.ms -} - -func (rs *RedisStoragev3) SelectDatabase(dbName string) (err error) { - return rs.Cmd(nil, redis_SELECT, dbName) -} - -func (rs *RedisStoragev3) IsDBEmpty() (resp bool, err error) { - var keys []string - keys, err = rs.GetKeysForPrefix("") - if err != nil { - return - } - if len(keys) != 0 { - return false, nil - } - return true, nil -} - -func (rs *RedisStoragev3) RebuildReverseForPrefix(prefix string) (err error) { - if !utils.SliceHasMember([]string{utils.AccountActionPlansPrefix, utils.REVERSE_DESTINATION_PREFIX}, prefix) { - return utils.ErrInvalidKey - } - var keys []string - keys, err = rs.GetKeysForPrefix(prefix) - if err != nil { - return - } - for _, key := range keys { - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - } - switch prefix { - case utils.REVERSE_DESTINATION_PREFIX: - if keys, err = rs.GetKeysForPrefix(utils.DESTINATION_PREFIX); err != nil { - return - } - for _, key := range keys { - dest, err := rs.GetDestinationDrv(key[len(utils.DESTINATION_PREFIX):], true, utils.NonTransactional) - if err != nil { - return err - } - if err = rs.SetReverseDestinationDrv(dest, utils.NonTransactional); err != nil { - return err - } - } - case utils.AccountActionPlansPrefix: - if keys, err = rs.GetKeysForPrefix(utils.ACTION_PLAN_PREFIX); err != nil { - return - } - for _, key := range keys { - apl, err := rs.GetActionPlanDrv(key[len(utils.ACTION_PLAN_PREFIX):], true, utils.NonTransactional) // skipCache on get since loader checks and caches empty data for loaded objects - if err != nil { - return err - } - for acntID := range apl.AccountIDs { - if err = rs.SetAccountActionPlansDrv(acntID, []string{apl.Id}, false); err != nil { - return err - } - } - } - } - return nil -} - -func (rs *RedisStoragev3) RemoveReverseForPrefix(prefix string) (err error) { - if !utils.SliceHasMember([]string{utils.AccountActionPlansPrefix, utils.REVERSE_DESTINATION_PREFIX}, prefix) { - return utils.ErrInvalidKey - } - var keys []string - keys, err = rs.GetKeysForPrefix(prefix) - if err != nil { - return - } - for _, key := range keys { - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - } - switch prefix { - case utils.REVERSE_DESTINATION_PREFIX: - if keys, err = rs.GetKeysForPrefix(utils.DESTINATION_PREFIX); err != nil { - return - } - for _, key := range keys { - dest, err := rs.GetDestinationDrv(key[len(utils.DESTINATION_PREFIX):], true, utils.NonTransactional) - if err != nil { - return err - } - if err := rs.RemoveDestinationDrv(dest.Id, utils.NonTransactional); err != nil { - return err - } - } - case utils.AccountActionPlansPrefix: - if keys, err = rs.GetKeysForPrefix(utils.ACTION_PLAN_PREFIX); err != nil { - return - } - for _, key := range keys { - apl, err := rs.GetActionPlanDrv(key[len(utils.ACTION_PLAN_PREFIX):], true, utils.NonTransactional) // skipCache on get since loader checks and caches empty data for loaded objects - if err != nil { - return err - } - for acntID := range apl.AccountIDs { - if err = rs.RemAccountActionPlansDrv(acntID, []string{apl.Id}); err != nil { - return err - } - } - } - } - return nil -} - -func (rs *RedisStoragev3) getKeysForFilterIndexesKeys(fkeys []string) (keys []string, err error) { - for _, itemIDPrefix := range fkeys { - mp := make(map[string]string) - if err = rs.Cmd(&mp, redis_HGETALL, itemIDPrefix); err != nil { - return - } else if len(mp) == 0 { - return nil, utils.ErrNotFound - } - for k := range mp { - keys = append(keys, utils.ConcatenatedKey(itemIDPrefix, k)) - } - } - return -} - -func (rs *RedisStoragev3) RebbuildActionPlanKeys() (err error) { - var keys []string - if err = rs.Cmd(&keys, redis_KEYS, utils.ACTION_PLAN_PREFIX+"*"); err != nil { - return - } - for _, key := range keys { - if err = rs.Cmd(nil, redis_SADD, utils.ActionPlanIndexes, key); err != nil { - return - } - } - return -} - -func (rs *RedisStoragev3) GetKeysForPrefix(prefix string) (keys []string, err error) { - if prefix == utils.ACTION_PLAN_PREFIX { // so we can avoid the full scan on scheduler reloads - err = rs.Cmd(&keys, redis_SMEMBERS, utils.ActionPlanIndexes) - } else { - err = rs.Cmd(&keys, redis_KEYS, prefix+"*") - } - if err != nil { - return - } - if len(keys) != 0 { - if filterIndexesPrefixMap.Has(prefix) { - return rs.getKeysForFilterIndexesKeys(keys) - } - return - } - return nil, nil -} - -// Used to check if specific subject is stored using prefix key attached to entity -func (rs *RedisStoragev3) HasDataDrv(category, subject, tenant string) (exists bool, err error) { - var i int - switch category { - case utils.DESTINATION_PREFIX, utils.RATING_PLAN_PREFIX, utils.RATING_PROFILE_PREFIX, - utils.ACTION_PREFIX, utils.ACTION_PLAN_PREFIX, utils.ACCOUNT_PREFIX: - err = rs.Cmd(&i, redis_EXISTS, category+subject) - return i == 1, err - case utils.ResourcesPrefix, utils.ResourceProfilesPrefix, utils.StatQueuePrefix, - utils.StatQueueProfilePrefix, utils.ThresholdPrefix, utils.ThresholdProfilePrefix, - utils.FilterPrefix, utils.RouteProfilePrefix, utils.AttributeProfilePrefix, - utils.ChargerProfilePrefix, utils.DispatcherProfilePrefix, utils.DispatcherHostPrefix, - utils.RateProfilePrefix: - err := rs.Cmd(&i, redis_EXISTS, category+utils.ConcatenatedKey(tenant, subject)) - return i == 1, err - } - return false, errors.New("unsupported HasData category") -} - -func (rs *RedisStoragev3) GetRatingPlanDrv(key string) (rp *RatingPlan, err error) { - key = utils.RATING_PLAN_PREFIX + key - var values []byte - if err = rs.Cmd(&values, redis_GET, key); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - b := bytes.NewBuffer(values) - var r io.ReadCloser - if r, err = zlib.NewReader(b); err != nil { - return - } - var out []byte - if out, err = ioutil.ReadAll(r); err != nil { - return - } - r.Close() - err = rs.ms.Unmarshal(out, &rp) - return -} - -func (rs *RedisStoragev3) SetRatingPlanDrv(rp *RatingPlan) (err error) { - result, err := rs.ms.Marshal(rp) - var b bytes.Buffer - w := zlib.NewWriter(&b) - w.Write(result) - w.Close() - err = rs.Cmd(nil, redis_SET, utils.RATING_PLAN_PREFIX+rp.Id, b.String()) - return -} - -func (rs *RedisStoragev3) RemoveRatingPlanDrv(key string) (err error) { - var keys []string - if err = rs.Cmd(&keys, redis_KEYS, utils.RATING_PLAN_PREFIX+key+"*"); err != nil { - return - } - for _, key := range keys { - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - } - return -} - -func (rs *RedisStoragev3) GetRatingProfileDrv(key string) (rpf *RatingProfile, err error) { - key = utils.RATING_PROFILE_PREFIX + key - var values []byte - if err = rs.Cmd(&values, redis_GET, key); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - err = rs.ms.Unmarshal(values, &rpf) - return -} - -func (rs *RedisStoragev3) SetRatingProfileDrv(rpf *RatingProfile) (err error) { - var result []byte - if result, err = rs.ms.Marshal(rpf); err != nil { - return - } - key := utils.RATING_PROFILE_PREFIX + rpf.Id - err = rs.Cmd(nil, redis_SET, key, string(result)) - return -} - -func (rs *RedisStoragev3) RemoveRatingProfileDrv(key string) (err error) { - var keys []string - if err = rs.Cmd(&keys, redis_KEYS, utils.RATING_PROFILE_PREFIX+key+"*"); err != nil { - return - } - for _, key := range keys { - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - } - return -} - -// GetDestination retrieves a destination with id from tp_db -func (rs *RedisStoragev3) GetDestinationDrv(key string, skipCache bool, - transactionID string) (dest *Destination, err error) { - if !skipCache { - if x, ok := Cache.Get(utils.CacheDestinations, key); ok { - if x == nil { - return nil, utils.ErrNotFound - } - return x.(*Destination), nil - } - } - var values []byte - if err = rs.Cmd(&values, redis_GET, utils.DESTINATION_PREFIX+key); err != nil { - if err == redis.ErrRespNil { // did not find the destination - if errCh := Cache.Set(utils.CacheDestinations, key, nil, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh - } - err = utils.ErrNotFound - } - return - } - b := bytes.NewBuffer(values) - var r io.ReadCloser - if r, err = zlib.NewReader(b); err != nil { - return - } - var out []byte - if out, err = ioutil.ReadAll(r); err != nil { - return - } - r.Close() - if err = rs.ms.Unmarshal(out, &dest); err != nil { - return - } - err = Cache.Set(utils.CacheDestinations, key, dest, nil, - cacheCommit(transactionID), transactionID) - return -} - -func (rs *RedisStoragev3) SetDestinationDrv(dest *Destination, transactionID string) (err error) { - var result []byte - if result, err = rs.ms.Marshal(dest); err != nil { - return - } - var b bytes.Buffer - w := zlib.NewWriter(&b) - w.Write(result) - w.Close() - err = rs.Cmd(nil, redis_SET, utils.DESTINATION_PREFIX+dest.Id, b.String()) - return -} - -func (rs *RedisStoragev3) GetReverseDestinationDrv(key string, - skipCache bool, transactionID string) (ids []string, err error) { - if !skipCache { - if x, ok := Cache.Get(utils.CacheReverseDestinations, key); ok { - if x == nil { - return nil, utils.ErrNotFound - } - return x.([]string), nil - } - } - if err = rs.Cmd(&ids, redis_SMEMBERS, utils.REVERSE_DESTINATION_PREFIX+key); err != nil { - return - } - if len(ids) == 0 { - if err = Cache.Set(utils.CacheReverseDestinations, key, nil, nil, - cacheCommit(transactionID), transactionID); err != nil { - return - } - err = utils.ErrNotFound - return - } - err = Cache.Set(utils.CacheReverseDestinations, key, ids, nil, - cacheCommit(transactionID), transactionID) - return -} - -func (rs *RedisStoragev3) SetReverseDestinationDrv(dest *Destination, transactionID string) (err error) { - for _, p := range dest.Prefixes { - if err = rs.Cmd(nil, redis_SADD, utils.REVERSE_DESTINATION_PREFIX+p, dest.Id); err != nil { - return - } - } - return -} - -func (rs *RedisStoragev3) RemoveDestinationDrv(destID, transactionID string) (err error) { - // get destination for prefix list - var d *Destination - if d, err = rs.GetDestinationDrv(destID, false, transactionID); err != nil { - return - } - if err = rs.Cmd(nil, redis_DEL, utils.DESTINATION_PREFIX+destID); err != nil { - return - } - if err = Cache.Remove(utils.CacheDestinations, destID, - cacheCommit(transactionID), transactionID); err != nil { - return - } - if d == nil { - return utils.ErrNotFound - } - for _, prefix := range d.Prefixes { - if err = rs.Cmd(nil, redis_SREM, utils.REVERSE_DESTINATION_PREFIX+prefix, destID); err != nil { - return - } - rs.GetReverseDestinationDrv(prefix, true, transactionID) // it will recache the destination - } - return -} - -func (rs *RedisStoragev3) UpdateReverseDestinationDrv(oldDest, newDest *Destination, transactionID string) (err error) { - //log.Printf("Old: %+v, New: %+v", oldDest, newDest) - var obsoletePrefixes []string - var addedPrefixes []string - var found bool - if oldDest == nil { - oldDest = new(Destination) // so we can process prefixes - } - for _, oldPrefix := range oldDest.Prefixes { - found = false - for _, newPrefix := range newDest.Prefixes { - if oldPrefix == newPrefix { - found = true - break - } - } - if !found { - obsoletePrefixes = append(obsoletePrefixes, oldPrefix) - } - } - for _, newPrefix := range newDest.Prefixes { - found = false - for _, oldPrefix := range oldDest.Prefixes { - if newPrefix == oldPrefix { - found = true - break - } - } - if !found { - addedPrefixes = append(addedPrefixes, newPrefix) - } - } - // remove id for all obsolete prefixes - cCommit := cacheCommit(transactionID) - for _, obsoletePrefix := range obsoletePrefixes { - if err = rs.Cmd(nil, redis_SREM, - utils.REVERSE_DESTINATION_PREFIX+obsoletePrefix, oldDest.Id); err != nil { - return - } - if err = Cache.Remove(utils.CacheReverseDestinations, obsoletePrefix, - cCommit, transactionID); err != nil { - return - } - } - - // add the id to all new prefixes - for _, addedPrefix := range addedPrefixes { - if err = rs.Cmd(nil, redis_SADD, utils.REVERSE_DESTINATION_PREFIX+addedPrefix, newDest.Id); err != nil { - return - } - } - return -} - -func (rs *RedisStoragev3) GetActionsDrv(key string) (as Actions, err error) { - key = utils.ACTION_PREFIX + key - var values []byte - if err = rs.Cmd(&values, redis_GET, key); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - err = rs.ms.Unmarshal(values, &as) - return -} - -func (rs *RedisStoragev3) SetActionsDrv(key string, as Actions) (err error) { - var result []byte - if result, err = rs.ms.Marshal(&as); err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.ACTION_PREFIX+key, string(result)) -} - -func (rs *RedisStoragev3) RemoveActionsDrv(key string) (err error) { - return rs.Cmd(nil, redis_DEL, utils.ACTION_PREFIX+key) -} - -func (rs *RedisStoragev3) GetSharedGroupDrv(key string) (sg *SharedGroup, err error) { - var values []byte - if err = rs.Cmd(&values, redis_GET, utils.SHARED_GROUP_PREFIX+key); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - err = rs.ms.Unmarshal(values, &sg) - return -} - -func (rs *RedisStoragev3) SetSharedGroupDrv(sg *SharedGroup) (err error) { - var result []byte - if result, err = rs.ms.Marshal(sg); err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.SHARED_GROUP_PREFIX+sg.Id, string(result)) -} - -func (rs *RedisStoragev3) RemoveSharedGroupDrv(id string) (err error) { - return rs.Cmd(nil, redis_DEL, utils.SHARED_GROUP_PREFIX+id) -} - -func (rs *RedisStoragev3) GetAccountDrv(key string) (ub *Account, err error) { - var values []byte - if err = rs.Cmd(&values, redis_GET, utils.ACCOUNT_PREFIX+key); err != nil { - return - // } else if rpl.IsType(redis.Nil) { - // return nil, utils.ErrNotFound - } - ub = &Account{ID: key} - if err = rs.ms.Unmarshal(values, ub); err != nil { - return nil, err - } - return ub, nil -} - -func (rs *RedisStoragev3) SetAccountDrv(acc *Account) (err error) { - // never override existing account with an empty one - // UPDATE: if all balances expired and were cleaned it makes - // sense to write empty balance map - if len(acc.BalanceMap) == 0 { - var ac *Account - if ac, err = rs.GetAccountDrv(acc.ID); err == nil && !ac.allBalancesExpired() { - ac.ActionTriggers = acc.ActionTriggers - ac.UnitCounters = acc.UnitCounters - ac.AllowNegative = acc.AllowNegative - ac.Disabled = acc.Disabled - acc = ac - } - } - acc.UpdateTime = time.Now() - var result []byte - if result, err = rs.ms.Marshal(acc); err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.ACCOUNT_PREFIX+acc.ID, string(result)) -} - -func (rs *RedisStoragev3) RemoveAccountDrv(key string) (err error) { - if err = rs.Cmd(nil, redis_DEL, utils.ACCOUNT_PREFIX+key); err == redis.ErrRespNil { - err = utils.ErrNotFound - } - return -} - -// Limit will only retrieve the last n items out of history, newest first -func (rs *RedisStoragev3) GetLoadHistory(limit int, skipCache bool, - transactionID string) (loadInsts []*utils.LoadInstance, err error) { - if limit == 0 { - return nil, nil - } - - if !skipCache { - if x, ok := Cache.Get(utils.LOADINST_KEY, ""); ok { - if x != nil { - items := x.([]*utils.LoadInstance) - if len(items) < limit || limit == -1 { - return items, nil - } - return items[:limit], nil - } - return nil, utils.ErrNotFound - } - } - if limit != -1 { - limit -= -1 // Decrease limit to match redis approach on lrange - } - cCommit := cacheCommit(transactionID) - var marshaleds [][]byte - if err = rs.Cmd(&marshaleds, redis_LRANGE, - utils.LOADINST_KEY, "0", strconv.Itoa(limit)); err != nil { - if errCh := Cache.Set(utils.LOADINST_KEY, "", nil, nil, - cCommit, transactionID); errCh != nil { - return nil, errCh - } - return - } - loadInsts = make([]*utils.LoadInstance, len(marshaleds)) - for idx, marshaled := range marshaleds { - if err = rs.ms.Unmarshal(marshaled, loadInsts[idx]); err != nil { - return nil, err - } - } - if err = Cache.Remove(utils.LOADINST_KEY, "", cCommit, transactionID); err != nil { - return nil, err - } - if err := Cache.Set(utils.LOADINST_KEY, "", loadInsts, nil, - cCommit, transactionID); err != nil { - return nil, err - } - if len(loadInsts) < limit || limit == -1 { - return loadInsts, nil - } - return loadInsts[:limit], nil -} - -// Adds a single load instance to load history -func (rs *RedisStoragev3) AddLoadHistory(ldInst *utils.LoadInstance, loadHistSize int, transactionID string) (err error) { - if loadHistSize == 0 { // Load history disabled - return - } - var marshaled []byte - if marshaled, err = rs.ms.Marshal(&ldInst); err != nil { - return - } - _, err = guardian.Guardian.Guard(func() (interface{}, error) { // Make sure we do it locked since other instance can modify history while we read it - var histLen int - if err := rs.Cmd(&histLen, redis_LLEN, utils.LOADINST_KEY); err != nil { - return nil, err - } - if histLen >= loadHistSize { // Have hit maximum history allowed, remove oldest element in order to add new one - if err = rs.Cmd(nil, redis_RPOP, utils.LOADINST_KEY); err != nil { - return nil, err - } - } - return nil, rs.Cmd(nil, redis_LPUSH, utils.LOADINST_KEY, string(marshaled)) - }, config.CgrConfig().GeneralCfg().LockingTimeout, utils.LOADINST_KEY) - - if errCh := Cache.Remove(utils.LOADINST_KEY, "", - cacheCommit(transactionID), transactionID); errCh != nil { - return errCh - } - return -} - -func (rs *RedisStoragev3) GetActionTriggersDrv(key string) (atrs ActionTriggers, err error) { - var values []byte - if err = rs.Cmd(&values, redis_GET, utils.ACTION_TRIGGER_PREFIX+key); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - err = rs.ms.Unmarshal(values, &atrs) - return -} - -func (rs *RedisStoragev3) SetActionTriggersDrv(key string, atrs ActionTriggers) (err error) { - if len(atrs) == 0 { - // delete the key - return rs.Cmd(nil, redis_DEL, utils.ACTION_TRIGGER_PREFIX+key) - } - var result []byte - if result, err = rs.ms.Marshal(atrs); err != nil { - return - } - if err = rs.Cmd(nil, redis_SET, utils.ACTION_TRIGGER_PREFIX+key, string(result)); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) RemoveActionTriggersDrv(key string) (err error) { - return rs.Cmd(nil, redis_DEL, utils.ACTION_TRIGGER_PREFIX+key) -} - -func (rs *RedisStoragev3) GetActionPlanDrv(key string, skipCache bool, - transactionID string) (ats *ActionPlan, err error) { - if !skipCache { - if x, err := Cache.GetCloned(utils.CacheActionPlans, key); err != nil { - if err != ltcache.ErrNotFound { // Only consider cache if item was found - return nil, err - } - } else if x == nil { // item was placed nil in cache - return nil, utils.ErrNotFound - } else { - return x.(*ActionPlan), nil - } - } - var values []byte - if err = rs.Cmd(&values, redis_GET, utils.ACTION_PLAN_PREFIX+key); err != nil { - if err == redis.ErrRespNil { // did not find the destination - if errCh := Cache.Set(utils.CacheActionPlans, key, nil, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh - } - err = utils.ErrNotFound - } - return - } - b := bytes.NewBuffer(values) - var r io.ReadCloser - if r, err = zlib.NewReader(b); err != nil { - return - } - var out []byte - if out, err = ioutil.ReadAll(r); err != nil { - return - } - r.Close() - if err = rs.ms.Unmarshal(out, &ats); err != nil { - return - } - err = Cache.Set(utils.CacheActionPlans, key, ats, nil, - cacheCommit(transactionID), transactionID) - return -} -func (rs *RedisStoragev3) RemoveActionPlanDrv(key string, - transactionID string) (err error) { - cCommit := cacheCommit(transactionID) - if err = rs.Cmd(nil, redis_SREM, utils.ActionPlanIndexes, utils.ACTION_PLAN_PREFIX+key); err != nil { - return - } - err = rs.Cmd(nil, redis_DEL, utils.ACTION_PLAN_PREFIX+key) - if errCh := Cache.Remove(utils.CacheActionPlans, key, - cCommit, transactionID); errCh != nil { - return errCh - } - return -} - -func (rs *RedisStoragev3) SetActionPlanDrv(key string, ats *ActionPlan, - overwrite bool, transactionID string) (err error) { - cCommit := cacheCommit(transactionID) - if len(ats.ActionTimings) == 0 { - // delete the key - if err = rs.Cmd(nil, redis_SREM, utils.ActionPlanIndexes, utils.ACTION_PLAN_PREFIX+key); err != nil { - return - } - err = rs.Cmd(nil, redis_DEL, utils.ACTION_PLAN_PREFIX+key) - if errCh := Cache.Remove(utils.CacheActionPlans, key, - cCommit, transactionID); errCh != nil { - return errCh - } - return - } - if !overwrite { - // get existing action plan to merge the account ids - if existingAts, _ := rs.GetActionPlanDrv(key, true, transactionID); existingAts != nil { - if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 { - ats.AccountIDs = make(utils.StringMap) - } - for accID := range existingAts.AccountIDs { - ats.AccountIDs[accID] = true - } - } - } - 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(nil, redis_SADD, utils.ActionPlanIndexes, utils.ACTION_PLAN_PREFIX+key); err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.ACTION_PLAN_PREFIX+key, b.String()) -} - -func (rs *RedisStoragev3) GetAllActionPlansDrv() (ats map[string]*ActionPlan, err error) { - var keys []string - if keys, err = rs.GetKeysForPrefix(utils.ACTION_PLAN_PREFIX); err != nil { - return - } - if len(keys) == 0 { - err = utils.ErrNotFound - return - } - ats = make(map[string]*ActionPlan, len(keys)) - for _, key := range keys { - if ats[key[len(utils.ACTION_PLAN_PREFIX):]], err = rs.GetActionPlanDrv(key[len(utils.ACTION_PLAN_PREFIX):], - false, utils.NonTransactional); err != nil { - return nil, err - } - } - return -} - -func (rs *RedisStoragev3) GetAccountActionPlansDrv(acntID string, skipCache bool, - transactionID string) (aPlIDs []string, err error) { - if !skipCache { - if x, ok := Cache.Get(utils.CacheAccountActionPlans, acntID); ok { - if x == nil { - return nil, utils.ErrNotFound - } - return x.([]string), nil - } - } - var values []byte - if err = rs.Cmd(&values, redis_GET, - utils.AccountActionPlansPrefix+acntID); err != nil { - if err == redis.ErrRespNil { // did not find the destination - if errCh := Cache.Set(utils.CacheAccountActionPlans, acntID, nil, nil, - cacheCommit(transactionID), transactionID); errCh != nil { - return nil, errCh - } - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &aPlIDs); err != nil { - return - } - err = Cache.Set(utils.CacheAccountActionPlans, acntID, aPlIDs, nil, - cacheCommit(transactionID), transactionID) - return -} - -func (rs *RedisStoragev3) SetAccountActionPlansDrv(acntID string, aPlIDs []string, overwrite bool) (err error) { - if !overwrite { - var oldaPlIDs []string - if oldaPlIDs, err = rs.GetAccountActionPlansDrv(acntID, true, utils.NonTransactional); err != nil && err != utils.ErrNotFound { - return - } - for _, oldAPid := range oldaPlIDs { - if !utils.IsSliceMember(aPlIDs, oldAPid) { - aPlIDs = append(aPlIDs, oldAPid) - } - } - } - var result []byte - if result, err = rs.ms.Marshal(aPlIDs); err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.AccountActionPlansPrefix+acntID, string(result)) -} - -func (rs *RedisStoragev3) RemAccountActionPlansDrv(acntID string, aPlIDs []string) (err error) { - key := utils.AccountActionPlansPrefix + acntID - if len(aPlIDs) == 0 { - return rs.Cmd(nil, redis_DEL, key) - } - var oldaPlIDs []string - if oldaPlIDs, err = rs.GetAccountActionPlansDrv(acntID, true, utils.NonTransactional); err != nil { - return - } - for i := 0; i < len(oldaPlIDs); { - if utils.IsSliceMember(aPlIDs, oldaPlIDs[i]) { - oldaPlIDs = append(oldaPlIDs[:i], oldaPlIDs[i+1:]...) - continue // if we have stripped, don't increase index so we can check next element by next run - } - i++ - } - if len(oldaPlIDs) == 0 { // no more elements, remove the reference - return rs.Cmd(nil, redis_DEL, key) - } - var result []byte - if result, err = rs.ms.Marshal(oldaPlIDs); err != nil { - return - } - return rs.Cmd(nil, redis_SET, key, string(result)) -} - -func (rs *RedisStoragev3) PushTask(t *Task) (err error) { - var result []byte - if result, err = rs.ms.Marshal(t); err != nil { - return - } - return rs.Cmd(nil, redis_RPUSH, utils.TASKS_KEY, string(result)) -} - -func (rs *RedisStoragev3) PopTask() (t *Task, err error) { - var values []byte - if err = rs.Cmd(&values, redis_LPOP, utils.TASKS_KEY); err != nil { - return - } - t = &Task{} - err = rs.ms.Unmarshal(values, t) - return -} - -func (rs *RedisStoragev3) GetResourceProfileDrv(tenant, id string) (rsp *ResourceProfile, err error) { - var values []byte - if err = rs.Cmd(&values, redis_GET, utils.ResourceProfilesPrefix+utils.ConcatenatedKey(tenant, id)); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - err = rs.ms.Unmarshal(values, &rsp) - return -} - -func (rs *RedisStoragev3) SetResourceProfileDrv(rsp *ResourceProfile) (err error) { - var result []byte - if result, err = rs.ms.Marshal(rsp); err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.ResourceProfilesPrefix+rsp.TenantID(), string(result)) -} - -func (rs *RedisStoragev3) RemoveResourceProfileDrv(tenant, id string) (err error) { - return rs.Cmd(nil, redis_DEL, utils.ResourceProfilesPrefix+utils.ConcatenatedKey(tenant, id)) -} - -func (rs *RedisStoragev3) GetResourceDrv(tenant, id string) (r *Resource, err error) { - var values []byte - if err = rs.Cmd(&values, redis_GET, utils.ResourcesPrefix+utils.ConcatenatedKey(tenant, id)); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - err = rs.ms.Unmarshal(values, &r) - return -} - -func (rs *RedisStoragev3) SetResourceDrv(r *Resource) (err error) { - var result []byte - if result, err = rs.ms.Marshal(r); err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.ResourcesPrefix+r.TenantID(), string(result)) -} - -func (rs *RedisStoragev3) RemoveResourceDrv(tenant, id string) (err error) { - return rs.Cmd(nil, redis_DEL, utils.ResourcesPrefix+utils.ConcatenatedKey(tenant, id)) -} - -func (rs *RedisStoragev3) GetTimingDrv(id string) (t *utils.TPTiming, err error) { - var values []byte - if err = rs.Cmd(&values, redis_GET, utils.TimingsPrefix+id); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - err = rs.ms.Unmarshal(values, &t) - return -} - -func (rs *RedisStoragev3) SetTimingDrv(t *utils.TPTiming) (err error) { - var result []byte - if result, err = rs.ms.Marshal(t); err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.TimingsPrefix+t.ID, string(result)) -} - -func (rs *RedisStoragev3) RemoveTimingDrv(id string) (err error) { - return rs.Cmd(nil, redis_DEL, utils.TimingsPrefix+id) -} - -func (rs *RedisStoragev3) GetVersions(itm string) (vrs Versions, err error) { - if itm != "" { - var fldVal string - if err = rs.Cmd(&fldVal, redis_HGET, utils.TBLVersions, itm); err != nil { - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } - return nil, err - } - var intVal int64 - if intVal, err = strconv.ParseInt(fldVal, 10, 64); err != nil { - return nil, err - } - return Versions{itm: intVal}, nil - } - var mp map[string]string - if err = rs.Cmd(&mp, redis_HGETALL, utils.TBLVersions); err != nil { - return nil, err - } - if len(mp) == 0 { - return nil, utils.ErrNotFound - } - if vrs, err = utils.MapStringToInt64(mp); err != nil { - return nil, err - } - return -} - -func (rs *RedisStoragev3) SetVersions(vrs Versions, overwrite bool) (err error) { - if overwrite { - if err = rs.RemoveVersions(nil); err != nil { - return - } - } - return rs.FlatCmd(nil, redis_HMSET, utils.TBLVersions, vrs) -} - -func (rs *RedisStoragev3) RemoveVersions(vrs Versions) (err error) { - if len(vrs) != 0 { - for key := range vrs { - if err = rs.Cmd(nil, redis_HDEL, utils.TBLVersions, key); err != nil { - return - } - } - return - } - return rs.Cmd(nil, redis_DEL, utils.TBLVersions) -} - -// GetStatQueueProfileDrv retrieves a StatQueueProfile from dataDB -func (rs *RedisStoragev3) GetStatQueueProfileDrv(tenant string, id string) (sq *StatQueueProfile, err error) { - key := utils.StatQueueProfilePrefix + utils.ConcatenatedKey(tenant, id) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &sq); err != nil { - return - } - return -} - -// SetStatsQueueDrv stores a StatsQueue into DataDB -func (rs *RedisStoragev3) SetStatQueueProfileDrv(sq *StatQueueProfile) (err error) { - result, err := rs.ms.Marshal(sq) - if err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.StatQueueProfilePrefix+utils.ConcatenatedKey(sq.Tenant, sq.ID), result) -} - -// RemStatsQueueDrv removes a StatsQueue from dataDB -func (rs *RedisStoragev3) RemStatQueueProfileDrv(tenant, id string) (err error) { - key := utils.StatQueueProfilePrefix + utils.ConcatenatedKey(tenant, id) - err = rs.Cmd(nil, redis_DEL, key) - return -} - -// GetStoredStatQueue retrieves the stored metrics for a StatsQueue -func (rs *RedisStoragev3) GetStatQueueDrv(tenant, id string) (sq *StatQueue, err error) { - key := utils.StatQueuePrefix + utils.ConcatenatedKey(tenant, id) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } - return - } - var ssq StoredStatQueue - if err = rs.ms.Unmarshal(values, &ssq); err != nil { - return - } - sq, err = ssq.AsStatQueue(rs.ms) - return -} - -// SetStoredStatQueue stores the metrics for a StatsQueue -func (rs *RedisStoragev3) SetStatQueueDrv(ssq *StoredStatQueue, sq *StatQueue) (err error) { - var result []byte - result, err = rs.ms.Marshal(ssq) - if err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.StatQueuePrefix+ssq.SqID(), result) -} - -// RemoveStatQueue removes a StatsQueue -func (rs *RedisStoragev3) RemStatQueueDrv(tenant, id string) (err error) { - key := utils.StatQueuePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - return -} - -// GetThresholdProfileDrv retrieves a ThresholdProfile from dataDB -func (rs *RedisStoragev3) GetThresholdProfileDrv(tenant, ID string) (tp *ThresholdProfile, err error) { - key := utils.ThresholdProfilePrefix + utils.ConcatenatedKey(tenant, ID) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &tp); err != nil { - return - } - return -} - -// SetThresholdProfileDrv stores a ThresholdProfile into DataDB -func (rs *RedisStoragev3) SetThresholdProfileDrv(tp *ThresholdProfile) (err error) { - var result []byte - result, err = rs.ms.Marshal(tp) - if err != nil { - return - } - return rs.Cmd(nil, redis_SET, utils.ThresholdProfilePrefix+tp.TenantID(), result) -} - -// RemoveThresholdProfile removes a ThresholdProfile from dataDB/cache -func (rs *RedisStoragev3) RemThresholdProfileDrv(tenant, id string) (err error) { - key := utils.ThresholdProfilePrefix + utils.ConcatenatedKey(tenant, id) - err = rs.Cmd(nil, redis_DEL, key) - return -} - -func (rs *RedisStoragev3) GetThresholdDrv(tenant, id string) (r *Threshold, err error) { - key := utils.ThresholdPrefix + utils.ConcatenatedKey(tenant, id) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) SetThresholdDrv(r *Threshold) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err - } - return rs.Cmd(nil, redis_SET, utils.ThresholdPrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result) -} - -func (rs *RedisStoragev3) RemoveThresholdDrv(tenant, id string) (err error) { - key := utils.ThresholdPrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) GetFilterDrv(tenant, id string) (r *Filter, err error) { - key := utils.FilterPrefix + utils.ConcatenatedKey(tenant, id) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { - return nil, err - } - return -} - -func (rs *RedisStoragev3) SetFilterDrv(r *Filter) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err - } - return rs.Cmd(nil, redis_SET, utils.FilterPrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result) -} - -func (rs *RedisStoragev3) RemoveFilterDrv(tenant, id string) (err error) { - key := utils.FilterPrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) GetRouteProfileDrv(tenant, id string) (r *RouteProfile, err error) { - key := utils.RouteProfilePrefix + utils.ConcatenatedKey(tenant, id) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) SetRouteProfileDrv(r *RouteProfile) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err - } - return rs.Cmd(nil, redis_SET, utils.RouteProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result) -} - -func (rs *RedisStoragev3) RemoveRouteProfileDrv(tenant, id string) (err error) { - key := utils.RouteProfilePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) GetAttributeProfileDrv(tenant, id string) (r *AttributeProfile, err error) { - key := utils.AttributeProfilePrefix + utils.ConcatenatedKey(tenant, id) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) SetAttributeProfileDrv(r *AttributeProfile) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err - } - return rs.Cmd(nil, redis_SET, utils.AttributeProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result) -} - -func (rs *RedisStoragev3) RemoveAttributeProfileDrv(tenant, id string) (err error) { - key := utils.AttributeProfilePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) GetChargerProfileDrv(tenant, id string) (r *ChargerProfile, err error) { - key := utils.ChargerProfilePrefix + utils.ConcatenatedKey(tenant, id) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) SetChargerProfileDrv(r *ChargerProfile) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err - } - return rs.Cmd(nil, redis_SET, utils.ChargerProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result) -} - -func (rs *RedisStoragev3) RemoveChargerProfileDrv(tenant, id string) (err error) { - key := utils.ChargerProfilePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) GetDispatcherProfileDrv(tenant, id string) (r *DispatcherProfile, err error) { - key := utils.DispatcherProfilePrefix + utils.ConcatenatedKey(tenant, id) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) SetDispatcherProfileDrv(r *DispatcherProfile) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err - } - return rs.Cmd(nil, redis_SET, utils.DispatcherProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result) -} - -func (rs *RedisStoragev3) RemoveDispatcherProfileDrv(tenant, id string) (err error) { - key := utils.DispatcherProfilePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) GetDispatcherHostDrv(tenant, id string) (r *DispatcherHost, err error) { - key := utils.DispatcherHostPrefix + utils.ConcatenatedKey(tenant, id) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &r); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) SetDispatcherHostDrv(r *DispatcherHost) (err error) { - result, err := rs.ms.Marshal(r) - if err != nil { - return err - } - return rs.Cmd(nil, redis_SET, utils.DispatcherHostPrefix+utils.ConcatenatedKey(r.Tenant, r.ID), result) -} - -func (rs *RedisStoragev3) RemoveDispatcherHostDrv(tenant, id string) (err error) { - key := utils.DispatcherHostPrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) GetStorageType() string { - return utils.REDIS -} - -func (rs *RedisStoragev3) GetItemLoadIDsDrv(itemIDPrefix string) (loadIDs map[string]int64, err error) { - if itemIDPrefix != "" { - fldVal, err := rs.Cmd(redis_HGET, utils.LoadIDs, itemIDPrefix).Int64() - if err != nil { - if err == redis.ErrRespNil { - err = utils.ErrNotFound - } - return nil, err - } - return map[string]int64{itemIDPrefix: fldVal}, nil - } - mpLoadIDs, err := rs.Cmd(redis_HGETALL, utils.LoadIDs).Map() - if err != nil { - return nil, err - } - loadIDs = make(map[string]int64) - for key, val := range mpLoadIDs { - intVal, err := strconv.ParseInt(val, 10, 64) - if err != nil { - return nil, err - } - loadIDs[key] = intVal - } - if len(loadIDs) == 0 { - return nil, utils.ErrNotFound - } - return -} - -func (rs *RedisStoragev3) SetLoadIDsDrv(loadIDs map[string]int64) error { - return rs.Cmd(nil, redis_HMSET, utils.LoadIDs, loadIDs) -} - -func (rs *RedisStoragev3) RemoveLoadIDsDrv() (err error) { - return rs.Cmd(nil, redis_DEL, utils.LoadIDs) -} - -func (rs *RedisStoragev3) GetRateProfileDrv(tenant, id string) (rpp *RateProfile, err error) { - key := utils.RateProfilePrefix + utils.ConcatenatedKey(tenant, id) - var values []byte - if values, err = rs.Cmd(redis_GET, key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } - return - } - if err = rs.ms.Unmarshal(values, &rpp); err != nil { - return - } - return -} - -func (rs *RedisStoragev3) SetRateProfileDrv(rpp *RateProfile) (err error) { - result, err := rs.ms.Marshal(rpp) - if err != nil { - return err - } - return rs.Cmd(nil, redis_SET, utils.RateProfilePrefix+utils.ConcatenatedKey(rpp.Tenant, rpp.ID), result) -} - -func (rs *RedisStoragev3) RemoveRateProfileDrv(tenant, id string) (err error) { - key := utils.RateProfilePrefix + utils.ConcatenatedKey(tenant, id) - if err = rs.Cmd(nil, redis_DEL, key); err != nil { - return - } - return -} - -// GetIndexesDrv retrieves Indexes from dataDB -func (rs *RedisStoragev3) GetIndexesDrv(idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) { - mp := make(map[string]string) - dbKey := utils.CacheInstanceToPrefix[idxItmType] + tntCtx - if len(idxKey) == 0 { - mp, err = rs.Cmd(redis_HGETALL, dbKey).Map() - if err != nil { - return - } else if len(mp) == 0 { - return nil, utils.ErrNotFound - } - } else { - var itmMpStrLst []string - itmMpStrLst, err = rs.Cmd(redis_HMGET, dbKey, idxKey).List() - if err != nil { - return - } else if itmMpStrLst[0] == utils.EmptyString { - return nil, utils.ErrNotFound - } - mp[idxKey] = itmMpStrLst[0] - } - indexes = make(map[string]utils.StringSet) - for k, v := range mp { - var sm utils.StringSet - if err = rs.ms.Unmarshal([]byte(v), &sm); err != nil { - return - } - indexes[k] = sm - } - return -} - -// SetIndexesDrv stores Indexes into DataDB -func (rs *RedisStoragev3) SetIndexesDrv(idxItmType, tntCtx string, - indexes map[string]utils.StringSet, commit bool, transactionID string) (err error) { - originKey := utils.CacheInstanceToPrefix[idxItmType] + tntCtx - dbKey := originKey - if transactionID != utils.EmptyString { - dbKey = "tmp_" + utils.ConcatenatedKey(dbKey, transactionID) - } - if commit && transactionID != utils.EmptyString { - return rs.Cmd(redis_RENAME, dbKey, originKey) - } - mp := make(map[string]string) - deleteArgs := []interface{}{dbKey} // the dbkey is necesary for the HDEL command - for key, strMp := range indexes { - if len(strMp) == 0 { // remove with no more elements inside - deleteArgs = append(deleteArgs, key) - continue - } - var encodedMp []byte - if encodedMp, err = rs.ms.Marshal(strMp); err != nil { - return - } - mp[key] = string(encodedMp) - } - if len(deleteArgs) != 1 { - if err = rs.Cmd(nil, redis_HDEL, deleteArgs...); err != nil { - return - } - } - if len(mp) != 0 { - return rs.Cmd(nil, redis_HMSET, dbKey, mp) - } - return -} - -func (rs *RedisStoragev3) RemoveIndexesDrv(idxItmType, tntCtx, idxKey string) (err error) { - if idxKey == utils.EmptyString { - return rs.Cmd(nil, redis_DEL, utils.CacheInstanceToPrefix[idxItmType]+tntCtx) - } - return rs.Cmd(nil, redis_HDEL, utils.CacheInstanceToPrefix[idxItmType]+tntCtx, idxKey) -} diff --git a/engine/storage_utils.go b/engine/storage_utils.go index 0ac2eb83b..1e1f14210 100644 --- a/engine/storage_utils.go +++ b/engine/storage_utils.go @@ -46,7 +46,7 @@ func NewDataDBConn(dbType, host, port, name, user, if port != "" && strings.Index(host, ":") == -1 { host += ":" + port } - d, err = NewRedisStorage(host, dbNo, pass, marshaler, utils.REDIS_MAX_CONNS, sentinelName) + d, err = NewRedisStorage(host, dbNo, user, pass, marshaler, utils.REDIS_MAX_CONNS, sentinelName) case utils.MONGO: d, err = NewMongoStorage(host, port, name, user, pass, marshaler, utils.DataDB, nil, true) case utils.INTERNAL: diff --git a/engine/z_datamanager_it_test.go b/engine/z_datamanager_it_test.go index 6e6af0de3..6181e1942 100644 --- a/engine/z_datamanager_it_test.go +++ b/engine/z_datamanager_it_test.go @@ -52,7 +52,7 @@ func TestDMitinitDB(t *testing.T) { case utils.MetaMySQL: dataDB, err = NewRedisStorage( fmt.Sprintf("%s:%s", cfg.DataDbCfg().DataDbHost, cfg.DataDbCfg().DataDbPort), - 4, cfg.DataDbCfg().DataDbPass, cfg.GeneralCfg().DBDataEncoding, + 4, cfg.DataDbCfg().DataDbUser, cfg.DataDbCfg().DataDbPass, cfg.GeneralCfg().DBDataEncoding, utils.REDIS_MAX_CONNS, "") if err != nil { t.Fatal("Could not connect to Redis", err.Error()) @@ -105,9 +105,9 @@ func testDMitCRUDStatQueue(t *testing.T) { Answered: 2, Count: 3, Events: map[string]*StatWithCompress{ - "cgrates.org:ev1": &StatWithCompress{Stat: 1}, - "cgrates.org:ev2": &StatWithCompress{Stat: 1}, - "cgrates.org:ev3": &StatWithCompress{Stat: 0}, + "cgrates.org:ev1": {Stat: 1}, + "cgrates.org:ev2": {Stat: 1}, + "cgrates.org:ev3": {Stat: 0}, }, }, }, diff --git a/engine/z_filterindexer_it_test.go b/engine/z_filterindexer_it_test.go index 016070c67..19757c6b6 100644 --- a/engine/z_filterindexer_it_test.go +++ b/engine/z_filterindexer_it_test.go @@ -79,7 +79,7 @@ func TestFilterIndexerIT(t *testing.T) { cfg, _ := config.NewDefaultCGRConfig() redisDB, err := NewRedisStorage( fmt.Sprintf("%s:%s", cfg.DataDbCfg().DataDbHost, cfg.DataDbCfg().DataDbPort), - 4, cfg.DataDbCfg().DataDbPass, cfg.GeneralCfg().DBDataEncoding, + 4, cfg.DataDbCfg().DataDbUser, cfg.DataDbCfg().DataDbPass, cfg.GeneralCfg().DBDataEncoding, utils.REDIS_MAX_CONNS, "") if err != nil { t.Fatal("Could not connect to Redis", err.Error()) diff --git a/engine/z_onstor_it_test.go b/engine/z_onstor_it_test.go index 7b2054aaf..5b969e223 100644 --- a/engine/z_onstor_it_test.go +++ b/engine/z_onstor_it_test.go @@ -93,7 +93,7 @@ func TestOnStorIT(t *testing.T) { cfg, _ := config.NewDefaultCGRConfig() rdsITdb, err = NewRedisStorage( fmt.Sprintf("%s:%s", cfg.DataDbCfg().DataDbHost, cfg.DataDbCfg().DataDbPort), - 4, cfg.DataDbCfg().DataDbPass, cfg.GeneralCfg().DBDataEncoding, + 4, cfg.DataDbCfg().DataDbUser, cfg.DataDbCfg().DataDbPass, cfg.GeneralCfg().DBDataEncoding, utils.REDIS_MAX_CONNS, "") if err != nil { t.Fatal("Could not connect to Redis", err.Error()) @@ -1377,7 +1377,8 @@ func testOnStorITCRUDHistory(t *testing.T) { } func testOnStorITCRUDStructVersion(t *testing.T) { - if _, err := onStor.DataDB().GetVersions(utils.Accounts); err != utils.ErrNotFound { + if vrst, err := onStor.DataDB().GetVersions(utils.Accounts); err != utils.ErrNotFound { + fmt.Println(vrst) t.Error(err) } vrs := Versions{ diff --git a/general_tests/sentinel_it_test.go b/general_tests/sentinel_it_test.go index a12f992ef..19608c803 100755 --- a/general_tests/sentinel_it_test.go +++ b/general_tests/sentinel_it_test.go @@ -156,7 +156,7 @@ func testRedisSentinelSetGetAttribute(t *testing.T) { var reply *engine.AttributeProfile if err := sentinelRPC.Call(utils.APIerSv1GetAttributeProfile, &utils.TenantID{Tenant: "cgrates.org", ID: "ApierTest"}, &reply); err != nil { - t.Error(err) + t.Fatal(err) } reply.Compile() if !reflect.DeepEqual(alsPrf, reply) { @@ -268,7 +268,7 @@ func testRedisSentinelGetAttrAfterFailover(t *testing.T) { var reply *engine.AttributeProfile if err := sentinelRPC.Call(utils.APIerSv1GetAttributeProfile, &utils.TenantID{Tenant: "cgrates.org", ID: "ApierTest"}, &reply); err != nil { - t.Error(err) + t.Fatal(err) } reply.Compile() if !reflect.DeepEqual(alsPrf, reply) { diff --git a/go.mod b/go.mod index 3ce043c43..8aafd9273 100644 --- a/go.mod +++ b/go.mod @@ -35,17 +35,20 @@ require ( github.com/ishidawataru/sctp v0.0.0-20191218070446-00ab2ac2db07 // indirect github.com/jinzhu/gorm v1.9.15 github.com/lib/pq v1.8.0 + github.com/mailru/easyjson v0.7.2 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect - github.com/mediocregopher/radix.v2 v0.0.0-20181115013041-b67df6e626f9 - github.com/mediocregopher/radix/v3 v3.5.2 // indirect + github.com/mediocregopher/radix/v3 v3.5.2 github.com/miekg/dns v1.1.30 github.com/mitchellh/mapstructure v1.3.3 github.com/nyaruka/phonenumbers v1.0.56 github.com/peterh/liner v1.2.0 + github.com/philhofer/fwd v1.0.0 // indirect + github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 // indirect github.com/robfig/cron/v3 v3.0.1 github.com/segmentio/kafka-go v0.3.7 github.com/streadway/amqp v1.0.0 - github.com/ugorji/go/codec v1.1.7 + github.com/tinylib/msgp v1.1.2 // indirect + github.com/ugorji/go v0.0.0-20171112025056-5a66da2e74af github.com/xdg/stringprep v1.0.1-0.20180714160509-73f8eece6fdc // indirect go.mongodb.org/mongo-driver v1.4.0 golang.org/x/net v0.0.0-20200707034311-ab3426394381 diff --git a/go.sum b/go.sum index f8306978a..bc94d41e2 100644 --- a/go.sum +++ b/go.sum @@ -194,6 +194,8 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc= github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4= @@ -213,6 +215,8 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.8.0 h1:9xohqzkUwzR4Ga4ivdTcawVS89YSDVxXMa3xJX3cGzg= github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mailru/easyjson v0.7.2 h1:V9ecaZWDYm7v9uJ15RZD6DajMu5sE0hdep0aoDwT9g4= +github.com/mailru/easyjson v0.7.2/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= @@ -220,8 +224,6 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= -github.com/mediocregopher/radix.v2 v0.0.0-20181115013041-b67df6e626f9 h1:ViNuGS149jgnttqhc6XQNPwdupEMBXqCx9wtlW7P3sA= -github.com/mediocregopher/radix.v2 v0.0.0-20181115013041-b67df6e626f9/go.mod h1:fLRUbhbSd5Px2yKUaGYYPltlyxi1guJz1vCmo1RQL50= github.com/mediocregopher/radix/v3 v3.5.2 h1:A9u3G7n4+fWmDZ2ZDHtlK+cZl4q55T+7RjKjR0/MAdk= github.com/mediocregopher/radix/v3 v3.5.2/go.mod h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i4n7wVopoX3x7Bv8= github.com/miekg/dns v1.1.30 h1:Qww6FseFn8PRfw07jueqIXqodm0JKiiKuK0DeXSqfyo= @@ -234,6 +236,8 @@ github.com/nyaruka/phonenumbers v1.0.56/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJr github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo= github.com/peterh/liner v1.2.0 h1:w/UPXyl5GfahFxcTOz2j9wCIHNI+pUPr2laqpojKNCg= github.com/peterh/liner v1.2.0/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0= +github.com/philhofer/fwd v1.0.0 h1:UbZqGr5Y38ApvM/V/jEljVxwocdweyH+vmYvRPBnbqQ= +github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -242,6 +246,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 h1:xoIK0ctDddBMnc74udxJYBqlo9Ylnsp1waqjLsnef20= +github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= @@ -267,10 +273,10 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= -github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= -github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= -github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= -github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/tinylib/msgp v1.1.2 h1:gWmO7n0Ys2RBEb7GPYB9Ujq8Mk5p2U08lRnmMcGy6BQ= +github.com/tinylib/msgp v1.1.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= +github.com/ugorji/go v0.0.0-20171112025056-5a66da2e74af h1:t75uFwPUcq1o8k4DCN3aDp6dNjSHLmp5mUc5E3PvD2k= +github.com/ugorji/go v0.0.0-20171112025056-5a66da2e74af/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I= github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y= diff --git a/migrator/action_plan.go b/migrator/action_plan.go index 9fb992f23..c7a644490 100644 --- a/migrator/action_plan.go +++ b/migrator/action_plan.go @@ -108,7 +108,7 @@ func (m *Migrator) migrateActionPlans() (err error) { if m.dmIN.DataManager().DataDB().GetStorageType() == utils.REDIS { // if redis rebuild action plans indexes redisDB, can := m.dmIN.DataManager().DataDB().(*engine.RedisStorage) if !can { - return fmt.Errorf("Storage type %s could not be cated to <*engine.RedisStorage>", m.dmIN.DataManager().DataDB().GetStorageType()) + return fmt.Errorf("Storage type %s could not be casted to <*engine.RedisStorage>", m.dmIN.DataManager().DataDB().GetStorageType()) } if err = redisDB.RebbuildActionPlanKeys(); err != nil { return diff --git a/migrator/storage_redis.go b/migrator/storage_redis.go index bff55bc58..f172637fa 100644 --- a/migrator/storage_redis.go +++ b/migrator/storage_redis.go @@ -23,7 +23,6 @@ import ( "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" - "github.com/mediocregopher/radix.v2/redis" ) type redisMigrator struct { @@ -66,8 +65,8 @@ func (v1rs *redisMigrator) getv1Account() (v1Acnt *v1Account, err error) { v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err := v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } v1Acnt = &v1Account{Id: v1rs.dataKeys[*v1rs.qryIdx]} @@ -89,7 +88,7 @@ func (v1rs *redisMigrator) setV1Account(x *v1Account) (err error) { if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -98,7 +97,7 @@ func (v1rs *redisMigrator) setV1Account(x *v1Account) (err error) { //rem func (v1rs *redisMigrator) remV1Account(id string) (err error) { key := v1AccountDBPrefix + id - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } //V2 @@ -114,8 +113,8 @@ func (v1rs *redisMigrator) getv2Account() (v2Acnt *v2Account, err error) { v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err := v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } v2Acnt = &v2Account{ID: v1rs.dataKeys[*v1rs.qryIdx]} @@ -137,7 +136,7 @@ func (v1rs *redisMigrator) setV2Account(x *v2Account) (err error) { if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -146,7 +145,7 @@ func (v1rs *redisMigrator) setV2Account(x *v2Account) (err error) { //rem func (v1rs *redisMigrator) remV2Account(id string) (err error) { key := utils.ACCOUNT_PREFIX + id - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } //ActionPlans methods @@ -162,8 +161,8 @@ func (v1rs *redisMigrator) getV1ActionPlans() (v1aps *v1ActionPlans, err error) v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err := v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1aps); err != nil { @@ -184,7 +183,7 @@ func (v1rs *redisMigrator) setV1ActionPlans(x *v1ActionPlans) (err error) { if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -193,7 +192,7 @@ func (v1rs *redisMigrator) setV1ActionPlans(x *v1ActionPlans) (err error) { //rem func (v1rs *redisMigrator) remV1ActionPlans(x *v1ActionPlans) (err error) { key := utils.ACTION_PLAN_PREFIX + (*x)[0].Id - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } //Actions methods @@ -209,8 +208,8 @@ func (v1rs *redisMigrator) getV1Actions() (v1acs *v1Actions, err error) { v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1acs); err != nil { @@ -231,7 +230,7 @@ func (v1rs *redisMigrator) setV1Actions(x *v1Actions) (err error) { if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -240,7 +239,7 @@ func (v1rs *redisMigrator) setV1Actions(x *v1Actions) (err error) { //rem func (v1rs *redisMigrator) remV1Actions(x v1Actions) (err error) { key := utils.ACTION_PREFIX + x[0].Id - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } @@ -257,8 +256,8 @@ func (v1rs *redisMigrator) getV1ActionTriggers() (v1acts *v1ActionTriggers, err v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1acts); err != nil { @@ -279,7 +278,7 @@ func (v1rs *redisMigrator) setV1ActionTriggers(x *v1ActionTriggers) (err error) if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -288,7 +287,7 @@ func (v1rs *redisMigrator) setV1ActionTriggers(x *v1ActionTriggers) (err error) //rem func (v1rs *redisMigrator) remV1ActionTriggers(x *v1ActionTriggers) (err error) { key := utils.ACTION_TRIGGER_PREFIX + (*x)[0].Id - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } //SharedGroup methods @@ -304,8 +303,8 @@ func (v1rs *redisMigrator) getV1SharedGroup() (v1sg *v1SharedGroup, err error) { v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1sg); err != nil { @@ -326,7 +325,7 @@ func (v1rs *redisMigrator) setV1SharedGroup(x *v1SharedGroup) (err error) { if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -345,8 +344,8 @@ func (v1rs *redisMigrator) getV1Stats() (v1st *v1Stat, err error) { v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1st); err != nil { @@ -371,8 +370,8 @@ func (v1rs *redisMigrator) getV3Stats() (v1st *engine.StatQueueProfile, err erro v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1st); err != nil { @@ -393,7 +392,7 @@ func (v1rs *redisMigrator) setV1Stats(x *v1Stat) (err error) { if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -411,8 +410,8 @@ func (v1rs *redisMigrator) getV2Stats() (v2 *engine.StatQueue, err error) { v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v2); err != nil { @@ -433,7 +432,7 @@ func (v1rs *redisMigrator) setV2Stats(v2 *engine.StatQueue) (err error) { if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -452,8 +451,8 @@ func (v1rs *redisMigrator) getV2ActionTrigger() (v2at *v2ActionTrigger, err erro v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v2at); err != nil { @@ -474,7 +473,7 @@ func (v1rs *redisMigrator) setV2ActionTrigger(x *v2ActionTrigger) (err error) { if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -494,8 +493,8 @@ func (v1rs *redisMigrator) getV1AttributeProfile() (v1attrPrf *v1AttributeProfil v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1attr); err != nil { @@ -516,7 +515,7 @@ func (v1rs *redisMigrator) setV1AttributeProfile(x *v1AttributeProfile) (err err if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -536,8 +535,8 @@ func (v1rs *redisMigrator) getV2ThresholdProfile() (v2T *v2Threshold, err error) v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v2Th); err != nil { @@ -562,8 +561,8 @@ func (v1rs *redisMigrator) getV3ThresholdProfile() (v2T *engine.ThresholdProfile v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v2T); err != nil { @@ -584,7 +583,7 @@ func (v1rs *redisMigrator) setV2ThresholdProfile(x *v2Threshold) (err error) { if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -593,7 +592,7 @@ func (v1rs *redisMigrator) setV2ThresholdProfile(x *v2Threshold) (err error) { //rem func (v1rs *redisMigrator) remV2ThresholdProfile(tenant, id string) (err error) { key := utils.ThresholdProfilePrefix + utils.ConcatenatedKey(tenant, id) - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } //ThresholdProfile methods @@ -611,8 +610,8 @@ func (v1rs *redisMigrator) getV1Alias() (v1a *v1Alias, err error) { } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { key := v1rs.dataKeys[*v1rs.qryIdx] - strVal, err := v1rs.rds.Cmd("GET", key).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", key); err != nil { return nil, err } v1a.SetId(strings.TrimPrefix(key, AliasesPrefix)) @@ -635,7 +634,7 @@ func (v1rs *redisMigrator) setV1Alias(al *v1Alias) (err error) { return } key := AliasesPrefix + al.GetId() - if err = v1rs.rds.Cmd("SET", key, result).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(result)); err != nil { return } return @@ -647,11 +646,11 @@ func (v1rs *redisMigrator) remV1Alias(key string) (err error) { // get alias for values list var values []byte - if values, err = v1rs.rds.Cmd("GET", - AliasesPrefix+key).Bytes(); err != nil { - if err == redis.ErrRespNil { // did not find the destination - err = utils.ErrNotFound - } + if err = v1rs.rds.Cmd(&values, "GET", + AliasesPrefix+key); err != nil { + return + } else if len(values) == 0 { + err = utils.ErrNotFound return } al := &v1Alias{Values: make(v1AliasValues, 0)} @@ -660,7 +659,7 @@ func (v1rs *redisMigrator) remV1Alias(key string) (err error) { return err } - err = v1rs.rds.Cmd("DEL", AliasesPrefix+key).Err + err = v1rs.rds.Cmd(nil, "DEL", AliasesPrefix+key) if err != nil { return err } @@ -669,7 +668,7 @@ func (v1rs *redisMigrator) remV1Alias(key string) (err error) { for target, pairs := range value.Pairs { for _, alias := range pairs { revID := alias + target + al.Context - err = v1rs.rds.Cmd("SREM", reverseAliasesPrefix+revID, tmpKey).Err + err = v1rs.rds.Cmd(nil, "SREM", reverseAliasesPrefix+revID, tmpKey) if err != nil { return err } @@ -678,7 +677,7 @@ func (v1rs *redisMigrator) remV1Alias(key string) (err error) { } return - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } // User methods @@ -694,8 +693,8 @@ func (v1rs *redisMigrator) getV1User() (v1u *v1UserProfile, err error) { v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } v1u = new(v1UserProfile) @@ -715,12 +714,12 @@ func (v1rs *redisMigrator) setV1User(us *v1UserProfile) (err error) { if err != nil { return err } - return v1rs.rds.Cmd("SET", utils.USERS_PREFIX+us.GetId(), bit).Err + return v1rs.rds.Cmd(nil, "SET", utils.USERS_PREFIX+us.GetId(), string(bit)) } //rem func (v1rs *redisMigrator) remV1User(key string) (err error) { - return v1rs.rds.Cmd("DEL", utils.USERS_PREFIX+key).Err + return v1rs.rds.Cmd(nil, "DEL", utils.USERS_PREFIX+key) } // DerivedChargers methods @@ -736,8 +735,8 @@ func (v1rs *redisMigrator) getV1DerivedChargers() (v1d *v1DerivedChargersWithKey v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } v1d = new(v1DerivedChargersWithKey) @@ -762,12 +761,12 @@ func (v1rs *redisMigrator) setV1DerivedChargers(dc *v1DerivedChargersWithKey) (e if err != nil { return err } - return v1rs.rds.Cmd("SET", utils.DERIVEDCHARGERS_PREFIX+dc.Key, bit).Err + return v1rs.rds.Cmd(nil, "SET", utils.DERIVEDCHARGERS_PREFIX+dc.Key, string(bit)) } //rem func (v1rs *redisMigrator) remV1DerivedChargers(key string) (err error) { - return v1rs.rds.Cmd("DEL", utils.DERIVEDCHARGERS_PREFIX+key).Err + return v1rs.rds.Cmd(nil, "DEL", utils.DERIVEDCHARGERS_PREFIX+key) } //AttributeProfile methods @@ -784,8 +783,8 @@ func (v1rs *redisMigrator) getV2AttributeProfile() (v2attrPrf *v2AttributeProfil v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v2attr); err != nil { @@ -806,7 +805,7 @@ func (v1rs *redisMigrator) setV2AttributeProfile(x *v2AttributeProfile) (err err if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -815,7 +814,7 @@ func (v1rs *redisMigrator) setV2AttributeProfile(x *v2AttributeProfile) (err err //rem func (v1rs *redisMigrator) remV2AttributeProfile(tenant, id string) (err error) { key := utils.AttributeProfilePrefix + utils.ConcatenatedKey(tenant, id) - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } //AttributeProfile methods @@ -832,8 +831,8 @@ func (v1rs *redisMigrator) getV3AttributeProfile() (v3attrPrf *v3AttributeProfil v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v3attr); err != nil { @@ -854,7 +853,7 @@ func (v1rs *redisMigrator) setV3AttributeProfile(x *v3AttributeProfile) (err err if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -863,7 +862,7 @@ func (v1rs *redisMigrator) setV3AttributeProfile(x *v3AttributeProfile) (err err //rem func (v1rs *redisMigrator) remV3AttributeProfile(tenant, id string) (err error) { key := utils.AttributeProfilePrefix + utils.ConcatenatedKey(tenant, id) - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } //AttributeProfile methods @@ -880,8 +879,8 @@ func (v1rs *redisMigrator) getV4AttributeProfile() (v3attrPrf *v4AttributeProfil v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v4attr); err != nil { @@ -906,8 +905,8 @@ func (v1rs *redisMigrator) getV5AttributeProfile() (v5attr *engine.AttributeProf v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v5attr); err != nil { @@ -928,7 +927,7 @@ func (v1rs *redisMigrator) setV4AttributeProfile(x *v4AttributeProfile) (err err if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -937,7 +936,7 @@ func (v1rs *redisMigrator) setV4AttributeProfile(x *v4AttributeProfile) (err err //rem func (v1rs *redisMigrator) remV4AttributeProfile(tenant, id string) (err error) { key := utils.AttributeProfilePrefix + utils.ConcatenatedKey(tenant, id) - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } // Filter Methods @@ -953,8 +952,8 @@ func (v1rs *redisMigrator) getV1Filter() (v1Fltr *v1Filter, err error) { v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1Fltr); err != nil { @@ -979,8 +978,8 @@ func (v1rs *redisMigrator) getV4Filter() (v4Fltr *engine.Filter, err error) { v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v4Fltr); err != nil { @@ -1001,13 +1000,13 @@ func (v1rs *redisMigrator) setV1Filter(x *v1Filter) (err error) { if err != nil { return err } - return v1rs.rds.Cmd("SET", key, bit).Err + return v1rs.rds.Cmd(nil, "SET", key, string(bit)) } //rem func (v1rs *redisMigrator) remV1Filter(tenant, id string) (err error) { key := utils.FilterPrefix + utils.ConcatenatedKey(tenant, id) - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } // SupplierMethods @@ -1022,8 +1021,8 @@ func (v1rs *redisMigrator) getSupplier() (spl *SupplierProfile, err error) { v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &spl); err != nil { @@ -1044,7 +1043,7 @@ func (v1rs *redisMigrator) setSupplier(spl *SupplierProfile) (err error) { if err != nil { return err } - if err = v1rs.rds.Cmd("SET", key, bit).Err; err != nil { + if err = v1rs.rds.Cmd(nil, "SET", key, string(bit)); err != nil { return err } return @@ -1053,7 +1052,7 @@ func (v1rs *redisMigrator) setSupplier(spl *SupplierProfile) (err error) { //rem func (v1rs *redisMigrator) remSupplier(tenant, id string) (err error) { key := SupplierProfilePrefix + utils.ConcatenatedKey(tenant, id) - return v1rs.rds.Cmd("DEL", key).Err + return v1rs.rds.Cmd(nil, "DEL", key) } func (v1rs *redisMigrator) getV1ChargerProfile() (v1chrPrf *engine.ChargerProfile, err error) { @@ -1067,8 +1066,8 @@ func (v1rs *redisMigrator) getV1ChargerProfile() (v1chrPrf *engine.ChargerProfil v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1chrPrf); err != nil { @@ -1093,8 +1092,8 @@ func (v1rs *redisMigrator) getV1DispatcherProfile() (v1chrPrf *engine.Dispatcher v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1chrPrf); err != nil { @@ -1119,8 +1118,8 @@ func (v1rs *redisMigrator) getV1RouteProfile() (v1chrPrf *engine.RouteProfile, e v1rs.qryIdx = utils.IntPointer(0) } if *v1rs.qryIdx <= len(v1rs.dataKeys)-1 { - strVal, err := v1rs.rds.Cmd("GET", v1rs.dataKeys[*v1rs.qryIdx]).Bytes() - if err != nil { + var strVal []byte + if err = v1rs.rds.Cmd(&strVal, "GET", v1rs.dataKeys[*v1rs.qryIdx]); err != nil { return nil, err } if err := v1rs.rds.Marshaler().Unmarshal(strVal, &v1chrPrf); err != nil {