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