Skip to content

Commit

Permalink
Some performance tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
aaberg committed Nov 2, 2024
1 parent f7a84b5 commit 434f77a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 200 deletions.
176 changes: 0 additions & 176 deletions core/src/main/java/org/sql2o/DefaultResultSetHandlerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import org.sql2o.quirks.Quirks;
import org.sql2o.reflection2.ObjectBuildableFactoryDelegate;

import java.sql.ResultSetMetaData;


public class DefaultResultSetHandlerFactory<T> implements ResultSetHandlerFactory<T> {
private final Quirks quirks;
private final ObjectBuildableFactoryDelegate<T> objectBuilderDelegate;
Expand All @@ -15,114 +13,9 @@ public DefaultResultSetHandlerFactory(ObjectBuildableFactoryDelegate<T> objectBu
this.quirks = quirks;
}

// @SuppressWarnings("unchecked")
// private static Setter getSetter(
// final Quirks quirks,
// final String propertyPath,
// final PojoMetadata metadata) {
// int index = propertyPath.indexOf('.');
// if (index <= 0) {
// // Simple path - fast way
// final Setter setter = metadata.getPropertySetterIfExists(propertyPath);
// // behavior change: do not throw if POJO contains less properties
// if (setter == null) return null;
// final Converter converter = quirks.converterOf(setter.getType());
// // setter without converter
// if (converter == null) return setter;
// return new Setter() {
// public void setProperty(Object obj, Object value) {
// try {
// setter.setProperty(obj, converter.convert(value));
// } catch (ConverterException e) {
// throw new Sql2oException("Error trying to convert column " + propertyPath + " to type " + setter.getType(), e);
// }
// }
//
// public Class getType() {
// return setter.getType();
// }
// };
// }
// // dot path - long way
// // i'm too lazy now to rewrite this case so I just call old unoptimized code...
// // TODO: rewrite, get rid of POJO class
// return new Setter() {
// public void setProperty(Object obj, Object value) {
// Pojo pojo = new Pojo(metadata, metadata.isCaseSensitive(), obj);
// pojo.setProperty(propertyPath, value, quirks);
// }
//
// public Class getType() {
// // doesn't used anyway
// return Object.class;
// }
// };
// }

// private static class Key {
// final String stringKey;
// final DefaultResultSetHandlerFactory f;
//
// DefaultResultSetHandlerFactory factory(){
// return f;
// }
//
// private PojoMetadata getMetadata() {
// return f.metadata;
// }
//
// private Quirks getQuirksMode() {
// return f.quirks;
// }
//
// private Key(String stringKey, DefaultResultSetHandlerFactory f) {
// this.stringKey = stringKey;
// this.f = f;
// }
//
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
//
// Key key = (Key) o;
//
// return f.metadata.equals(key.getMetadata())
// && f.quirks == key.getQuirksMode()
// && stringKey.equals(key.stringKey);
//
// }
//
// @Override
// public int hashCode() {
// int result = f.metadata.hashCode();
// result = 31 * result + f.quirks.hashCode();
// result = 31 * result + stringKey.hashCode();
// return result;
// }
// }
//
//
// private static final AbstractCache<Key, ResultSetHandler, ResultSetMetaData> cache =
// new AbstractCache<Key, ResultSetHandler, ResultSetMetaData>() {
// @Override
// protected ResultSetHandler evaluate(Key key, ResultSetMetaData param) {
// try {
// return key.factory().newResultSetHandler0(param);
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
// }
// };

@SuppressWarnings("unchecked")
public ResultSetHandler<T> newResultSetHandler(final ResultSetMetaData meta) {
// StringBuilder stringBuilder = new StringBuilder();
// for (int i = 1; i <= meta.getColumnCount(); i++) {
// stringBuilder.append(quirks.getColumnName(meta,i)).append("\n");
// }
// return cache.get(new Key(stringBuilder.toString(), this),meta);

return resultSet -> {

final var objectBuilder = objectBuilderDelegate.newObjectBuilder();
Expand All @@ -142,74 +35,5 @@ public ResultSetHandler<T> newResultSetHandler(final ResultSetMetaData meta) {
throw new Sql2oException("Error occurred while creating object from ResultSet", e);
}
};


}


// @SuppressWarnings("unchecked")
// private ResultSetHandler<T> newResultSetHandler0(final ResultSetMetaData meta) throws SQLException {
//
// return resultSet -> {
// try {
// while (resultSet.next()) {
// for (int i = 1; i <= metadata.getColumnCount(); i++) {
// String colName = quirks.getColumnName(resultSet.getMetaData(), i);
// objectBuilder.withValue(colName, resultSet.getObject(i));
// }
// }
// return objectBuilder.build();
// } catch (ReflectiveOperationException e) {
// throw new Sql2oException("Error occurred while creating object from ResultSet", e);
// }
// };
// }


// final Setter[] setters;
// final Converter converter;
// final boolean useExecuteScalar;
// //TODO: it's possible to cache converter/setters/getters
// // cache key is ResultSetMetadata + Bean type
//
// converter = quirks.converterOf(metadata.getType());
// final int columnCount = meta.getColumnCount();
//
// setters = new Setter[columnCount + 1]; // setters[0] is always null
// for (int i = 1; i <= columnCount; i++) {
// String colName = quirks.getColumnName(meta, i);
//
// setters[i] = getSetter(quirks, colName, metadata);
//
// // If more than 1 column is fetched (we cannot fall back to executeScalar),
// // and the setter doesn't exist, throw exception.
// if (this.metadata.throwOnMappingFailure && setters[i] == null && columnCount > 1) {
// throw new Sql2oException("Could not map " + colName + " to any property.");
// }
// }
// /**
// * Fallback to executeScalar if converter exists,
// * we're selecting 1 column, and no property setter exists for the column.
// */
// useExecuteScalar = converter != null && columnCount == 1 && setters[1] == null;
// return resultSet -> {
// if (useExecuteScalar) {
// try {
// return (T) converter.convert(quirks.getRSVal(resultSet, 1));
// } catch (ConverterException e) {
// throw new Sql2oException("Error occurred while converting value from database to type " + metadata.getType(), e);
// }
// }
//
// // otherwise we want executeAndFetch with object mapping
// Object pojo = metadata.getObjectConstructor().newInstance();
// for (int colIdx = 1; colIdx <= columnCount; colIdx++) {
// Setter setter = setters[colIdx];
// if (setter == null) continue;
// setter.setProperty(pojo, quirks.getRSVal(resultSet, colIdx));
// }
//
// return (T) pojo;
// };
// }
}
11 changes: 3 additions & 8 deletions core/src/main/java/org/sql2o/converters/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,12 @@ public static <E> Converter<E> throwIfNull(Class<E> clazz, Converter<E> converte
}

