diff --git a/engine/datamanager.go b/engine/datamanager.go index b12f8b6bd..f27bce55d 100644 --- a/engine/datamanager.go +++ b/engine/datamanager.go @@ -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) } diff --git a/engine/onstor_it_test.go b/engine/onstor_it_test.go index c0f7aee32..2268428af 100644 --- a/engine/onstor_it_test.go +++ b/engine/onstor_it_test.go @@ -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) } } diff --git a/engine/reqfilterhelpers.go b/engine/reqfilterhelpers.go index a18444a4c..5e7458963 100644 --- a/engine/reqfilterhelpers.go +++ b/engine/reqfilterhelpers.go @@ -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 diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 81e4091a5..cf48be095 100755 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -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) diff --git a/engine/storage_map.go b/engine/storage_map.go index 30a0c3247..d6bda37be 100755 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -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 } diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index cd37b51d1..794680957 100755 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -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 } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index c3df8c36d..cab12a4a5 100755 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -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 }