Skip to content

Commit d34d5dc

Browse files
MySQL O(1) collation lookup table (#1574)
* O(1) collation lookup table * Safeguard idToJavaCharsetMapping initialization Signed-off-by: Thomas Segismont <tsegismont@gmail.com> --------- Signed-off-by: Thomas Segismont <tsegismont@gmail.com> Co-authored-by: Francesco Nigro <nigro.fra@gmail.com>
1 parent 978cd52 commit d34d5dc

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLCollation.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
package io.vertx.mysqlclient.impl;
1313

14-
import io.netty.util.collection.IntObjectHashMap;
15-
import io.netty.util.collection.IntObjectMap;
1614
import io.vertx.core.internal.logging.Logger;
1715
import io.vertx.core.internal.logging.LoggerFactory;
1816

1917
import java.nio.charset.Charset;
2018
import java.nio.charset.StandardCharsets;
21-
import java.util.*;
19+
import java.util.Arrays;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
2223
import java.util.stream.Collectors;
2324

2425
/**
@@ -255,7 +256,7 @@ public enum MySQLCollation {
255256
public static final List<String> SUPPORTED_CHARSET_NAMES = Arrays.stream(values()).map(MySQLCollation::mysqlCharsetName).distinct().collect(Collectors.toList());
256257

257258
private static final Map<String, String> charsetToDefaultCollationMapping = new HashMap<>();
258-
private static final IntObjectMap<Charset> idToJavaCharsetMapping = new IntObjectHashMap<>();
259+
private static final Charset[] idToJavaCharsetMapping = new Charset[256];
259260

260261
static {
261262
charsetToDefaultCollationMapping.put("big5", "big5_chinese_ci");
@@ -303,11 +304,21 @@ public enum MySQLCollation {
303304
for (MySQLCollation collation : MySQLCollation.values()) {
304305
try {
305306
Charset charset = Charset.forName(collation.mappedJavaCharsetName);
306-
idToJavaCharsetMapping.put(collation.collationId, charset);
307+
if (idToJavaCharsetMapping[collation.collationId] != null) {
308+
// This shouldn't happen unless the enum values are modified and the collation id gets mistaken
309+
throw new IllegalArgumentException("Duplicate collation id: " + collation.collationId);
310+
}
311+
idToJavaCharsetMapping[collation.collationId] = charset;
307312
} catch (Exception e) {
308313
LOGGER.warn(String.format("Java charset: [%s] is not supported by this platform, data with collation[%s] will be decoded in UTF-8 instead.", collation.mysqlCharsetName, collation.name()));
309314
}
310315
}
316+
// set the remaining missing ones to the default charset
317+
for (int i = 0; i < idToJavaCharsetMapping.length; i++) {
318+
if (idToJavaCharsetMapping[i] == null) {
319+
idToJavaCharsetMapping[i] = StandardCharsets.UTF_8;
320+
}
321+
}
311322
}
312323

313324
public static final MySQLCollation DEFAULT_COLLATION = utf8mb4_general_ci;
@@ -337,12 +348,10 @@ public static MySQLCollation valueOfName(String collationName) throws IllegalArg
337348
* @return the charset
338349
*/
339350
public static Charset getJavaCharsetByCollationId(int collationId) {
340-
Charset charset = idToJavaCharsetMapping.get(collationId);
341-
if (charset == null) {
351+
if (collationId >= idToJavaCharsetMapping.length) {
342352
return StandardCharsets.UTF_8;
343-
} else {
344-
return charset;
345353
}
354+
return idToJavaCharsetMapping[collationId];
346355
}
347356

348357
public static String getDefaultCollationFromCharsetName(String charset) {

0 commit comments

Comments
 (0)