use of org.apache.kafka.common.utils.Bytes in project kafka by apache.
the class InMemoryTimeOrderedKeyValueBuffer method put.
@Override
public void put(final long time, final Record<K, Change<V>> record, final ProcessorRecordContext recordContext) {
requireNonNull(record.value(), "value cannot be null");
requireNonNull(recordContext, "recordContext cannot be null");
final Bytes serializedKey = Bytes.wrap(keySerde.serializer().serialize(changelogTopic, record.key()));
final Change<byte[]> serialChange = valueSerde.serializeParts(changelogTopic, record.value());
final BufferValue buffered = getBuffered(serializedKey);
final byte[] serializedPriorValue;
if (buffered == null) {
serializedPriorValue = serialChange.oldValue;
} else {
serializedPriorValue = buffered.priorValue();
}
cleanPut(time, serializedKey, new BufferValue(serializedPriorValue, serialChange.oldValue, serialChange.newValue, recordContext));
dirtyKeys.add(serializedKey);
updateBufferMetrics();
}
use of org.apache.kafka.common.utils.Bytes in project kafka by apache.
the class InMemoryWindowStore method registerNewWindowedKeyValueIterator.
private WrappedWindowedKeyValueIterator registerNewWindowedKeyValueIterator(final Bytes keyFrom, final Bytes keyTo, final Iterator<Map.Entry<Long, ConcurrentNavigableMap<Bytes, byte[]>>> segmentIterator, final boolean forward) {
final Bytes from = (retainDuplicates && keyFrom != null) ? wrapForDups(keyFrom, 0) : keyFrom;
final Bytes to = (retainDuplicates && keyTo != null) ? wrapForDups(keyTo, Integer.MAX_VALUE) : keyTo;
final WrappedWindowedKeyValueIterator iterator = new WrappedWindowedKeyValueIterator(from, to, segmentIterator, openIterators::remove, retainDuplicates, windowSize, forward);
openIterators.add(iterator);
return iterator;
}
use of org.apache.kafka.common.utils.Bytes in project kafka by apache.
the class CachingSessionStore method findSessions.
@Override
public KeyValueIterator<Windowed<Bytes>, byte[]> findSessions(final Bytes keyFrom, final Bytes keyTo, final long earliestSessionEndTime, final long latestSessionStartTime) {
if (keyFrom != null && keyTo != null && keyFrom.compareTo(keyTo) > 0) {
LOG.warn(INVALID_RANGE_WARN_MSG);
return KeyValueIterators.emptyIterator();
}
validateStoreOpen();
final Bytes cacheKeyFrom = keyFrom == null ? null : cacheFunction.cacheKey(keySchema.lowerRange(keyFrom, earliestSessionEndTime));
final Bytes cacheKeyTo = keyTo == null ? null : cacheFunction.cacheKey(keySchema.upperRange(keyTo, latestSessionStartTime));
final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = context.cache().range(cacheName, cacheKeyFrom, cacheKeyTo);
final KeyValueIterator<Windowed<Bytes>, byte[]> storeIterator = wrapped().findSessions(keyFrom, keyTo, earliestSessionEndTime, latestSessionStartTime);
final HasNextCondition hasNextCondition = keySchema.hasNextCondition(keyFrom, keyTo, earliestSessionEndTime, latestSessionStartTime);
final PeekingKeyValueIterator<Bytes, LRUCacheEntry> filteredCacheIterator = new FilteredCacheIterator(cacheIterator, hasNextCondition, cacheFunction);
return new MergedSortedCacheSessionStoreIterator(filteredCacheIterator, storeIterator, cacheFunction, true);
}
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 = cacheFunction.key(entry.key());
final Windowed<Bytes> bytesKey = SessionKeySchema.from(binaryKey);
if (flushListener != null) {
final byte[] newValueBytes = entry.newValue();
final byte[] oldValueBytes = newValueBytes == null || sendOldValues ? wrapped().fetchSession(bytesKey.key(), bytesKey.window().start(), bytesKey.window().end()) : null;
// we can skip flushing to downstream as well as writing to underlying store
if (newValueBytes != null || oldValueBytes != null) {
// we need to get the old values if needed, and then put to store, and then flush
wrapped().put(bytesKey, entry.newValue());
final ProcessorRecordContext current = context.recordContext();
context.setRecordContext(entry.entry().context());
try {
flushListener.apply(new Record<>(binaryKey.get(), new Change<>(newValueBytes, sendOldValues ? oldValueBytes : null), entry.entry().context().timestamp(), entry.entry().context().headers()));
} finally {
context.setRecordContext(current);
}
}
} else {
wrapped().put(bytesKey, entry.newValue());
}
}
use of org.apache.kafka.common.utils.Bytes in project kafka by apache.
the class CachingKeyValueStore method runKeyQuery.
@SuppressWarnings("unchecked")
private <R> QueryResult<R> runKeyQuery(final Query<R> query, final Position mergedPosition, final PositionBound positionBound, final QueryConfig config) {
QueryResult<R> result = null;
final KeyQuery<Bytes, byte[]> keyQuery = (KeyQuery<Bytes, byte[]>) query;
if (keyQuery.isSkipCache()) {
return wrapped().query(query, positionBound, config);
}
final Bytes key = keyQuery.getKey();
if (context.cache() != null) {
final LRUCacheEntry lruCacheEntry = context.cache().get(cacheName, key);
if (lruCacheEntry != null) {
final byte[] rawValue;
if (timestampedSchema && !WrappedStateStore.isTimestamped(wrapped())) {
rawValue = ValueAndTimestampDeserializer.rawValue(lruCacheEntry.value());
} else {
rawValue = lruCacheEntry.value();
}
result = (QueryResult<R>) QueryResult.forResult(rawValue);
}
}
// the merged position above
if (result == null) {
result = wrapped().query(query, PositionBound.unbounded(), config);
}
result.setPosition(mergedPosition);
return result;
}
Aggregations