Skip to content

MINOR: Simplify PartitionStates update logic by removing unnecessary grouping #19442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,28 +129,14 @@ public int size() {
*/
public void set(Map<TopicPartition, S> partitionToState) {
map.clear();
update(partitionToState);
map.putAll(partitionToState);
updateSize();
}

private void updateSize() {
size = map.size();
}

private void update(Map<TopicPartition, S> partitionToState) {
LinkedHashMap<String, List<TopicPartition>> topicToPartitions = new LinkedHashMap<>();
for (TopicPartition tp : partitionToState.keySet()) {
List<TopicPartition> partitions = topicToPartitions.computeIfAbsent(tp.topic(), k -> new ArrayList<>());
partitions.add(tp);
}
for (Map.Entry<String, List<TopicPartition>> entry : topicToPartitions.entrySet()) {
for (TopicPartition tp : entry.getValue()) {
S state = partitionToState.get(tp);
map.put(tp, state);
}
}
}

public static class PartitionState<S> {
private final TopicPartition topicPartition;
private final S value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PartitionStatesTest {

Expand Down Expand Up @@ -166,7 +167,8 @@ public void testPartitionValues() {
expected.add("blah 1");
expected.add("baz 2");
expected.add("baz 3");
assertEquals(expected, states.partitionStateValues());
assertEquals(states.partitionStateValues().size(), expected.size());
assertTrue(states.partitionStateValues().containsAll(expected));
}

@Test
Expand Down