Search in sources :

Example 1 with MappedIterator

use of org.apache.kafka.common.utils.MappedIterator in project kafka by apache.

the class StopReplicaRequest method topicStates.

/**
 * Note that this method has allocation overhead per iterated element, so callers should copy the result into
 * another collection if they need to iterate more than once.
 *
 * Implementation note: we should strive to avoid allocation overhead per element, see
 * `UpdateMetadataRequest.partitionStates()` for the preferred approach. That's not possible in this case and
 * StopReplicaRequest should be relatively rare in comparison to other request types.
 */
public Iterable<StopReplicaTopicState> topicStates() {
    if (version() < 1) {
        Map<String, StopReplicaTopicState> topicStates = new HashMap<>();
        for (StopReplicaPartitionV0 partition : data.ungroupedPartitions()) {
            StopReplicaTopicState topicState = topicStates.computeIfAbsent(partition.topicName(), topic -> new StopReplicaTopicState().setTopicName(topic));
            topicState.partitionStates().add(new StopReplicaPartitionState().setPartitionIndex(partition.partitionIndex()).setDeletePartition(data.deletePartitions()));
        }
        return topicStates.values();
    } else if (version() < 3) {
        return () -> new MappedIterator<>(data.topics().iterator(), topic -> new StopReplicaTopicState().setTopicName(topic.name()).setPartitionStates(topic.partitionIndexes().stream().map(partition -> new StopReplicaPartitionState().setPartitionIndex(partition).setDeletePartition(data.deletePartitions())).collect(Collectors.toList())));
    } else {
        return data.topicStates();
    }
}
Also used : StopReplicaPartitionV0(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionV0) Utils(org.apache.kafka.common.utils.Utils) TopicPartition(org.apache.kafka.common.TopicPartition) StopReplicaPartitionV0(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionV0) StopReplicaPartitionError(org.apache.kafka.common.message.StopReplicaResponseData.StopReplicaPartitionError) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) HashMap(java.util.HashMap) StopReplicaTopicV1(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicV1) MappedIterator(org.apache.kafka.common.utils.MappedIterator) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Errors(org.apache.kafka.common.protocol.Errors) StopReplicaTopicState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState) StopReplicaResponseData(org.apache.kafka.common.message.StopReplicaResponseData) StopReplicaRequestData(org.apache.kafka.common.message.StopReplicaRequestData) StopReplicaPartitionState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState) StopReplicaTopicState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState) HashMap(java.util.HashMap) StopReplicaPartitionState(org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 StopReplicaRequestData (org.apache.kafka.common.message.StopReplicaRequestData)1 StopReplicaPartitionState (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionState)1 StopReplicaPartitionV0 (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaPartitionV0)1 StopReplicaTopicState (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicState)1 StopReplicaTopicV1 (org.apache.kafka.common.message.StopReplicaRequestData.StopReplicaTopicV1)1 StopReplicaResponseData (org.apache.kafka.common.message.StopReplicaResponseData)1 StopReplicaPartitionError (org.apache.kafka.common.message.StopReplicaResponseData.StopReplicaPartitionError)1 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)1 ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)1 Errors (org.apache.kafka.common.protocol.Errors)1 MappedIterator (org.apache.kafka.common.utils.MappedIterator)1 Utils (org.apache.kafka.common.utils.Utils)1