mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 10:06:24 +05:00
improve index cache and remove empty string checks
- all-or-nothing cache strategy for multiple keys (fetch all keys from database if any key is missing) - only len(idxKeys) == 0 triggers "get all" behavior - empty string is now treated as a valid index key
This commit is contained in:
committed by
Dan Christian Bogos
parent
e7b30a139f
commit
102abf559b
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user