mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-24 00:28:44 +05:00
fix GetAccounts with mongo
This commit is contained in:
@@ -33,7 +33,7 @@ import (
|
||||
type Storage interface {
|
||||
Close()
|
||||
Flush(string) error
|
||||
GetKeysForPrefix(string) ([]string, error)
|
||||
GetKeysForPrefix(string, bool) ([]string, error)
|
||||
}
|
||||
|
||||
// Interface for storage providers.
|
||||
|
||||
@@ -53,14 +53,17 @@ func (ms *MapStorage) Flush(ignore string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *MapStorage) GetKeysForPrefix(prefix string) ([]string, error) {
|
||||
keysForPrefix := make([]string, 0)
|
||||
for key := range ms.dict {
|
||||
if strings.HasPrefix(key, prefix) {
|
||||
keysForPrefix = append(keysForPrefix, key)
|
||||
func (ms *MapStorage) GetKeysForPrefix(prefix string, skipCache bool) ([]string, error) {
|
||||
if skipCache {
|
||||
keysForPrefix := make([]string, 0)
|
||||
for key := range ms.dict {
|
||||
if strings.HasPrefix(key, prefix) {
|
||||
keysForPrefix = append(keysForPrefix, key)
|
||||
}
|
||||
}
|
||||
return keysForPrefix, nil
|
||||
}
|
||||
return keysForPrefix, nil
|
||||
return cache2go.GetEntriesKeys(prefix), nil
|
||||
}
|
||||
|
||||
func (ms *MapStorage) CacheRatingAll() error {
|
||||
|
||||
@@ -244,8 +244,60 @@ func (ms *MongoStorage) Close() {
|
||||
ms.session.Close()
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetKeysForPrefix(prefix string) ([]string, error) {
|
||||
return nil, nil
|
||||
func (ms *MongoStorage) GetKeysForPrefix(prefix string, skipCache bool) ([]string, error) {
|
||||
var category, subject string
|
||||
length := len(utils.DESTINATION_PREFIX)
|
||||
if len(prefix) >= length {
|
||||
category = prefix[:length] // prefix lenght
|
||||
subject = fmt.Sprintf("^%s", prefix[length:])
|
||||
} else {
|
||||
return nil, fmt.Errorf("unsupported prefix in GetKeysForPrefix: %s", prefix)
|
||||
}
|
||||
var result []string
|
||||
if skipCache {
|
||||
keyResult := struct{ Key string }{}
|
||||
idResult := struct{ Id string }{}
|
||||
switch category {
|
||||
case utils.DESTINATION_PREFIX:
|
||||
iter := ms.db.C(colDst).Find(bson.M{"key": bson.M{"$regex": bson.RegEx{Pattern: subject}}}).Select(bson.M{"key": 1}).Iter()
|
||||
for iter.Next(&keyResult) {
|
||||
result = append(result, utils.DESTINATION_PREFIX+keyResult.Key)
|
||||
}
|
||||
return result, nil
|
||||
case utils.RATING_PLAN_PREFIX:
|
||||
iter := ms.db.C(colRpl).Find(bson.M{"key": bson.M{"$regex": bson.RegEx{Pattern: subject}}}).Select(bson.M{"key": 1}).Iter()
|
||||
for iter.Next(&keyResult) {
|
||||
result = append(result, utils.RATING_PLAN_PREFIX+keyResult.Key)
|
||||
}
|
||||
return result, nil
|
||||
case utils.RATING_PROFILE_PREFIX:
|
||||
iter := ms.db.C(colRpf).Find(bson.M{"id": bson.M{"$regex": bson.RegEx{Pattern: subject}}}).Select(bson.M{"id": 1}).Iter()
|
||||
for iter.Next(&idResult) {
|
||||
result = append(result, utils.RATING_PROFILE_PREFIX+idResult.Id)
|
||||
}
|
||||
return result, nil
|
||||
case utils.ACTION_PREFIX:
|
||||
iter := ms.db.C(colAct).Find(bson.M{"key": bson.M{"$regex": bson.RegEx{Pattern: subject}}}).Select(bson.M{"key": 1}).Iter()
|
||||
for iter.Next(&keyResult) {
|
||||
result = append(result, utils.ACTION_PREFIX+keyResult.Key)
|
||||
}
|
||||
return result, nil
|
||||
case utils.ACTION_PLAN_PREFIX:
|
||||
iter := ms.db.C(colApl).Find(bson.M{"key": bson.M{"$regex": bson.RegEx{Pattern: subject}}}).Select(bson.M{"key": 1}).Iter()
|
||||
for iter.Next(&keyResult) {
|
||||
result = append(result, utils.ACTION_PLAN_PREFIX+keyResult.Key)
|
||||
}
|
||||
return result, nil
|
||||
case utils.ACCOUNT_PREFIX:
|
||||
iter := ms.db.C(colAcc).Find(bson.M{"id": bson.M{"$regex": bson.RegEx{Pattern: subject}}}).Select(bson.M{"id": 1}).Iter()
|
||||
for iter.Next(&idResult) {
|
||||
result = append(result, utils.ACCOUNT_PREFIX+idResult.Id)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
return result, fmt.Errorf("unsupported prefix in GetKeysForPrefix: %s", prefix)
|
||||
}
|
||||
return cache2go.GetEntriesKeys(prefix), nil
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) Flush(ignore string) (err error) {
|
||||
@@ -627,7 +679,7 @@ func (ms *MongoStorage) HasData(category, subject string) (bool, error) {
|
||||
count, err := ms.db.C(colAcc).Find(bson.M{"id": subject}).Count()
|
||||
return count > 0, err
|
||||
}
|
||||
return false, errors.New("Unsupported category in HasData")
|
||||
return false, errors.New("unsupported category in HasData")
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetRatingPlan(key string, skipCache bool) (rp *RatingPlan, err error) {
|
||||
|
||||
@@ -84,12 +84,15 @@ func (rs *RedisStorage) Flush(ignore string) error {
|
||||
return rs.db.Cmd("FLUSHDB").Err
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetKeysForPrefix(prefix string) ([]string, error) {
|
||||
r := rs.db.Cmd("KEYS", prefix+"*")
|
||||
if r.Err != nil {
|
||||
return nil, r.Err
|
||||
func (rs *RedisStorage) GetKeysForPrefix(prefix string, skipCache bool) ([]string, error) {
|
||||
if skipCache {
|
||||
r := rs.db.Cmd("KEYS", prefix+"*")
|
||||
if r.Err != nil {
|
||||
return nil, r.Err
|
||||
}
|
||||
return r.List()
|
||||
}
|
||||
return r.List()
|
||||
return cache2go.GetEntriesKeys(prefix), nil
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) CacheRatingAll() error {
|
||||
|
||||
@@ -55,7 +55,7 @@ func (self *SQLStorage) Flush(scriptsPath string) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) GetKeysForPrefix(prefix string) ([]string, error) {
|
||||
func (self *SQLStorage) GetKeysForPrefix(prefix string, skipCache bool) ([]string, error) {
|
||||
return nil, utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
|
||||
@@ -433,7 +433,7 @@ func (tpr *TpReader) LoadLCRs() (err error) {
|
||||
}
|
||||
}
|
||||
if !found && tpr.ratingStorage != nil {
|
||||
if keys, err := tpr.ratingStorage.GetKeysForPrefix(utils.RATING_PROFILE_PREFIX + ratingProfileSearchKey); err != nil {
|
||||
if keys, err := tpr.ratingStorage.GetKeysForPrefix(utils.RATING_PROFILE_PREFIX+ratingProfileSearchKey, true); err != nil {
|
||||
return fmt.Errorf("[LCR] error querying ratingDb %s", err.Error())
|
||||
} else if len(keys) != 0 {
|
||||
found = true
|
||||
|
||||
Reference in New Issue
Block a user