/** * Create a new instance of {@link StateSerdes} for the given state name and key-/value-type classes. * * @param topic the topic name * @param keyClass the class of the key type * @param valueClass the class of the value type * @param <K> the key type * @param <V> the value type * @return a new instance of {@link StateSerdes} */ public static <K, V> StateSerdes<K, V> withBuiltinTypes( final String topic, final Class<K> keyClass, final Class<V> valueClass) { return new StateSerdes<>(topic, Serdes.serdeFrom(keyClass), Serdes.serdeFrom(valueClass)); }
void logChange(final K key, final V value) { if (collector != null) { final Serializer<K> keySerializer = serialization.keySerializer(); final Serializer<V> valueSerializer = serialization.valueSerializer(); // Sending null headers to changelog topics (KIP-244) collector.send(this.topic, key, value, null, this.partition, context.timestamp(), keySerializer, valueSerializer); } } }
/** * {@inheritDoc} */ @Override public void branch(final Stage<K, V> stage, final Event<K, V> event, final DeweyVersion version) { Matched key = Matched.from(stage, event); MatchedEvent.Pointer pointer = new MatchedEvent.Pointer(version, key); while(pointer != null && (key = pointer.getKey()) != null) { byte[] bytes = this.bytesStore.get(Bytes.wrap(serdes.rawKey(key))); final MatchedEvent<K, V> val = serdes.valueFrom(bytes); val.incrementRefAndGet(); this.bytesStore.put(Bytes.wrap(serdes.rawKey(key)), serdes.rawValue(val)); pointer = val.getPointerByVersion(pointer.getVersion()); } }
@Override @SuppressWarnings("unchecked") public void init(final ProcessorContext context, final StateStore root) { // construct the serde this.serdes = new StateSerdes<>( ProcessorStateManager.storeChangelogTopic(context.applicationId(), name), keySerde == null ? (Serde<K>) context.keySerde() : keySerde, valueSerde == null ? (Serde<V>) context.valueSerde() : valueSerde); if (root != null) { // register the store context.register(root, (key, value) -> { // this is a delete if (value == null) { delete(serdes.keyFrom(key)); } else { put(serdes.keyFrom(key), serdes.valueFrom(value)); } }); } this.open = true; }
private V outerValue(final byte[] value) { return value == null ? null : serdes.valueFrom(value); }
private Bytes keyBytes(final K key) { return Bytes.wrap(serdes.rawKey(key)); }
@Override public KeyValue<Windowed<K>, V> next() { final KeyValue<Bytes, byte[]> next = bytesIterator.next(); return KeyValue.pair(SessionKeySchema.from(next.key.get(), serdes.keyDeserializer(), serdes.topic()), serdes.valueFrom(next.value)); }
@Override public void put(final Windowed<K> sessionKey, final AGG aggregate) { bytesStore.put(Bytes.wrap(SessionKeySchema.toBinary(sessionKey, serdes.keySerializer(), topic)), serdes.rawValue(aggregate)); } }
private void putAndMaybeForward(final ThreadCache.DirtyEntry entry, final InternalProcessorContext context) { final Bytes binaryKey = cacheFunction.key(entry.key()); final ProcessorRecordContext current = context.recordContext(); context.setRecordContext(entry.entry().context()); try { final Windowed<K> key = SessionKeySchema.from(binaryKey.get(), serdes.keyDeserializer(), topic); final Bytes rawKey = Bytes.wrap(serdes.rawKey(key.key())); if (flushListener != null) { final AGG newValue = serdes.valueFrom(entry.newValue()); final AGG oldValue = newValue == null || sendOldValues ? fetchPrevious(rawKey, key.window()) : null; if (!(newValue == null && oldValue == null)) { flushListener.apply(key, newValue, oldValue); } } bytesStore.put(new Windowed<>(rawKey, key.window()), entry.newValue()); } finally { context.setRecordContext(current); } }
@Override public void put(final K key, final V value, final long windowStartTimestamp) { maybeUpdateSeqnumForDups(); bytesStore.put(WindowKeySchema.toStoreKeyBinary(key, windowStartTimestamp, seqnum, serdes), serdes.rawValue(value)); }
/** * Serialize the given key. * * @param key the key to be serialized * @return the serialized key */ public byte[] rawKey(final K key) { try { return keySerde.serializer().serialize(topic, key); } catch (final ClassCastException e) { final String keyClass = key == null ? "unknown because key is null" : key.getClass().getName(); throw new StreamsException( String.format("A serializer (key: %s) is not compatible to the actual key type " + "(key type: %s). Change the default Serdes in StreamConfig or " + "provide correct Serdes via method parameters.", keySerializer().getClass().getName(), keyClass), e); } }
@Override public Windowed<K> peekNextKey() { final Bytes bytes = bytesIterator.peekNextKey(); return SessionKeySchema.from(bytes.get(), serdes.keyDeserializer(), serdes.topic()); }
/** * Serialize the given value. * * @param value the value to be serialized * @return the serialized value */ public byte[] rawValue(final V value) { try { return valueSerde.serializer().serialize(topic, value); } catch (final ClassCastException e) { final String valueClass = value == null ? "unknown because value is null" : value.getClass().getName(); throw new StreamsException( String.format("A serializer (value: %s) is not compatible to the actual value type " + "(value type: %s). Change the default Serdes in StreamConfig or " + "provide correct Serdes via method parameters.", valueSerializer().getClass().getName(), valueClass), e); } } }
public static <K> K extractStoreKey(final byte[] binaryKey, final StateSerdes<K, ?> serdes) { final byte[] bytes = new byte[binaryKey.length - TIMESTAMP_SIZE - SEQNUM_SIZE]; System.arraycopy(binaryKey, 0, bytes, 0, bytes.length); return serdes.keyFrom(bytes); }
Matched currEventKey = Matched.from(currStage, currEvent); byte[] prevBytes = this.bytesStore.get(Bytes.wrap(serdes.rawKey(prevEventKey))); MatchedEvent sharedPrevEvent = serdes.valueFrom(prevBytes); byte[] currBytes = this.bytesStore.get(Bytes.wrap(serdes.rawKey(currEventKey))); MatchedEvent sharedCurrEvent = serdes.valueFrom(currBytes); this.bytesStore.put(Bytes.wrap(serdes.rawKey(currEventKey)), serdes.rawValue(sharedCurrEvent));
private V fetchPrevious(final Bytes key, final long timestamp) { final byte[] value = underlying.fetch(key, timestamp); if (value != null) { return serdes.valueFrom(value); } return null; }
public static <K> Bytes toStoreKeyBinary(final K key, final long timestamp, final int seqnum, final StateSerdes<K, ?> serdes) { final byte[] serializedKey = serdes.rawKey(key); return toStoreKeyBinary(serializedKey, timestamp, seqnum); }