Skip to content

Commit 2efbc4f

Browse files
authored
Minor fix to idBySchemaBySubject cache (confluentinc#2332)
1 parent 15f06d3 commit 2efbc4f

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/Confluent.SchemaRegistry/CachedSchemaRegistryClient.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public class CachedSchemaRegistryClient : ISchemaRegistryClient, IDisposable
6464
private int latestCacheTtlSecs;
6565
private readonly Dictionary<int, Schema> schemaById = new Dictionary<int, Schema>();
6666

67-
private readonly Dictionary<string /*subject*/, Dictionary<string, int>> idBySchemaBySubject =
68-
new Dictionary<string, Dictionary<string, int>>();
67+
private readonly Dictionary<string /*subject*/, Dictionary<Schema, int>> idBySchemaBySubject =
68+
new Dictionary<string, Dictionary<Schema, int>>();
6969

7070
private readonly Dictionary<string /*subject*/, Dictionary<int, RegisteredSchema>> schemaByVersionBySubject =
7171
new Dictionary<string, Dictionary<int, RegisteredSchema>>();
@@ -430,24 +430,24 @@ public async Task<int> GetSchemaIdAsync(string subject, Schema schema, bool norm
430430
await cacheMutex.WaitAsync().ConfigureAwait(continueOnCapturedContext: false);
431431
try
432432
{
433-
if (!this.idBySchemaBySubject.TryGetValue(subject, out Dictionary<string, int> idBySchema))
433+
if (!this.idBySchemaBySubject.TryGetValue(subject, out Dictionary<Schema, int> idBySchema))
434434
{
435-
idBySchema = new Dictionary<string, int>();
435+
idBySchema = new Dictionary<Schema, int>();
436436
this.idBySchemaBySubject.Add(subject, idBySchema);
437437
}
438438

439439
// TODO: The following could be optimized in the usual case where idBySchema only
440440
// contains very few elements and the schema string passed in is always the same
441441
// instance.
442442

443-
if (!idBySchema.TryGetValue(schema.SchemaString, out int schemaId))
443+
if (!idBySchema.TryGetValue(schema, out int schemaId))
444444
{
445445
CleanCacheIfFull();
446446

447447
// throws SchemaRegistryException if schema is not known.
448448
var registeredSchema = await restService.LookupSchemaAsync(subject, schema, true, normalize)
449449
.ConfigureAwait(continueOnCapturedContext: false);
450-
idBySchema[schema.SchemaString] = registeredSchema.Id;
450+
idBySchema[schema] = registeredSchema.Id;
451451
schemaById[registeredSchema.Id] = registeredSchema.Schema;
452452
schemaId = registeredSchema.Id;
453453
}
@@ -467,23 +467,23 @@ public async Task<int> RegisterSchemaAsync(string subject, Schema schema, bool n
467467
await cacheMutex.WaitAsync().ConfigureAwait(continueOnCapturedContext: false);
468468
try
469469
{
470-
if (!this.idBySchemaBySubject.TryGetValue(subject, out Dictionary<string, int> idBySchema))
470+
if (!this.idBySchemaBySubject.TryGetValue(subject, out Dictionary<Schema, int> idBySchema))
471471
{
472-
idBySchema = new Dictionary<string, int>();
472+
idBySchema = new Dictionary<Schema, int>();
473473
this.idBySchemaBySubject[subject] = idBySchema;
474474
}
475475

476476
// TODO: This could be optimized in the usual case where idBySchema only
477477
// contains very few elements and the schema string passed in is always
478478
// the same instance.
479479

480-
if (!idBySchema.TryGetValue(schema.SchemaString, out int schemaId))
480+
if (!idBySchema.TryGetValue(schema, out int schemaId))
481481
{
482482
CleanCacheIfFull();
483483

484484
schemaId = await restService.RegisterSchemaAsync(subject, schema, normalize)
485485
.ConfigureAwait(continueOnCapturedContext: false);
486-
idBySchema[schema.SchemaString] = schemaId;
486+
idBySchema[schema] = schemaId;
487487
}
488488

489489
return schemaId;

0 commit comments

Comments
 (0)