@@ -58,6 +58,8 @@ namespace Confluent.SchemaRegistry
58
58
/// </summary>
59
59
public class CachedSchemaRegistryClient : ISchemaRegistryClient
60
60
{
61
+ private record struct SchemaId ( int Id , string Format ) ;
62
+
61
63
private readonly List < SchemaReference > EmptyReferencesList = new List < SchemaReference > ( ) ;
62
64
63
65
private IEnumerable < KeyValuePair < string , string > > config ;
@@ -67,7 +69,7 @@ public class CachedSchemaRegistryClient : ISchemaRegistryClient
67
69
private IRestService restService ;
68
70
private int identityMapCapacity ;
69
71
private int latestCacheTtlSecs ;
70
- private readonly ConcurrentDictionary < int , Schema > schemaById = new ConcurrentDictionary < int , Schema > ( ) ;
72
+ private readonly ConcurrentDictionary < SchemaId , Schema > schemaById = new ConcurrentDictionary < SchemaId , Schema > ( ) ;
71
73
72
74
private readonly ConcurrentDictionary < string /*subject*/ , ConcurrentDictionary < Schema , int > > idBySchemaBySubject =
73
75
new ConcurrentDictionary < string , ConcurrentDictionary < Schema , int > > ( ) ;
@@ -632,7 +634,9 @@ public async Task<int> GetSchemaIdAsync(string subject, Schema schema, bool norm
632
634
var registeredSchema = await restService . LookupSchemaAsync ( subject , schema , true , normalize )
633
635
. ConfigureAwait ( continueOnCapturedContext : false ) ;
634
636
idBySchema [ schema ] = registeredSchema . Id ;
635
- schemaById [ registeredSchema . Id ] = registeredSchema . Schema ;
637
+
638
+ var format = GetSchemaFormat ( schema . SchemaString ) ;
639
+ schemaById . TryAdd ( new SchemaId ( registeredSchema . Id , format ) , registeredSchema . Schema ) ;
636
640
schemaId = registeredSchema . Id ;
637
641
}
638
642
@@ -695,26 +699,11 @@ public Task<int> RegisterSchemaAsync(string subject, string avroSchema, bool nor
695
699
/// <summary>
696
700
/// Check if the given schema string matches a given format name.
697
701
/// </summary>
698
- private bool checkSchemaMatchesFormat ( string format , string schemaString )
702
+ private static string GetSchemaFormat ( string schemaString )
699
703
{
700
- // if a format isn't specified, then assume text is desired.
701
- if ( format == null )
702
- {
703
- // If schemaString is not Base64, infer the schemaString format is text.
704
- return ! Utils . IsBase64String ( schemaString ) ;
705
- }
706
- else
707
- {
708
- if ( format != "serialized" )
709
- {
710
- throw new ArgumentException ( $ "Invalid schema format was specified: { format } .") ;
711
- }
712
-
713
- return Utils . IsBase64String ( schemaString ) ;
714
- }
704
+ return Utils . IsBase64String ( schemaString ) ? "serialized" : null ;
715
705
}
716
706
717
-
718
707
/// <inheritdoc/>
719
708
public async Task < RegisteredSchema > LookupSchemaAsync ( string subject , Schema schema , bool ignoreDeletedSchemas ,
720
709
bool normalize = false )
@@ -753,21 +742,20 @@ public async Task<RegisteredSchema> LookupSchemaAsync(string subject, Schema sch
753
742
/// <inheritdoc/>
754
743
public async Task < Schema > GetSchemaAsync ( int id , string format = null )
755
744
{
756
- if ( schemaById . TryGetValue ( id , out var schema ) && checkSchemaMatchesFormat ( format , schema . SchemaString ) )
745
+ var schemaId = new SchemaId ( id , format ) ;
746
+ if ( schemaById . TryGetValue ( schemaId , out var schema ) )
757
747
{
758
748
return schema ;
759
749
}
760
750
761
751
await cacheMutex . WaitAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
762
752
try
763
753
{
764
- if ( ! this . schemaById . TryGetValue ( id , out schema ) ||
765
- ! checkSchemaMatchesFormat ( format , schema . SchemaString ) )
754
+ if ( ! this . schemaById . TryGetValue ( schemaId , out schema ) )
766
755
{
767
756
CleanCacheIfFull ( ) ;
768
- schema = ( await restService . GetSchemaAsync ( id , format )
769
- . ConfigureAwait ( continueOnCapturedContext : false ) ) ;
770
- schemaById [ id ] = schema ;
757
+ schema = await restService . GetSchemaAsync ( id , format ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
758
+ schemaById . TryAdd ( schemaId , schema ) ;
771
759
}
772
760
773
761
return schema ;
@@ -782,20 +770,21 @@ public async Task<Schema> GetSchemaAsync(int id, string format = null)
782
770
/// <inheritdoc/>
783
771
public async Task < Schema > GetSchemaBySubjectAndIdAsync ( string subject , int id , string format = null )
784
772
{
785
- if ( this . schemaById . TryGetValue ( id , out var schema ) && checkSchemaMatchesFormat ( format , schema . SchemaString ) )
773
+ var schemaId = new SchemaId ( id , format ) ;
774
+ if ( this . schemaById . TryGetValue ( schemaId , out var schema ) )
786
775
{
787
776
return schema ;
788
777
}
789
778
790
779
await cacheMutex . WaitAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
791
780
try
792
781
{
793
- if ( ! this . schemaById . TryGetValue ( id , out schema ) || ! checkSchemaMatchesFormat ( format , schema . SchemaString ) )
782
+ if ( ! this . schemaById . TryGetValue ( schemaId , out schema ) )
794
783
{
795
784
CleanCacheIfFull ( ) ;
796
- schema = ( await restService . GetSchemaBySubjectAndIdAsync ( subject , id , format )
797
- . ConfigureAwait ( continueOnCapturedContext : false ) ) ;
798
- schemaById [ id ] = schema ;
785
+ schema = await restService . GetSchemaBySubjectAndIdAsync ( subject , id , format )
786
+ . ConfigureAwait ( continueOnCapturedContext : false ) ;
787
+ schemaById . TryAdd ( schemaId , schema ) ;
799
788
}
800
789
801
790
return schema ;
@@ -832,7 +821,8 @@ public async Task<RegisteredSchema> GetRegisteredSchemaAsync(string subject, int
832
821
schema = await restService . GetSchemaAsync ( subject , version )
833
822
. ConfigureAwait ( continueOnCapturedContext : false ) ;
834
823
schemaByVersion [ version ] = schema ;
835
- schemaById [ schema . Id ] = schema . Schema ;
824
+ var format = GetSchemaFormat ( schema . SchemaString ) ;
825
+ schemaById . TryAdd ( new SchemaId ( schema . Id , format ) , schema . Schema ) ;
836
826
}
837
827
838
828
return schema ;
0 commit comments