You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/09/20 19:33:22 UTC

[GitHub] [kafka] cmccabe commented on a change in pull request #11311: KAFKA-13280: Avoid O(N) behavior in KRaftMetadataCache#topicNamesToIds

cmccabe commented on a change in pull request #11311:
URL: https://github.com/apache/kafka/pull/11311#discussion_r712456110



##########
File path: metadata/src/main/java/org/apache/kafka/image/TopicsImage.java
##########
@@ -92,6 +98,148 @@ public int hashCode() {
         return Objects.hash(topicsById, topicsByName);
     }
 
+    /**
+     * Expose a view of this TopicsImage as a map from topic names to IDs.
+     *
+     * Like TopicsImage itself, this map is immutable.
+     */
+    public Map<String, Uuid> topicNameToIdView() {
+        return new TopicNameToIdMap();
+    }
+
+    class TopicNameToIdMap extends AbstractMap<String, Uuid> {
+        private final TopicNameToIdMapEntrySet set = new TopicNameToIdMapEntrySet();
+
+        @Override
+        public boolean containsKey(Object key) {
+            return topicsByName.containsKey(key);
+        }
+
+        @Override
+        public Uuid get(Object key) {
+            TopicImage image = topicsByName.get(key);
+            if (image == null) return null;
+            return image.id();
+        }
+
+        @Override
+        public Set<Entry<String, Uuid>> entrySet() {
+            return set;
+        }
+    }
+
+    class TopicNameToIdMapEntrySet extends AbstractSet<Entry<String, Uuid>> {
+        @Override
+        public Iterator<Entry<String, Uuid>> iterator() {
+            return new TopicNameToIdMapEntrySetIterator(topicsByName.entrySet().iterator());
+        }
+
+        @SuppressWarnings("rawtypes")
+        @Override
+        public boolean contains(Object o) {
+            if (!(o instanceof Entry)) return false;
+            Entry other = (Entry) o;
+            TopicImage image = topicsByName.get(other.getKey());
+            if (image == null) return false;
+            return image.id().equals(other.getValue());
+        }
+
+        @Override
+        public int size() {
+            return topicsByName.size();
+        }
+    }
+
+    static class TopicNameToIdMapEntrySetIterator implements Iterator<Entry<String, Uuid>> {
+        private final Iterator<Entry<String, TopicImage>> iterator;
+
+        TopicNameToIdMapEntrySetIterator(Iterator<Entry<String, TopicImage>> iterator) {
+            this.iterator = iterator;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return this.iterator.hasNext();
+        }
+
+        @Override
+        public Entry<String, Uuid> next() {
+            Entry<String, TopicImage> entry = iterator.next();
+            return new SimpleImmutableEntry<>(entry.getKey(), entry.getValue().id());

Review comment:
       Allocating a short-lived temporary object is typically almost free, unless the number of objects you allocate overwhelms the eden space in the GC. And people are not supposed to be doing O(num_topics) iterations anyway (if they are, that is a bug)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org