Skip to content

Commit

Permalink
KAFKA-9855 - return cached Structs for Schemas with no fields (#8472)
Browse files Browse the repository at this point in the history
At the time of this writing there are 6 schemas in kafka APIs with no fields - 3
versions each of LIST_GROUPS and API_VERSIONS.

When reading instances of these schemas off the wire there's little point in
returning a unique Struct object (or a unique values array inside that Struct)
since there is no payload.

Reviewers: Ismael Juma <ismael@juma.me.uk>
  • Loading branch information
radai-rosenblatt committed May 21, 2020
1 parent ab4b4d7 commit aa1b3c1
Showing 1 changed file with 10 additions and 2 deletions.
Expand Up @@ -25,10 +25,12 @@
* The schema for a compound record definition
*/
public class Schema extends Type {
private final static Object[] NO_VALUES = new Object[0];

private final BoundField[] fields;
private final Map<String, BoundField> fieldsByName;
private final boolean tolerateMissingFieldsWithDefaults;
private final Struct cachedStruct;

/**
* Construct the schema with a given list of its field values
Expand Down Expand Up @@ -62,6 +64,9 @@ public Schema(boolean tolerateMissingFieldsWithDefaults, Field... fs) {
this.fields[i] = new BoundField(def, this, i);
this.fieldsByName.put(def.name, this.fields[i]);
}
//6 schemas have no fields at the time of this writing (3 versions each of list_groups and api_versions)
//for such schemas there's no point in even creating a unique Struct object when deserializing.
this.cachedStruct = this.fields.length > 0 ? null : new Struct(this, NO_VALUES);
}

/**
Expand Down Expand Up @@ -90,6 +95,9 @@ public void write(ByteBuffer buffer, Object o) {
*/
@Override
public Struct read(ByteBuffer buffer) {
if (cachedStruct != null) {
return cachedStruct;
}
Object[] objects = new Object[fields.length];
for (int i = 0; i < fields.length; i++) {
try {
Expand Down Expand Up @@ -140,7 +148,7 @@ public int numFields() {

/**
* Get a field by its slot in the record array
*
*
* @param slot The slot at which this field sits
* @return The field
*/
Expand All @@ -150,7 +158,7 @@ public BoundField get(int slot) {

/**
* Get a field by its name
*
*
* @param name The name of the field
* @return The field
*/
Expand Down

0 comments on commit aa1b3c1

Please sign in to comment.