/** * Gets an iterator on the coordinator stream, starting from the starting offset the consumer was registered with. * * @return an iterator on the coordinator stream pointing to the starting offset the consumer was registered with. */ public SystemStreamPartitionIterator getStartIterator() { return new SystemStreamPartitionIterator(systemConsumer, coordinatorSystemStreamPartition); }
/** * Checks whether or not there are any messages after a given iterator on the coordinator stream * * @param iterator The iterator to check if there are any new messages after this point * @return True if there are new messages after the iterator, false otherwise */ public boolean hasNewMessages(SystemStreamPartitionIterator iterator) { if (iterator == null) { return false; } return iterator.hasNext(); }
@Override public boolean hasNext() { refresh(); return peeks.size() > 0; }
@Test public void testHasNextShouldWork() { int numMessages = 10; MockSystemConsumer consumer = new MockSystemConsumer(numMessages); SystemStreamPartitionIterator iterator = new SystemStreamPartitionIterator(consumer, SSP); while (iterator.hasNext()) { assertEquals(--numMessages, iterator.next().getMessage()); } assertFalse(iterator.hasNext()); assertEquals(0, numMessages); }
/** * Returns all the messages from the earliest offset all the way to the latest. */ private void bootstrapMessagesFromStream() { synchronized (bootstrapLock) { while (iterator.hasNext()) { IncomingMessageEnvelope envelope = iterator.next(); byte[] keyAsBytes = (byte[]) envelope.getKey(); Object[] keyArray = keySerde.fromBytes(keyAsBytes).toArray(); CoordinatorStreamMessage coordinatorStreamMessage = new CoordinatorStreamMessage(keyArray, new HashMap<>()); if (Objects.equals(coordinatorStreamMessage.getType(), type)) { if (envelope.getMessage() != null) { bootstrappedMessages.put(keyAsBytes, (byte[]) envelope.getMessage()); } else { bootstrappedMessages.remove(keyAsBytes); } } } } }
new SystemStreamPartitionIterator(systemConsumer, coordinatorSystemStreamPartition); while (iterator.hasNext()) { IncomingMessageEnvelope envelope = iterator.next(); Object[] keyArray = keySerde.fromBytes((byte[]) envelope.getKey()).toArray(); Map<String, Object> valueMap = null;
/** * Returns all the messages from the earliest offset all the way to the latest. */ private void bootstrapMessagesFromStream() { synchronized (bootstrapLock) { while (iterator.hasNext()) { IncomingMessageEnvelope envelope = iterator.next(); byte[] keyAsBytes = (byte[]) envelope.getKey(); Object[] keyArray = keySerde.fromBytes(keyAsBytes).toArray(); CoordinatorStreamMessage coordinatorStreamMessage = new CoordinatorStreamMessage(keyArray, new HashMap<>()); if (Objects.equals(coordinatorStreamMessage.getType(), type)) { if (envelope.getMessage() != null) { bootstrappedMessages.put(keyAsBytes, (byte[]) envelope.getMessage()); } else { bootstrappedMessages.remove(keyAsBytes); } } } } }
@Test public void testNoMessages() { int numMessages = 0; MockSystemConsumer consumer = new MockSystemConsumer(numMessages); SystemStreamPartitionIterator iterator = new SystemStreamPartitionIterator(consumer, SSP); assertFalse(iterator.hasNext()); try { iterator.next(); fail("Expected not to get any more messages from iterator."); } catch (NoSuchElementException e) { // expected } }
/** * Returns all the messages from the earliest offset all the way to the latest. */ private void bootstrapMessagesFromStream() { synchronized (bootstrapLock) { while (iterator.hasNext()) { IncomingMessageEnvelope envelope = iterator.next(); byte[] keyAsBytes = (byte[]) envelope.getKey(); Object[] keyArray = keySerde.fromBytes(keyAsBytes).toArray(); CoordinatorStreamMessage coordinatorStreamMessage = new CoordinatorStreamMessage(keyArray, new HashMap<>()); if (Objects.equals(coordinatorStreamMessage.getType(), type)) { if (envelope.getMessage() != null) { bootstrappedMessages.put(keyAsBytes, (byte[]) envelope.getMessage()); } else { bootstrappedMessages.remove(keyAsBytes); } } } } }
/** * Gets an iterator on the coordinator stream, starting from the starting offset the consumer was registered with. * * @return an iterator on the coordinator stream pointing to the starting offset the consumer was registered with. */ public SystemStreamPartitionIterator getStartIterator() { return new SystemStreamPartitionIterator(systemConsumer, coordinatorSystemStreamPartition); }
/** * Checks whether or not there are any messages after a given iterator on the coordinator stream * * @param iterator The iterator to check if there are any new messages after this point * @return True if there are new messages after the iterator, false otherwise */ public boolean hasNewMessages(SystemStreamPartitionIterator iterator) { if (iterator == null) { return false; } return iterator.hasNext(); }
@Override public boolean hasNext() { refresh(); return peeks.size() > 0; }
new SystemStreamPartitionIterator(systemConsumer, coordinatorSystemStreamPartition); while (iterator.hasNext()) { IncomingMessageEnvelope envelope = iterator.next(); Object[] keyArray = keySerde.fromBytes((byte[]) envelope.getKey()).toArray(); Map<String, Object> valueMap = null;
/** * Returns all the messages from the earliest offset all the way to the latest. */ private void bootstrapMessagesFromStream() { synchronized (bootstrapLock) { while (iterator.hasNext()) { IncomingMessageEnvelope envelope = iterator.next(); byte[] keyAsBytes = (byte[]) envelope.getKey(); Object[] keyArray = keySerde.fromBytes(keyAsBytes).toArray(); CoordinatorStreamMessage coordinatorStreamMessage = new CoordinatorStreamMessage(keyArray, new HashMap<>()); if (Objects.equals(coordinatorStreamMessage.getType(), type)) { if (envelope.getMessage() != null) { bootstrappedMessages.put(keyAsBytes, (byte[]) envelope.getMessage()); } else { bootstrappedMessages.remove(keyAsBytes); } } } } }
/** * Gets an iterator on the coordinator stream, starting from the starting offset the consumer was registered with. * * @return an iterator on the coordinator stream pointing to the starting offset the consumer was registered with. */ public SystemStreamPartitionIterator getStartIterator() { return new SystemStreamPartitionIterator(systemConsumer, coordinatorSystemStreamPartition); }
/** * Checks whether or not there are any messages after a given iterator on the coordinator stream * * @param iterator The iterator to check if there are any new messages after this point * @return True if there are new messages after the iterator, false otherwise */ public boolean hasNewMessages(SystemStreamPartitionIterator iterator) { if (iterator == null) { return false; } return iterator.hasNext(); }
@Override public IncomingMessageEnvelope next() { refresh(); if (peeks.size() == 0) { throw new NoSuchElementException(); } IncomingMessageEnvelope envelope = peeks.poll(); if (envelope.isEndOfStream()) { endOfStreamReached = true; } return envelope; }
new SystemStreamPartitionIterator(systemConsumer, coordinatorSystemStreamPartition); while (iterator.hasNext()) { IncomingMessageEnvelope envelope = iterator.next(); Object[] keyArray = keySerde.fromBytes((byte[]) envelope.getKey()).toArray(); Map<String, Object> valueMap = null;
/** * returns all unread messages of a specific type, after an iterator on the stream * * @param iterator the iterator pointing to an offset in the coordinator stream. All unread messages after this iterator are returned * @param type the type of the messages to be returned * @return a set of unread messages of a given type, after a given iterator */ public Set<CoordinatorStreamMessage> getUnreadMessages(SystemStreamPartitionIterator iterator, String type) { LinkedHashSet<CoordinatorStreamMessage> messages = new LinkedHashSet<CoordinatorStreamMessage>(); while (iterator.hasNext()) { IncomingMessageEnvelope envelope = iterator.next(); Object[] keyArray = keySerde.fromBytes((byte[]) envelope.getKey()).toArray(); Map<String, Object> valueMap = null; if (envelope.getMessage() != null) { valueMap = messageSerde.fromBytes((byte[]) envelope.getMessage()); } CoordinatorStreamMessage coordinatorStreamMessage = new CoordinatorStreamMessage(keyArray, valueMap); if (type == null || type.equals(coordinatorStreamMessage.getType())) { messages.add(coordinatorStreamMessage); } } return messages; }
/** * Gets an iterator on the coordinator stream, starting from the starting offset the consumer was registered with. * * @return an iterator on the coordinator stream pointing to the starting offset the consumer was registered with. */ public SystemStreamPartitionIterator getStartIterator() { return new SystemStreamPartitionIterator(systemConsumer, coordinatorSystemStreamPartition); }