vvcephei commented on a change in pull request #11582: URL: https://github.com/apache/kafka/pull/11582#discussion_r771520958
########## File path: streams/src/main/java/org/apache/kafka/streams/state/internals/StoreQueryUtils.java ########## @@ -16,18 +16,78 @@ */ package org.apache.kafka.streams.state.internals; +import org.apache.kafka.common.utils.Bytes; import org.apache.kafka.streams.processor.StateStore; import org.apache.kafka.streams.processor.StateStoreContext; import org.apache.kafka.streams.processor.api.RecordMetadata; +import org.apache.kafka.streams.query.FailureReason; +import org.apache.kafka.streams.query.KeyQuery; import org.apache.kafka.streams.query.Position; import org.apache.kafka.streams.query.PositionBound; import org.apache.kafka.streams.query.Query; import org.apache.kafka.streams.query.QueryResult; +import org.apache.kafka.streams.state.KeyValueStore; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.Map; +import static org.apache.kafka.common.utils.Utils.mkEntry; +import static org.apache.kafka.common.utils.Utils.mkMap; + public final class StoreQueryUtils { + /** + * a utility interface to facilitate stores' query dispatch logic, + * allowing them to generically store query execution logic as the values + * in a map. + */ + @FunctionalInterface + public interface QueryHandler { + QueryResult<?> apply( + final Query<?> query, + final PositionBound positionBound, + final boolean collectExecutionInfo, + final StateStore store + ); + } + + + @SuppressWarnings("unchecked") + private static final Map<Class<?>, QueryHandler> QUERY_HANDLER_MAP = + mkMap( + mkEntry( + PingQuery.class, + (query, positionBound, collectExecutionInfo, store) -> QueryResult.forResult(true) + ), + mkEntry(KeyQuery.class, + (query, positionBound, collectExecutionInfo, store) -> { + if (store instanceof KeyValueStore) { + final KeyQuery<Bytes, byte[]> rawKeyQuery = (KeyQuery<Bytes, byte[]>) query; + final KeyValueStore<Bytes, byte[]> keyValueStore = + (KeyValueStore<Bytes, byte[]>) store; + try { + final byte[] bytes = keyValueStore.get(rawKeyQuery.getKey()); + return QueryResult.forResult(bytes); + } catch (final Throwable t) { Review comment: Good point. It's fine to catch Throwables, but it's not fine to swallow Errors, as I'm doing here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org