Skip to content

Commit d88370e

Browse files
committed
minor optimization
1 parent 66db101 commit d88370e

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

src/main/java/org/apache/ibatis/executor/resultset/NestedResultSetHandler.java

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2012 The MyBatis Team
2+
* Copyright 2009-2013 The MyBatis Team
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,22 +42,18 @@
4242

4343
public class NestedResultSetHandler extends FastResultSetHandler {
4444

45-
private final Map<CacheKey, Object> objectCache;
46-
private final Map<CacheKey, Object> ancestorCache;
45+
private final Map<CacheKey, Object> objectCache = new HashMap<CacheKey, Object>();
46+
private final Map<CacheKey, Object> ancestorCache = new HashMap<CacheKey, Object>();
4747

4848
public NestedResultSetHandler(Executor executor, MappedStatement mappedStatement, ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSql boundSql, RowBounds rowBounds) {
4949
super(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
50-
objectCache = new HashMap<CacheKey, Object>();
51-
ancestorCache = new HashMap<CacheKey, Object>();
5250
if (configuration.isSafeRowBoundsEnabled()) {
5351
ensureNoRowBounds(rowBounds);
5452
}
5553
}
5654

5755
private void ensureNoRowBounds(RowBounds rowBounds) {
58-
if (rowBounds != null
59-
&& (rowBounds.getLimit() < RowBounds.NO_ROW_LIMIT
60-
|| rowBounds.getOffset() > RowBounds.NO_ROW_OFFSET)) {
56+
if (rowBounds != null && (rowBounds.getLimit() < RowBounds.NO_ROW_LIMIT || rowBounds.getOffset() > RowBounds.NO_ROW_OFFSET)) {
6157
throw new ExecutorException("Mapped Statements with nested result mappings cannot be safely constrained by RowBounds. "
6258
+ "Use safeRowBoundsEnabled=false setting to bypass this check.");
6359
}
@@ -105,36 +101,36 @@ protected Object getRowValue(ResultSet rs, ResultMap resultMap, CacheKey rowKey,
105101
}
106102

107103
protected Object getRowValue(ResultSet rs, ResultMap resultMap, CacheKey combinedKey, CacheKey absoluteKey, String columnPrefix, ResultColumnCache resultColumnCache) throws SQLException {
108-
if (ancestorCache.containsKey(absoluteKey)) {
109-
return ancestorCache.get(absoluteKey);
110-
} else if (objectCache.containsKey(combinedKey)) {
111-
final Object resultObject = objectCache.get(combinedKey);
112-
ancestorCache.put(absoluteKey, resultObject);
113-
final MetaObject metaObject = configuration.newMetaObject(resultObject);
114-
applyNestedResultMappings(rs, resultMap, metaObject, columnPrefix, resultColumnCache, combinedKey, false);
115-
ancestorCache.remove(absoluteKey);
116-
return resultObject;
117-
} else {
118-
final ResultLoaderMap lazyLoader = instantiateResultLoaderMap();
119-
Object resultObject = createResultObject(rs, resultMap, lazyLoader, columnPrefix, resultColumnCache);
120-
if (resultObject != null && !typeHandlerRegistry.hasTypeHandler(resultMap.getType())) {
104+
Object resultObject = ancestorCache.get(absoluteKey);
105+
if (resultObject == null) {
106+
resultObject = objectCache.get(combinedKey);
107+
if (resultObject != null) {
121108
ancestorCache.put(absoluteKey, resultObject);
122109
final MetaObject metaObject = configuration.newMetaObject(resultObject);
123-
boolean foundValues = resultMap.getConstructorResultMappings().size() > 0;
124-
if (shouldApplyAutomaticMappings(resultMap, AutoMappingBehavior.FULL.equals(configuration.getAutoMappingBehavior()))) {
125-
final List<String> unmappedColumnNames = resultColumnCache.getUnmappedColumnNames(resultMap, columnPrefix);
126-
foundValues = applyAutomaticMappings(rs, unmappedColumnNames, metaObject, columnPrefix, resultColumnCache) || foundValues;
127-
}
128-
final List<String> mappedColumnNames = resultColumnCache.getMappedColumnNames(resultMap, columnPrefix);
129-
foundValues = applyPropertyMappings(rs, resultMap, mappedColumnNames, metaObject, lazyLoader, columnPrefix) || foundValues;
130-
foundValues = applyNestedResultMappings(rs, resultMap, metaObject, columnPrefix, resultColumnCache, combinedKey, true) || foundValues;
131-
foundValues = (lazyLoader != null && lazyLoader.size() > 0) || foundValues;
132-
resultObject = foundValues ? resultObject : null;
110+
applyNestedResultMappings(rs, resultMap, metaObject, columnPrefix, resultColumnCache, combinedKey, false);
133111
ancestorCache.remove(absoluteKey);
112+
} else {
113+
final ResultLoaderMap lazyLoader = instantiateResultLoaderMap();
114+
resultObject = createResultObject(rs, resultMap, lazyLoader, columnPrefix, resultColumnCache);
115+
if (resultObject != null && !typeHandlerRegistry.hasTypeHandler(resultMap.getType())) {
116+
ancestorCache.put(absoluteKey, resultObject);
117+
final MetaObject metaObject = configuration.newMetaObject(resultObject);
118+
boolean foundValues = resultMap.getConstructorResultMappings().size() > 0;
119+
if (shouldApplyAutomaticMappings(resultMap, AutoMappingBehavior.FULL.equals(configuration.getAutoMappingBehavior()))) {
120+
final List<String> unmappedColumnNames = resultColumnCache.getUnmappedColumnNames(resultMap, columnPrefix);
121+
foundValues = applyAutomaticMappings(rs, unmappedColumnNames, metaObject, columnPrefix, resultColumnCache) || foundValues;
122+
}
123+
final List<String> mappedColumnNames = resultColumnCache.getMappedColumnNames(resultMap, columnPrefix);
124+
foundValues = applyPropertyMappings(rs, resultMap, mappedColumnNames, metaObject, lazyLoader, columnPrefix) || foundValues;
125+
foundValues = applyNestedResultMappings(rs, resultMap, metaObject, columnPrefix, resultColumnCache, combinedKey, true) || foundValues;
126+
foundValues = (lazyLoader != null && lazyLoader.size() > 0) || foundValues;
127+
resultObject = foundValues ? resultObject : null;
128+
ancestorCache.remove(absoluteKey);
129+
}
130+
if (combinedKey != CacheKey.NULL_CACHE_KEY) objectCache.put(combinedKey, resultObject);
134131
}
135-
if (combinedKey.getUpdateCount() > 1) objectCache.put(combinedKey, resultObject);
136-
return resultObject;
137132
}
133+
return resultObject;
138134
}
139135

140136
//
@@ -153,7 +149,7 @@ private boolean applyNestedResultMappings(ResultSet rs, ResultMap resultMap, Met
153149
final String columnPrefix = columnPrefixBuilder.length() == 0 ? null : columnPrefixBuilder.toString().toUpperCase(Locale.ENGLISH);
154150
final ResultMap nestedResultMap = getNestedResultMap(rs, nestedResultMapId, columnPrefix);
155151
final CacheKey absoluteKey = createAbsoluteKey(nestedResultMap, rs, columnPrefix, resultColumnCache);
156-
final CacheKey combinedKey = getCombinedKey(absoluteKey, parentRowKey);
152+
final CacheKey combinedKey = combineKeys(absoluteKey, parentRowKey);
157153
final boolean knownValue = objectCache.containsKey(combinedKey);
158154
final boolean isAncestor = ancestorCache.containsKey(absoluteKey);
159155
Object rowValue = getRowValue(rs, nestedResultMap, combinedKey, absoluteKey, columnPrefix, resultColumnCache);
@@ -245,8 +241,8 @@ private CacheKey createAbsoluteKey(ResultMap resultMap, ResultSet rs, String col
245241
}
246242
return cacheKey;
247243
}
248-
249-
private CacheKey getCombinedKey(CacheKey rowKey, CacheKey parentRowKey) {
244+
245+
private CacheKey combineKeys(CacheKey rowKey, CacheKey parentRowKey) {
250246
if (rowKey.getUpdateCount() > 1 && parentRowKey.getUpdateCount() > 1) {
251247
CacheKey combinedKey;
252248
try {
@@ -268,7 +264,13 @@ private List<ResultMapping> getResultMappingsForRowKey(ResultMap resultMap) {
268264
return resultMappings;
269265
}
270266

271-
private void createRowKeyForMappedProperties(ResultMap resultMap, ResultSet rs, CacheKey cacheKey, List<ResultMapping> resultMappings, String columnPrefix, ResultColumnCache resultColumnCache) throws SQLException {
267+
private void createRowKeyForMappedProperties(
268+
ResultMap resultMap,
269+
ResultSet rs,
270+
CacheKey cacheKey,
271+
List<ResultMapping> resultMappings,
272+
String columnPrefix,
273+
ResultColumnCache resultColumnCache) throws SQLException {
272274
for (ResultMapping resultMapping : resultMappings) {
273275
if (resultMapping.getNestedResultMapId() != null) {
274276
final ResultMap nestedResultMap = configuration.getResultMap(resultMapping.getNestedResultMapId());
@@ -278,7 +280,7 @@ private void createRowKeyForMappedProperties(ResultMap resultMap, ResultSet rs,
278280
final String column = prependPrefix(resultMapping.getColumn(), columnPrefix);
279281
final TypeHandler<?> th = resultMapping.getTypeHandler();
280282
List<String> mappedColumnNames = resultColumnCache.getMappedColumnNames(resultMap, columnPrefix);
281-
if (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) { // issue #114
283+
if (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) { // Issue #114
282284
final Object value = th.getResult(rs, column);
283285
if (value != null) {
284286
cacheKey.update(column);
@@ -320,7 +322,7 @@ private void createRowKeyForMap(ResultSet rs, CacheKey cacheKey, ResultColumnCac
320322
if (value != null) {
321323
cacheKey.update(columnName);
322324
cacheKey.update(value);
323-
}
325+
}
324326
}
325327
}
326328

0 commit comments

Comments
 (0)