Skip to content

Commit

Permalink
resolve comments, Improve performance by indexing potentially collidi…
Browse files Browse the repository at this point in the history
…ng topics
  • Loading branch information
dengziming committed Apr 2, 2022
1 parent 885365d commit adfd2c0
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public static boolean hasCollisionChars(String topic) {
return topic.contains("_") || topic.contains(".");
}

/**
* Unify topic name with a period ('.') or underscore ('_'), this is only used to check collision and will not
* be used to really change topic name.
*
* @param topic A topic to unify
* @return A unified topic name
*/
public static String unifyCollisionChars(String topic) {
return topic.replace('.', '_');
}

/**
* Returns true if the topicNames collide due to a period ('.') or underscore ('_') in the same position.
*
Expand All @@ -75,7 +86,7 @@ public static boolean hasCollisionChars(String topic) {
* @return true if the topics collide
*/
public static boolean hasCollision(String topicA, String topicB) {
return topicA.replace('.', '_').equals(topicB.replace('.', '_'));
return unifyCollisionChars(topicA).equals(unifyCollisionChars(topicB));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -81,6 +82,14 @@ public void testTopicHasCollisionChars() {
assertTrue(Topic.hasCollisionChars(topic));
}

@Test
public void testUnifyCollisionChars() {
assertEquals("topic", Topic.unifyCollisionChars("topic"));
assertEquals("_topic", Topic.unifyCollisionChars(".topic"));
assertEquals("_topic", Topic.unifyCollisionChars("_topic"));
assertEquals("__topic", Topic.unifyCollisionChars("_.topic"));
}

@Test
public void testTopicHasCollision() {
List<String> periodFirstMiddleLastNone = Arrays.asList(".topic", "to.pic", "topic.", "topic");
Expand Down

0 comments on commit adfd2c0

Please sign in to comment.