Skip to content
Merged
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 @@ -32,13 +32,34 @@ public TopicIdPartition(Uuid topicId, TopicPartition topicPartition) {
this.topicPartition = Objects.requireNonNull(topicPartition, "topicPartition can not be null");
}

public TopicIdPartition(String topic, Uuid topicId, int partition) {
this.topicId = Objects.requireNonNull(topicId, "topicId can not be null");
this.topicPartition = new TopicPartition(
Objects.requireNonNull(topic, "topic can not be null"),
partition);
}

/**
* @return Universally unique id representing this topic partition.
*/
public Uuid topicId() {
return topicId;
}

/**
* @return the topic name.
*/
public String topic() {
return topicPartition.topic();
}

/**
* @return the partition id.
*/
public int partition() {
return topicPartition.partition();
}

/**
* @return Topic partition representing this instance.
*/
Expand All @@ -55,20 +76,20 @@ public boolean equals(Object o) {
return false;
}
TopicIdPartition that = (TopicIdPartition) o;
return Objects.equals(topicId, that.topicId) &&
Objects.equals(topicPartition, that.topicPartition);
return topicId.equals(that.topicId) &&
topicPartition.equals(that.topicPartition);
}

@Override
public int hashCode() {
return Objects.hash(topicId, topicPartition);
final int prime = 31;
int result = prime + topicId.hashCode();
result = prime * result + topicPartition.hashCode();
return result;
}

@Override
public String toString() {
return "TopicIdPartition{" +
"topicId=" + topicId +
", topicPartition=" + topicPartition +
'}';
return topicId + ":" + topic() + "-" + partition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public int hashCode() {
if (hash != 0)
return hash;
final int prime = 31;
int result = 1;
result = prime * result + partition;
int result = prime + partition;
result = prime * result + Objects.hashCode(topic);
this.hash = result;
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kafka.common;

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

import java.util.Objects;
import org.junit.jupiter.api.Test;

class TopicIdPartitionTest {

private final Uuid topicId0 = new Uuid(-4883993789924556279L, -5960309683534398572L);
private final String topicName0 = "a_topic_name";
private final int partition1 = 1;
private final TopicPartition topicPartition0 = new TopicPartition(topicName0, partition1);
private final TopicIdPartition topicIdPartition0 = new TopicIdPartition(topicId0, topicPartition0);
private final TopicIdPartition topicIdPartition1 = new TopicIdPartition(topicName0, topicId0,
partition1);

private final Uuid topicId1 = new Uuid(7759286116672424028L, -5081215629859775948L);
private final String topicName1 = "another_topic_name";
private final TopicIdPartition topicIdPartition2 = new TopicIdPartition(topicName1, topicId1,
partition1);

@Test
public void testEquals() {
assertEquals(topicIdPartition0, topicIdPartition1);
assertEquals(topicIdPartition1, topicIdPartition0);

assertNotEquals(topicIdPartition0, topicIdPartition2);
assertNotEquals(topicIdPartition2, topicIdPartition0);
}

@Test
public void testHashCode() {
assertEquals(Objects.hash(topicIdPartition0.topicId(), topicIdPartition0.topicPartition()),
topicIdPartition0.hashCode());
assertEquals(topicIdPartition0.hashCode(), topicIdPartition1.hashCode());
assertNotEquals(topicIdPartition0.hashCode(), topicIdPartition2.hashCode());
}

@Test
public void testToString() {
assertEquals("vDiRhkpVQgmtSLnsAZx7lA:a_topic_name-1", topicIdPartition0.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private byte[] toBytes(TopicIdPartition topicIdPartition) {
// We do not want to depend upon hash code generation of Uuid as that may change.
int hash = Objects.hash(topicIdPartition.topicId().getLeastSignificantBits(),
topicIdPartition.topicId().getMostSignificantBits(),
topicIdPartition.topicPartition().partition());
topicIdPartition.partition());

return toBytes(hash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ private RemoteLogSegmentMetadataRecord.RemoteLogSegmentIdEntry createRemoteLogSe
.setTopicIdPartition(
new RemoteLogSegmentMetadataRecord.TopicIdPartitionEntry()
.setId(data.remoteLogSegmentId().topicIdPartition().topicId())
.setName(data.remoteLogSegmentId().topicIdPartition().topicPartition().topic())
.setPartition(data.remoteLogSegmentId().topicIdPartition().topicPartition().partition()))
.setName(data.remoteLogSegmentId().topicIdPartition().topic())
.setPartition(data.remoteLogSegmentId().topicIdPartition().partition()))
.setId(data.remoteLogSegmentId().id());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ private RemoteLogSegmentMetadataUpdateRecord.RemoteLogSegmentIdEntry createRemot
.setId(data.remoteLogSegmentId().id())
.setTopicIdPartition(
new RemoteLogSegmentMetadataUpdateRecord.TopicIdPartitionEntry()
.setName(data.remoteLogSegmentId().topicIdPartition().topicPartition().topic())
.setPartition(data.remoteLogSegmentId().topicIdPartition().topicPartition().partition())
.setName(data.remoteLogSegmentId().topicIdPartition().topic())
.setPartition(data.remoteLogSegmentId().topicIdPartition().partition())
.setId(data.remoteLogSegmentId().topicIdPartition().topicId()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public ApiMessageAndVersion toApiMessageAndVersion(RemotePartitionDeleteMetadata

private RemotePartitionDeleteMetadataRecord.TopicIdPartitionEntry createTopicIdPartitionEntry(TopicIdPartition topicIdPartition) {
return new RemotePartitionDeleteMetadataRecord.TopicIdPartitionEntry()
.setName(topicIdPartition.topicPartition().topic())
.setPartition(topicIdPartition.topicPartition().partition())
.setName(topicIdPartition.topic())
.setPartition(topicIdPartition.partition())
.setId(topicIdPartition.topicId());
}

Expand Down