[jOOQ/jOOQ#11126] Avoid calling AbstractQueryPart::equals

- Slight improvement to AbstractName::equals
- Function.equals(Function)
- QueryPartCollectionView.equals(QueryPartCollectionView)
- SequenceFunction.equals(SequenceFunction)
- SequenceImpl.equals(SequenceImpl)
- TableAlias.equals(TableAlias)
This commit is contained in:
Lukas Eder 2020-12-14 17:40:12 +01:00
parent 8be226cac3
commit a59fe42c49
5 changed files with 109 additions and 28 deletions

View File

@ -375,10 +375,16 @@ abstract class AbstractName extends AbstractQueryPart implements Name {
if (this == that)
return true;
// [#1626] NameImpl equality can be decided without executing the
// [#1626] [#11126] NameImpl equality can be decided without executing the
// rather expensive implementation of AbstractQueryPart.equals()
if (that instanceof AbstractName)
return Arrays.equals(getName(), (((AbstractName) that).getName()));
if (that instanceof AbstractName) {
// [#11126] No need to access name arrays if not both names are equally qualified
if (qualified() != ((AbstractName) that).qualified())
return false;
else
return Arrays.equals(getName(), (((AbstractName) that).getName()));
}
return super.equals(that);
}

View File

@ -70,4 +70,17 @@ final class Function<T> extends AbstractField<T> {
public final void accept(Context<?> ctx) {
ctx.visit(getQualifiedName()).sql('(').visit(arguments).sql(')');
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof Function)
return getQualifiedName().equals(((Function<?>) that).getQualifiedName())
&& arguments.equals(((Function<?>) that).arguments);
else
return super.equals(that);
}
}

View File

@ -297,4 +297,23 @@ class QueryPartCollectionView<T extends QueryPart> extends AbstractQueryPart imp
public final void clear() {
wrapped.clear();
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public int hashCode() {
return wrapped.hashCode();
}
@Override
public boolean equals(Object that) {
// [#11126] Speed up comparisons of two QueryPartCollectionViews of the same type
if (that instanceof QueryPartCollectionView && getClass() == that.getClass())
return wrapped.equals(((QueryPartCollectionView<?>) that).wrapped);
else
return super.equals(that);
}
}

View File

