HHH-14236 Improve efficiency of ForeignKeyKey

This commit is contained in:
Sanne Grinovero 2020-09-28 14:41:07 +01:00
parent a6eaed2db6
commit f2a742e3c4
1 changed files with 16 additions and 15 deletions

View File

@ -8,12 +8,14 @@ package org.hibernate.mapping;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.MappingException; import org.hibernate.MappingException;
@ -42,6 +44,7 @@ import org.jboss.logging.Logger;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class Table implements RelationalModel, Serializable, Exportable { public class Table implements RelationalModel, Serializable, Exportable {
private static final Logger log = Logger.getLogger( Table.class ); private static final Logger log = Logger.getLogger( Table.class );
private static final Column[] EMPTY_COLUMN_ARRAY = new Column[0];
private Identifier catalog; private Identifier catalog;
private Identifier schema; private Identifier schema;
@ -858,40 +861,38 @@ public class Table implements RelationalModel, Serializable, Exportable {
return identifier == null ? null : identifier.render(); return identifier == null ? null : identifier.render();
} }
public static class ForeignKeyKey implements Serializable { public static class ForeignKeyKey implements Serializable {
String referencedClassName; private final String referencedClassName;
List<Column> columns; private final Column[] columns;
List<Column> referencedColumns; private final Column[] referencedColumns;
ForeignKeyKey(List<Column> columns, String referencedClassName, List<Column> referencedColumns) { ForeignKeyKey(List<Column> columns, String referencedClassName, List<Column> referencedColumns) {
Objects.requireNonNull( columns );
Objects.requireNonNull( referencedClassName );
this.referencedClassName = referencedClassName; this.referencedClassName = referencedClassName;
this.columns = new ArrayList<>(); this.columns = columns.toArray( EMPTY_COLUMN_ARRAY );
this.columns.addAll( columns );
if ( referencedColumns != null ) { if ( referencedColumns != null ) {
this.referencedColumns = new ArrayList<>(); this.referencedColumns = referencedColumns.toArray( EMPTY_COLUMN_ARRAY );
this.referencedColumns.addAll( referencedColumns );
} }
else { else {
this.referencedColumns = Collections.emptyList(); this.referencedColumns = EMPTY_COLUMN_ARRAY;
} }
} }
public int hashCode() { public int hashCode() {
return columns.hashCode() + referencedColumns.hashCode(); return Arrays.hashCode( columns ) + Arrays.hashCode( referencedColumns );
} }
public boolean equals(Object other) { public boolean equals(Object other) {
ForeignKeyKey fkk = (ForeignKeyKey) other; ForeignKeyKey fkk = (ForeignKeyKey) other;
return fkk != null && fkk.columns.equals( columns ) && fkk.referencedColumns.equals( referencedColumns ); return fkk != null && Arrays.equals( fkk.columns, columns ) && Arrays.equals( fkk.referencedColumns, referencedColumns );
} }
@Override @Override
public String toString() { public String toString() {
return "ForeignKeyKey{" + return "ForeignKeyKey{columns=" + Arrays.toString( columns ) +
"columns=" + columns + ", referencedClassName='" + referencedClassName +
", referencedClassName='" + referencedClassName + '\'' + "', referencedColumns=" + Arrays.toString( referencedColumns ) +
", referencedColumns=" + referencedColumns +
'}'; '}';
} }
} }