|
1 | 1 | package com.fasterxml.jackson.dataformat.avro.schema;
|
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.databind.util.LRUMap; |
3 | 4 | import java.io.File;
|
4 | 5 | import java.io.IOException;
|
5 | 6 | import java.math.BigDecimal;
|
@@ -320,33 +321,56 @@ public static String getTypeId(Schema schema) {
|
320 | 321 | * </p>
|
321 | 322 | */
|
322 | 323 | public static String getFullName(Schema schema) {
|
323 |
| - switch (schema.getType()) { |
| 324 | + final Schema.Type type = schema.getType(); |
| 325 | + switch (type) { |
324 | 326 | case RECORD:
|
325 | 327 | case ENUM:
|
326 | 328 | case FIXED:
|
327 |
| - String namespace = schema.getNamespace(); |
328 |
| - String name = schema.getName(); |
| 329 | + final String namespace = schema.getNamespace(); |
| 330 | + final String name = schema.getName(); |
| 331 | + |
| 332 | + // Handle (presumed) common case |
329 | 333 | if (namespace == null) {
|
330 |
| - return schema.getName(); |
| 334 | + return name; |
331 | 335 | }
|
332 |
| - if (namespace.endsWith("$")) { |
| 336 | + final int len = namespace.length(); |
| 337 | + if (namespace.charAt(len-1) == '$') { |
333 | 338 | return namespace + name;
|
334 | 339 | }
|
335 |
| - StringBuilder sb = new StringBuilder(1 + namespace.length() + name.length()); |
336 |
| - // 28-Feb-2020: [dataformats-binary#195] somewhat complicated logic of trying |
337 |
| - // to support differences between avro-lib 1.8 and 1.9... |
338 |
| - // Check if this is a nested class |
339 |
| - String nestedClassName = sb.append(namespace).append('$').append(name).toString(); |
340 |
| - try { |
341 |
| - Class.forName(nestedClassName); |
342 |
| - return nestedClassName; |
343 |
| - } catch (ClassNotFoundException e) { |
344 |
| - // Could not find a nested class, must be a regular class |
345 |
| - sb.setLength(0); |
346 |
| - return sb.append(namespace).append('.').append(name).toString(); |
347 |
| - } |
348 |
| - default: |
349 |
| - return schema.getType().getName(); |
| 340 | + return _findFullName(namespace, name); |
| 341 | + |
| 342 | + default: |
| 343 | + return type.getName(); |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + private static String _findFullName(final String namespace, final String name) { |
| 348 | + final String cacheKey = namespace + "." + name; |
| 349 | + String schemaName = SCHEMA_NAME_CACHE.get(cacheKey); |
| 350 | + |
| 351 | + if (schemaName == null) { |
| 352 | + schemaName = _resolveFullName(namespace, name); |
| 353 | + SCHEMA_NAME_CACHE.put(cacheKey, schemaName); |
| 354 | + } |
| 355 | + return schemaName; |
| 356 | + } |
| 357 | + |
| 358 | + private static String _resolveFullName(final String namespace, final String name) { |
| 359 | + StringBuilder sb = new StringBuilder(1 + namespace.length() + name.length()); |
| 360 | + |
| 361 | + // 28-Feb-2020: [dataformats-binary#195] somewhat complicated logic of trying |
| 362 | + // to support differences between avro-lib 1.8 and 1.9... |
| 363 | + // Check if this is a nested class |
| 364 | + final String nestedClassName = sb.append(namespace).append('$').append(name).toString(); |
| 365 | + try { |
| 366 | + Class.forName(nestedClassName); |
| 367 | + |
| 368 | + return nestedClassName; |
| 369 | + } catch (ClassNotFoundException e) { |
| 370 | + // Could not find a nested class, must be a regular class |
| 371 | + sb.setLength(0); |
| 372 | + |
| 373 | + return sb.append(namespace).append('.').append(name).toString(); |
350 | 374 | }
|
351 | 375 | }
|
352 | 376 |
|
@@ -391,4 +415,7 @@ public static JsonNode parseDefaultValue(String defaultValue) throws JsonMapping
|
391 | 415 | throw new JsonMappingException(null, "Failed to parse default value", e);
|
392 | 416 | }
|
393 | 417 | }
|
| 418 | + |
| 419 | + // @since 2.11.3 |
| 420 | + private static final LRUMap<String, String> SCHEMA_NAME_CACHE = new LRUMap<>(80, 800); |
394 | 421 | }
|
0 commit comments