You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by hy...@apache.org on 2020/05/24 20:48:43 UTC
[calcite] branch master updated: [CALCITE-3999] Simplify
DialectPool implementation using Guava cache
This is an automated email from the ASF dual-hosted git repository.
hyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/master by this push:
new 258f791 [CALCITE-3999] Simplify DialectPool implementation using Guava cache
258f791 is described below
commit 258f791eff12b4bac0fc11b4025aa5d99dcbbed1
Author: Jesus Camacho Rodriguez <jc...@apache.org>
AuthorDate: Wed May 13 18:27:13 2020 -0700
[CALCITE-3999] Simplify DialectPool implementation using Guava cache
---
.../org/apache/calcite/adapter/jdbc/JdbcUtils.java | 44 ++++++++--------------
1 file changed, 15 insertions(+), 29 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcUtils.java b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcUtils.java
index 0d207e1..b104b73 100644
--- a/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcUtils.java
+++ b/core/src/main/java/org/apache/calcite/adapter/jdbc/JdbcUtils.java
@@ -30,7 +30,6 @@ import org.apache.commons.dbcp2.BasicDataSource;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
-import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import java.sql.Connection;
@@ -41,10 +40,7 @@ import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
-import java.util.HashMap;
-import java.util.IdentityHashMap;
import java.util.List;
-import java.util.Map;
import java.util.TimeZone;
import javax.annotation.Nonnull;
import javax.sql.DataSource;
@@ -59,37 +55,21 @@ final class JdbcUtils {
/** Pool of dialects. */
static class DialectPool {
- final Map<DataSource, Map<SqlDialectFactory, SqlDialect>> map0 = new IdentityHashMap<>();
- final Map<List, SqlDialect> map = new HashMap<>();
-
public static final DialectPool INSTANCE = new DialectPool();
- // TODO: Discuss why we need a pool. If we do, I'd like to improve performance
- synchronized SqlDialect get(SqlDialectFactory dialectFactory, DataSource dataSource) {
- Map<SqlDialectFactory, SqlDialect> dialectMap = map0.get(dataSource);
- if (dialectMap != null) {
- final SqlDialect sqlDialect = dialectMap.get(dialectFactory);
- if (sqlDialect != null) {
- return sqlDialect;
- }
- }
+ private final LoadingCache<Pair<SqlDialectFactory, DataSource>, SqlDialect> cache =
+ CacheBuilder.newBuilder().softValues()
+ .build(CacheLoader.from(DialectPool::dialect));
+
+ private static @Nonnull SqlDialect dialect(
+ @Nonnull Pair<SqlDialectFactory, DataSource> key) {
+ SqlDialectFactory dialectFactory = key.left;
+ DataSource dataSource = key.right;
Connection connection = null;
try {
connection = dataSource.getConnection();
DatabaseMetaData metaData = connection.getMetaData();
- String productName = metaData.getDatabaseProductName();
- String productVersion = metaData.getDatabaseProductVersion();
- List key = ImmutableList.of(productName, productVersion, dialectFactory);
- SqlDialect dialect = map.get(key);
- if (dialect == null) {
- dialect = dialectFactory.create(metaData);
- map.put(key, dialect);
- if (dialectMap == null) {
- dialectMap = new IdentityHashMap<>();
- map0.put(dataSource, dialectMap);
- }
- dialectMap.put(dialectFactory, dialect);
- }
+ SqlDialect dialect = dialectFactory.create(metaData);
connection.close();
connection = null;
return dialect;
@@ -105,6 +85,12 @@ final class JdbcUtils {
}
}
}
+
+ public SqlDialect get(SqlDialectFactory dialectFactory, DataSource dataSource) {
+ final Pair<SqlDialectFactory, DataSource> key =
+ Pair.of(dialectFactory, dataSource);
+ return cache.getUnchecked(key);
+ }
}
/** Builder that calls {@link ResultSet#getObject(int)} for every column,