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