Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Status

Current state: Under DiscussionAccepted

Discussion thread: here

JIRA: KAFKA-15346

...

In this KIP we introduce the class VersionedKeyQuery with an Optional field to store the asOfTimestamp value. The method asOf  creates key queries having asOfTimestamp value as well.  
Defining the latest() method is not needed since returning the latest value has been always the default assumption. In other words, If a query is created without calling the asOf() the query returns the method, it will return the latest value of the key.

Code Block
languagejava
firstline1
titleVersionedKeyQuery.java
package org.apache.kafka.streams.query;

/**
 * Interactive query for retrieving a single record  from a versioned state store based on its key and timestamp.
 * <p>
 * See KIP-960 for more details.
 */
@Evolving
public final class VersionedKeyQuery<K, V> implements Query<VersionedRecord<V>> {

    private final K key;
    private final Optional<Instant> asOfTimestamp;

    private VersionedKeyQuery(final K key, final Optional<Instant> asOfTimestamp) {
        this.key = Objects.requireNonNull(key);
        this.asOfTimestamp = asOfTimestamp;
    }

    /**
     * Creates a query that will retrieve the latest record from a versioned state store identified by {@code key} if the key exists
     * (or {@code null} otherwise).
     * @param key The key to retrieve
     * @throws NullPointerException if @param key is null            
     * @param <K> The type of the key
     * @param <V> The type of the value that will be retrieved
     * @throws NullPointerException if @param key is null            
     */
    public static <K, V> VersionedKeyQuery<K, V> withKey(final K key);

    /**
     * Specifies the as of timestamp for the key query. The key query returns the record
     * with the greatest timestamp <= asOfTimestamp
     * @param asOfTimestamp The as of timestamp for timestamp
     * @throws NullPointerException if @param asOfTimestamp is null, it will be considered        as Optional.empty()
     */
    public VersionedKeyQuery<K, V> asOf(final Instant asOfTimestamp);

    /**
     * The key that was specified for this query.
     */
    public K key();

    /**
     * The timestamp of the query, if specified
     */
    public Optional<Instant> getAsOfTimestampasOfTimestamp();
}  


Examples

The following example illustrates the use of the VersionedKeyQuery class to query a versioned state store.

...