Package org.apache.druid.query
Interface CacheStrategy<T,CacheType,QueryType extends Query<T>>
public interface CacheStrategy<T,CacheType,QueryType extends Query<T>>
Handles caching-related tasks for a particular query type.
Generally returned by the toolchest method
QueryToolChest.getCacheStrategy(QueryType).-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionbyte[]computeCacheKey(QueryType query) Computes the per-segment cache key for the given query.byte[]Computes the result-level cache key for the given query.static voidfetchAggregatorsFromCache(List<AggregatorFactory> aggregators, Iterator<Object> resultIter, boolean isResultLevelCache, CacheStrategy.AddToResultFunction addToResultFunction) Helper function used by TopN, GroupBy, Timeseries queries inpullFromCache(boolean).com.fasterxml.jackson.core.type.TypeReference<CacheType> Returns the class type of what is used in the cachedefault booleanisCacheable(QueryType ignoredQuery, boolean ignoredWillMergeRunners) Deprecated.default booleanisCacheable(QueryType query, boolean willMergeRunners, boolean segmentLevel) Returns whether the given query is cacheable or not.prepareForCache(boolean isResultLevelCache) Returns a function that converts from the QueryType's result type to something cacheable.pullFromCache(boolean isResultLevelCache) A function that does the inverse of the operation that the function prepareForCache returns
-
Method Details
-
isCacheable
Deprecated.This method is deprecated and retained for backward incompatibility. Returns whether the given query is cacheable or not. ThewillMergeRunnersparameter can be used for distinguishing the caller is a broker or a data node.- Parameters:
ignoredQuery- the query to be cachedignoredWillMergeRunners- indicates thatQueryRunnerFactory.mergeRunners(QueryProcessingPool, Iterable)will be called on the cached by-segment results- Returns:
- true if the query is cacheable, otherwise false.
-
isCacheable
Returns whether the given query is cacheable or not. ThewillMergeRunnersparameter can be used for distinguishing the caller is a broker or a data node.- Parameters:
query- the query to be cachedwillMergeRunners- indicates thatQueryRunnerFactory.mergeRunners(QueryProcessingPool, Iterable)will be called on the cached by-segment resultssegmentLevel- segment level or result level cache- Returns:
- true if the query is cacheable, otherwise false.
-
computeCacheKey
Computes the per-segment cache key for the given query. Because this is a per-segment cache key, it should only include parts of the query that affect the results for a specific segment (i.e., the results returned fromQueryRunnerFactory.createRunner(org.apache.druid.segment.Segment)).- Parameters:
query- the query to be cached- Returns:
- the per-segment cache key
-
computeResultLevelCacheKey
Computes the result-level cache key for the given query. The result-level cache will tack on datasource and interval details, so this key does not need to include datasource and interval. But it should include anything else that might affect the results of the query. Some implementations will need to include query parameters that are not used incomputeCacheKey(QueryType)for the same query.- Parameters:
query- the query to be cached- Returns:
- the result-level cache key
-
getCacheObjectClazz
com.fasterxml.jackson.core.type.TypeReference<CacheType> getCacheObjectClazz()Returns the class type of what is used in the cache- Returns:
- Returns the class type of what is used in the cache
-
prepareForCache
Returns a function that converts from the QueryType's result type to something cacheable.The resulting function must be thread-safe.
- Parameters:
isResultLevelCache- indicates whether the function is invoked for result-level caching or segment-level caching- Returns:
- a thread-safe function that converts the QueryType's result type into something cacheable
-
pullFromCache
A function that does the inverse of the operation that the function prepareForCache returns- Parameters:
isResultLevelCache- indicates whether the function is invoked for result-level caching or segment-level caching- Returns:
- A function that does the inverse of the operation that the function prepareForCache returns
-
prepareForSegmentLevelCache
-
pullFromSegmentLevelCache
-
fetchAggregatorsFromCache
static void fetchAggregatorsFromCache(List<AggregatorFactory> aggregators, Iterator<Object> resultIter, boolean isResultLevelCache, CacheStrategy.AddToResultFunction addToResultFunction) Helper function used by TopN, GroupBy, Timeseries queries inpullFromCache(boolean). When using the result level cache, the agg values seen here are finalized values generated by AggregatorFactory.finalizeComputation(). These finalized values are deserialized from the cache as generic Objects, which will later be reserialized and returned to the user without further modification. Because the agg values are deserialized as generic Objects, the values are subject to the same type consistency issues handled by DimensionHandlerUtils.convertObjectToType() in the pullFromCache implementations for dimension values (e.g., a Float would become Double).
-