Connect Schema comparison is slow for large schemas
Re-arrange order of comparisons in equals() to evaluate non-composite fields first
Cache hash code
Author: tedyu
Reviewers: Randall Hauch Konstantine Karantasis Ewen Cheslack-Postava
Closes from tedyu/trunk
@@ -93,6 +93,8 @@ public class ConnectSchema implements Schema {
// Optional human readable documentation describing this schema.
private final String doc;
private final Map<String, String> parameters;
+ // precomputed hash code. There is no need to re-compute every time hashCode() is called.
+ private Integer hash = null;
/**
* Construct a Schema. Most users should not construct schemas manually, preferring {@link SchemaBuilder} instead.
@@ -283,20 +285,24 @@ public class ConnectSchema implements Schema {
if (o == null || getClass() != o.getClass()) return false;
ConnectSchema schema = (ConnectSchema) o;
return Objects.equals(optional, schema.optional) &&
+ Objects.equals(version, schema.version) &&
+ Objects.equals(name, schema.name) &&
+ Objects.equals(doc, schema.doc) &&
Objects.equals(type, schema.type) &&
Objects.equals(defaultValue, schema.defaultValue) &&
Objects.equals(fields, schema.fields) &&
Objects.equals(keySchema, schema.keySchema) &&
Objects.equals(valueSchema, schema.valueSchema) &&
- Objects.equals(name, schema.name) &&
- Objects.equals(version, schema.version) &&
- Objects.equals(doc, schema.doc) &&
Objects.equals(parameters, schema.parameters);
}
@Override
public int hashCode() {
- return Objects.hash(type, optional, defaultValue, fields, keySchema, valueSchema, name, version, doc, parameters);
+ if (this.hash == null) {
+ this.hash = Objects.hash(type, optional, defaultValue, fields, keySchema, valueSchema, name, version, doc,
+ parameters);
+ }
+ return this.hash;
}
@Override
