mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
Add doCopySourcePrimaryKeyToCache option - to, copy primary keys from source to cache; apply this in qqq-table cache
This commit is contained in:
@ -46,7 +46,7 @@ public class CacheUtils
|
|||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
**
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
static QRecord mapSourceRecordToCacheRecord(QTableMetaData table, QRecord recordFromSource)
|
static QRecord mapSourceRecordToCacheRecord(QTableMetaData table, QRecord recordFromSource, CacheUseCase cacheUseCase)
|
||||||
{
|
{
|
||||||
QRecord cacheRecord = new QRecord(recordFromSource);
|
QRecord cacheRecord = new QRecord(recordFromSource);
|
||||||
|
|
||||||
@ -57,9 +57,12 @@ public class CacheUtils
|
|||||||
for(String fieldName : table.getFields().keySet())
|
for(String fieldName : table.getFields().keySet())
|
||||||
{
|
{
|
||||||
if(fieldName.equals(table.getPrimaryKeyField()))
|
if(fieldName.equals(table.getPrimaryKeyField()))
|
||||||
|
{
|
||||||
|
if(!cacheUseCase.getDoCopySourcePrimaryKeyToCache())
|
||||||
{
|
{
|
||||||
cacheRecord.removeValue(fieldName);
|
cacheRecord.removeValue(fieldName);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if(!cacheRecord.getValues().containsKey(fieldName))
|
else if(!cacheRecord.getValues().containsKey(fieldName))
|
||||||
{
|
{
|
||||||
cacheRecord.setValue(fieldName, null);
|
cacheRecord.setValue(fieldName, null);
|
||||||
|
@ -80,8 +80,8 @@ public class QueryActionCacheHelper
|
|||||||
private static final QLogger LOG = QLogger.getLogger(QueryActionCacheHelper.class);
|
private static final QLogger LOG = QLogger.getLogger(QueryActionCacheHelper.class);
|
||||||
|
|
||||||
private boolean isQueryInputCacheable = false;
|
private boolean isQueryInputCacheable = false;
|
||||||
private Set<CacheUseCase.Type> cacheUseCases = new HashSet<>();
|
private Map<CacheUseCase.Type, CacheUseCase> cacheUseCaseMap = new HashMap<>();
|
||||||
private CacheUseCase.Type activeCacheUseCase = null;
|
private CacheUseCase activeCacheUseCase = null;
|
||||||
|
|
||||||
private UniqueKey cacheUniqueKey = null;
|
private UniqueKey cacheUniqueKey = null;
|
||||||
private ListingHash<String, Serializable> uniqueKeyValues = new ListingHash<>();
|
private ListingHash<String, Serializable> uniqueKeyValues = new ListingHash<>();
|
||||||
@ -122,7 +122,7 @@ public class QueryActionCacheHelper
|
|||||||
QTableMetaData table = queryInput.getTable();
|
QTableMetaData table = queryInput.getTable();
|
||||||
|
|
||||||
List<QRecord> recordsToReturn = recordsFromSource.stream()
|
List<QRecord> recordsToReturn = recordsFromSource.stream()
|
||||||
.map(r -> CacheUtils.mapSourceRecordToCacheRecord(table, r))
|
.map(r -> CacheUtils.mapSourceRecordToCacheRecord(table, r, activeCacheUseCase))
|
||||||
.toList();
|
.toList();
|
||||||
queryOutput.addRecords(recordsToReturn);
|
queryOutput.addRecords(recordsToReturn);
|
||||||
|
|
||||||
@ -220,7 +220,7 @@ public class QueryActionCacheHelper
|
|||||||
List<QRecord> refreshedRecordsToReturn = recordsFromSource.stream()
|
List<QRecord> refreshedRecordsToReturn = recordsFromSource.stream()
|
||||||
.map(r ->
|
.map(r ->
|
||||||
{
|
{
|
||||||
QRecord recordToCache = CacheUtils.mapSourceRecordToCacheRecord(table, r);
|
QRecord recordToCache = CacheUtils.mapSourceRecordToCacheRecord(table, r, activeCacheUseCase);
|
||||||
recordToCache.setValue(table.getPrimaryKeyField(), uniqueKeyToPrimaryKeyMap.get(getUniqueKeyValues(recordToCache)));
|
recordToCache.setValue(table.getPrimaryKeyField(), uniqueKeyToPrimaryKeyMap.get(getUniqueKeyValues(recordToCache)));
|
||||||
return (recordToCache);
|
return (recordToCache);
|
||||||
})
|
})
|
||||||
@ -391,10 +391,10 @@ public class QueryActionCacheHelper
|
|||||||
|
|
||||||
for(CacheUseCase cacheUseCase : CollectionUtils.nonNullList(table.getCacheOf().getUseCases()))
|
for(CacheUseCase cacheUseCase : CollectionUtils.nonNullList(table.getCacheOf().getUseCases()))
|
||||||
{
|
{
|
||||||
cacheUseCases.add(cacheUseCase.getType());
|
cacheUseCaseMap.put(cacheUseCase.getType(), cacheUseCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(cacheUseCases.contains(CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY))
|
if(cacheUseCaseMap.containsKey(CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY))
|
||||||
{
|
{
|
||||||
if(queryInput.getFilter() == null)
|
if(queryInput.getFilter() == null)
|
||||||
{
|
{
|
||||||
@ -475,7 +475,7 @@ public class QueryActionCacheHelper
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG.trace("Unable to cache: No supported use case: " + cacheUseCases);
|
LOG.trace("Unable to cache: No supported use case: " + cacheUseCaseMap.keySet());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -491,7 +491,7 @@ public class QueryActionCacheHelper
|
|||||||
{
|
{
|
||||||
this.cacheUniqueKey = uniqueKey;
|
this.cacheUniqueKey = uniqueKey;
|
||||||
isQueryInputCacheable = true;
|
isQueryInputCacheable = true;
|
||||||
activeCacheUseCase = CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY;
|
activeCacheUseCase = cacheUseCaseMap.get(CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -536,14 +536,14 @@ public class QueryActionCacheHelper
|
|||||||
List<QRecord> recordsFromSource = null;
|
List<QRecord> recordsFromSource = null;
|
||||||
QTableMetaData table = queryInput.getTable();
|
QTableMetaData table = queryInput.getTable();
|
||||||
|
|
||||||
if(CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY.equals(activeCacheUseCase))
|
if(CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY.equals(activeCacheUseCase.getType()))
|
||||||
{
|
{
|
||||||
recordsFromSource = getFromCachedSourceForUniqueKeyToUniqueKey(queryInput, uniqueKeyValues, table.getCacheOf().getSourceTable());
|
recordsFromSource = getFromCachedSourceForUniqueKeyToUniqueKey(queryInput, uniqueKeyValues, table.getCacheOf().getSourceTable());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// todo!!
|
// todo!!
|
||||||
throw (new NotImplementedException("Not-yet-implemented cache use case type: " + activeCacheUseCase));
|
throw (new NotImplementedException("Not-yet-implemented cache use case type: " + activeCacheUseCase.getType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return (recordsFromSource);
|
return (recordsFromSource);
|
||||||
|
@ -49,6 +49,7 @@ public class CacheUseCase
|
|||||||
//////////////////////////
|
//////////////////////////
|
||||||
private UniqueKey cacheUniqueKey;
|
private UniqueKey cacheUniqueKey;
|
||||||
private UniqueKey sourceUniqueKey;
|
private UniqueKey sourceUniqueKey;
|
||||||
|
private boolean doCopySourcePrimaryKeyToCache = false;
|
||||||
|
|
||||||
private List<QQueryFilter> excludeRecordsMatching;
|
private List<QQueryFilter> excludeRecordsMatching;
|
||||||
|
|
||||||
@ -222,4 +223,35 @@ public class CacheUseCase
|
|||||||
return (this);
|
return (this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Getter for doCopySourcePrimaryKeyToCache
|
||||||
|
*******************************************************************************/
|
||||||
|
public boolean getDoCopySourcePrimaryKeyToCache()
|
||||||
|
{
|
||||||
|
return (this.doCopySourcePrimaryKeyToCache);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Setter for doCopySourcePrimaryKeyToCache
|
||||||
|
*******************************************************************************/
|
||||||
|
public void setDoCopySourcePrimaryKeyToCache(boolean doCopySourcePrimaryKeyToCache)
|
||||||
|
{
|
||||||
|
this.doCopySourcePrimaryKeyToCache = doCopySourcePrimaryKeyToCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Fluent setter for doCopySourcePrimaryKeyToCache
|
||||||
|
*******************************************************************************/
|
||||||
|
public CacheUseCase withDoCopySourcePrimaryKeyToCache(boolean doCopySourcePrimaryKeyToCache)
|
||||||
|
{
|
||||||
|
this.doCopySourcePrimaryKeyToCache = doCopySourcePrimaryKeyToCache;
|
||||||
|
return (this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -105,6 +105,7 @@ public class QQQTablesMetaDataProvider
|
|||||||
.withCacheSourceMisses(false)
|
.withCacheSourceMisses(false)
|
||||||
.withCacheUniqueKey(new UniqueKey("name"))
|
.withCacheUniqueKey(new UniqueKey("name"))
|
||||||
.withSourceUniqueKey(new UniqueKey("name"))
|
.withSourceUniqueKey(new UniqueKey("name"))
|
||||||
|
.withDoCopySourcePrimaryKeyToCache(true)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user