@@ -39,9 +39,9 @@ class CachedCodecLookup implements CodecLookup {
39
39
40
40
private final Map <Integer , Codec <?>> decodeCodecsCache = new ConcurrentHashMap <>();
41
41
42
- private final Map <Integer , Codec <?>> encodeCodecsCache = new ConcurrentHashMap <>();
42
+ private final Map <Class <?> , Codec <?>> encodeCodecsCache = new ConcurrentHashMap <>();
43
43
44
- private final Map <Integer , Codec <?>> encodeNullCodecsCache = new ConcurrentHashMap <>();
44
+ private final Map <Class <?> , Codec <?>> encodeNullCodecsCache = new ConcurrentHashMap <>();
45
45
46
46
private final CodecLookup delegate ;
47
47
@@ -93,7 +93,7 @@ public void afterCodecAdded() {
93
93
@ Override
94
94
public <T > Codec <T > findDecodeCodec (int dataType , Format format , Class <? extends T > type ) {
95
95
Integer hash = generateCodecHash (dataType , format , type );
96
- return findCodec (hash , this .decodeCodecsCache , () -> {
96
+ return findCodec (hash , dataType , format , type , this .decodeCodecsCache , () -> {
97
97
LOG .trace ("[codec-finder dataType={}, format={}, type={}] Decode codec not found in cache" , dataType , format , type .getName ());
98
98
Codec <T > c = this .delegate .findDecodeCodec (dataType , format , type );
99
99
if (c != null ) {
@@ -105,25 +105,23 @@ public <T> Codec<T> findDecodeCodec(int dataType, Format format, Class<? extends
105
105
106
106
@ Override
107
107
public <T > Codec <T > findEncodeCodec (T value ) {
108
- Integer hash = generateCodecHash (value .getClass ());
109
- return findCodec (hash , this .encodeCodecsCache , () -> {
108
+ return findCodec (value .getClass (), this .encodeCodecsCache , () -> {
110
109
LOG .trace ("[codec-finder type={}] Encode codec not found in cache" , value .getClass ().getName ());
111
110
Codec <T > c = this .delegate .findEncodeCodec (value );
112
111
if (c != null ) {
113
- this .encodeCodecsCache .putIfAbsent (hash , c );
112
+ this .encodeCodecsCache .putIfAbsent (value . getClass () , c );
114
113
}
115
114
return c ;
116
115
});
117
116
}
118
117
119
118
@ Override
120
119
public <T > Codec <T > findEncodeNullCodec (Class <T > type ) {
121
- Integer hash = generateCodecHash (type );
122
- return findCodec (hash , this .encodeNullCodecsCache , () -> {
120
+ return findCodec (type , this .encodeNullCodecsCache , () -> {
123
121
LOG .trace ("[codec-finder type={}] Encode null codec not found in cache" , type .getName ());
124
122
Codec <T > c = this .delegate .findEncodeNullCodec (type );
125
123
if (c != null ) {
126
- this .encodeNullCodecsCache .putIfAbsent (hash , c );
124
+ this .encodeNullCodecsCache .putIfAbsent (type , c );
127
125
}
128
126
return c ;
129
127
});
@@ -135,32 +133,36 @@ private void cacheDecode(Codec<?> c, Class<?> type, PostgresqlObjectId identifie
135
133
}
136
134
137
135
private void cacheEncode (Codec <?> c , Class <?> type ) {
138
- Integer encodeHash = generateCodecHash (type );
139
- this .encodeCodecsCache .putIfAbsent (encodeHash , c );
136
+ this .encodeCodecsCache .putIfAbsent (type , c );
140
137
if (c .canEncodeNull (type )) {
141
- this .encodeNullCodecsCache .putIfAbsent (encodeHash , c );
138
+ this .encodeNullCodecsCache .putIfAbsent (type , c );
142
139
}
143
140
}
144
141
145
142
@ SuppressWarnings ("unchecked" )
146
- private synchronized <T > Codec <T > findCodec (Integer codecHash , Map <Integer , Codec <?>> cache , Supplier <Codec <T >> fallback ) {
147
- Codec <T > value = (Codec <T >) cache .get (codecHash );
143
+ private synchronized <T > Codec <T > findCodec (Class <?> cacheKey , Map <Class <?> , Codec <?>> cache , Supplier <Codec <T >> fallback ) {
144
+ Codec <T > value = (Codec <T >) cache .get (cacheKey );
148
145
return value != null ? value : fallback .get ();
149
146
}
150
147
151
- private static Integer generateCodecHash (int dataType , Format format , Class <?> type ) {
152
- int hash = (dataType << 5 ) - dataType ;
153
- hash = (hash << 5 ) - hash + format .hashCode ();
154
- hash = (hash << 5 ) - hash + generateCodecHash (type );
155
- return hash ;
148
+ @ SuppressWarnings ("unchecked" )
149
+ private synchronized <T > Codec <T > findCodec (Integer cacheKey , int dataType , Format format , Class <? extends T > type , Map <Integer , Codec <?>> cache , Supplier <Codec <T >> fallback ) {
150
+ Codec <T > value = (Codec <T >) cache .get (cacheKey );
151
+ return (value != null && value .canDecode (dataType , format , type )) ? value : fallback .get ();
156
152
}
157
153
158
- private static Integer generateCodecHash (Class <?> type ) {
159
- int hash = type .hashCode ();
160
- if (type .getComponentType () != null ) {
161
- hash = (hash << 5 ) - hash + generateCodecHash (type .getComponentType ());
154
+ private static Integer generateCodecHash (int dataType , Format format , Class <?> type ) {
155
+ int result = 1 ;
156
+
157
+ result = 31 * result + dataType ;
158
+ result = 31 * result + format .hashCode ();
159
+ result = 31 * result + type .hashCode ();
160
+
161
+ if (type .isArray ()) {
162
+ result = 31 * result + type .getComponentType ().hashCode ();
162
163
}
163
- return hash ;
164
+
165
+ return result ;
164
166
}
165
167
166
168
}
0 commit comments