@ -85,17 +85,19 @@ public class SequenceImpl<T extends Number> extends AbstractTypedNamed<T> implem
/**
* Generated UID
*/
private static final long serialVersionUID = 6224349401603636427L;
private static final Clause[] CLAUSES = { SEQUENCE, SEQUENCE_REFERENCE };
private static final long serialVersionUID = 6224349401603636427L;
private static final Clause[] CLAUSES = { SEQUENCE, SEQUENCE_REFERENCE };
private final boolean nameIsPlainSQL;
private final Schema schema;
private final Field<T> startWith;
private final Field<T> incrementBy;
private final Field<T> minvalue;
private final Field<T> maxvalue;
private final boolean cycle;
private final Field<T> cache;
private final boolean nameIsPlainSQL;
private final Schema schema;
private final Field<T> startWith;
private final Field<T> incrementBy;
private final Field<T> minvalue;
private final Field<T> maxvalue;
private final boolean cycle;
private final Field<T> cache;
private final SequenceFunction<T> currval;
private final SequenceFunction<T> nextval;
@Deprecated
public SequenceImpl(String name, Schema schema, DataType<T> type) {
@ -133,6 +135,8 @@ public class SequenceImpl<T extends Number> extends AbstractTypedNamed<T> implem
this.maxvalue = maxvalue;
this.cycle = cycle;
this.cache = cache;
this.currval = new SequenceFunction<>(SequenceMethod.CURRVAL, this);
this.nextval = new SequenceFunction<>(SequenceMethod.NEXTVAL, this);
}
@Override
@ -177,12 +181,12 @@ public class SequenceImpl<T extends Number> extends AbstractTypedNamed<T> implem
@Override
public final Field<T> currval() {
return new SequenceFunction(SequenceMethod.CURRVAL);
return currval;
}
@Override
public final Field<T> nextval() {
return new SequenceFunction(SequenceMethod.NEXTVAL);
return nextval;
}
@Override
@ -203,18 +207,20 @@ public class SequenceImpl<T extends Number> extends AbstractTypedNamed<T> implem
}
}
private class SequenceFunction extends AbstractField<T> {
private static class SequenceFunction<T extends Number> extends AbstractField<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = 2292275568395094887L;
private final SequenceMethod method;
private static final long serialVersionUID = 2292275568395094887L;
private final SequenceMethod method;
private final SequenceImpl<T> sequence;
SequenceFunction(SequenceMethod method) {
super(method.name, SequenceImpl.this.getDataType());
SequenceFunction(SequenceMethod method, SequenceImpl<T> sequence) {
super(method.name, sequence.getDataType());
this.method = method;
this.sequence = sequence;
}
@Override
@ -237,7 +243,7 @@ public class SequenceImpl<T extends Number> extends AbstractTypedNamed<T> implem
case POSTGRES: {
ctx.visit(method.keyword).sql('(');
ctx.sql('\'').stringLiteral(true).visit(SequenceImpl.this).stringLiteral(false).sql('\'');
ctx.sql('\'').stringLiteral(true).visit(sequence).stringLiteral(false).sql('\'');
ctx.sql(')');
break;
}
@ -251,15 +257,15 @@ public class SequenceImpl<T extends Number> extends AbstractTypedNamed<T> implem
case HSQLDB:
case MARIADB: {
if (method == SequenceMethod.NEXTVAL)
ctx.visit(K_NEXT_VALUE_FOR).sql(' ').visit(SequenceImpl.this);
ctx.visit(K_NEXT_VALUE_FOR).sql(' ').visit(sequence);
else if (family == H2)
ctx.visit(SequenceImpl.this).sql('.').visit(method.keyword);
ctx.visit(sequence).sql('.').visit(method.keyword);
else if (family == HSQLDB)
ctx.visit(K_CURRENT_VALUE_FOR).sql(' ').visit(SequenceImpl.this);
ctx.visit(K_CURRENT_VALUE_FOR).sql(' ').visit(sequence);
else if (family == MARIADB)
ctx.visit(K_PREVIOUS_VALUE_FOR).sql(' ').visit(SequenceImpl.this);
ctx.visit(K_PREVIOUS_VALUE_FOR).sql(' ').visit(sequence);
else if (family == FIREBIRD)
ctx.visit(N_GEN_ID).sql('(').visit(SequenceImpl.this).sql(", 0)");
ctx.visit(N_GEN_ID).sql('(').visit(sequence).sql(", 0)");
@ -281,7 +287,7 @@ public class SequenceImpl<T extends Number> extends AbstractTypedNamed<T> implem
}
case CUBRID: {
ctx.visit(SequenceImpl.this).sql('.');
ctx.visit(sequence).sql('.');
if (method == SequenceMethod.NEXTVAL)
ctx.visit(DSL.keyword("next_value"));
@ -293,11 +299,24 @@ public class SequenceImpl<T extends Number> extends AbstractTypedNamed<T> implem
// Default is needed for hashCode() and toString()
default: {
ctx.visit(SequenceImpl.this).sql('.').visit(method.keyword);
ctx.visit(sequence).sql('.').visit(method.keyword);
break;
}
}
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof SequenceFunction)
return method == ((SequenceFunction<?>) that).method
&& sequence.equals(((SequenceFunction<?>) that).sequence);
else
return super.equals(that);
}
}
// ------------------------------------------------------------------------
@ -322,4 +341,16 @@ public class SequenceImpl<T extends Number> extends AbstractTypedNamed<T> implem
public final Clause[] clauses(Context<?> ctx) {
return CLAUSES;
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof SequenceImpl)
return getQualifiedName().equals(((SequenceImpl) that).getQualifiedName());
else
return super.equals(that);
}
}

View File

@ -171,4 +171,16 @@ final class TableAlias<R extends Record> extends AbstractTable<R> {
public Class<? extends R> getRecordType() {
return alias.wrapped().getRecordType();
}
// ------------------------------------------------------------------------
// XXX: Object API
// ------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof TableAlias)
return getUnqualifiedName().equals(((TableAlias<?>) that).getUnqualifiedName());
else
return super.equals(that);
}
}