|
22 | 22 | import reactor.util.Loggers;
|
23 | 23 |
|
24 | 24 | import java.lang.reflect.Array;
|
25 |
| -import java.util.Collections; |
26 |
| -import java.util.List; |
| 25 | +import java.util.Iterator; |
27 | 26 | import java.util.Map;
|
28 | 27 | import java.util.Optional;
|
29 | 28 | import java.util.concurrent.ConcurrentHashMap;
|
30 | 29 | import java.util.function.Supplier;
|
31 | 30 |
|
32 | 31 | /**
|
33 |
| - * Cache implementation of the {@link CodecFinder}. This will keep the relevant {@link Codec} for the type, format and database type cached for faster access. |
34 |
| - * In case the {@link Codec} can't be found in the cache, a fallback search using {@link CodecFinderDefaultImpl} will be done. |
| 32 | + * Cache implementation of the {@link CodecLookup}. This will keep the relevant {@link Codec} for the type, format and database type cached for faster access. |
| 33 | + * In case the {@link Codec} can't be found in the cache, a fallback search using {@link DefaultCodecLookup} will be done. |
| 34 | + * |
| 35 | + * @since 0.9 |
35 | 36 | */
|
36 |
| -public class CodecFinderCacheImpl implements CodecFinder { |
37 |
| - |
38 |
| - private static final Logger LOG = Loggers.getLogger(CodecFinderCacheImpl.class); |
| 37 | +class CachedCodecLookup implements CodecLookup { |
39 | 38 |
|
40 |
| - List<Codec<?>> codecs = Collections.emptyList(); |
| 39 | + private static final Logger LOG = Loggers.getLogger(CachedCodecLookup.class); |
41 | 40 |
|
42 | 41 | private final Map<Integer, Codec<?>> decodeCodecsCache = new ConcurrentHashMap<>();
|
43 | 42 |
|
44 | 43 | private final Map<Integer, Codec<?>> encodeCodecsCache = new ConcurrentHashMap<>();
|
45 | 44 |
|
46 | 45 | private final Map<Integer, Codec<?>> encodeNullCodecsCache = new ConcurrentHashMap<>();
|
47 | 46 |
|
48 |
| - private final CodecFinder fallBackFinder; |
| 47 | + private final CodecLookup delegate; |
49 | 48 |
|
50 |
| - public CodecFinderCacheImpl() { |
51 |
| - this(new CodecFinderDefaultImpl()); |
| 49 | + public CachedCodecLookup(Iterable<Codec<?>> codecRegistry) { |
| 50 | + this.delegate = new DefaultCodecLookup(codecRegistry); |
52 | 51 | }
|
53 | 52 |
|
54 |
| - public CodecFinderCacheImpl(CodecFinder fallBackFinder) { |
55 |
| - Assert.requireNonType(fallBackFinder, CodecFinderCacheImpl.class, "fallBackFinder must not be of type CodecFinderCacheImpl"); |
56 |
| - this.fallBackFinder = fallBackFinder; |
| 53 | + public CachedCodecLookup(CodecLookup delegate) { |
| 54 | + Assert.requireNonType(delegate, CachedCodecLookup.class, "delegate must not be of type CodecFinderCacheImpl"); |
| 55 | + this.delegate = delegate; |
57 | 56 | }
|
58 | 57 |
|
59 |
| - private static <T> int generateCodecHash(int dataType, Format format, Class<? extends T> type) { |
60 |
| - int hash = (dataType << 5) - dataType; |
61 |
| - hash = (hash << 5) - hash + format.hashCode(); |
62 |
| - hash = (hash << 5) - hash + generateCodecHash(type); |
63 |
| - return hash; |
| 58 | + @Override |
| 59 | + public Iterator<Codec<?>> iterator() { |
| 60 | + return this.delegate.iterator(); |
64 | 61 | }
|
65 | 62 |
|
66 |
| - private static <T> int generateCodecHash(Class<? extends T> type) { |
67 |
| - int hash = type.hashCode(); |
68 |
| - if (type.getComponentType() != null) { |
69 |
| - hash = (hash << 5) - hash + generateCodecHash(type.getComponentType()); |
70 |
| - } |
71 |
| - return hash; |
72 |
| - } |
| 63 | + @Override |
| 64 | + public void afterCodecAdded() { |
73 | 65 |
|
74 |
| - void invalidateCaches() { |
75 | 66 | this.decodeCodecsCache.clear();
|
76 | 67 | this.encodeCodecsCache.clear();
|
77 | 68 | this.encodeNullCodecsCache.clear();
|
78 |
| - buildCaches(); |
79 |
| - } |
80 | 69 |
|
81 |
| - void buildCaches() { |
82 |
| - for (Codec<?> c : this.codecs) { |
| 70 | + for (Codec<?> c : this.delegate) { |
83 | 71 | Optional<Class<?>> arrayClass = Optional.empty();
|
84 | 72 | if (c instanceof ArrayCodec) {
|
85 | 73 | ArrayCodec<?> ac = (ArrayCodec<?>) c;
|
86 | 74 | arrayClass = Optional.of(Array.newInstance(ac.getComponentType(), 0).getClass());
|
87 | 75 | }
|
88 |
| - cacheEncode(c, c.type()); |
89 |
| - arrayClass.ifPresent(ac -> cacheEncode(c, ac)); |
90 |
| - for (PostgresTypeIdentifier identifier : c.getDataTypes()) { |
91 |
| - for (Format format : c.getFormats()) { |
92 |
| - cacheDecode(c, c.type(), identifier, format); |
93 |
| - arrayClass.ifPresent(ac -> cacheDecode(c, ac, identifier, format)); |
| 76 | + |
| 77 | + if (c instanceof CodecMetadata) { |
| 78 | + CodecMetadata metadata = (CodecMetadata) c; |
| 79 | + cacheEncode(c, metadata.type()); |
| 80 | + arrayClass.ifPresent(ac -> cacheEncode(c, ac)); |
| 81 | + for (PostgresTypeIdentifier identifier : metadata.getDataTypes()) { |
| 82 | + for (Format format : metadata.getFormats()) { |
| 83 | + cacheDecode(c, metadata.type(), identifier, format); |
| 84 | + arrayClass.ifPresent(ac -> cacheDecode(c, ac, identifier, format)); |
| 85 | + } |
94 | 86 | }
|
95 | 87 | }
|
96 | 88 | }
|
97 | 89 | // Handle decode to Object.class support
|
98 | 90 | for (PostgresqlObjectId identifier : PostgresqlObjectId.values()) {
|
99 | 91 | for (Format format : Format.all()) {
|
100 |
| - Codec<?> c = fallBackFinder.findDecodeCodec(identifier.getObjectId(), format, Object.class); |
| 92 | + Codec<?> c = this.delegate.findDecodeCodec(identifier.getObjectId(), format, Object.class); |
101 | 93 | if (c != null) {
|
102 | 94 | cacheDecode(c, Object.class, identifier, format);
|
103 | 95 | }
|
104 | 96 | }
|
105 | 97 | }
|
106 | 98 | }
|
107 | 99 |
|
108 |
| - private void cacheDecode(Codec<?> c, Class<?> type, PostgresTypeIdentifier identifier, Format format) { |
109 |
| - int decodeHash = generateCodecHash(identifier.getObjectId(), format, type); |
110 |
| - decodeCodecsCache.putIfAbsent(decodeHash, c); |
111 |
| - } |
112 |
| - |
113 |
| - private void cacheEncode(Codec<?> c, Class<?> type) { |
114 |
| - int encodeHash = generateCodecHash(type); |
115 |
| - encodeCodecsCache.putIfAbsent(encodeHash, c); |
116 |
| - if (c.canEncodeNull(type)) { |
117 |
| - encodeNullCodecsCache.putIfAbsent(encodeHash, c); |
118 |
| - } |
119 |
| - } |
120 |
| - |
121 |
| - @SuppressWarnings("unchecked") |
122 |
| - synchronized <T> Codec<T> findCodec(int codecHash, Map<Integer, Codec<?>> cache, Supplier<Codec<T>> fallback) { |
123 |
| - return Optional.ofNullable((Codec<T>) cache.get(codecHash)).orElseGet(fallback); |
124 |
| - } |
125 |
| - |
126 |
| - @Override |
127 |
| - public synchronized void updateCodecs(List<Codec<?>> codecs) { |
128 |
| - this.codecs = codecs; |
129 |
| - fallBackFinder.updateCodecs(codecs); |
130 |
| - invalidateCaches(); |
131 |
| - } |
132 |
| - |
133 | 100 | @Override
|
134 | 101 | public <T> Codec<T> findDecodeCodec(int dataType, Format format, Class<? extends T> type) {
|
135 |
| - int hash = generateCodecHash(dataType, format, type); |
136 |
| - return findCodec(hash, decodeCodecsCache, () -> { |
| 102 | + Integer hash = generateCodecHash(dataType, format, type); |
| 103 | + return findCodec(hash, this.decodeCodecsCache, () -> { |
137 | 104 | LOG.trace("[codec-finder dataType={}, format={}, type={}] Decode codec not found in cache", dataType, format, type.getName());
|
138 |
| - Codec<T> c = fallBackFinder.findDecodeCodec(dataType, format, type); |
| 105 | + Codec<T> c = this.delegate.findDecodeCodec(dataType, format, type); |
139 | 106 | if (c != null) {
|
140 |
| - decodeCodecsCache.putIfAbsent(hash, c); |
| 107 | + this.decodeCodecsCache.putIfAbsent(hash, c); |
141 | 108 | }
|
142 | 109 | return c;
|
143 | 110 | });
|
144 | 111 | }
|
145 | 112 |
|
146 | 113 | @Override
|
147 | 114 | public <T> Codec<T> findEncodeCodec(T value) {
|
148 |
| - int hash = generateCodecHash(value.getClass()); |
149 |
| - return findCodec(hash, encodeCodecsCache, () -> { |
| 115 | + Integer hash = generateCodecHash(value.getClass()); |
| 116 | + return findCodec(hash, this.encodeCodecsCache, () -> { |
150 | 117 | LOG.trace("[codec-finder type={}] Encode codec not found in cache", value.getClass().getName());
|
151 |
| - Codec<T> c = fallBackFinder.findEncodeCodec(value); |
| 118 | + Codec<T> c = this.delegate.findEncodeCodec(value); |
152 | 119 | if (c != null) {
|
153 |
| - encodeCodecsCache.putIfAbsent(hash, c); |
| 120 | + this.encodeCodecsCache.putIfAbsent(hash, c); |
154 | 121 | }
|
155 | 122 | return c;
|
156 | 123 | });
|
157 | 124 | }
|
158 | 125 |
|
159 | 126 | @Override
|
160 | 127 | public <T> Codec<T> findEncodeNullCodec(Class<T> type) {
|
161 |
| - int hash = generateCodecHash(type); |
162 |
| - return findCodec(hash, encodeNullCodecsCache, () -> { |
| 128 | + Integer hash = generateCodecHash(type); |
| 129 | + return findCodec(hash, this.encodeNullCodecsCache, () -> { |
163 | 130 | LOG.trace("[codec-finder type={}] Encode null codec not found in cache", type.getName());
|
164 |
| - Codec<T> c = fallBackFinder.findEncodeNullCodec(type); |
| 131 | + Codec<T> c = this.delegate.findEncodeNullCodec(type); |
165 | 132 | if (c != null) {
|
166 |
| - encodeNullCodecsCache.putIfAbsent(hash, c); |
| 133 | + this.encodeNullCodecsCache.putIfAbsent(hash, c); |
167 | 134 | }
|
168 | 135 | return c;
|
169 | 136 | });
|
170 | 137 | }
|
171 | 138 |
|
| 139 | + private void cacheDecode(Codec<?> c, Class<?> type, PostgresTypeIdentifier identifier, Format format) { |
| 140 | + Integer decodeHash = generateCodecHash(identifier.getObjectId(), format, type); |
| 141 | + this.decodeCodecsCache.putIfAbsent(decodeHash, c); |
| 142 | + } |
| 143 | + |
| 144 | + private void cacheEncode(Codec<?> c, Class<?> type) { |
| 145 | + Integer encodeHash = generateCodecHash(type); |
| 146 | + this.encodeCodecsCache.putIfAbsent(encodeHash, c); |
| 147 | + if (c.canEncodeNull(type)) { |
| 148 | + this.encodeNullCodecsCache.putIfAbsent(encodeHash, c); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @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); |
| 155 | + return value != null ? value : fallback.get(); |
| 156 | + } |
| 157 | + |
| 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; |
| 163 | + } |
| 164 | + |
| 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()); |
| 169 | + } |
| 170 | + return hash; |
| 171 | + } |
| 172 | + |
172 | 173 | }
|
0 commit comments