@@ -35,23 +35,8 @@ internal class SerializerSchemaData
35
35
{
36
36
private string writerSchemaString ;
37
37
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
-
45
38
private SpecificWriter < T > avroWriter ;
46
39
47
- private HashSet < string > subjectsRegistered = new HashSet < string > ( ) ;
48
-
49
- public HashSet < string > SubjectsRegistered
50
- {
51
- get => subjectsRegistered ;
52
- set => subjectsRegistered = value ;
53
- }
54
-
55
40
public string WriterSchemaString
56
41
{
57
42
get => writerSchemaString ;
@@ -64,12 +49,6 @@ public Avro.Schema WriterSchema
64
49
set => writerSchema = value ;
65
50
}
66
51
67
- public int ? WriterSchemaId
68
- {
69
- get => writerSchemaId ;
70
- set => writerSchemaId = value ;
71
- }
72
-
73
52
public SpecificWriter < T > AvroWriter
74
53
{
75
54
get => avroWriter ;
@@ -79,20 +58,14 @@ public SpecificWriter<T> AvroWriter
79
58
80
59
private Dictionary < Type , SerializerSchemaData > multiSchemaData =
81
60
new Dictionary < Type , SerializerSchemaData > ( ) ;
82
-
83
- private SerializerSchemaData singleSchemaData ;
61
+ private Dictionary < KeyValuePair < string , string > , int > registeredSchemas =
62
+ new Dictionary < KeyValuePair < string , string > , int > ( ) ;
84
63
85
64
public SpecificSerializerImpl (
86
65
ISchemaRegistryClient schemaRegistryClient ,
87
66
AvroSerializerConfig config ,
88
67
RuleRegistry ruleRegistry ) : base ( schemaRegistryClient , config , ruleRegistry )
89
68
{
90
- Type writerType = typeof ( T ) ;
91
- if ( writerType != typeof ( ISpecificRecord ) )
92
- {
93
- singleSchemaData = ExtractSchemaData ( writerType ) ;
94
- }
95
-
96
69
if ( config == null ) { return ; }
97
70
98
71
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
177
150
{
178
151
try
179
152
{
153
+ int schemaId ;
180
154
string subject ;
181
155
RegisteredSchema latestSchema = null ;
182
156
SerializerSchemaData currentSchemaData ;
183
157
await serdeMutex . WaitAsync ( ) . ConfigureAwait ( continueOnCapturedContext : false ) ;
184
158
try
185
159
{
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 ) )
196
162
{
197
- currentSchemaData = singleSchemaData ;
163
+ currentSchemaData = ExtractSchemaData ( key ) ;
164
+ multiSchemaData [ key ] = currentSchemaData ;
198
165
}
199
166
200
167
string fullname = null ;
@@ -204,25 +171,26 @@ public async Task<byte[]> Serialize(string topic, Headers headers, T data, bool
204
171
}
205
172
206
173
subject = GetSubjectName ( topic , isKey , fullname ) ;
174
+ var subjectSchemaPair = new KeyValuePair < string , string > ( subject , currentSchemaData . WriterSchemaString ) ;
207
175
latestSchema = await GetReaderSchema ( subject )
208
176
. ConfigureAwait ( continueOnCapturedContext : false ) ;
209
177
210
178
if ( latestSchema != null )
211
179
{
212
- currentSchemaData . WriterSchemaId = latestSchema . Id ;
180
+ schemaId = latestSchema . Id ;
213
181
}
214
- else if ( ! currentSchemaData . SubjectsRegistered . Contains ( subject ) )
182
+ else if ( ! registeredSchemas . TryGetValue ( subjectSchemaPair , out schemaId ) )
215
183
{
216
184
// first usage: register/get schema to check compatibility
217
- currentSchemaData . WriterSchemaId = autoRegisterSchema
185
+ schemaId = autoRegisterSchema
218
186
? await schemaRegistryClient
219
187
. RegisterSchemaAsync ( subject , currentSchemaData . WriterSchemaString , normalizeSchemas )
220
188
. ConfigureAwait ( continueOnCapturedContext : false )
221
189
: await schemaRegistryClient
222
190
. GetSchemaIdAsync ( subject , currentSchemaData . WriterSchemaString , normalizeSchemas )
223
191
. ConfigureAwait ( continueOnCapturedContext : false ) ;
224
192
225
- currentSchemaData . SubjectsRegistered . Add ( subject ) ;
193
+ registeredSchemas . Add ( subjectSchemaPair , schemaId ) ;
226
194
}
227
195
}
228
196
finally
@@ -248,7 +216,7 @@ public async Task<byte[]> Serialize(string topic, Headers headers, T data, bool
248
216
{
249
217
stream . WriteByte ( Constants . MagicByte ) ;
250
218
251
- writer . Write ( IPAddress . HostToNetworkOrder ( currentSchemaData . WriterSchemaId . Value ) ) ;
219
+ writer . Write ( IPAddress . HostToNetworkOrder ( schemaId ) ) ;
252
220
currentSchemaData . AvroWriter . Write ( data , new BinaryEncoder ( stream ) ) ;
253
221
254
222
// TODO: maybe change the ISerializer interface so that this copy isn't necessary.
0 commit comments