You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by GitBox <gi...@apache.org> on 2022/06/16 13:55:18 UTC

[GitHub] [avro] clesaec commented on a diff in pull request #1688: AVRO-3374: special cases for qualified name

clesaec commented on code in PR #1688:
URL: https://github.com/apache/avro/pull/1688#discussion_r899110660


##########
lang/java/avro/src/main/java/org/apache/avro/Schema.java:
##########
@@ -751,8 +751,32 @@ public void writeName(Names names, JsonGenerator gen) throws IOException {
     }
 
     public String getQualified(String defaultSpace) {
-      return (space == null || space.equals(defaultSpace)) ? name : full;
+      return this.shouldWriteFull(defaultSpace) ? full : name;
     }
+
+    /**
+     * Determine if full name must be written. There are 2 cases for true :
+     * defaultSpace != from this.space or name is already a Schema.Type (int, array
+     * ...)
+     *
+     * @param defaultSpace : default name space.
+     * @return true if full name must be written.
+     */
+    private boolean shouldWriteFull(String defaultSpace) {
+      if (space != null && space.equals(defaultSpace)) {
+        try {
+          // name is a 'Type', so namespace must be written (int should return true, Int
+          // should return false).
+          return Type.valueOf(this.name.toUpperCase(Locale.ENGLISH)).name.equals(this.name);
+        } catch (IllegalArgumentException ex) {
+          // namespace can be omitted, (default & name is not a type)
+          return false;
+        }

Review Comment:
   Well, Type.valueOf use an internal hashmap (see Class.enumConstantDirectory method), that will have a direct access to Type values ...
   And, as reminded [here](https://medium.com/javarevisited/micro-optimizations-in-java-good-nice-and-slow-enum-261e6f77bd2e), Type.values() method will build a clone of all values.
   So, the code i proposed will be faster when exist (true return without exception), but i don't know for exception creation vs Type.values() with clone.
   Tell me what you think, i can change code.



-- 
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: issues-unsubscribe@avro.apache.org

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