Search in sources :

Example 6 with Bytes

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

the class AbstractMergedSortedCacheStoreIterator method next.

@Override
public KeyValue<K, V> next() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }
    final Bytes nextCacheKey = cacheIterator.hasNext() ? cacheIterator.peekNextKey() : null;
    final KS nextStoreKey = storeIterator.hasNext() ? storeIterator.peekNextKey() : null;
    if (nextCacheKey == null) {
        return nextStoreValue(nextStoreKey);
    }
    if (nextStoreKey == null) {
        return nextCacheValue(nextCacheKey);
    }
    final int comparison = compare(nextCacheKey, nextStoreKey);
    if (comparison > 0) {
        return nextStoreValue(nextStoreKey);
    } else if (comparison < 0) {
        return nextCacheValue(nextCacheKey);
    } else {
        // skip the same keyed element
        storeIterator.next();
        return nextCacheValue(nextCacheKey);
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) NoSuchElementException(java.util.NoSuchElementException)

Example 7 with Bytes

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

the class AbstractMergedSortedCacheStoreIterator method peekNextKey.

@Override
public K peekNextKey() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }
    final Bytes nextCacheKey = cacheIterator.hasNext() ? cacheIterator.peekNextKey() : null;
    final KS nextStoreKey = storeIterator.hasNext() ? storeIterator.peekNextKey() : null;
    if (nextCacheKey == null) {
        return deserializeStoreKey(nextStoreKey);
    }
    if (nextStoreKey == null) {
        return serdes.keyFrom(nextCacheKey.get());
    }
    final int comparison = compare(nextCacheKey, nextStoreKey);
    if (comparison > 0) {
        return deserializeStoreKey(nextStoreKey);
    } else if (comparison < 0) {
        return deserializeCacheKey(nextCacheKey);
    } else {
        // skip the same keyed element
        storeIterator.next();
        return deserializeCacheKey(nextCacheKey);
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) NoSuchElementException(java.util.NoSuchElementException)

Example 8 with Bytes

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

the class CachingKeyValueStore method delete.

@Override
public synchronized V delete(final K key) {
    validateStoreOpen();
    final byte[] rawKey = serdes.rawKey(key);
    final Bytes bytesKey = Bytes.wrap(rawKey);
    final V v = get(rawKey);
    cache.delete(cacheName, bytesKey);
    underlying.delete(bytesKey);
    return v;
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes)

Example 9 with Bytes

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

the class CachingKeyValueStore method get.

private V get(final byte[] rawKey) {
    final Bytes key = Bytes.wrap(rawKey);
    final LRUCacheEntry entry = cache.get(cacheName, key);
    if (entry == null) {
        final byte[] rawValue = underlying.get(key);
        if (rawValue == null) {
            return null;
        }
        // as we don't want other threads to trigger an eviction/flush
        if (Thread.currentThread().equals(streamThread)) {
            cache.put(cacheName, key, new LRUCacheEntry(rawValue));
        }
        return serdes.valueFrom(rawValue);
    }
    if (entry.value == null) {
        return null;
    }
    return serdes.valueFrom(entry.value);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes)

Example 10 with Bytes

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

the class CachingSessionStore method findSessions.

public KeyValueIterator<Windowed<K>, AGG> findSessions(final K key, final long earliestSessionEndTime, final long latestSessionStartTime) {
    validateStoreOpen();
    final Bytes binarySessionId = Bytes.wrap(keySerde.serializer().serialize(this.name(), key));
    final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(cacheName, keySchema.lowerRange(binarySessionId, earliestSessionEndTime), keySchema.upperRange(binarySessionId, latestSessionStartTime));
    final KeyValueIterator<Windowed<Bytes>, byte[]> storeIterator = bytesStore.findSessions(binarySessionId, earliestSessionEndTime, latestSessionStartTime);
    final HasNextCondition hasNextCondition = keySchema.hasNextCondition(binarySessionId, earliestSessionEndTime, latestSessionStartTime);
    final PeekingKeyValueIterator<Bytes, LRUCacheEntry> filteredCacheIterator = new FilteredCacheIterator(cacheIterator, hasNextCondition);
    return new MergedSortedCacheSessionStoreIterator<>(filteredCacheIterator, storeIterator, serdes);
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) 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