|
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.math.BigDecimal;
|
5 | 6 | import java.math.BigInteger;
|
@@ -332,33 +333,56 @@ public static String getTypeId(Schema schema) {
|
332 | 333 | * </p>
|
333 | 334 | */
|
334 | 335 | public static String getFullName(Schema schema) {
|
335 |
| - switch (schema.getType()) { |
| 336 | + final Schema.Type type = schema.getType(); |
| 337 | + switch (type) { |
336 | 338 | case RECORD:
|
337 | 339 | case ENUM:
|
338 | 340 | case FIXED:
|
339 |
| - String namespace = schema.getNamespace(); |
340 |
| - String name = schema.getName(); |
| 341 | + final String namespace = schema.getNamespace(); |
| 342 | + final String name = schema.getName(); |
| 343 | + |
| 344 | + // Handle (presumed) common case |
341 | 345 | if (namespace == null) {
|
342 |
| - return schema.getName(); |
| 346 | + return name; |
343 | 347 | }
|
344 |
| - if (namespace.endsWith("$")) { |
| 348 | + final int len = namespace.length(); |
| 349 | + if (namespace.charAt(len-1) == '$') { |
345 | 350 | return namespace + name;
|
346 | 351 | }
|
347 |
| - StringBuilder sb = new StringBuilder(1 + namespace.length() + name.length()); |
348 |
| - // 28-Feb-2020: [dataformats-binary#195] somewhat complicated logic of trying |
349 |
| - // to support differences between avro-lib 1.8 and 1.9... |
350 |
| - // Check if this is a nested class |
351 |
| - String nestedClassName = sb.append(namespace).append('$').append(name).toString(); |
352 |
| - try { |
353 |
| - Class.forName(nestedClassName); |
354 |
| - return nestedClassName; |
355 |
| - } catch (ClassNotFoundException e) { |
356 |
| - // Could not find a nested class, must be a regular class |
357 |
| - sb.setLength(0); |
358 |
| - return sb.append(namespace).append('.').append(name).toString(); |
359 |
| - } |
360 |
| - default: |
361 |
| - return schema.getType().getName(); |
| 352 | + return _findFullName(namespace, name); |
| 353 | + |
| 354 | + default: |
| 355 | + return type.getName(); |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + private static String _findFullName(final String namespace, final String name) { |
| 360 | + final String cacheKey = namespace + "." + name; |
| 361 | + String schemaName = SCHEMA_NAME_CACHE.get(cacheKey); |
| 362 | + |
| 363 | + if (schemaName == null) { |
| 364 | + schemaName = _resolveFullName(namespace, name); |
| 365 | + SCHEMA_NAME_CACHE.put(cacheKey, schemaName); |
| 366 | + } |
| 367 | + return schemaName; |
| 368 | + } |
| 369 | + |
| 370 | + private static String _resolveFullName(final String namespace, final String name) { |
| 371 | + StringBuilder sb = new StringBuilder(1 + namespace.length() + name.length()); |
| 372 | + |
| 373 | + // 28-Feb-2020: [dataformats-binary#195] somewhat complicated logic of trying |
| 374 | + // to support differences between avro-lib 1.8 and 1.9... |
| 375 | + // Check if this is a nested class |
| 376 | + final String nestedClassName = sb.append(namespace).append('$').append(name).toString(); |
| 377 | + try { |
| 378 | + Class.forName(nestedClassName); |
| 379 | + |
| 380 | + return nestedClassName; |
| 381 | + } catch (ClassNotFoundException e) { |
| 382 | + // Could not find a nested class, must be a regular class |
| 383 | + sb.setLength(0); |
| 384 | + |
| 385 | + return sb.append(namespace).append('.').append(name).toString(); |
362 | 386 | }
|
363 | 387 | }
|
364 | 388 |
|
@@ -414,4 +438,7 @@ public static JsonNode parseDefaultValue(String defaultValue) throws JsonMapping
|
414 | 438 | throw new JsonMappingException(null, "Failed to parse default value", e);
|
415 | 439 | }
|
416 | 440 | }
|
| 441 | + |
| 442 | + // @since 2.11.3 |
| 443 | + private static final LRUMap<String, String> SCHEMA_NAME_CACHE = new LRUMap<>(80, 800); |
417 | 444 | }
|
0 commit comments