use of org.apache.kafka.streams.KeyValue in project apache-kafka-on-k8s by banzaicloud.
the class ReadOnlyWindowStoreStub method fetch.
@Override
public KeyValueIterator<Windowed<K>, V> fetch(K from, K to, long timeFrom, long timeTo) {
if (!open) {
throw new InvalidStateStoreException("Store is not open");
}
final List<KeyValue<Windowed<K>, V>> results = new ArrayList<>();
for (long now = timeFrom; now <= timeTo; now++) {
final NavigableMap<K, V> kvMap = data.get(now);
if (kvMap != null) {
for (Entry<K, V> entry : kvMap.subMap(from, true, to, true).entrySet()) {
results.add(new KeyValue<>(new Windowed<>(entry.getKey(), new TimeWindow(now, now + windowSize)), entry.getValue()));
}
}
}
final Iterator<KeyValue<Windowed<K>, V>> iterator = results.iterator();
return new KeyValueIterator<Windowed<K>, V>() {
@Override
public void close() {
}
@Override
public Windowed<K> peekNextKey() {
throw new UnsupportedOperationException("peekNextKey() not supported in " + getClass().getName());
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public KeyValue<Windowed<K>, V> next() {
return iterator.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove() not supported in " + getClass().getName());
}
};
}
use of org.apache.kafka.streams.KeyValue in project apache-kafka-on-k8s by banzaicloud.
the class ReadOnlyWindowStoreStub method fetchAll.
@Override
public KeyValueIterator<Windowed<K>, V> fetchAll(long timeFrom, long timeTo) {
if (!open) {
throw new InvalidStateStoreException("Store is not open");
}
final List<KeyValue<Windowed<K>, V>> results = new ArrayList<>();
for (long now : data.keySet()) {
if (!(now >= timeFrom && now <= timeTo))
continue;
final NavigableMap<K, V> kvMap = data.get(now);
if (kvMap != null) {
for (Entry<K, V> entry : kvMap.entrySet()) {
results.add(new KeyValue<>(new Windowed<>(entry.getKey(), new TimeWindow(now, now + windowSize)), entry.getValue()));
}
}
}
final Iterator<KeyValue<Windowed<K>, V>> iterator = results.iterator();
return new KeyValueIterator<Windowed<K>, V>() {
@Override
public void close() {
}
@Override
public Windowed<K> peekNextKey() {
throw new UnsupportedOperationException("peekNextKey() not supported in " + getClass().getName());
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public KeyValue<Windowed<K>, V> next() {
return iterator.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove() not supported in " + getClass().getName());
}
};
}
use of org.apache.kafka.streams.KeyValue in project apache-kafka-on-k8s by banzaicloud.
the class InternalMockProcessorContext method restore.
public void restore(final String storeName, final Iterable<KeyValue<byte[], byte[]>> changeLog) {
final BatchingStateRestoreCallback restoreCallback = getBatchingRestoreCallback(restoreFuncs.get(storeName));
final StateRestoreListener restoreListener = getStateRestoreListener(restoreCallback);
restoreListener.onRestoreStart(null, storeName, 0L, 0L);
List<KeyValue<byte[], byte[]>> records = new ArrayList<>();
for (KeyValue<byte[], byte[]> keyValue : changeLog) {
records.add(keyValue);
}
restoreCallback.restoreAll(records);
restoreListener.onRestoreEnd(null, storeName, 0L);
}
use of org.apache.kafka.streams.KeyValue in project apache-kafka-on-k8s by banzaicloud.
the class RocksDBSegmentedBytesStoreTest method shouldGetAllSegments.
@Test
public void shouldGetAllSegments() {
// just to validate directories
final Segments segments = new Segments(storeName, retention, numSegments);
final String key = "a";
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(0L, 0L))), serializeValue(50L));
assertEquals(Collections.singleton(segments.segmentName(0)), segmentDirs());
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(30000L, 60000L))), serializeValue(100L));
assertEquals(Utils.mkSet(segments.segmentName(0), segments.segmentName(1)), segmentDirs());
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(61000L, 120000L))), serializeValue(200L));
assertEquals(Utils.mkSet(segments.segmentName(0), segments.segmentName(1), segments.segmentName(2)), segmentDirs());
final List<KeyValue<Windowed<String>, Long>> results = toList(bytesStore.all());
assertEquals(Arrays.asList(KeyValue.pair(new Windowed<>(key, new SessionWindow(0L, 0L)), 50L), KeyValue.pair(new Windowed<>(key, new SessionWindow(30000L, 60000L)), 100L), KeyValue.pair(new Windowed<>(key, new SessionWindow(61000L, 120000L)), 200L)), results);
}
use of org.apache.kafka.streams.KeyValue in project apache-kafka-on-k8s by banzaicloud.
the class RocksDBSegmentedBytesStoreTest method shouldFetchAllSegments.
@Test
public void shouldFetchAllSegments() {
// just to validate directories
final Segments segments = new Segments(storeName, retention, numSegments);
final String key = "a";
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(0L, 0L))), serializeValue(50L));
assertEquals(Collections.singleton(segments.segmentName(0)), segmentDirs());
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(30000L, 60000L))), serializeValue(100L));
assertEquals(Utils.mkSet(segments.segmentName(0), segments.segmentName(1)), segmentDirs());
bytesStore.put(serializeKey(new Windowed<>(key, new SessionWindow(61000L, 120000L))), serializeValue(200L));
assertEquals(Utils.mkSet(segments.segmentName(0), segments.segmentName(1), segments.segmentName(2)), segmentDirs());
final List<KeyValue<Windowed<String>, Long>> results = toList(bytesStore.fetchAll(0L, 60000L));
assertEquals(Arrays.asList(KeyValue.pair(new Windowed<>(key, new SessionWindow(0L, 0L)), 50L), KeyValue.pair(new Windowed<>(key, new SessionWindow(30000L, 60000L)), 100L)), results);
}
Aggregations