public static <E> Converter<E> getConverterIfExists(Class<E> clazz) {
Converter c;
rl.lock();
try {
c = registeredConverters.get(clazz);
} finally {
rl.unlock();
}
final var c = (Converter<E>)registeredConverters.get(clazz);

if (c != null) return c;

if (clazz.isEnum()) {
return registeredEnumConverterFactory.newConverter((Class) clazz);
return registeredEnumConverterFactory.newConverter((Class)clazz);
}
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/org/sql2o/converters/IntegerConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public IntegerConverter(boolean primitive) {

@Override
protected Integer convertNumberValue(Number val) {
if (val instanceof Integer intVal) {
return intVal;
}
return val.intValue();
}

Expand Down
21 changes: 7 additions & 14 deletions core/src/main/java/org/sql2o/converters/NumberConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
public abstract class NumberConverter<V extends Number> extends ConverterBase<V> {

private boolean isPrimitive;
private final boolean isPrimitive;

public NumberConverter(boolean primitive) {
isPrimitive = primitive;
Expand All @@ -16,23 +16,16 @@ public V convert(Object val) {
return isPrimitive ? convertNumberValue(0) : null;
}

// val.getClass().isPrimitive() is ALWAYS false
// since boxing (i.e. Object val=(int)1;)
// changes type from Integet.TYPE to Integer.class
// learn 2 java :)

else if (/*val.getClass().isPrimitive() || */val instanceof Number ) {
return convertNumberValue((Number)val);
else if (val instanceof Number num) {
return convertNumberValue(num);
}
else if (val instanceof String){
String stringVal = ((String)val).trim();
stringVal = stringVal.isEmpty() ? null : stringVal;
else if (val instanceof String strVal){
strVal = strVal.trim();

if (stringVal == null) {
if (strVal.isEmpty()) {
return isPrimitive ? convertNumberValue(0) : null;
}

return convertStringValue(stringVal);
return convertStringValue(strVal);
}
else{
throw new IllegalArgumentException("Cannot convert type " + val.getClass().toString() + " to " + getTypeDescription());
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/sql2o/converters/StringConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public String convert(Object val) throws ConverterException {
return null;
}

if (val instanceof String stringVal){
return stringVal;
}

if (val instanceof Clob) {
Clob clobVal = (Clob)val;
try
Expand Down
11 changes: 9 additions & 2 deletions core/src/main/java/org/sql2o/reflection2/PojoProperty.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.sql2o.reflection2;

import org.sql2o.Settings;
import org.sql2o.Sql2o;
import org.sql2o.Sql2oException;
import org.sql2o.converters.ConverterException;

Expand Down Expand Up @@ -41,7 +40,7 @@ public String getAnnotatedName() {
public void SetProperty(Object obj, Object value) throws ReflectiveOperationException {
if (setter != null) {
try {
final var propertyType = setter.getParameters()[0].getType();
final var propertyType = getSetterType();
final var convertedValue = settings.getQuirks().converterOf(propertyType).convert(value);
if (convertedValue == null && propertyType.isPrimitive()) {
return; // don't try to set null to a setter to a primitive type.
Expand Down Expand Up @@ -109,4 +108,12 @@ public Object initializeWithNewInstance(Object obj) throws ReflectiveOperationEx

throw new Sql2oException("Could not initialize property " + getName() + " no setter or field found.");
}

private Class<?> setterType = null;
private Class<?> getSetterType() {
if (setterType == null) {
setterType = setter.getParameters()[0].getType();
}
return setterType;
}
}

0 comments on commit 434f77a

Please sign in to comment.