Skip to content

Commit 0cc42af

Browse files
authored
Minor optimization to not derive schema when using existing schema (#140)
1 parent ea9513e commit 0cc42af

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

schemaregistry/serde/avro.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,19 @@ export class AvroSerializer extends Serializer implements AvroSerde {
7171
throw new Error('message is empty')
7272
}
7373

74-
let avroSchema = AvroSerializer.messageToSchema(msg)
75-
const schema: SchemaInfo = {
76-
schemaType: 'AVRO',
77-
schema: JSON.stringify(avroSchema),
74+
let schema: SchemaInfo | undefined = undefined
75+
// Don't derive the schema if it is being looked up in the following ways
76+
if (this.config().useSchemaId == null &&
77+
!this.config().useLatestVersion &&
78+
this.config().useLatestWithMetadata == null) {
79+
const avroSchema = AvroSerializer.messageToSchema(msg)
80+
schema = {
81+
schemaType: 'AVRO',
82+
schema: JSON.stringify(avroSchema),
83+
}
7884
}
7985
const [id, info] = await this.getId(topic, msg, schema)
86+
let avroSchema: avro.Type
8087
let deps: Map<string, string>
8188
[avroSchema, deps] = await this.toType(info)
8289
const subject = this.subjectName(topic, info)

schemaregistry/serde/json.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,16 @@ export class JsonSerializer extends Serializer implements JsonSerde {
9191
throw new Error('message is empty')
9292
}
9393

94-
const jsonSchema = JsonSerializer.messageToSchema(msg)
95-
const schema: SchemaInfo = {
96-
schemaType: 'JSON',
97-
schema: JSON.stringify(jsonSchema),
94+
let schema: SchemaInfo | undefined = undefined
95+
// Don't derive the schema if it is being looked up in the following ways
96+
if (this.config().useSchemaId == null &&
97+
!this.config().useLatestVersion &&
98+
this.config().useLatestWithMetadata == null) {
99+
const jsonSchema = JsonSerializer.messageToSchema(msg)
100+
schema = {
101+
schemaType: 'JSON',
102+
schema: JSON.stringify(jsonSchema),
103+
}
98104
}
99105
const [id, info] = await this.getId(topic, msg, schema)
100106
const subject = this.subjectName(topic, info)

schemaregistry/serde/protobuf.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,15 @@ export class ProtobufSerializer extends Serializer implements ProtobufSerde {
149149
if (messageDesc == null) {
150150
throw new SerializationError('message descriptor not in registry')
151151
}
152-
const fileDesc = messageDesc.file
153-
const schema = await this.getSchemaInfo(fileDesc)
152+
153+
let schema: SchemaInfo | undefined = undefined
154+
// Don't derive the schema if it is being looked up in the following ways
155+
if (this.config().useSchemaId == null &&
156+
!this.config().useLatestVersion &&
157+
this.config().useLatestWithMetadata == null) {
158+
const fileDesc = messageDesc.file
159+
schema = await this.getSchemaInfo(fileDesc)
160+
}
154161
const [id, info] = await this.getId(topic, msg, schema, 'serialized')
155162
const subject = this.subjectName(topic, info)
156163
msg = await this.executeRules(subject, topic, RuleMode.WRITE, null, info, msg, null)

schemaregistry/serde/serde.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export abstract class Serializer extends Serde {
244244
abstract serialize(topic: string, msg: any): Promise<Buffer>
245245

246246
// GetID returns a schema ID for the given schema
247-
async getId(topic: string, msg: any, info: SchemaInfo, format?: string): Promise<[number, SchemaInfo]> {
247+
async getId(topic: string, msg: any, info?: SchemaInfo, format?: string): Promise<[number, SchemaInfo]> {
248248
let autoRegister = this.config().autoRegisterSchemas
249249
let useSchemaId = this.config().useSchemaId
250250
let useLatestWithMetadata = this.conf.useLatestWithMetadata
@@ -254,7 +254,7 @@ export abstract class Serializer extends Serde {
254254
let id = -1
255255
let subject = this.subjectName(topic, info)
256256
if (autoRegister) {
257-
id = await this.client.register(subject, info, Boolean(normalizeSchema))
257+
id = await this.client.register(subject, info!, Boolean(normalizeSchema))
258258
} else if (useSchemaId != null && useSchemaId >= 0) {
259259
info = await this.client.getBySubjectAndId(subject, useSchemaId, format)
260260
id = useSchemaId
@@ -267,9 +267,9 @@ export abstract class Serializer extends Serde {
267267
info = metadata
268268
id = metadata.id
269269
} else {
270-
id = await this.client.getId(subject, info, Boolean(normalizeSchema))
270+
id = await this.client.getId(subject, info!, Boolean(normalizeSchema))
271271
}
272-
return [id, info]
272+
return [id, info!]
273273
}
274274

275275
writeBytes(id: number, msgBytes: Buffer): Buffer {

0 commit comments

Comments
 (0)