Skip to content

Commit

Permalink
[jOOQ#8334] ParsingConnection should cache frequent input / output SQ…
Browse files Browse the repository at this point in the history
…L string pairs

This includes:
- [jOOQ#11684] Add a CacheProvider SPI to allow for overriding the default ConcurrentHashMap
  • Loading branch information
lukaseder committed Mar 21, 2021
1 parent b0ff984 commit 6206129
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 77 deletions.
36 changes: 36 additions & 0 deletions jOOQ/src/main/java/org/jooq/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.sql.Wrapper;
import java.time.Clock;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import java.util.function.Supplier;
Expand Down Expand Up @@ -357,6 +358,18 @@ public interface Configuration extends Serializable {
@NotNull
ExecutorProvider executorProvider();

/**
* Get this configuration's underlying cache provider.
* <p>
* Cached operations will call this SPI to obtain a thread safe cache
* implementation to cache various things internally. This SPI allows for
* replacing the default cache implementation, which is mostly a
* {@link ConcurrentHashMap}, by a more specialised one, e.g. a LRU cache,
* e.g. from Guava.
*/
@NotNull
CacheProvider cacheProvider();

/**
* Get this configuration's underlying transaction provider.
* <p>
Expand Down Expand Up @@ -629,6 +642,19 @@ public interface Configuration extends Serializable {
@NotNull
Configuration set(ExecutorProvider newExecutorProvider);

/**
* Change this configuration to hold a new cache provider.
* <p>
* This method is not thread-safe and should not be used in globally
* available <code>Configuration</code> objects.
*
* @param newCacheProvider The new cache provider to be contained in the
* changed configuration.
* @return The changed configuration.
*/
@NotNull
Configuration set(CacheProvider newCacheProvider);

/**
* Change this configuration to hold a new executor.
* <p>
Expand Down Expand Up @@ -1339,6 +1365,16 @@ public interface Configuration extends Serializable {
@NotNull
Configuration derive(ExecutorProvider newExecutorProvider);

/**
* Create a derived configuration from this one, with a new cache provider.
*
* @param newCacheProvider The new cache provider to be contained in the
* derived configuration.
* @return The derived configuration.
*/
@NotNull
Configuration derive(CacheProvider newCacheProvider);

/**
* Create a derived configuration from this one, with a new transaction
* provider.
Expand Down
84 changes: 79 additions & 5 deletions jOOQ/src/main/java/org/jooq/impl/DefaultConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.function.Supplier;

import javax.sql.DataSource;

import org.jooq.CacheProvider;
import org.jooq.CharsetProvider;
import org.jooq.CommitProvider;
import org.jooq.Configuration;
Expand Down Expand Up @@ -89,11 +89,8 @@
import org.jooq.conf.Settings;
import org.jooq.conf.SettingsTools;
import org.jooq.exception.ConfigurationException;
import org.jooq.exception.DataAccessException;
import org.jooq.impl.ThreadLocalTransactionProvider.ThreadLocalConnectionProvider;
import org.jooq.impl.Tools.DataCacheKey;
import org.jooq.migrations.xml.jaxb.MigrationsType;
import org.jooq.tools.JooqLogger;

/**
* A default implementation for configurations within a {@link DSLContext}, if no
Expand Down Expand Up @@ -122,6 +119,7 @@ public class DefaultConfiguration extends AbstractConfiguration {
private transient MetaProvider metaProvider;
private transient CommitProvider commitProvider;
private transient ExecutorProvider executorProvider;
private transient CacheProvider cacheProvider;
private transient TransactionProvider transactionProvider;
private transient RecordMapperProvider recordMapperProvider;
private transient RecordUnmapperProvider recordUnmapperProvider;
Expand Down Expand Up @@ -192,6 +190,7 @@ public DefaultConfiguration() {
null,
null,
null,
null,



Expand All @@ -218,6 +217,7 @@ public DefaultConfiguration() {
configuration.metaProvider,
configuration.commitProvider,
configuration.executorProvider,
configuration.cacheProvider,
configuration.transactionProvider,
configuration.recordMapperProvider,
configuration.recordUnmapperProvider,
Expand Down Expand Up @@ -255,6 +255,7 @@ public DefaultConfiguration() {
MetaProvider metaProvider,
CommitProvider commitProvider,
ExecutorProvider executorProvider,
CacheProvider cacheProvider,
TransactionProvider transactionProvider,
RecordMapperProvider recordMapperProvider,
RecordUnmapperProvider recordUnmapperProvider,
Expand All @@ -281,6 +282,7 @@ public DefaultConfiguration() {
set(metaProvider);
set(commitProvider);
set(executorProvider);
set(cacheProvider);
set(transactionProvider);
set(recordMapperProvider);
set(recordUnmapperProvider);
Expand Down Expand Up @@ -342,6 +344,7 @@ public final Configuration derive(ConnectionProvider newConnectionProvider) {
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -373,6 +376,7 @@ public final Configuration derive(MetaProvider newMetaProvider) {
newMetaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -404,6 +408,7 @@ public final Configuration derive(CommitProvider newCommitProvider) {
metaProvider,
newCommitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -440,6 +445,39 @@ public final Configuration derive(ExecutorProvider newExecutorProvider) {
metaProvider,
commitProvider,
newExecutorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
recordListenerProviders,
executeListenerProviders,
migrationListenerProviders,
visitListenerProviders,
transactionListenerProviders,
diagnosticsListenerProviders,
unwrapperProvider,
charsetProvider,
converterProvider,



clock,
dialect,
settings,
data
);
}

@Override
public final Configuration derive(CacheProvider newCacheProvider) {
return new DefaultConfiguration(
connectionProvider,
interpreterConnectionProvider,
systemConnectionProvider,
metaProvider,
commitProvider,
executorProvider,
newCacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -471,6 +509,7 @@ public final Configuration derive(TransactionProvider newTransactionProvider) {
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
newTransactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -507,6 +546,7 @@ public final Configuration derive(RecordMapperProvider newRecordMapperProvider)
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
newRecordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -543,6 +583,7 @@ public final Configuration derive(RecordUnmapperProvider newRecordUnmapperProvid
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
newRecordUnmapperProvider,
Expand Down Expand Up @@ -574,6 +615,7 @@ public final Configuration derive(RecordListenerProvider... newRecordListenerPro
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -605,6 +647,7 @@ public final Configuration derive(ExecuteListenerProvider... newExecuteListenerP
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -636,6 +679,7 @@ public final Configuration derive(MigrationListenerProvider... newMigrationListe
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -667,6 +711,7 @@ public final Configuration derive(VisitListenerProvider... newVisitListenerProvi
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -698,6 +743,7 @@ public final Configuration derive(TransactionListenerProvider... newTransactionL
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -729,6 +775,7 @@ public final Configuration derive(DiagnosticsListenerProvider... newDiagnosticsL
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -765,6 +812,7 @@ public final Configuration derive(UnwrapperProvider newUnwrapperProvider) {
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -796,6 +844,7 @@ public final Configuration derive(CharsetProvider newCharsetProvider) {
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -827,6 +876,7 @@ public final Configuration derive(ConverterProvider newConverterProvider) {
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -881,6 +931,7 @@ public final Configuration derive(ConverterProvider newConverterProvider) {






@Override
Expand All @@ -892,6 +943,7 @@ public final Configuration derive(Clock newClock) {
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -923,6 +975,7 @@ public final Configuration derive(SQLDialect newDialect) {
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -954,6 +1007,7 @@ public final Configuration derive(Settings newSettings) {
metaProvider,
commitProvider,
executorProvider,
cacheProvider,
transactionProvider,
recordMapperProvider,
recordUnmapperProvider,
Expand Down Expand Up @@ -1031,6 +1085,12 @@ public final Configuration set(ExecutorProvider newExecutorProvider) {
return this;
}

@Override
public final Configuration set(CacheProvider newCacheProvider) {
this.cacheProvider = newCacheProvider;
return this;
}

@Override
public final Configuration set(TransactionProvider newTransactionProvider) {
if (newTransactionProvider != null) {
Expand Down Expand Up @@ -1258,6 +1318,13 @@ public final void setExecutorProvider(ExecutorProvider newExecutorProvider) {
set(newExecutorProvider);
}

/**
* @see #set(CacheProvider)
*/
public final void setCacheProvider(CacheProvider newCacheProvider) {
set(newCacheProvider);
}

/**
* @see #set(TransactionProvider)
*/
Expand Down Expand Up @@ -1474,6 +1541,13 @@ public final ExecutorProvider executorProvider() {
: new DefaultExecutorProvider();
}

@Override
public final CacheProvider cacheProvider() {
return cacheProvider != null
? cacheProvider
: new DefaultCacheProvider();
}

@Override
public final TransactionProvider transactionProvider() {

Expand Down Expand Up @@ -1673,7 +1747,7 @@ private void writeObject(ObjectOutputStream oos) throws IOException {

// [#7062] Exclude reflection cache from serialisation
for (Entry<Object, Object> entry : data.entrySet()) {
if (entry.getKey() instanceof DataCacheKey)
if (entry.getKey() instanceof CacheType)
continue;

oos.writeObject(entry.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
package org.jooq.impl;

import static java.lang.Boolean.TRUE;
import static org.jooq.impl.Tools.DataCacheKey.DATA_CACHE_RECORD_MAPPERS;
import static org.jooq.impl.CacheType.CACHE_RECORD_MAPPERS;

import java.io.Serializable;

Expand Down Expand Up @@ -80,7 +80,7 @@ protected DefaultRecordMapperProvider(Configuration configuration) {
@Override
public final <R extends Record, E> RecordMapper<R, E> provide(final RecordType<R> rowType, final Class<? extends E> type) {
if (configuration != null && TRUE.equals(configuration.settings().isCacheRecordMappers()))
return Cache.run(configuration, () -> new DefaultRecordMapper<>(rowType, type, configuration), DATA_CACHE_RECORD_MAPPERS, Cache.key(rowType, type));
return Cache.run(configuration, () -> new DefaultRecordMapper<>(rowType, type, configuration), CACHE_RECORD_MAPPERS, Cache.key(rowType, type));
else
return new DefaultRecordMapper<>(rowType, type, configuration);
}
Expand Down
Loading

0 comments on commit 6206129

Please sign in to comment.