Skip to content

Commit 2db6db1

Browse files
authored
Minor optimization to reduce schema ID lookups (#123)
1 parent 8086016 commit 2db6db1

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

schemaregistry/mock-schemaregistry-client.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ class MockClient implements Client {
176176
throw new RestError("Schema not found", 404, 40400);
177177
}
178178

179-
180179
return {
181180
id,
182181
version,
@@ -202,7 +201,6 @@ class MockClient implements Client {
202201
if (parsedKey.subject === subject && (!value.softDeleted || deleted)) {
203202
if (parsedKey.schema.metadata && this.isSubset(metadata, parsedKey.schema.metadata.properties)) {
204203
results.push({
205-
id: parsedKey.schema.id,
206204
version: value.version,
207205
subject,
208206
...parsedKey.schema
@@ -223,6 +221,18 @@ class MockClient implements Client {
223221
}
224222
});
225223

224+
let id: number = -1;
225+
for (const [key, value] of this.idToSchemaCache.entries()) {
226+
const parsedKey = JSON.parse(key);
227+
if (parsedKey.subject === subject && value.info.schema === latest.schema) {
228+
id = parsedKey.id;
229+
}
230+
}
231+
if (id === -1) {
232+
throw new RestError("Schema not found", 404, 40400);
233+
}
234+
235+
latest.id = id;
226236
return latest;
227237
}
228238

schemaregistry/serde/serde.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,15 @@ export abstract class Serializer extends Serde {
257257
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)
260-
id = await this.client.getId(subject, info, false)
261-
if (id !== useSchemaId) {
262-
throw new SerializationError(`failed to match schema ID (${id} != ${useSchemaId})`)
263-
}
260+
id = useSchemaId
264261
} else if (useLatestWithMetadata != null && Object.keys(useLatestWithMetadata).length !== 0) {
265-
info = await this.client.getLatestWithMetadata(subject, useLatestWithMetadata, true, format)
266-
id = await this.client.getId(subject, info, false)
262+
let metadata = await this.client.getLatestWithMetadata(subject, useLatestWithMetadata, true, format)
263+
info = metadata
264+
id = metadata.id
267265
} else if (useLatest) {
268-
info = await this.client.getLatestSchemaMetadata(subject, format)
269-
id = await this.client.getId(subject, info, false)
266+
let metadata = await this.client.getLatestSchemaMetadata(subject, format)
267+
info = metadata
268+
id = metadata.id
270269
} else {
271270
id = await this.client.getId(subject, info, Boolean(normalizeSchema))
272271
}

0 commit comments

Comments
 (0)