Skip to content

Use StringUtils.replace(…) instead of String.replaceAll(…) for mapKeyDotReplacement #3613

Closed
@chzhong

Description

@chzhong

When configured to use mapKeyDotReplacement, MappingMongoConverter will use String.replaceAll to replace all dots in keys into specified replacement, which might cause unnecessary creation of java.util.regex.Pattern repeatly. This will lead to rapid GC serialize/deserialize a huge (>= 10MB) mongo document multiple times.

Codes in current version:

	/**
	 * Potentially replaces dots in the given map key with the configured map key replacement if configured or aborts
	 * conversion if none is configured.
	 *
	 * @see #setMapKeyDotReplacement(String)
	 * @param source
	 * @return
	 */
	protected String potentiallyEscapeMapKey(String source) {

		if (!source.contains(".")) {
			return source;
		}

		if (mapKeyDotReplacement == null) {
			throw new MappingException(String.format(
					"Map key %s contains dots but no replacement was configured! Make "
							+ "sure map keys don't contain dots in the first place or configure an appropriate replacement!",
					source));
		}

		return source.replaceAll("\\.", mapKeyDotReplacement);
	}


	/**
	 * Translates the map key replacements in the given key just read with a dot in case a map key replacement has been
	 * configured.
	 *
	 * @param source
	 * @return
	 */
	protected String potentiallyUnescapeMapKey(String source) {
		return mapKeyDotReplacement == null ? source : source.replaceAll(mapKeyDotReplacement, "\\.");
	}

Suggestion:
Use org.springframework.util.StringUtils#replace or org.apache.commons.lang3.StringUtils#replace(java.lang.String, java.lang.String, java.lang.String) to replace dots instead.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions