@@ -35,23 +35,8 @@ internal class SerializerSchemaData
3535 {
3636 private string writerSchemaString ;
3737 private global ::Avro . Schema writerSchema ;
38-
39- /// <remarks>
40- /// A given schema is uniquely identified by a schema id, even when
41- /// registered against multiple subjects.
42- /// </remarks>
43- private int ? writerSchemaId ;
44-
4538 private SpecificWriter < T > avroWriter ;
4639
47- private HashSet < string > subjectsRegistered = new HashSet < string > ( ) ;
48-
49- public HashSet < string > SubjectsRegistered
50- {
51- get => subjectsRegistered ;
52- set => subjectsRegistered = value ;
53- }
54-
5540 public string WriterSchemaString
5641 {
5742 get => writerSchemaString ;
@@ -64,12 +49,6 @@ public Avro.Schema WriterSchema
6449 set => writerSchema = value ;
6550 }
6651
67- public int ? WriterSchemaId
68- {
69- get => writerSchemaId ;
70- set => writerSchemaId = value ;
71- }
72-
7352 public SpecificWriter < T > AvroWriter
7453 {
7554 get => avroWriter ;
@@ -79,20 +58,14 @@ public SpecificWriter<T> AvroWriter
7958
8059 private Dictionary < Type , SerializerSchemaData > multiSchemaData =
8160 new Dictionary < Type , SerializerSchemaData > ( ) ;
82-
83- private SerializerSchemaData singleSchemaData ;
61+ private Dictionary < KeyValuePair < string , string > , int > registeredSchemas =
62+ new Dictionary < KeyValuePair < string , string > , int > ( ) ;
8463
8564 public SpecificSerializerImpl (
8665 ISchemaRegistryClient schemaRegistryClient ,
8766 AvroSerializerConfig config ,
8867 RuleRegistry ruleRegistry ) : base ( schemaRegistryClient , config , ruleRegistry )
8968 {
90- Type writerType = typeof ( T ) ;
91- if ( writerType != typeof ( ISpecificRecord ) )
92- {
93- singleSchemaData = ExtractSchemaData ( writerType ) ;
94- }
95-
9669 if ( config == null ) { return ; }
9770
9871 if ( config . BufferBytes != null ) { this . initialBufferSize = config . BufferBytes . Value ; }
@@ -177,24 +150,18 @@ public async Task<byte[]> Serialize(string topic, Headers headers, T data, bool
177150 {
178151 try
179152 {
153+ int schemaId ;
180154 string subject ;
181155 RegisteredSchema latestSchema = null ;
182156 SerializerSchemaData currentSchemaData ;
183157 await serdeMutex . WaitAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
184158 try
185159 {
186- if ( singleSchemaData == null )
187- {
188- var key = data . GetType ( ) ;
189- if ( ! multiSchemaData . TryGetValue ( key , out currentSchemaData ) )
190- {
191- currentSchemaData = ExtractSchemaData ( key ) ;
192- multiSchemaData [ key ] = currentSchemaData ;
193- }
194- }
195- else
160+ var key = data != null ? data . GetType ( ) : typeof ( Null ) ;
161+ if ( ! multiSchemaData . TryGetValue ( key , out currentSchemaData ) )
196162 {
197- currentSchemaData = singleSchemaData ;
163+ currentSchemaData = ExtractSchemaData ( key ) ;
164+ multiSchemaData [ key ] = currentSchemaData ;
198165 }
199166
200167 string fullname = null ;
@@ -204,25 +171,26 @@ public async Task<byte[]> Serialize(string topic, Headers headers, T data, bool
204171 }
205172
206173 subject = GetSubjectName ( topic , isKey , fullname ) ;
174+ var subjectSchemaPair = new KeyValuePair < string , string > ( subject , currentSchemaData . WriterSchemaString ) ;
207175 latestSchema = await GetReaderSchema ( subject )
208176 . ConfigureAwait ( continueOnCapturedContext : false ) ;
209177
210178 if ( latestSchema != null )
211179 {
212- currentSchemaData . WriterSchemaId = latestSchema . Id ;
180+ schemaId = latestSchema . Id ;
213181 }
214- else if ( ! currentSchemaData . SubjectsRegistered . Contains ( subject ) )
182+ else if ( ! registeredSchemas . TryGetValue ( subjectSchemaPair , out schemaId ) )
215183 {
216184 // first usage: register/get schema to check compatibility
217- currentSchemaData . WriterSchemaId = autoRegisterSchema
185+ schemaId = autoRegisterSchema
218186 ? await schemaRegistryClient
219187 . RegisterSchemaAsync ( subject , currentSchemaData . WriterSchemaString , normalizeSchemas )
220188 . ConfigureAwait ( continueOnCapturedContext : false )
221189 : await schemaRegistryClient
222190 . GetSchemaIdAsync ( subject , currentSchemaData . WriterSchemaString , normalizeSchemas )
223191 . ConfigureAwait ( continueOnCapturedContext : false ) ;
224192
225- currentSchemaData . SubjectsRegistered . Add ( subject ) ;
193+ registeredSchemas . Add ( subjectSchemaPair , schemaId ) ;
226194 }
227195 }
228196 finally
@@ -248,7 +216,7 @@ public async Task<byte[]> Serialize(string topic, Headers headers, T data, bool
248216 {
249217 stream . WriteByte ( Constants . MagicByte ) ;
250218
251- writer . Write ( IPAddress . HostToNetworkOrder ( currentSchemaData . WriterSchemaId . Value ) ) ;
219+ writer . Write ( IPAddress . HostToNetworkOrder ( schemaId ) ) ;
252220 currentSchemaData . AvroWriter . Write ( data , new BinaryEncoder ( stream ) ) ;
253221
254222 // TODO: maybe change the ISerializer interface so that this copy isn't necessary.
0 commit comments