use of org.apache.kafka.common.utils.Bytes in project kafka by apache.
the class WindowKeySchema method hasNextCondition.
@Override
public HasNextCondition hasNextCondition(final Bytes binaryKey, final long from, final long to) {
return new HasNextCondition() {
@Override
public boolean hasNext(final KeyValueIterator<Bytes, ?> iterator) {
if (iterator.hasNext()) {
final Bytes bytes = iterator.peekNextKey();
final Bytes keyBytes = WindowStoreUtils.bytesKeyFromBinaryKey(bytes.get());
if (!keyBytes.equals(binaryKey)) {
return false;
}
final long time = WindowStoreUtils.timestampFromBinaryKey(bytes.get());
return time >= from && time <= to;
}
return false;
}
};
}
use of org.apache.kafka.common.utils.Bytes in project kafka by apache.
the class NamedCacheTest method shouldBeReentrantAndNotBreakLRU.
@Test
public void shouldBeReentrantAndNotBreakLRU() throws Exception {
final LRUCacheEntry dirty = new LRUCacheEntry(new byte[] { 3 }, true, 0, 0, 0, "");
final LRUCacheEntry clean = new LRUCacheEntry(new byte[] { 3 });
cache.put(Bytes.wrap(new byte[] { 0 }), dirty);
cache.put(Bytes.wrap(new byte[] { 1 }), clean);
cache.put(Bytes.wrap(new byte[] { 2 }), clean);
assertEquals(3 * cache.head().size(), cache.sizeInBytes());
cache.setListener(new ThreadCache.DirtyEntryFlushListener() {
@Override
public void apply(final List<ThreadCache.DirtyEntry> dirty) {
cache.put(Bytes.wrap(new byte[] { 3 }), clean);
// evict key 1
cache.evict();
// evict key 2
cache.evict();
}
});
assertEquals(3 * cache.head().size(), cache.sizeInBytes());
// Evict key 0
cache.evict();
final Bytes entryFour = Bytes.wrap(new byte[] { 4 });
cache.put(entryFour, dirty);
// check that the LRU is still correct
final NamedCache.LRUNode head = cache.head();
final NamedCache.LRUNode tail = cache.tail();
assertEquals(2, cache.size());
assertEquals(2 * head.size(), cache.sizeInBytes());
// dirty should be the newest
assertEquals(entryFour, head.key());
assertEquals(Bytes.wrap(new byte[] { 3 }), tail.key());
assertSame(tail, head.next());
assertNull(head.previous());
assertSame(head, tail.previous());
assertNull(tail.next());
// evict key 3
cache.evict();
assertSame(cache.head(), cache.tail());
assertEquals(entryFour, cache.head().key());
assertNull(cache.head().next());
assertNull(cache.head().previous());
}
use of org.apache.kafka.common.utils.Bytes in project kafka by apache.
the class MergedSortedCacheKeyValueStoreIteratorTest method shouldIterateOverRange.
@Test
public void shouldIterateOverRange() throws Exception {
final byte[][] bytes = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 } };
for (int i = 0; i < bytes.length; i += 2) {
store.put(Bytes.wrap(bytes[i]), bytes[i]);
cache.put(namespace, Bytes.wrap(bytes[i + 1]), new LRUCacheEntry(bytes[i + 1]));
}
final Bytes from = Bytes.wrap(new byte[] { 2 });
final Bytes to = Bytes.wrap(new byte[] { 9 });
final KeyValueIterator<Bytes, byte[]> storeIterator = new DelegatingPeekingKeyValueIterator<>("store", store.range(from, to));
final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(namespace, from, to);
final MergedSortedCacheKeyValueStoreIterator<byte[], byte[]> iterator = new MergedSortedCacheKeyValueStoreIterator<>(cacheIterator, storeIterator, serdes);
byte[][] values = new byte[8][];
int index = 0;
int bytesIndex = 2;
while (iterator.hasNext()) {
final byte[] value = iterator.next().value;
values[index++] = value;
assertArrayEquals(bytes[bytesIndex++], value);
}
}
use of org.apache.kafka.common.utils.Bytes in project kafka by apache.
the class MergedSortedCacheKeyValueStoreIteratorTest method shouldSkipAllDeletedFromCache.
@Test
public void shouldSkipAllDeletedFromCache() throws Exception {
final byte[][] bytes = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 } };
for (byte[] aByte : bytes) {
Bytes aBytes = Bytes.wrap(aByte);
store.put(aBytes, aByte);
cache.put(namespace, aBytes, new LRUCacheEntry(aByte));
}
cache.put(namespace, Bytes.wrap(bytes[1]), new LRUCacheEntry(null));
cache.put(namespace, Bytes.wrap(bytes[2]), new LRUCacheEntry(null));
cache.put(namespace, Bytes.wrap(bytes[3]), new LRUCacheEntry(null));
cache.put(namespace, Bytes.wrap(bytes[8]), new LRUCacheEntry(null));
cache.put(namespace, Bytes.wrap(bytes[11]), new LRUCacheEntry(null));
final MergedSortedCacheKeyValueStoreIterator<byte[], byte[]> iterator = createIterator();
assertArrayEquals(bytes[0], iterator.next().key);
assertArrayEquals(bytes[4], iterator.next().key);
assertArrayEquals(bytes[5], iterator.next().key);
assertArrayEquals(bytes[6], iterator.next().key);
assertArrayEquals(bytes[7], iterator.next().key);
assertArrayEquals(bytes[9], iterator.next().key);
assertArrayEquals(bytes[10], iterator.next().key);
assertFalse(iterator.hasNext());
}
use of org.apache.kafka.common.utils.Bytes in project kafka by apache.
the class MergedSortedCacheKeyValueStoreIteratorTest method shouldPeekNextKey.
@Test
public void shouldPeekNextKey() throws Exception {
final KeyValueStore<Bytes, byte[]> kv = new InMemoryKeyValueStore<>("one", Serdes.Bytes(), Serdes.ByteArray());
final ThreadCache cache = new ThreadCache("testCache", 1000000L, new MockStreamsMetrics(new Metrics()));
byte[][] bytes = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 } };
final String namespace = "one";
for (int i = 0; i < bytes.length - 1; i += 2) {
kv.put(Bytes.wrap(bytes[i]), bytes[i]);
cache.put(namespace, Bytes.wrap(bytes[i + 1]), new LRUCacheEntry(bytes[i + 1]));
}
final Bytes from = Bytes.wrap(new byte[] { 2 });
final Bytes to = Bytes.wrap(new byte[] { 9 });
final KeyValueIterator<Bytes, byte[]> storeIterator = kv.range(from, to);
final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(namespace, from, to);
final MergedSortedCacheKeyValueStoreIterator<byte[], byte[]> iterator = new MergedSortedCacheKeyValueStoreIterator<>(cacheIterator, storeIterator, serdes);
final byte[][] values = new byte[8][];
int index = 0;
int bytesIndex = 2;
while (iterator.hasNext()) {
final byte[] keys = iterator.peekNextKey();
values[index++] = keys;
assertArrayEquals(bytes[bytesIndex++], keys);
iterator.next();
}
}
Aggregations