Search in sources :

Example 21 with Bytes

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

the class WindowStoreUtilsTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    final String key = "key1";
    final long timestamp = 99L;
    final int seqNum = 3;
    Bytes bytes = WindowStoreUtils.toBinaryKey(key, timestamp, seqNum, serdes);
    final String parsedKey = WindowStoreUtils.keyFromBinaryKey(bytes.get(), serdes);
    final long parsedTs = WindowStoreUtils.timestampFromBinaryKey(bytes.get());
    final int parsedSeqNum = WindowStoreUtils.sequenceNumberFromBinaryKey(bytes.get());
    assertEquals(key, parsedKey);
    assertEquals(timestamp, parsedTs);
    assertEquals(seqNum, parsedSeqNum);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

Example 22 with Bytes

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

the class FilteredCacheIteratorTest method shouldPeekNextKey.

@Test
public void shouldPeekNextKey() throws Exception {
    while (allIterator.hasNext()) {
        final Bytes nextKey = allIterator.peekNextKey();
        final KeyValue<Bytes, LRUCacheEntry> next = allIterator.next();
        assertThat(next.key, equalTo(nextKey));
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

Example 23 with Bytes

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

the class MergedSortedCacheKeyValueStoreIteratorTest method shouldNotHaveNextIfAllCachedItemsDeleted.

@Test
public void shouldNotHaveNextIfAllCachedItemsDeleted() throws Exception {
    final byte[][] bytes = { { 0 }, { 1 }, { 2 } };
    for (byte[] aByte : bytes) {
        Bytes aBytes = Bytes.wrap(aByte);
        store.put(aBytes, aByte);
        cache.put(namespace, aBytes, new LRUCacheEntry(null));
    }
    assertFalse(createIterator().hasNext());
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

Example 24 with Bytes

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

the class MergedSortedCacheWrappedWindowStoreIteratorTest method shouldPeekNextKey.

@Test
public void shouldPeekNextKey() throws Exception {
    windowStoreKvPairs.add(KeyValue.pair(10L, "a".getBytes()));
    cache.put(namespace, WindowStoreUtils.toBinaryKey("a", 0, 0, stateSerdes), new LRUCacheEntry("b".getBytes()));
    Bytes fromBytes = WindowStoreUtils.toBinaryKey("a", 0, 0, stateSerdes);
    Bytes toBytes = WindowStoreUtils.toBinaryKey("a", 100, 0, stateSerdes);
    final KeyValueIterator<Long, byte[]> storeIterator = new DelegatingPeekingKeyValueIterator<>("store", new KeyValueIteratorStub<>(windowStoreKvPairs.iterator()));
    final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(namespace, fromBytes, toBytes);
    final MergedSortedCacheWindowStoreIterator<byte[]> iterator = new MergedSortedCacheWindowStoreIterator<>(cacheIterator, storeIterator, new StateSerdes<>("name", Serdes.Long(), Serdes.ByteArray()));
    assertThat(iterator.peekNextKey(), equalTo(0L));
    iterator.next();
    assertThat(iterator.peekNextKey(), equalTo(10L));
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

Example 25 with Bytes

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

the class MergedSortedCacheWrappedWindowStoreIteratorTest method shouldIterateOverValueFromBothIterators.

@Test
public void shouldIterateOverValueFromBothIterators() throws Exception {
    final List<KeyValue<Long, byte[]>> expectedKvPairs = new ArrayList<>();
    for (long t = 0; t < 100; t += 20) {
        final byte[] v1Bytes = String.valueOf(t).getBytes();
        final KeyValue<Long, byte[]> v1 = KeyValue.pair(t, v1Bytes);
        windowStoreKvPairs.add(v1);
        expectedKvPairs.add(KeyValue.pair(t, v1Bytes));
        final Bytes keyBytes = WindowStoreUtils.toBinaryKey("a", t + 10, 0, stateSerdes);
        final byte[] valBytes = String.valueOf(t + 10).getBytes();
        expectedKvPairs.add(KeyValue.pair(t + 10, valBytes));
        cache.put(namespace, keyBytes, new LRUCacheEntry(valBytes));
    }
    Bytes fromBytes = WindowStoreUtils.toBinaryKey("a", 0, 0, stateSerdes);
    Bytes toBytes = WindowStoreUtils.toBinaryKey("a", 100, 0, stateSerdes);
    final KeyValueIterator<Long, byte[]> storeIterator = new DelegatingPeekingKeyValueIterator<>("store", new KeyValueIteratorStub<>(windowStoreKvPairs.iterator()));
    final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(namespace, fromBytes, toBytes);
    final MergedSortedCacheWindowStoreIterator<byte[]> iterator = new MergedSortedCacheWindowStoreIterator<>(cacheIterator, storeIterator, new StateSerdes<>("name", Serdes.Long(), Serdes.ByteArray()));
    int index = 0;
    while (iterator.hasNext()) {
        final KeyValue<Long, byte[]> next = iterator.next();
        final KeyValue<Long, byte[]> expected = expectedKvPairs.get(index++);
        assertArrayEquals(expected.value, next.value);
        assertEquals(expected.key, next.key);
    }
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

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