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 methodQueryToolChest.getCacheStrategy(QueryType)
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
CacheStrategy.AddToResultFunction
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description byte[]
computeCacheKey(QueryType query)
Computes the per-segment cache key for the given query.byte[]
computeResultLevelCacheKey(QueryType query)
Computes the result-level cache key for the given query.static void
fetchAggregatorsFromCache(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>
getCacheObjectClazz()
Returns the class type of what is used in the cachedefault boolean
isCacheable(QueryType ignoredQuery, boolean ignoredWillMergeRunners)
Deprecated.default boolean
isCacheable(QueryType query, boolean willMergeRunners, boolean bySegment)
Returns whether the given query is cacheable or not.com.google.common.base.Function<T,CacheType>
prepareForCache(boolean isResultLevelCache)
Returns a function that converts from the QueryType's result type to something cacheable.default com.google.common.base.Function<T,CacheType>
prepareForSegmentLevelCache()
com.google.common.base.Function<CacheType,T>
pullFromCache(boolean isResultLevelCache)
A function that does the inverse of the operation that the function prepareForCache returnsdefault com.google.common.base.Function<CacheType,T>
pullFromSegmentLevelCache()
-
-
-
Method Detail
-
isCacheable
@Deprecated default boolean isCacheable(QueryType ignoredQuery, boolean ignoredWillMergeRunners)
Deprecated.This method is deprecated and retained for backward incompatibility. Returns whether the given query is cacheable or not. ThewillMergeRunners
parameter 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
default boolean isCacheable(QueryType query, boolean willMergeRunners, boolean bySegment)
Returns whether the given query is cacheable or not. ThewillMergeRunners
parameter 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 resultsbySegment
- segment level or result level cache- Returns:
- true if the query is cacheable, otherwise false.
-
computeCacheKey
byte[] computeCacheKey(QueryType query)
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
byte[] computeResultLevelCacheKey(QueryType query)
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
com.google.common.base.Function<T,CacheType> prepareForCache(boolean isResultLevelCache)
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
com.google.common.base.Function<CacheType,T> pullFromCache(boolean isResultLevelCache)
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
default com.google.common.base.Function<T,CacheType> prepareForSegmentLevelCache()
-
pullFromSegmentLevelCache
default com.google.common.base.Function<CacheType,T> 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).
-
-