KAFKA-6168: 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 <yuzhihong@gmail.com>

Reviewers: Randall Hauch <rhauch@gmail.com>, Konstantine Karantasis <konstantine@confluent.io>, Ewen Cheslack-Postava <ewen@confluent.io>

Closes #4176 from tedyu/trunk
This commit is contained in:
tedyu 2017-11-21 14:26:32 -08:00 committed by Ewen Cheslack-Postava
parent a133e69b45
commit fd8eb268d6
1 changed files with 10 additions and 4 deletions

View File

@ -93,6 +93,8 @@ public class ConnectSchema implements Schema {
// Optional human readable documentation describing this schema. // Optional human readable documentation describing this schema.
private final String doc; private final String doc;
private final Map<String, String> parameters; 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. * 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; if (o == null || getClass() != o.getClass()) return false;
ConnectSchema schema = (ConnectSchema) o; ConnectSchema schema = (ConnectSchema) o;
return Objects.equals(optional, schema.optional) && 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(type, schema.type) &&
Objects.equals(defaultValue, schema.defaultValue) && Objects.equals(defaultValue, schema.defaultValue) &&
Objects.equals(fields, schema.fields) && Objects.equals(fields, schema.fields) &&
Objects.equals(keySchema, schema.keySchema) && Objects.equals(keySchema, schema.keySchema) &&
Objects.equals(valueSchema, schema.valueSchema) && 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); Objects.equals(parameters, schema.parameters);
} }
@Override @Override
public int hashCode() { 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 @Override