Search in sources :

Example 36 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class SessionKeySerdeTest method shouldConvertToBinaryAndBack.

@Test
public void shouldConvertToBinaryAndBack() throws Exception {
    final Bytes serialized = SessionKeySerde.toBinary(windowedKey, serde.serializer());
    final Windowed<String> result = SessionKeySerde.from(serialized.get(), Serdes.String().deserializer());
    assertEquals(windowedKey, result);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

Example 37 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class CachingKeyValueStore method range.

@Override
public KeyValueIterator<K, V> range(final K from, final K to) {
    validateStoreOpen();
    final Bytes origFrom = Bytes.wrap(serdes.rawKey(from));
    final Bytes origTo = Bytes.wrap(serdes.rawKey(to));
    final KeyValueIterator<Bytes, byte[]> storeIterator = underlying.range(origFrom, origTo);
    final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(cacheName, origFrom, origTo);
    return new MergedSortedCacheKeyValueStoreIterator<>(cacheIterator, storeIterator, serdes);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes)

Example 38 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class CachingSessionStore method putAndMaybeForward.

private void putAndMaybeForward(final ThreadCache.DirtyEntry entry, final InternalProcessorContext context) {
    final Bytes binaryKey = entry.key();
    final RecordContext current = context.recordContext();
    context.setRecordContext(entry.recordContext());
    try {
        final Windowed<K> key = SessionKeySerde.from(binaryKey.get(), keySerde.deserializer());
        if (flushListener != null) {
            final AGG newValue = serdes.valueFrom(entry.newValue());
            final AGG oldValue = fetchPrevious(binaryKey);
            if (!(newValue == null && oldValue == null)) {
                flushListener.apply(key, newValue == null ? null : newValue, oldValue);
            }
        }
        bytesStore.put(new Windowed<>(Bytes.wrap(serdes.rawKey(key.key())), key.window()), entry.newValue());
    } finally {
        context.setRecordContext(current);
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) RecordContext(org.apache.kafka.streams.processor.internals.RecordContext)

Example 39 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class CachingWindowStore method fetch.

@Override
public synchronized WindowStoreIterator<V> fetch(final K key, final long timeFrom, final long timeTo) {
    // since this function may not access the underlying inner store, we need to validate
    // if store is open outside as well.
    validateStoreOpen();
    Bytes fromBytes = WindowStoreUtils.toBinaryKey(key, timeFrom, 0, serdes);
    Bytes toBytes = WindowStoreUtils.toBinaryKey(key, timeTo, 0, serdes);
    final Bytes keyBytes = Bytes.wrap(serdes.rawKey(key));
    final WindowStoreIterator<byte[]> underlyingIterator = underlying.fetch(keyBytes, timeFrom, timeTo);
    final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(name, fromBytes, toBytes);
    final HasNextCondition hasNextCondition = keySchema.hasNextCondition(keyBytes, timeFrom, timeTo);
    final PeekingKeyValueIterator<Bytes, LRUCacheEntry> filteredCacheIterator = new FilteredCacheIterator(cacheIterator, hasNextCondition);
    return new MergedSortedCacheWindowStoreIterator<>(filteredCacheIterator, underlyingIterator, new StateSerdes<>(serdes.stateName(), Serdes.Long(), serdes.valueSerde()));
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes)

Example 40 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class ChangeLoggingKeyValueStore method put.

@Override
public void put(final K key, final V value) {
    final Bytes bytesKey = Bytes.wrap(serdes.rawKey(key));
    final byte[] bytesValue = serdes.rawValue(value);
    innerBytes.put(bytesKey, bytesValue);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes)

Aggregations

Bytes (org.apache.kafka.common.utils.Bytes)54 Test (org.junit.Test)34 Metrics (org.apache.kafka.common.metrics.Metrics)14 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)14 Windowed (org.apache.kafka.streams.kstream.Windowed)5 ArrayList (java.util.ArrayList)4 KeyValue (org.apache.kafka.streams.KeyValue)4 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)3 MockProcessorContext (org.apache.kafka.test.MockProcessorContext)3 ByteBuffer (java.nio.ByteBuffer)2 NoSuchElementException (java.util.NoSuchElementException)2 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)2 KeyValueIterator (org.apache.kafka.streams.state.KeyValueIterator)2 Before (org.junit.Before)2 MockProducer (org.apache.kafka.clients.producer.MockProducer)1 Serializer (org.apache.kafka.common.serialization.Serializer)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1 RecordContext (org.apache.kafka.streams.processor.internals.RecordContext)1 StateSerdes (org.apache.kafka.streams.state.StateSerdes)1