Skip to content

Commit

Permalink
[#3944] AbstractScope creates an excessive amount of HashMaps,
Browse files Browse the repository at this point in the history
internally
  • Loading branch information
lukaseder committed Jan 14, 2015
1 parent 4a876ee commit 5163c4a
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 102 deletions.
14 changes: 9 additions & 5 deletions jOOQ/src/main/java/org/jooq/impl/AbstractRoutine.java
Expand Up @@ -337,7 +337,7 @@ private final int executeCallableStatement() {

listener.bindStart(ctx);
using(configuration).bindContext(ctx.statement()).visit(this);
registerOutParameters(configuration, (CallableStatement) ctx.statement());
registerOutParameters(ctx);
listener.bindEnd(ctx);

execute0(ctx, listener);
Expand Down Expand Up @@ -598,6 +598,7 @@ private final void fetchOutParameters(ExecuteContext ctx) throws SQLException {
private final <U> void fetchOutParameter(ExecuteContext ctx, Parameter<U> parameter) throws SQLException {
DefaultBindingGetStatementContext<U> out = new DefaultBindingGetStatementContext<U>(
ctx.configuration(),
ctx.data(),
(CallableStatement) ctx.statement(),
parameterIndexes.get(parameter)
);
Expand All @@ -606,21 +607,24 @@ private final <U> void fetchOutParameter(ExecuteContext ctx, Parameter<U> parame
outValues.put(parameter, out.value());
}

private final void registerOutParameters(Configuration c, CallableStatement statement) throws SQLException {
private final void registerOutParameters(ExecuteContext ctx) throws SQLException {
Configuration c = ctx.configuration();
Map<Object, Object> data = ctx.data();
CallableStatement statement = (CallableStatement) ctx.statement();

// Register all out / inout parameters according to their position
// Note that some RDBMS do not support binding by name very well
for (Parameter<?> parameter : getParameters()) {
if (parameter.equals(getReturnParameter()) ||
getOutParameters().contains(parameter)) {

registerOutParameter(c, statement, parameter);
registerOutParameter(c, data, statement, parameter);
}
}
}

private final <U> void registerOutParameter(Configuration c, CallableStatement statement, Parameter<U> parameter) throws SQLException {
parameter.getBinding().register(new DefaultBindingRegisterContext<U>(c, statement, parameterIndexes.get(parameter)));
private final <U> void registerOutParameter(Configuration c, Map<Object, Object> data, CallableStatement statement, Parameter<U> parameter) throws SQLException {
parameter.getBinding().register(new DefaultBindingRegisterContext<U>(c, data, statement, parameterIndexes.get(parameter)));
}

// ------------------------------------------------------------------------
Expand Down
19 changes: 11 additions & 8 deletions jOOQ/src/main/java/org/jooq/impl/AbstractScope.java
Expand Up @@ -53,24 +53,27 @@
*/
abstract class AbstractScope implements Scope {

private final Configuration configuration;
private final Map<Object, Object> data;
final Configuration configuration;
final Map<Object, Object> data;

AbstractScope(Configuration configuration) {
this.data = new HashMap<Object, Object>();
this(configuration, null);
}

AbstractScope(Configuration configuration, Map<Object, Object> data) {

// The Configuration can be null when unattached objects are
// executed or when unattached Records are stored...
if (configuration == null) {
configuration = new DefaultConfiguration();
}

this.configuration = configuration;
}
if (data == null) {
data = new HashMap<Object, Object>();
}

AbstractScope(AbstractScope scope) {
this.data = scope.data;
this.configuration = scope.configuration;
this.configuration = configuration;
this.data = data;
}

// ------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/CursorImpl.java
Expand Up @@ -1488,7 +1488,7 @@ private final <T> void setValue(AbstractRecord record, Field<T> field, int index
offset += emulatedFields.length - 1;
}
else {
DefaultBindingGetResultSetContext<T> out = new DefaultBindingGetResultSetContext<T>(ctx.configuration(), ctx.resultSet(), offset + index + 1);
DefaultBindingGetResultSetContext<T> out = new DefaultBindingGetResultSetContext<T>(ctx.configuration(), ctx.data(), ctx.resultSet(), offset + index + 1);
field.getBinding().get(out);
value = out.value();
}
Expand Down
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/DefaultBindContext.java
Expand Up @@ -60,7 +60,7 @@ class DefaultBindContext extends AbstractBindContext {
@SuppressWarnings({ "unchecked" })
protected final BindContext bindValue0(Object value, Field<?> field) throws SQLException {
((Field<Object>) field).getBinding().set(
new DefaultBindingSetStatementContext<Object>(configuration(), stmt, nextIndex(), value)
new DefaultBindingSetStatementContext<Object>(configuration(), data(), stmt, nextIndex(), value)
);

return this;
Expand Down
10 changes: 5 additions & 5 deletions jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java
Expand Up @@ -621,7 +621,7 @@ else if (type.isArray()) {

for (Object o : ((Object[]) val)) {
render.sql(separator);
new DefaultBinding<Object, Object>(new IdentityConverter(type.getComponentType()), isLob).sql(new DefaultBindingSQLContext<Object>(ctx.configuration(), ctx.render(), o));
new DefaultBinding<Object, Object>(new IdentityConverter(type.getComponentType()), isLob).sql(new DefaultBindingSQLContext<Object>(ctx.configuration(), ctx.data(), ctx.render(), o));
separator = ", ";
}

Expand All @@ -635,7 +635,7 @@ else if (type.isArray()) {

for (Object o : ((Object[]) val)) {
render.sql(separator);
new DefaultBinding<Object, Object>(new IdentityConverter(type.getComponentType()), isLob).sql(new DefaultBindingSQLContext<Object>(ctx.configuration(), ctx.render(), o));
new DefaultBinding<Object, Object>(new IdentityConverter(type.getComponentType()), isLob).sql(new DefaultBindingSQLContext<Object>(ctx.configuration(), ctx.data(), ctx.render(), o));
separator = ", ";
}

Expand All @@ -657,10 +657,10 @@ else if (EnumType.class.isAssignableFrom(type)) {
String literal = ((EnumType) val).getLiteral();

if (literal == null) {
new DefaultBinding<Object, Object>(new IdentityConverter(String.class), isLob).sql(new DefaultBindingSQLContext<Object>(ctx.configuration(), ctx.render(), literal));
new DefaultBinding<Object, Object>(new IdentityConverter(String.class), isLob).sql(new DefaultBindingSQLContext<Object>(ctx.configuration(), ctx.data(), ctx.render(), literal));
}
else {
new DefaultBinding<Object, Object>(new IdentityConverter(String.class), isLob).sql(new DefaultBindingSQLContext<Object>(ctx.configuration(), ctx.render(), literal));
new DefaultBinding<Object, Object>(new IdentityConverter(String.class), isLob).sql(new DefaultBindingSQLContext<Object>(ctx.configuration(), ctx.data(), ctx.render(), literal));
}
}
else if (UDTRecord.class.isAssignableFrom(type)) {
Expand Down Expand Up @@ -2066,7 +2066,7 @@ private static final <T> T pgGetArray(Scope ctx, ResultSet rs, Class<T> type, in
// Try fetching the array as a JDBC ResultSet
try {
while (rs.next()) {
DefaultBindingGetResultSetContext<T> out = new DefaultBindingGetResultSetContext<T>(ctx.configuration(), rs, 2);
DefaultBindingGetResultSetContext<T> out = new DefaultBindingGetResultSetContext<T>(ctx.configuration(), ctx.data(), rs, 2);
new DefaultBinding<T, T>(new IdentityConverter<T>((Class<T>) type.getComponentType()), false).get(out);
result.add(out.value());
}
Expand Down
Expand Up @@ -41,6 +41,7 @@
package org.jooq.impl;

import java.sql.ResultSet;
import java.util.Map;

import org.jooq.BindingGetResultSetContext;
import org.jooq.Configuration;
Expand All @@ -55,15 +56,8 @@ class DefaultBindingGetResultSetContext<U> extends AbstractScope implements Bind
private final int index;
private U value;

DefaultBindingGetResultSetContext(Configuration configuration, ResultSet resultSet, int index) {
super(configuration);

this.resultSet = resultSet;
this.index = index;
}

private DefaultBindingGetResultSetContext(AbstractScope scope, ResultSet resultSet, int index) {
super(scope);
DefaultBindingGetResultSetContext(Configuration configuration, Map<Object, Object> data, ResultSet resultSet, int index) {
super(configuration, data);

this.resultSet = resultSet;
this.index = index;
Expand All @@ -90,7 +84,7 @@ final U value() {

@Override
public final <T> BindingGetResultSetContext<T> convert(final Converter<T, U> converter) {
return new DefaultBindingGetResultSetContext<T>(this, resultSet, index) {
return new DefaultBindingGetResultSetContext<T>(configuration, data, resultSet, index) {
@Override
public void value(T v) {
value = converter.from(v);
Expand Down
Expand Up @@ -41,6 +41,7 @@
package org.jooq.impl;

import java.sql.SQLInput;
import java.util.Map;

import org.jooq.BindingGetSQLInputContext;
import org.jooq.Configuration;
Expand All @@ -54,14 +55,8 @@ class DefaultBindingGetSQLInputContext<U> extends AbstractScope implements Bindi
private final SQLInput input;
private U value;

DefaultBindingGetSQLInputContext(Configuration configuration, SQLInput input) {
super(configuration);

this.input = input;
}

private DefaultBindingGetSQLInputContext(AbstractScope scope, SQLInput input) {
super(scope);
DefaultBindingGetSQLInputContext(Configuration configuration, Map<Object, Object> data, SQLInput input) {
super(configuration, data);

this.input = input;
}
Expand All @@ -82,7 +77,7 @@ final U value() {

@Override
public final <T> BindingGetSQLInputContext<T> convert(final Converter<T, U> converter) {
return new DefaultBindingGetSQLInputContext<T>(this, input) {
return new DefaultBindingGetSQLInputContext<T>(configuration, data, input) {
@Override
public void value(T v) {
value = converter.from(v);
Expand Down
Expand Up @@ -41,6 +41,7 @@
package org.jooq.impl;

import java.sql.CallableStatement;
import java.util.Map;

import org.jooq.BindingGetStatementContext;
import org.jooq.Configuration;
Expand All @@ -55,15 +56,8 @@ class DefaultBindingGetStatementContext<U> extends AbstractScope implements Bind
private final int index;
private U value;

DefaultBindingGetStatementContext(Configuration configuration, CallableStatement statement, int index) {
super(configuration);

this.statement = statement;
this.index = index;
}

private DefaultBindingGetStatementContext(AbstractScope scope, CallableStatement statement, int index) {
super(scope);
DefaultBindingGetStatementContext(Configuration configuration, Map<Object, Object> data, CallableStatement statement, int index) {
super(configuration, data);

this.statement = statement;
this.index = index;
Expand All @@ -90,7 +84,7 @@ final U value() {

@Override
public final <T> BindingGetStatementContext<T> convert(final Converter<T, U> converter) {
return new DefaultBindingGetStatementContext<T>(this, statement, index) {
return new DefaultBindingGetStatementContext<T>(configuration, data, statement, index) {
@Override
public void value(T v) {
value = converter.from(v);
Expand Down
Expand Up @@ -41,6 +41,7 @@
package org.jooq.impl;

import java.sql.CallableStatement;
import java.util.Map;

import org.jooq.BindingRegisterContext;
import org.jooq.Configuration;
Expand All @@ -54,15 +55,8 @@ class DefaultBindingRegisterContext<U> extends AbstractScope implements BindingR
private final CallableStatement statement;
private final int index;

DefaultBindingRegisterContext(Configuration configuration, CallableStatement statement, int index) {
super(configuration);

this.statement = statement;
this.index = index;
}

private DefaultBindingRegisterContext(AbstractScope scope, CallableStatement statement, int index) {
super(scope);
DefaultBindingRegisterContext(Configuration configuration, Map<Object, Object> data, CallableStatement statement, int index) {
super(configuration, data);

this.statement = statement;
this.index = index;
Expand All @@ -80,6 +74,6 @@ public final int index() {

@Override
public final <T> BindingRegisterContext<T> convert(Converter<T, U> converter) {
return new DefaultBindingRegisterContext<T>(this, statement, index);
return new DefaultBindingRegisterContext<T>(configuration, data, statement, index);
}
}
20 changes: 7 additions & 13 deletions jOOQ/src/main/java/org/jooq/impl/DefaultBindingSQLContext.java
Expand Up @@ -40,6 +40,8 @@
*/
package org.jooq.impl;

import java.util.Map;

import org.jooq.BindingSQLContext;
import org.jooq.Configuration;
import org.jooq.Converter;
Expand All @@ -54,20 +56,12 @@ class DefaultBindingSQLContext<U> extends AbstractScope implements BindingSQLCon
private final U value;
private final String variable;

DefaultBindingSQLContext(Configuration configuration, RenderContext render, U value) {
this(configuration, render, value, "?");
}

DefaultBindingSQLContext(Configuration configuration, RenderContext render, U value, String variable) {
super(configuration);

this.render = render;
this.value = value;
this.variable = variable;
DefaultBindingSQLContext(Configuration configuration, Map<Object, Object> data, RenderContext render, U value) {
this(configuration, data, render, value, "?");
}

private DefaultBindingSQLContext(AbstractScope other, RenderContext render, U value, String variable) {
super(other);
DefaultBindingSQLContext(Configuration configuration, Map<Object, Object> data, RenderContext render, U value, String variable) {
super(configuration, data);

this.render = render;
this.value = value;
Expand All @@ -91,6 +85,6 @@ public final String variable() {

@Override
public <T> BindingSQLContext<T> convert(Converter<T, U> converter) {
return new DefaultBindingSQLContext<T>(this, render, converter.to(value), variable);
return new DefaultBindingSQLContext<T>(configuration, data, render, converter.to(value), variable);
}
}
Expand Up @@ -41,6 +41,7 @@
package org.jooq.impl;

import java.sql.SQLOutput;
import java.util.Map;

import org.jooq.BindingSetSQLOutputContext;
import org.jooq.Configuration;
Expand All @@ -54,15 +55,8 @@ class DefaultBindingSetSQLOutputContext<U> extends AbstractScope implements Bind
private final SQLOutput output;
private final U value;

DefaultBindingSetSQLOutputContext(Configuration configuration, SQLOutput output, U value) {
super(configuration);

this.output = output;
this.value = value;
}

private DefaultBindingSetSQLOutputContext(AbstractScope other, SQLOutput output, U value) {
super(other);
DefaultBindingSetSQLOutputContext(Configuration configuration, Map<Object, Object> data, SQLOutput output, U value) {
super(configuration, configuration.data());

this.output = output;
this.value = value;
Expand All @@ -80,6 +74,6 @@ public final U value() {

@Override
public final <T> BindingSetSQLOutputContext<T> convert(Converter<T, U> converter) {
return new DefaultBindingSetSQLOutputContext<T>(this, output, converter.to(value));
return new DefaultBindingSetSQLOutputContext<T>(configuration, data, output, converter.to(value));
}
}
Expand Up @@ -41,6 +41,7 @@
package org.jooq.impl;

import java.sql.PreparedStatement;
import java.util.Map;

import org.jooq.BindingSetStatementContext;
import org.jooq.Configuration;
Expand All @@ -55,16 +56,8 @@ class DefaultBindingSetStatementContext<U> extends AbstractScope implements Bind
private final int index;
private final U value;

DefaultBindingSetStatementContext(Configuration configuration, PreparedStatement statement, int index, U value) {
super(configuration);

this.statement = statement;
this.index = index;
this.value = value;
}

private DefaultBindingSetStatementContext(AbstractScope other, PreparedStatement statement, int index, U value) {
super(other);
DefaultBindingSetStatementContext(Configuration configuration, Map<Object, Object> data, PreparedStatement statement, int index, U value) {
super(configuration, configuration.data());

this.statement = statement;
this.index = index;
Expand All @@ -88,6 +81,6 @@ public final U value() {

@Override
public final <T> BindingSetStatementContext<T> convert(Converter<T, U> converter) {
return new DefaultBindingSetStatementContext<T>(this, statement, index, converter.to(value));
return new DefaultBindingSetStatementContext<T>(configuration, data, statement, index, converter.to(value));
}
}

0 comments on commit 5163c4a

Please sign in to comment.