diff --git a/engine/datamanager.go b/engine/datamanager.go index 3e07762b0..5702714c0 100644 --- a/engine/datamanager.go +++ b/engine/datamanager.go @@ -3760,10 +3760,7 @@ func (dm *DataManager) Reconnect(marshaller string, newcfg *config.DataDbCfg, it return } -// GetIndexesBatch supports retrieving multiple index keys in a single operation -// When idxKeys is empty or contains only empty string, retrieves all indexes -// Otherwise retrieves only the specified keys, returning only found keys (no error if some missing) -// Returns ErrNotFound only if no keys found at all +// GetIndexes retrieves the specified index keys. func (dm *DataManager) GetIndexes(idxItmType, tntCtx string, cacheRead, cacheWrite bool, idxKeys ...string) (indexes map[string]utils.StringSet, err error) { if dm == nil { @@ -3771,15 +3768,29 @@ func (dm *DataManager) GetIndexes(idxItmType, tntCtx string, cacheRead, cacheWri return } - // Handle backward compatibility: single key cache check - if cacheRead && len(idxKeys) == 1 && idxKeys[0] != utils.EmptyString { - if x, ok := Cache.Get(idxItmType, utils.ConcatenatedKey(tntCtx, idxKeys[0])); ok { - if x == nil { + // All-or-nothing cache strategy for multiple keys. + if cacheRead && len(idxKeys) > 0 { + allKeysInCache := true + cachedIndexes := make(map[string]utils.StringSet) + + for _, idxKey := range idxKeys { + if x, ok := Cache.Get(idxItmType, utils.ConcatenatedKey(tntCtx, idxKey)); ok { + if x == nil { + continue // cached as non-existent, skip it + } + cachedIndexes[idxKey] = x.(utils.StringSet) + } else { + // Key missing from cache, fetch all from database. + allKeysInCache = false + break + } + } + + if allKeysInCache { + if len(cachedIndexes) == 0 { return nil, utils.ErrNotFound } - return map[string]utils.StringSet{ - idxKeys[0]: x.(utils.StringSet), - }, nil + return cachedIndexes, nil } } diff --git a/engine/storage_internal_datadb.go b/engine/storage_internal_datadb.go index e5a569d5b..0ae4d8d52 100644 --- a/engine/storage_internal_datadb.go +++ b/engine/storage_internal_datadb.go @@ -912,7 +912,7 @@ func (iDB *InternalDB) RemoveLoadIDsDrv() (err error) { } func (iDB *InternalDB) GetIndexesDrv(idxItmType, tntCtx string, idxKeys ...string) (indexes map[string]utils.StringSet, err error) { - if len(idxKeys) == 0 || (len(idxKeys) == 1 && idxKeys[0] == utils.EmptyString) { // return all + if len(idxKeys) == 0 { // return all indexes = make(map[string]utils.StringSet) for _, dbKey := range iDB.db.GetGroupItemIDs(idxItmType, tntCtx) { x, ok := iDB.db.Get(idxItmType, dbKey) @@ -977,7 +977,7 @@ func (iDB *InternalDB) SetIndexesDrv(idxItmType, tntCtx string, } func (iDB *InternalDB) RemoveIndexesDrv(idxItmType, tntCtx string, idxKeys ...string) (err error) { - if len(idxKeys) == 0 || (len(idxKeys) == 1 && idxKeys[0] == utils.EmptyString) { // remove all + if len(idxKeys) == 0 { // remove all iDB.db.RemoveGroup(idxItmType, tntCtx, true, utils.EmptyString) return } diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 108004be1..d331ce8fb 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -2071,7 +2071,7 @@ func (ms *MongoStorage) GetIndexesDrv(idxItmType, tntCtx string, idxKeys ...stri } dbKey := utils.CacheInstanceToPrefix[idxItmType] + tntCtx var q bson.M - if len(idxKeys) == 0 || (len(idxKeys) == 1 && idxKeys[0] == utils.EmptyString) { + if len(idxKeys) == 0 { for _, character := range []string{".", "*"} { dbKey = strings.Replace(dbKey, character, `\`+character, strings.Count(dbKey, character)) } @@ -2178,7 +2178,7 @@ func (ms *MongoStorage) SetIndexesDrv(idxItmType, tntCtx string, // RemoveIndexesDrv removes the indexes func (ms *MongoStorage) RemoveIndexesDrv(idxItmType, tntCtx string, idxKeys ...string) error { - if len(idxKeys) == 0 || (len(idxKeys) == 1 && idxKeys[0] == utils.EmptyString) { // remove all + if len(idxKeys) == 0 { // remove all regexKey := utils.CacheInstanceToPrefix[idxItmType] + tntCtx for _, character := range []string{".", "*"} { regexKey = strings.ReplaceAll(regexKey, character, `\`+character) diff --git a/engine/storage_redis.go b/engine/storage_redis.go index f16993c6b..faa951cd7 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -1353,7 +1353,7 @@ func (rs *RedisStorage) RemoveLoadIDsDrv() (err error) { func (rs *RedisStorage) GetIndexesDrv(idxItmType, tntCtx string, idxKeys ...string) (indexes map[string]utils.StringSet, err error) { mp := make(map[string]string) dbKey := utils.CacheInstanceToPrefix[idxItmType] + tntCtx - if len(idxKeys) == 0 || (len(idxKeys) == 1 && idxKeys[0] == utils.EmptyString) { + if len(idxKeys) == 0 { if err = rs.Cmd(&mp, redis_HGETALL, dbKey); err != nil { return } else if len(mp) == 0 { @@ -1437,7 +1437,7 @@ func (rs *RedisStorage) SetIndexesDrv(idxItmType, tntCtx string, } func (rs *RedisStorage) RemoveIndexesDrv(idxItmType, tntCtx string, idxKeys ...string) (err error) { - if len(idxKeys) == 0 || (len(idxKeys) == 1 && idxKeys[0] == utils.EmptyString) { + if len(idxKeys) == 0 { return rs.Cmd(nil, redis_DEL, utils.CacheInstanceToPrefix[idxItmType]+tntCtx) } args := make([]string, len(idxKeys)+1)