Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ResultFlag #2947

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ public ResultMap addResultMap(String id, Class<?> type, String extend, Discrimin
// Remove parent constructor if this resultMap declares a constructor.
boolean declaresConstructor = false;
for (ResultMapping resultMapping : resultMappings) {
if (resultMapping.getFlags().contains(ResultFlag.CONSTRUCTOR)) {
if (ResultFlag.containsConstructor(resultMapping.getFlags())) {
declaresConstructor = true;
break;
}
}
if (declaresConstructor) {
extendedResultMappings.removeIf(resultMapping -> resultMapping.getFlags().contains(ResultFlag.CONSTRUCTOR));
extendedResultMappings.removeIf(resultMapping -> ResultFlag.containsConstructor(resultMapping.getFlags()));
}
resultMappings.addAll(extendedResultMappings);
}
Expand All @@ -185,7 +185,7 @@ public ResultMap addResultMap(String id, Class<?> type, String extend, Discrimin
public Discriminator buildDiscriminator(Class<?> resultType, String column, Class<?> javaType, JdbcType jdbcType,
Class<? extends TypeHandler<?>> typeHandler, Map<String, String> discriminatorMap) {
ResultMapping resultMapping = buildResultMapping(resultType, null, column, javaType, jdbcType, null, null, null,
null, typeHandler, new ArrayList<>(), null, null, false);
null, typeHandler, ResultFlag.NO_FLAG, null, null, false);
Map<String, String> namespaceDiscriminatorMap = new HashMap<>();
for (Map.Entry<String, String> e : discriminatorMap.entrySet()) {
String resultMap = e.getValue();
Expand Down Expand Up @@ -334,8 +334,7 @@ private List<ResultMap> getStatementResultMaps(String resultMap, Class<?> result

public ResultMapping buildResultMapping(Class<?> resultType, String property, String column, Class<?> javaType,
JdbcType jdbcType, String nestedSelect, String nestedResultMap, String notNullColumn, String columnPrefix,
Class<? extends TypeHandler<?>> typeHandler, List<ResultFlag> flags, String resultSet, String foreignColumn,
boolean lazy) {
Class<? extends TypeHandler<?>> typeHandler, byte flags, String resultSet, String foreignColumn, boolean lazy) {
Class<?> javaTypeClass = resolveResultJavaType(resultType, property, javaType);
TypeHandler<?> typeHandlerInstance = resolveTypeHandler(javaTypeClass, typeHandler);
List<ResultMapping> composites;
Expand All @@ -347,7 +346,7 @@ public ResultMapping buildResultMapping(Class<?> resultType, String property, St
return new ResultMapping.Builder(configuration, property, column, javaTypeClass).jdbcType(jdbcType)
.nestedQueryId(applyCurrentNamespace(nestedSelect, true))
.nestedResultMapId(applyCurrentNamespace(nestedResultMap, true)).resultSet(resultSet)
.typeHandler(typeHandlerInstance).flags(flags == null ? new ArrayList<>() : flags).composites(composites)
.typeHandler(typeHandlerInstance).flags(flags).composites(composites)
.notNullColumns(parseMultipleColumnNames(notNullColumn)).columnPrefix(columnPrefix).foreignColumn(foreignColumn)
.lazy(lazy).build();
}
Expand Down Expand Up @@ -382,7 +381,7 @@ public ResultMapping buildResultMapping(Class<?> resultType, String property, St
*/
public ResultMapping buildResultMapping(Class<?> resultType, String property, String column, Class<?> javaType,
JdbcType jdbcType, String nestedSelect, String nestedResultMap, String notNullColumn, String columnPrefix,
Class<? extends TypeHandler<?>> typeHandler, List<ResultFlag> flags) {
Class<? extends TypeHandler<?>> typeHandler, byte flags) {
return buildResultMapping(resultType, property, column, javaType, jdbcType, nestedSelect, nestedResultMap,
notNullColumn, columnPrefix, typeHandler, flags, null, null, configuration.isLazyLoadingEnabled());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,9 @@ private static Class<?> getReturnType(Method method, Class<?> type) {

private void applyResults(Result[] results, Class<?> resultType, List<ResultMapping> resultMappings) {
for (Result result : results) {
List<ResultFlag> flags = new ArrayList<>();
byte flags = ResultFlag.NO_FLAG;
if (result.id()) {
flags.add(ResultFlag.ID);
flags |= ResultFlag.ID;
}
@SuppressWarnings("unchecked")
Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>) (result
Expand Down Expand Up @@ -531,10 +531,10 @@ private boolean hasNestedSelect(Result result) {

private void applyConstructorArgs(Arg[] args, Class<?> resultType, List<ResultMapping> resultMappings) {
for (Arg arg : args) {
List<ResultFlag> flags = new ArrayList<>();
flags.add(ResultFlag.CONSTRUCTOR);
byte flags = ResultFlag.NO_FLAG;
flags |= ResultFlag.CONSTRUCTOR;
if (arg.id()) {
flags.add(ResultFlag.ID);
flags |= ResultFlag.ID;
}
@SuppressWarnings("unchecked")
Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>) (arg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> addi
} else if ("discriminator".equals(resultChild.getName())) {
discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings);
} else {
List<ResultFlag> flags = new ArrayList<>();
byte flags = ResultFlag.NO_FLAG;
if ("id".equals(resultChild.getName())) {
flags.add(ResultFlag.ID);
flags |= ResultFlag.ID;
}
resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags));
}
Expand Down Expand Up @@ -316,10 +316,10 @@ protected Class<?> inheritEnclosingType(XNode resultMapNode, Class<?> enclosingT
private void processConstructorElement(XNode resultChild, Class<?> resultType, List<ResultMapping> resultMappings) {
List<XNode> argChildren = resultChild.getChildren();
for (XNode argChild : argChildren) {
List<ResultFlag> flags = new ArrayList<>();
flags.add(ResultFlag.CONSTRUCTOR);
byte flags = ResultFlag.NO_FLAG;
flags |= ResultFlag.CONSTRUCTOR;
if ("idArg".equals(argChild.getName())) {
flags.add(ResultFlag.ID);
flags |= ResultFlag.ID;
}
resultMappings.add(buildResultMappingFromContext(argChild, resultType, flags));
}
Expand Down Expand Up @@ -378,9 +378,9 @@ private boolean databaseIdMatchesCurrent(String id, String databaseId, String re
return context.getStringAttribute("databaseId") == null;
}

private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resultType, List<ResultFlag> flags) {
private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resultType, byte flags) {
String property;
if (flags.contains(ResultFlag.CONSTRUCTOR)) {
if (ResultFlag.containsConstructor(flags)) {
property = context.getStringAttribute("name");
} else {
property = context.getStringAttribute("property");
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/apache/ibatis/mapping/ResultFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@
/**
* @author Clinton Begin
*/
public enum ResultFlag {
public class ResultFlag {

ID,
public static final byte NO_FLAG = 0;
public static final byte ID = 1;
public static final byte CONSTRUCTOR = 2;

CONSTRUCTOR
public static boolean containsId(byte flags) {
return (flags & ID) == ID;
}

public static boolean containsConstructor(byte flags) {
return (flags & CONSTRUCTOR) == CONSTRUCTOR;
}

}
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/mapping/ResultMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ public ResultMap build() {
if (property != null) {
resultMap.mappedProperties.add(property);
}
if (resultMapping.getFlags().contains(ResultFlag.CONSTRUCTOR)) {
if (ResultFlag.containsConstructor(resultMapping.getFlags())) {
resultMap.constructorResultMappings.add(resultMapping);
if (resultMapping.getProperty() != null) {
constructorArgNames.add(resultMapping.getProperty());
}
} else {
resultMap.propertyResultMappings.add(resultMapping);
}
if (resultMapping.getFlags().contains(ResultFlag.ID)) {
if (ResultFlag.containsId(resultMapping.getFlags())) {
resultMap.idResultMappings.add(resultMapping);
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/apache/ibatis/mapping/ResultMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ResultMapping {
private String nestedQueryId;
private Set<String> notNullColumns;
private String columnPrefix;
private List<ResultFlag> flags;
private byte flags;
private List<ResultMapping> composites;
private String resultSet;
private String foreignColumn;
Expand All @@ -67,7 +67,7 @@ public Builder(Configuration configuration, String property, String column, Clas
public Builder(Configuration configuration, String property) {
resultMapping.configuration = configuration;
resultMapping.property = property;
resultMapping.flags = new ArrayList<>();
resultMapping.flags = ResultFlag.NO_FLAG;
resultMapping.composites = new ArrayList<>();
resultMapping.lazy = configuration.isLazyLoadingEnabled();
}
Expand Down Expand Up @@ -112,7 +112,7 @@ public Builder columnPrefix(String columnPrefix) {
return this;
}

public Builder flags(List<ResultFlag> flags) {
public Builder flags(byte flags) {
resultMapping.flags = flags;
return this;
}
Expand All @@ -134,7 +134,6 @@ public Builder lazy(boolean lazy) {

public ResultMapping build() {
// lock down collections
resultMapping.flags = Collections.unmodifiableList(resultMapping.flags);
resultMapping.composites = Collections.unmodifiableList(resultMapping.composites);
resolveTypeHandler();
validate();
Expand Down Expand Up @@ -223,7 +222,7 @@ public String getColumnPrefix() {
return columnPrefix;
}

public List<ResultFlag> getFlags() {
public byte getFlags() {
return flags;
}

Expand Down
94 changes: 17 additions & 77 deletions src/test/java/org/apache/ibatis/executor/ExecutorTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,7 @@ static MappedStatement prepareSelectOneAuthorMappedStatementWithConstructorResul
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, null, "id", registry.getTypeHandler(Integer.class))
.javaType(int.class).flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.CONSTRUCTOR);
}
}).build());
.javaType(int.class).flags(ResultFlag.CONSTRUCTOR).build());
add(new ResultMapping.Builder(config, "username", "username", registry.getTypeHandler(String.class))
.build());
add(new ResultMapping.Builder(config, "password", "password", registry.getTypeHandler(String.class))
Expand Down Expand Up @@ -388,13 +383,8 @@ static MappedStatement prepareComplexSelectBlogMappedStatement(final Configurati
new ArrayList<ResultMapping>() {
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).flags(ResultFlag.ID)
.build());
add(new ResultMapping.Builder(config, "title", "title", registry.getTypeHandler(String.class)).build());
add(new ResultMapping.Builder(config, "author.id", "author_id", registry.getTypeHandler(int.class))
.build());
Expand Down Expand Up @@ -436,13 +426,8 @@ static MappedStatement prepareSelectBlogByIdAndAuthor(final Configuration config
new ArrayList<ResultMapping>() {
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).flags(ResultFlag.ID)
.build());
add(new ResultMapping.Builder(config, "title", "title", registry.getTypeHandler(String.class)).build());
add(new ResultMapping.Builder(config, "author.id", "author_id", registry.getTypeHandler(int.class))
.build());
Expand Down Expand Up @@ -487,12 +472,7 @@ static MappedStatement prepareSelectPostsForBlogMappedStatement(final Configurat
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "tag_id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
.flags(ResultFlag.ID).build());
add(new ResultMapping.Builder(config, "name", "tag_name", registry.getTypeHandler(String.class)).build());
}
}).build();
Expand All @@ -501,12 +481,7 @@ static MappedStatement prepareSelectPostsForBlogMappedStatement(final Configurat
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "comment_id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
.flags(ResultFlag.ID).build());
add(new ResultMapping.Builder(config, "name", "comment_name", registry.getTypeHandler(String.class))
.build());
add(new ResultMapping.Builder(config, "comment", "comment", registry.getTypeHandler(String.class)).build());
Expand All @@ -518,13 +493,8 @@ static MappedStatement prepareSelectPostsForBlogMappedStatement(final Configurat
new ArrayList<ResultMapping>() {
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).flags(ResultFlag.ID)
.build());
add(new ResultMapping.Builder(config, "blog", "blog_id", registry.getTypeHandler(int.class))
.javaType(Blog.class).nestedQueryId("selectBlogById").build());
add(new ResultMapping.Builder(config, "createdOn", "created_on", registry.getTypeHandler(Date.class))
Expand Down Expand Up @@ -565,12 +535,7 @@ static MappedStatement prepareSelectPostMappedStatement(final Configuration conf
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "tag_id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
.flags(ResultFlag.ID).build());
add(new ResultMapping.Builder(config, "name", "tag_name", registry.getTypeHandler(String.class)).build());
}
}).build();
Expand All @@ -579,12 +544,7 @@ static MappedStatement prepareSelectPostMappedStatement(final Configuration conf
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "comment_id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
.flags(ResultFlag.ID).build());
add(new ResultMapping.Builder(config, "name", "comment_name", registry.getTypeHandler(String.class))
.build());
add(new ResultMapping.Builder(config, "comment", "comment", registry.getTypeHandler(String.class)).build());
Expand All @@ -595,13 +555,8 @@ static MappedStatement prepareSelectPostMappedStatement(final Configuration conf
final ResultMap postResultMap = new ResultMap.Builder(config, "", Post.class, new ArrayList<ResultMapping>() {
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).flags(ResultFlag.ID)
.build());
add(new ResultMapping.Builder(config, "blog", "blog_id", registry.getTypeHandler(int.class))
.javaType(Blog.class).nestedQueryId("selectBlogById").build());
add(new ResultMapping.Builder(config, "createdOn", "created_on", registry.getTypeHandler(Date.class)).build());
Expand Down Expand Up @@ -641,12 +596,7 @@ static MappedStatement prepareSelectPostWithBlogByAuthorMappedStatement(final Co
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "tag_id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
.flags(ResultFlag.ID).build());
add(new ResultMapping.Builder(config, "name", "tag_name", registry.getTypeHandler(String.class)).build());
}
}).build();
Expand All @@ -655,12 +605,7 @@ static MappedStatement prepareSelectPostWithBlogByAuthorMappedStatement(final Co
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "comment_id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
.flags(ResultFlag.ID).build());
add(new ResultMapping.Builder(config, "name", "comment_name", registry.getTypeHandler(String.class))
.build());
add(new ResultMapping.Builder(config, "comment", "comment", registry.getTypeHandler(String.class)).build());
Expand All @@ -672,13 +617,8 @@ static MappedStatement prepareSelectPostWithBlogByAuthorMappedStatement(final Co
new ArrayList<ResultMapping>() {
private static final long serialVersionUID = 1L;
{
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class))
.flags(new ArrayList<ResultFlag>() {
private static final long serialVersionUID = 1L;
{
add(ResultFlag.ID);
}
}).build());
add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).flags(ResultFlag.ID)
.build());

add(new ResultMapping.Builder(config, "blog").nestedQueryId("selectBlogByIdAndAuthor")
.composites(new ArrayList<ResultMapping>() {
Expand Down