use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.
the class AbstractSessionBytesStoreTest method shouldFetchAndIterateOverExactBinaryKeys.
@Test
public void shouldFetchAndIterateOverExactBinaryKeys() {
final SessionStore<Bytes, String> sessionStore = buildSessionStore(RETENTION_PERIOD, Serdes.Bytes(), Serdes.String());
sessionStore.init((StateStoreContext) context, sessionStore);
final Bytes key1 = Bytes.wrap(new byte[] { 0 });
final Bytes key2 = Bytes.wrap(new byte[] { 0, 0 });
final Bytes key3 = Bytes.wrap(new byte[] { 0, 0, 0 });
sessionStore.put(new Windowed<>(key1, new SessionWindow(1, 100)), "1");
sessionStore.put(new Windowed<>(key2, new SessionWindow(2, 100)), "2");
sessionStore.put(new Windowed<>(key3, new SessionWindow(3, 100)), "3");
sessionStore.put(new Windowed<>(key1, new SessionWindow(4, 100)), "4");
sessionStore.put(new Windowed<>(key2, new SessionWindow(5, 100)), "5");
sessionStore.put(new Windowed<>(key3, new SessionWindow(6, 100)), "6");
sessionStore.put(new Windowed<>(key1, new SessionWindow(7, 100)), "7");
sessionStore.put(new Windowed<>(key2, new SessionWindow(8, 100)), "8");
sessionStore.put(new Windowed<>(key3, new SessionWindow(9, 100)), "9");
final List<String> expectedKey1 = asList("1", "4", "7");
try (KeyValueIterator<Windowed<Bytes>, String> iterator = sessionStore.findSessions(key1, 0L, Long.MAX_VALUE)) {
assertThat(valuesToSet(iterator), equalTo(new HashSet<>(expectedKey1)));
}
final List<String> expectedKey2 = asList("2", "5", "8");
try (KeyValueIterator<Windowed<Bytes>, String> iterator = sessionStore.findSessions(key2, 0L, Long.MAX_VALUE)) {
assertThat(valuesToSet(iterator), equalTo(new HashSet<>(expectedKey2)));
}
final List<String> expectedKey3 = asList("3", "6", "9");
try (KeyValueIterator<Windowed<Bytes>, String> iterator = sessionStore.findSessions(key3, 0L, Long.MAX_VALUE)) {
assertThat(valuesToSet(iterator), equalTo(new HashSet<>(expectedKey3)));
}
sessionStore.close();
}
use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.
the class AbstractSessionBytesStoreTest method shouldBackwardFetchAllSessionsWithSameRecordKey.
@Test
public void shouldBackwardFetchAllSessionsWithSameRecordKey() {
final LinkedList<KeyValue<Windowed<String>, Long>> expected = new LinkedList<>();
expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L));
expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(10, 10)), 2L));
expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(100, 100)), 3L));
expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(1000, 1000)), 4L));
for (final KeyValue<Windowed<String>, Long> kv : expected) {
sessionStore.put(kv.key, kv.value);
}
// add one that shouldn't appear in the results
sessionStore.put(new Windowed<>("aa", new SessionWindow(0, 0)), 5L);
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFetch("a")) {
assertEquals(toList(expected.descendingIterator()), toList(values));
}
}
use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.
the class AbstractSessionBytesStoreTest method shouldRestore.
@SuppressWarnings("unchecked")
@Test
public void shouldRestore() {
final List<KeyValue<Windowed<String>, Long>> expected = Arrays.asList(KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L), KeyValue.pair(new Windowed<>("a", new SessionWindow(10, 10)), 2L), KeyValue.pair(new Windowed<>("a", new SessionWindow(100, 100)), 3L), KeyValue.pair(new Windowed<>("a", new SessionWindow(1000, 1000)), 4L));
for (final KeyValue<Windowed<String>, Long> kv : expected) {
sessionStore.put(kv.key, kv.value);
}
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.fetch("a")) {
assertEquals(expected, toList(values));
}
sessionStore.close();
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.fetch("a")) {
assertEquals(Collections.emptyList(), toList(values));
}
final List<KeyValue<byte[], byte[]>> changeLog = new ArrayList<>();
for (final ProducerRecord<Object, Object> record : recordCollector.collected()) {
changeLog.add(new KeyValue<>(((Bytes) record.key()).get(), (byte[]) record.value()));
}
context.restore(sessionStore.name(), changeLog);
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.fetch("a")) {
assertEquals(expected, toList(values));
}
}
use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.
the class AbstractSessionBytesStoreTest method shouldBackwardFetchAllSessionsWithinKeyRange.
@Test
public void shouldBackwardFetchAllSessionsWithinKeyRange() {
final LinkedList<KeyValue<Windowed<String>, Long>> expected = new LinkedList<>();
expected.add(KeyValue.pair(new Windowed<>("aa", new SessionWindow(10, 10)), 2L));
expected.add(KeyValue.pair(new Windowed<>("aaa", new SessionWindow(100, 100)), 3L));
expected.add(KeyValue.pair(new Windowed<>("aaaa", new SessionWindow(100, 100)), 6L));
expected.add(KeyValue.pair(new Windowed<>("b", new SessionWindow(1000, 1000)), 4L));
expected.add(KeyValue.pair(new Windowed<>("bb", new SessionWindow(1500, 2000)), 5L));
for (final KeyValue<Windowed<String>, Long> kv : expected) {
sessionStore.put(kv.key, kv.value);
}
// add some that should only be fetched in infinite fetch
sessionStore.put(new Windowed<>("a", new SessionWindow(0, 0)), 1L);
sessionStore.put(new Windowed<>("bbb", new SessionWindow(2500, 3000)), 6L);
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFetch("aa", "bb")) {
assertEquals(toList(expected.descendingIterator()), toList(values));
}
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFindSessions("aa", "bb", 0L, Long.MAX_VALUE)) {
assertEquals(toList(expected.descendingIterator()), toList(values));
}
// infinite keyFrom fetch case
expected.add(0, KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L));
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFetch(null, "bb")) {
assertEquals(toList(expected.descendingIterator()), toList(values));
}
// remove the one added for unlimited start fetch case
expected.remove(0);
// infinite keyTo fetch case
expected.add(KeyValue.pair(new Windowed<>("bbb", new SessionWindow(2500, 3000)), 6L));
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFetch("aa", null)) {
assertEquals(toList(expected.descendingIterator()), toList(values));
}
// fetch all case
expected.add(0, KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L));
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFetch(null, null)) {
assertEquals(toList(expected.descendingIterator()), toList(values));
}
}
use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.
the class AbstractSessionBytesStoreTest method shouldFetchAllSessionsWithSameRecordKey.
@Test
public void shouldFetchAllSessionsWithSameRecordKey() {
final LinkedList<KeyValue<Windowed<String>, Long>> expected = new LinkedList<>();
expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L));
expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(10, 10)), 2L));
expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(100, 100)), 3L));
expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(1000, 1000)), 4L));
for (final KeyValue<Windowed<String>, Long> kv : expected) {
sessionStore.put(kv.key, kv.value);
}
// add one that shouldn't appear in the results
sessionStore.put(new Windowed<>("aa", new SessionWindow(0, 0)), 5L);
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.fetch("a")) {
assertEquals(expected, toList(values));
}
}
Aggregations