org.apache.kafka.streams.state.QueryableStoreTypes Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kafka.streams.state;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StoreQueryParameters;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.processor.StateStore;
import org.apache.kafka.streams.state.internals.CompositeReadOnlyKeyValueStore;
import org.apache.kafka.streams.state.internals.CompositeReadOnlySessionStore;
import org.apache.kafka.streams.state.internals.CompositeReadOnlyWindowStore;
import org.apache.kafka.streams.state.internals.StateStoreProvider;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* Provides access to the {@link QueryableStoreType}s provided with {@link KafkaStreams}.
* These can be used with {@link KafkaStreams#store(StoreQueryParameters)}.
* To access and query the {@link StateStore}s that are part of a {@link Topology}.
*/
public final class QueryableStoreTypes {
/**
* A {@link QueryableStoreType} that accepts {@link ReadOnlyKeyValueStore}.
*
* @param key type of the store
* @param value type of the store
* @return {@link QueryableStoreTypes.KeyValueStoreType}
*/
public static QueryableStoreType> keyValueStore() {
return new KeyValueStoreType<>();
}
/**
* A {@link QueryableStoreType} that accepts {@link ReadOnlyKeyValueStore ReadOnlyKeyValueStore>}.
*
* @param key type of the store
* @param value type of the store
* @return {@link QueryableStoreTypes.TimestampedKeyValueStoreType}
*/
public static QueryableStoreType>> timestampedKeyValueStore() {
return new TimestampedKeyValueStoreType<>();
}
/**
* A {@link QueryableStoreType} that accepts {@link ReadOnlyWindowStore}.
*
* @param key type of the store
* @param value type of the store
* @return {@link QueryableStoreTypes.WindowStoreType}
*/
public static QueryableStoreType> windowStore() {
return new WindowStoreType<>();
}
/**
* A {@link QueryableStoreType} that accepts {@link ReadOnlyWindowStore ReadOnlyWindowStore>}.
*
* @param key type of the store
* @param value type of the store
* @return {@link QueryableStoreTypes.TimestampedWindowStoreType}
*/
public static QueryableStoreType>> timestampedWindowStore() {
return new TimestampedWindowStoreType<>();
}
/**
* A {@link QueryableStoreType} that accepts {@link ReadOnlySessionStore}.
*
* @param key type of the store
* @param value type of the store
* @return {@link QueryableStoreTypes.SessionStoreType}
*/
public static QueryableStoreType> sessionStore() {
return new SessionStoreType<>();
}
private static abstract class QueryableStoreTypeMatcher implements QueryableStoreType {
private final Set matchTo;
QueryableStoreTypeMatcher(final Set matchTo) {
this.matchTo = matchTo;
}
@SuppressWarnings("unchecked")
@Override
public boolean accepts(final StateStore stateStore) {
for (final Class matchToClass : matchTo) {
if (!matchToClass.isAssignableFrom(stateStore.getClass())) {
return false;
}
}
return true;
}
}
public static class KeyValueStoreType extends QueryableStoreTypeMatcher> {
KeyValueStoreType() {
super(Collections.singleton(ReadOnlyKeyValueStore.class));
}
@Override
public ReadOnlyKeyValueStore create(final StateStoreProvider storeProvider,
final String storeName) {
return new CompositeReadOnlyKeyValueStore<>(storeProvider, this, storeName);
}
}
private static class TimestampedKeyValueStoreType
extends QueryableStoreTypeMatcher>> {
TimestampedKeyValueStoreType() {
super(new HashSet<>(Arrays.asList(
TimestampedKeyValueStore.class,
ReadOnlyKeyValueStore.class)));
}
@Override
public ReadOnlyKeyValueStore> create(final StateStoreProvider storeProvider,
final String storeName) {
return new CompositeReadOnlyKeyValueStore<>(storeProvider, this, storeName);
}
}
public static class WindowStoreType extends QueryableStoreTypeMatcher> {
WindowStoreType() {
super(Collections.singleton(ReadOnlyWindowStore.class));
}
@Override
public ReadOnlyWindowStore create(final StateStoreProvider storeProvider,
final String storeName) {
return new CompositeReadOnlyWindowStore<>(storeProvider, this, storeName);
}
}
private static class TimestampedWindowStoreType
extends QueryableStoreTypeMatcher>> {
TimestampedWindowStoreType() {
super(new HashSet<>(Arrays.asList(
TimestampedWindowStore.class,
ReadOnlyWindowStore.class)));
}
@Override
public ReadOnlyWindowStore> create(final StateStoreProvider storeProvider,
final String storeName) {
return new CompositeReadOnlyWindowStore<>(storeProvider, this, storeName);
}
}
public static class SessionStoreType extends QueryableStoreTypeMatcher> {
SessionStoreType() {
super(Collections.singleton(ReadOnlySessionStore.class));
}
@Override
public ReadOnlySessionStore create(final StateStoreProvider storeProvider,
final String storeName) {
return new CompositeReadOnlySessionStore<>(storeProvider, this, storeName);
}
}
}