mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add MatchReqFilterIndex in data manager
This commit is contained in:
committed by
Dan Christian Bogos
parent
cec4a4338e
commit
55528c7aff
@@ -809,6 +809,27 @@ func (dm *DataManager) SetReqFilterIndexes(dbKey string, indexes map[string]map[
|
||||
return dm.DataDB().SetReqFilterIndexesDrv(dbKey, indexes)
|
||||
}
|
||||
|
||||
//complete here with cache
|
||||
func (dm *DataManager) MatchReqFilterIndex(dbKey, fieldName, fieldVal string) (itemIDs utils.StringMap, err error) {
|
||||
fieldValKey := utils.ConcatenatedKey(fieldName, fieldVal)
|
||||
cacheKey := dbKey + fieldValKey
|
||||
if x, ok := cache.Get(cacheKey); ok { // Attempt to find in cache first
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return x.(utils.StringMap), nil
|
||||
}
|
||||
itemIDs, err = dm.DataDB().MatchReqFilterIndexDrv(dbKey, fieldName, fieldVal)
|
||||
if err != nil {
|
||||
if err == utils.ErrNotFound {
|
||||
cache.Set(cacheKey, nil, true, utils.NonTransactional)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
cache.Set(cacheKey, itemIDs, true, utils.NonTransactional)
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *DataManager) GetCdrStatsQueue(key string) (sq *CDRStatsQueue, err error) {
|
||||
return dm.DataDB().GetCdrStatsQueueDrv(key)
|
||||
}
|
||||
|
||||
@@ -265,12 +265,12 @@ func testOnStorITMatchReqFilterIndex(t *testing.T) {
|
||||
"RL1": true,
|
||||
"RL2": true,
|
||||
}
|
||||
if rcvMp, err := onStor.DataDB().MatchReqFilterIndex(utils.ResourceProfilesStringIndex, "Account", "1002"); err != nil {
|
||||
if rcvMp, err := onStor.MatchReqFilterIndex(utils.ResourceProfilesStringIndex, "Account", "1002"); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eMp, rcvMp) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eMp, rcvMp)
|
||||
}
|
||||
if _, err := onStor.DataDB().MatchReqFilterIndex(utils.ResourceProfilesStringIndex, "NonexistentField", "1002"); err == nil || err != utils.ErrNotFound {
|
||||
if _, err := onStor.MatchReqFilterIndex(utils.ResourceProfilesStringIndex, "NonexistentField", "1002"); err == nil || err != utils.ErrNotFound {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func matchingItemIDsForEvent(ev map[string]interface{}, fieldIDs []string,
|
||||
if !canCast {
|
||||
return nil, fmt.Errorf("Cannot cast field: %s into string", fldName)
|
||||
}
|
||||
dbItemIDs, err := dm.DataDB().MatchReqFilterIndex(dbIdxKey, fldName, fldVal)
|
||||
dbItemIDs, err := dm.MatchReqFilterIndex(dbIdxKey, fldName, fldVal)
|
||||
if err != nil {
|
||||
if err == utils.ErrNotFound {
|
||||
continue
|
||||
@@ -61,7 +61,7 @@ func matchingItemIDsForEvent(ev map[string]interface{}, fieldIDs []string,
|
||||
}
|
||||
}
|
||||
}
|
||||
dbItemIDs, err := dm.DataDB().MatchReqFilterIndex(dbIdxKey, utils.NOT_AVAILABLE, utils.NOT_AVAILABLE) // add unindexed itemIDs to be checked
|
||||
dbItemIDs, err := dm.MatchReqFilterIndex(dbIdxKey, utils.NOT_AVAILABLE, utils.NOT_AVAILABLE) // add unindexed itemIDs to be checked
|
||||
if err != nil {
|
||||
if err != utils.ErrNotFound {
|
||||
return nil, err
|
||||
|
||||
@@ -112,7 +112,7 @@ type DataDB interface {
|
||||
AddLoadHistory(*utils.LoadInstance, int, string) error
|
||||
GetReqFilterIndexesDrv(dbKey string) (indexes map[string]map[string]utils.StringMap, err error)
|
||||
SetReqFilterIndexesDrv(dbKey string, indexes map[string]map[string]utils.StringMap) (err error)
|
||||
MatchReqFilterIndex(dbKey, fieldName, fieldVal string) (itemIDs utils.StringMap, err error)
|
||||
MatchReqFilterIndexDrv(dbKey, fieldName, fieldVal string) (itemIDs utils.StringMap, err error)
|
||||
GetStatQueueProfileDrv(tenant string, ID string) (sq *StatQueueProfile, err error)
|
||||
SetStatQueueProfileDrv(sq *StatQueueProfile) (err error)
|
||||
RemStatQueueProfileDrv(tenant, id string) (err error)
|
||||
|
||||
@@ -1173,20 +1173,12 @@ func (ms *MapStorage) SetReqFilterIndexesDrv(dbKey string, indexes map[string]ma
|
||||
ms.dict[dbKey] = result
|
||||
return
|
||||
}
|
||||
func (ms *MapStorage) MatchReqFilterIndex(dbKey, fldName, fldVal string) (itemIDs utils.StringMap, err error) {
|
||||
cacheKey := dbKey + utils.ConcatenatedKey(fldName, fldVal)
|
||||
func (ms *MapStorage) MatchReqFilterIndexDrv(dbKey, fldName, fldVal string) (itemIDs utils.StringMap, err error) {
|
||||
ms.mu.RLock()
|
||||
defer ms.mu.RUnlock()
|
||||
if x, ok := cache.Get(cacheKey); ok { // Attempt to find in cache first
|
||||
if x != nil {
|
||||
return x.(utils.StringMap), nil
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
// Not found in cache, check in DB
|
||||
values, ok := ms.dict[dbKey]
|
||||
if !ok {
|
||||
cache.Set(cacheKey, nil, true, utils.NonTransactional)
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
var indexes map[string]map[string]utils.StringMap
|
||||
@@ -1196,12 +1188,6 @@ func (ms *MapStorage) MatchReqFilterIndex(dbKey, fldName, fldVal string) (itemID
|
||||
if _, hasIt := indexes[fldName]; hasIt {
|
||||
itemIDs = indexes[fldName][fldVal]
|
||||
}
|
||||
//Verify items
|
||||
if len(itemIDs) == 0 {
|
||||
cache.Set(cacheKey, nil, true, utils.NonTransactional)
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
cache.Set(cacheKey, itemIDs, true, utils.NonTransactional)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1800,15 +1800,7 @@ func (ms *MongoStorage) SetReqFilterIndexesDrv(dbKey string, indexes map[string]
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) MatchReqFilterIndex(dbKey, fldName, fldVal string) (itemIDs utils.StringMap, err error) {
|
||||
fieldValKey := utils.ConcatenatedKey(fldName, fldVal)
|
||||
cacheKey := dbKey + fieldValKey
|
||||
if x, ok := cache.Get(cacheKey); ok { // Attempt to find in cache first
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return x.(utils.StringMap), nil
|
||||
}
|
||||
func (ms *MongoStorage) MatchReqFilterIndexDrv(dbKey, fldName, fldVal string) (itemIDs utils.StringMap, err error) {
|
||||
session, col := ms.conn(colRFI)
|
||||
defer session.Close()
|
||||
var result struct {
|
||||
@@ -1821,12 +1813,10 @@ func (ms *MongoStorage) MatchReqFilterIndex(dbKey, fldName, fldVal string) (item
|
||||
bson.M{fldKey: true}).One(&result); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
err = utils.ErrNotFound
|
||||
cache.Set(cacheKey, nil, true, utils.NonTransactional)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
itemIDs = result.Value[fldName][fldVal]
|
||||
cache.Set(cacheKey, itemIDs, true, utils.NonTransactional)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1295,27 +1295,18 @@ func (rs *RedisStorage) SetReqFilterIndexesDrv(dbKey string, indexes map[string]
|
||||
return rs.Cmd("HMSET", dbKey, mp).Err
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) MatchReqFilterIndex(dbKey, fldName, fldVal string) (itemIDs utils.StringMap, err error) {
|
||||
func (rs *RedisStorage) MatchReqFilterIndexDrv(dbKey, fldName, fldVal string) (itemIDs utils.StringMap, err error) {
|
||||
fieldValKey := utils.ConcatenatedKey(fldName, fldVal)
|
||||
cacheKey := dbKey + fieldValKey
|
||||
if x, ok := cache.Get(cacheKey); ok { // Attempt to find in cache first
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return x.(utils.StringMap), nil
|
||||
}
|
||||
// Not found in cache, check in DB
|
||||
fldValBytes, err := rs.Cmd("HGET", dbKey, fieldValKey).Bytes()
|
||||
if err != nil {
|
||||
if err == redis.ErrRespNil { // did not find the destination
|
||||
cache.Set(cacheKey, nil, true, utils.NonTransactional)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
} else if err = rs.ms.Unmarshal(fldValBytes, &itemIDs); err != nil {
|
||||
return
|
||||
}
|
||||
cache.Set(cacheKey, itemIDs, true, utils.NonTransactional)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user