Skip to content

Commit f042ba2

Browse files
BewareMyPowerlhotari
authored andcommitted
[improve][broker] Improve the performance of TopicName constructor (#24463)
(cherry picked from commit 3130a93)
1 parent f7e790b commit f042ba2

1 file changed

Lines changed: 32 additions & 31 deletions

File tree

  • pulsar-common/src/main/java/org/apache/pulsar/common/naming

pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -114,56 +114,57 @@ private TopicName(String completeTopicName) {
114114
try {
115115
// The topic name can be in two different forms, one is fully qualified topic name,
116116
// the other one is short topic name
117-
if (!completeTopicName.contains("://")) {
117+
int index = completeTopicName.indexOf("://");
118+
if (index < 0) {
118119
// The short topic name can be:
119120
// - <topic>
120121
// - <tenant>/<namespace>/<topic>
121-
String[] parts = StringUtils.split(completeTopicName, '/');
122-
if (parts.length == 3) {
123-
completeTopicName = TopicDomain.persistent.name() + "://" + completeTopicName;
124-
} else if (parts.length == 1) {
125-
completeTopicName = TopicDomain.persistent.name() + "://"
126-
+ PUBLIC_TENANT + "/" + DEFAULT_NAMESPACE + "/" + parts[0];
122+
List<String> parts = splitBySlash(completeTopicName, 0);
123+
this.domain = TopicDomain.persistent;
124+
if (parts.size() == 3) {
125+
this.tenant = parts.get(0);
126+
this.namespacePortion = parts.get(1);
127+
this.localName = parts.get(2);
128+
} else if (parts.size() == 1) {
129+
this.tenant = PUBLIC_TENANT;
130+
this.namespacePortion = DEFAULT_NAMESPACE;
131+
this.localName = parts.get(0);
127132
} else {
128133
throw new IllegalArgumentException(
129134
"Invalid short topic name '" + completeTopicName + "', it should be in the format of "
130135
+ "<tenant>/<namespace>/<topic> or <topic>");
131136
}
132-
}
133-
134-
// Expected format: persistent://tenant/namespace/topic
135-
List<String> parts = Splitter.on("://").limit(2).splitToList(completeTopicName);
136-
this.domain = TopicDomain.getEnum(parts.get(0));
137-
138-
String rest = parts.get(1);
139-
140-
// Expected format: tenant/namespace/<localName>
141-
parts = Splitter.on("/").limit(4).splitToList(rest);
142-
if (parts.size() == 4) {
143-
throw new IllegalArgumentException(
144-
"V1 topic names (with cluster component) are no longer supported. "
145-
+ "Please use the V2 format: '<domain>://tenant/namespace/topic'. Got: "
146-
+ completeTopicName);
147-
} else if (parts.size() == 3) {
148-
this.tenant = parts.get(0);
149-
this.namespacePortion = parts.get(1);
150-
this.localName = parts.get(2);
151-
this.partitionIndex = getPartitionIndex(completeTopicName);
152-
this.namespaceName = NamespaceName.get(tenant, namespacePortion);
137+
this.completeTopicName = domain.name() + "://" + tenant + "/" + namespacePortion + "/" + localName;
153138
} else {
154-
throw new IllegalArgumentException("Invalid topic name: " + completeTopicName);
139+
// Expected format: persistent://tenant/namespace/topic
140+
List<String> parts = splitBySlash(completeTopicName.substring(index + "://".length()), 4);
141+
if (parts.size() == 4) {
142+
throw new IllegalArgumentException(
143+
"V1 topic names (with cluster component) are no longer supported. "
144+
+ "Please use the V2 format: '<domain>://tenant/namespace/topic'. Got: "
145+
+ completeTopicName);
146+
}
147+
if (parts.size() == 3) {
148+
this.tenant = parts.get(0);
149+
this.namespacePortion = parts.get(1);
150+
this.localName = parts.get(2);
151+
} else {
152+
throw new IllegalArgumentException("Invalid topic name " + completeTopicName);
153+
}
154+
this.completeTopicName = completeTopicName;
155+
this.domain = TopicDomain.getEnum(completeTopicName.substring(0, index));
155156
}
156157

157158
if (StringUtils.isBlank(localName)) {
158159
throw new IllegalArgumentException(String.format("Invalid topic name: %s. Topic local name must not"
159160
+ " be blank.", completeTopicName));
160161
}
161162

163+
this.partitionIndex = getPartitionIndex(localName);
164+
this.namespaceName = NamespaceName.get(tenant, namespacePortion);
162165
} catch (NullPointerException e) {
163166
throw new IllegalArgumentException("Invalid topic name: " + completeTopicName, e);
164167
}
165-
this.completeTopicName = String.format("%s://%s/%s/%s",
166-
domain, tenant, namespacePortion, localName);
167168
}
168169

169170
public boolean isPersistent() {

0 commit comments

Comments
 (0)