Skip to content

Commit e94a028

Browse files
committed
O(1) collation lookup table
1 parent acb02de commit e94a028

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public enum MySQLCollation {
255255
public static final List<String> SUPPORTED_CHARSET_NAMES = Arrays.stream(values()).map(MySQLCollation::mysqlCharsetName).distinct().collect(Collectors.toList());
256256

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

260260
static {
261261
charsetToDefaultCollationMapping.put("big5", "big5_chinese_ci");
@@ -303,11 +303,17 @@ public enum MySQLCollation {
303303
for (MySQLCollation collation : MySQLCollation.values()) {
304304
try {
305305
Charset charset = Charset.forName(collation.mappedJavaCharsetName);
306-
idToJavaCharsetMapping.put(collation.collationId, charset);
306+
idToJavaCharsetMapping[collation.collationId] = charset;
307307
} catch (Exception e) {
308308
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()));
309309
}
310310
}
311+
// set the remaining missing ones to the default charset
312+
for (int i = 0; i < idToJavaCharsetMapping.length; i++) {
313+
if (idToJavaCharsetMapping[i] == null) {
314+
idToJavaCharsetMapping[i] = StandardCharsets.UTF_8;
315+
}
316+
}
311317
}
312318

313319
public static final MySQLCollation DEFAULT_COLLATION = utf8mb4_general_ci;
@@ -337,12 +343,10 @@ public static MySQLCollation valueOfName(String collationName) throws IllegalArg
337343
* @return the charset
338344
*/
339345
public static Charset getJavaCharsetByCollationId(int collationId) {
340-
Charset charset = idToJavaCharsetMapping.get(collationId);
341-
if (charset == null) {
346+
if (collationId >= idToJavaCharsetMapping.length) {
342347
return StandardCharsets.UTF_8;
343-
} else {
344-
return charset;
345348
}
349+
return idToJavaCharsetMapping[collationId];
346350
}
347351

348352
public static String getDefaultCollationFromCharsetName(String charset) {

0 commit comments

Comments
 (0)