@@ -77,12 +77,13 @@ public class CachedSchemaRegistryClient implements SchemaRegistryClient {
7777
7878 private final RestService restService ;
7979 private final int cacheCapacity ;
80- private final Cache <String , Cache <ParsedSchema , RegisterSchemaResponse >> schemaToResponseCache ;
81- private final Cache <String , Cache <ParsedSchema , Integer >> schemaToIdCache ;
80+ private final Cache <String , Cache <SchemaAndNormalize , RegisterSchemaResponse >>
81+ schemaToResponseCache ;
82+ private final Cache <String , Cache <SchemaAndNormalize , Integer >> schemaToIdCache ;
8283 private final Cache <String , Cache <Integer , ParsedSchema >> idToSchemaCache ;
8384 private final Cache <String , ParsedSchema > guidToSchemaCache ;
84- private final Cache <String , Cache <ParsedSchema , String >> schemaToGuidCache ;
85- private final Cache <String , Cache <ParsedSchema , Integer >> schemaToVersionCache ;
85+ private final Cache <String , Cache <SchemaAndNormalize , String >> schemaToGuidCache ;
86+ private final Cache <String , Cache <SchemaAndNormalize , Integer >> schemaToVersionCache ;
8687 private final Cache <String , Cache <Integer , Schema >> versionToSchemaCache ;
8788 private final Cache <String , SchemaMetadata > latestVersionCache ;
8889 private final Cache <SubjectAndMetadata , SchemaMetadata > latestWithMetadataCache ;
@@ -524,26 +525,27 @@ private RegisterSchemaResponse registerWithResponse(
524525 boolean normalize , boolean propagateSchemaTags )
525526 throws IOException , RestClientException {
526527 try {
527- final Cache <ParsedSchema , RegisterSchemaResponse > schemaResponseMap =
528+ final Cache <SchemaAndNormalize , RegisterSchemaResponse > schemaResponseMap =
528529 schemaToResponseCache .get (subject , () -> CacheBuilder .newBuilder ()
529530 .maximumSize (cacheCapacity )
530531 .build ());
531532
532- RegisterSchemaResponse cachedResponse = schemaResponseMap .getIfPresent (schema );
533+ SchemaAndNormalize cacheKey = new SchemaAndNormalize (schema , normalize );
534+ RegisterSchemaResponse cachedResponse = schemaResponseMap .getIfPresent (cacheKey );
533535 if (cachedResponse != null && (id < 0 || id == cachedResponse .getId ())) {
534536 return cachedResponse ;
535537 }
536538
537539 synchronized (this ) {
538- cachedResponse = schemaResponseMap .getIfPresent (schema );
540+ cachedResponse = schemaResponseMap .getIfPresent (cacheKey );
539541 if (cachedResponse != null && (id < 0 || id == cachedResponse .getId ())) {
540542 return cachedResponse ;
541543 }
542544
543545 final RegisterSchemaResponse retrievedResponse = id >= 0
544546 ? registerAndGetId (subject , schema , version , id , normalize , propagateSchemaTags )
545547 : registerAndGetId (subject , schema , normalize , propagateSchemaTags );
546- schemaResponseMap .put (schema , retrievedResponse );
548+ schemaResponseMap .put (cacheKey , retrievedResponse );
547549 if (retrievedResponse .getSchema () != null ) {
548550 String context = toQualifiedContext (subject );
549551 final Cache <Integer , ParsedSchema > idSchemaMap = idToSchemaCache .get (
@@ -767,24 +769,25 @@ public int getVersion(String subject, ParsedSchema schema)
767769 public int getVersion (String subject , ParsedSchema schema , boolean normalize )
768770 throws IOException , RestClientException {
769771 try {
770- final Cache <ParsedSchema , Integer > schemaVersionMap = schemaToVersionCache .get (
772+ final Cache <SchemaAndNormalize , Integer > schemaVersionMap = schemaToVersionCache .get (
771773 subject , () -> CacheBuilder .newBuilder ()
772774 .maximumSize (cacheCapacity )
773775 .build ());
774776
775- Integer cachedVersion = schemaVersionMap .getIfPresent (schema );
777+ SchemaAndNormalize cacheKey = new SchemaAndNormalize (schema , normalize );
778+ Integer cachedVersion = schemaVersionMap .getIfPresent (cacheKey );
776779 if (cachedVersion != null ) {
777780 return cachedVersion ;
778781 }
779782
780783 synchronized (this ) {
781- cachedVersion = schemaVersionMap .getIfPresent (schema );
784+ cachedVersion = schemaVersionMap .getIfPresent (cacheKey );
782785 if (cachedVersion != null ) {
783786 return cachedVersion ;
784787 }
785788
786789 final int retrievedVersion = getVersionFromRegistry (subject , schema , normalize );
787- schemaVersionMap .put (schema , retrievedVersion );
790+ schemaVersionMap .put (cacheKey , retrievedVersion );
788791 return retrievedVersion ;
789792 }
790793 } catch (ExecutionException e ) {
@@ -815,26 +818,27 @@ public int getId(String subject, ParsedSchema schema)
815818 public int getId (String subject , ParsedSchema schema , boolean normalize )
816819 throws IOException , RestClientException {
817820 try {
818- final Cache <ParsedSchema , Integer > schemaIdMap = schemaToIdCache .get (
821+ final Cache <SchemaAndNormalize , Integer > schemaIdMap = schemaToIdCache .get (
819822 subject , () -> CacheBuilder .newBuilder ()
820823 .maximumSize (cacheCapacity )
821824 .build ());
822825
823- Integer cachedId = schemaIdMap .getIfPresent (schema );
826+ SchemaAndNormalize cacheKey = new SchemaAndNormalize (schema , normalize );
827+ Integer cachedId = schemaIdMap .getIfPresent (cacheKey );
824828 if (cachedId != null ) {
825829 return cachedId ;
826830 }
827831
828832 synchronized (this ) {
829- cachedId = schemaIdMap .getIfPresent (schema );
833+ cachedId = schemaIdMap .getIfPresent (cacheKey );
830834 if (cachedId != null ) {
831835 return cachedId ;
832836 }
833837
834838 final RegisterSchemaResponse retrievedResponse =
835839 getIdWithResponseFromRegistry (subject , schema , normalize , false );
836840 final int retrievedId = retrievedResponse .getId ();
837- schemaIdMap .put (schema , retrievedId );
841+ schemaIdMap .put (cacheKey , retrievedId );
838842 if (retrievedResponse .getSchema () != null ) {
839843 String context = toQualifiedContext (subject );
840844 final Cache <Integer , ParsedSchema > idSchemaMap = idToSchemaCache .get (
@@ -861,24 +865,25 @@ public String getGuid(
861865 String subject , ParsedSchema schema , boolean normalize )
862866 throws IOException , RestClientException {
863867 try {
864- final Cache <ParsedSchema , String > guidMap = schemaToGuidCache .get (
868+ final Cache <SchemaAndNormalize , String > guidMap = schemaToGuidCache .get (
865869 subject , () -> CacheBuilder .newBuilder ()
866870 .maximumSize (cacheCapacity )
867871 .build ());
868872
869- String cachedGuid = guidMap .getIfPresent (schema );
873+ SchemaAndNormalize cacheKey = new SchemaAndNormalize (schema , normalize );
874+ String cachedGuid = guidMap .getIfPresent (cacheKey );
870875 if (cachedGuid != null ) {
871876 return cachedGuid ;
872877 }
873878
874879 synchronized (this ) {
875- cachedGuid = guidMap .getIfPresent (schema );
880+ cachedGuid = guidMap .getIfPresent (cacheKey );
876881 if (cachedGuid != null ) {
877882 return cachedGuid ;
878883 }
879884
880885 final String retrievedGuid = getGuidFromRegistry (subject , schema , normalize );
881- guidMap .put (schema , retrievedGuid );
886+ guidMap .put (cacheKey , retrievedGuid );
882887 guidToSchemaCache .put (retrievedGuid , schema );
883888 return retrievedGuid ;
884889 }
@@ -892,12 +897,13 @@ public RegisterSchemaResponse getIdWithResponse(
892897 String subject , ParsedSchema schema , boolean normalize )
893898 throws IOException , RestClientException {
894899 try {
895- final Cache <ParsedSchema , RegisterSchemaResponse > schemaResponseMap =
900+ final Cache <SchemaAndNormalize , RegisterSchemaResponse > schemaResponseMap =
896901 schemaToResponseCache .get (subject , () -> CacheBuilder .newBuilder ()
897902 .maximumSize (cacheCapacity )
898903 .build ());
899904
900- RegisterSchemaResponse cachedResponse = schemaResponseMap .getIfPresent (schema );
905+ SchemaAndNormalize cacheKey = new SchemaAndNormalize (schema , normalize );
906+ RegisterSchemaResponse cachedResponse = schemaResponseMap .getIfPresent (cacheKey );
901907 if (cachedResponse != null ) {
902908 // Allow the schema to be looked up again if version is not valid
903909 // This is for backward compatibility with versions before CP 8.0
@@ -907,7 +913,7 @@ public RegisterSchemaResponse getIdWithResponse(
907913 }
908914
909915 synchronized (this ) {
910- cachedResponse = schemaResponseMap .getIfPresent (schema );
916+ cachedResponse = schemaResponseMap .getIfPresent (cacheKey );
911917 if (cachedResponse != null ) {
912918 // Allow the schema to be looked up again if version is not valid
913919 // This is for backward compatibility with versions before CP 8.0
@@ -918,7 +924,7 @@ public RegisterSchemaResponse getIdWithResponse(
918924
919925 final RegisterSchemaResponse retrievedResponse =
920926 getIdWithResponseFromRegistry (subject , schema , normalize , false );
921- schemaResponseMap .put (schema , retrievedResponse );
927+ schemaResponseMap .put (cacheKey , retrievedResponse );
922928 if (retrievedResponse .getSchema () != null ) {
923929 String context = toQualifiedContext (subject );
924930 final Cache <Integer , ParsedSchema > idSchemaMap = idToSchemaCache .get (
@@ -972,7 +978,7 @@ public synchronized Integer deleteSchemaVersion(
972978 String version ,
973979 boolean isPermanent )
974980 throws IOException , RestClientException {
975- Cache <ParsedSchema , Integer > versionCache = schemaToVersionCache .getIfPresent (subject );
981+ Cache <SchemaAndNormalize , Integer > versionCache = schemaToVersionCache .getIfPresent (subject );
976982 if (versionCache != null ) {
977983 versionCache .asMap ().values ().remove (Integer .valueOf (version ));
978984 }
@@ -1205,6 +1211,33 @@ private static String toQualifiedContext(String subject) {
12051211 return qualifiedSubject != null ? qualifiedSubject .toQualifiedContext () : NO_SUBJECT ;
12061212 }
12071213
1214+ static class SchemaAndNormalize {
1215+ private final ParsedSchema schema ;
1216+ private final boolean normalize ;
1217+
1218+ SchemaAndNormalize (ParsedSchema schema , boolean normalize ) {
1219+ this .schema = schema ;
1220+ this .normalize = normalize ;
1221+ }
1222+
1223+ @ Override
1224+ public boolean equals (Object o ) {
1225+ if (this == o ) {
1226+ return true ;
1227+ }
1228+ if (o == null || getClass () != o .getClass ()) {
1229+ return false ;
1230+ }
1231+ SchemaAndNormalize that = (SchemaAndNormalize ) o ;
1232+ return normalize == that .normalize && schema .equals (that .schema );
1233+ }
1234+
1235+ @ Override
1236+ public int hashCode () {
1237+ return Objects .hash (schema , normalize );
1238+ }
1239+ }
1240+
12081241 static class SubjectAndSchema {
12091242 private final String subject ;
12101243 private final ParsedSchema schema ;
0 commit comments