Elffuss BraidHub

kikocis/java-driver public

Warn if a custom codec collides with an existing one (JAVA-934).
Changeset7726f0cb3c916e67bc4de116e1eb267ea44d72a5b215cac11a4c2abc72cec690
Authorolim7t <omichallat+github@gmail.com> · 2015-10-08 16:55 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-10-14 22:35 +02:00
Parents0fef13bfb9a0

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -13,6 +13,7 @@
1313 - [bug] Add crcCheckChance to TableOptionsMetadata#equals/hashCode (JAVA-939)
1414 - [bug] Make TypeCodec return mutable collections (JAVA-922)
1515 - [improvement] Limit visibility of codec internals (JAVA-932)
16+- [improvement] Warn if a custom codec collides with an existing one (JAVA-934)
1617
1718
1819 ### 3.0.0-alpha3
modified driver-core/src/main/java/com/datastax/driver/core/CodecRegistry.java +32 −7
@@ -320,12 +320,32 @@
320320
321321 /**
322322 * Register the given codec with this registry.
323+ * <p>
324+ * This method will log a warning and ignore the codec if it collides with a previously registered one.
325+ * Note that this check is not done in a completely thread-safe manner; codecs should typically be registered
326+ * at application startup, not in a highly concurrent context (if a race condition occurs, the worst outcome
327+ * is that no warning gets logged, and the codec gets registered but will never actually be used).
323328 *
324- * @param codec The codec to add to the registry.
329+ * @param newCodec The codec to add to the registry.
325330 * @return this CodecRegistry (for method chaining).
326331 */
327- public CodecRegistry register(TypeCodec<?> codec) {
328- return register(Collections.singleton(codec));
332+ public CodecRegistry register(TypeCodec<?> newCodec) {
333+ for (TypeCodec<?> oldCodec : codecs) {
334+ if (oldCodec.accepts(newCodec.getCqlType()) && oldCodec.accepts(newCodec.getJavaType())) {
335+ logger.warn("Ignoring codec {} because it collides with previously registered codec {}", newCodec, oldCodec);
336+ return this;
337+ }
338+ }
339+
340+ CacheKey key = new CacheKey(newCodec.getCqlType(), newCodec.getJavaType());
341+ TypeCodec<?> existing = cache.getIfPresent(key);
342+ if (existing != null) {
343+ logger.warn("Ignoring codec {} because it collides with previously generated codec {}", newCodec, existing);
344+ return this;
345+ }
346+
347+ this.codecs.add(newCodec);
348+ return this;
329349 }
330350
331351 /**
@@ -333,9 +353,13 @@
333353 *
334354 * @param codecs The codecs to add to the registry.
335355 * @return this Builder (for method chaining).
356+ *
357+ * @see #register(TypeCodec)
336358 */
337359 public CodecRegistry register(TypeCodec<?>... codecs) {
338- return register(Arrays.asList(codecs));
360+ for (TypeCodec<?> codec : codecs)
361+ register(codec);
362+ return this;
339363 }
340364
341365 /**
@@ -343,11 +367,12 @@
343367 *
344368 * @param codecs The codecs to add to the registry.
345369 * @return this Builder (for method chaining).
370+ *
371+ * @see #register(TypeCodec)
346372 */
347373 public CodecRegistry register(Iterable<? extends TypeCodec<?>> codecs) {
348- for (TypeCodec<?> codec : codecs) {
349- this.codecs.add(codec);
350- }
374+ for (TypeCodec<?> codec : codecs)
375+ register(codec);
351376 return this;
352377 }
353378
added driver-core/src/test/java/com/datastax/driver/core/CodecRegistryTest.java +92 −0
@@ -0,0 +1,92 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core;
17+
18+import java.util.List;
19+
20+import com.google.common.reflect.TypeToken;
21+import org.apache.log4j.Level;
22+import org.apache.log4j.Logger;
23+import org.testng.annotations.Test;
24+
25+import static org.mockito.Mockito.mock;
26+import static org.mockito.Mockito.when;
27+
28+import static com.datastax.driver.core.Assertions.assertThat;
29+
30+public class CodecRegistryTest {
31+ TypeToken<List<Integer>> LIST_OF_INT_TOKEN = new TypeToken<List<Integer>>() {
32+ };
33+
34+ @Test(groups = "unit")
35+ public void should_ignore_codec_colliding_with_already_registered_codec() {
36+ MemoryAppender logs = startCapturingLogs();
37+
38+ CodecRegistry registry = new CodecRegistry();
39+
40+ TypeCodec newCodec = mock(TypeCodec.class);
41+ when(newCodec.getCqlType()).thenReturn(DataType.cint());
42+ when(newCodec.getJavaType()).thenReturn(TypeToken.of(Integer.class));
43+ when(newCodec.toString()).thenReturn("newCodec");
44+
45+ registry.register(newCodec);
46+
47+ assertThat(logs.getNext()).contains("Ignoring codec newCodec");
48+ assertThat(
49+ registry.codecFor(DataType.cint(), Integer.class)
50+ ).isNotSameAs(newCodec);
51+
52+ stopCapturingLogs(logs);
53+ }
54+
55+ @Test(groups = "unit")
56+ public void should_ignore_codec_colliding_with_already_generated_codec() {
57+ MemoryAppender logs = startCapturingLogs();
58+
59+ CodecRegistry registry = new CodecRegistry();
60+
61+ // Force generation of a list token from the default token
62+ registry.codecFor(DataType.list(DataType.cint()), LIST_OF_INT_TOKEN);
63+
64+ TypeCodec newCodec = mock(TypeCodec.class);
65+ when(newCodec.getCqlType()).thenReturn(DataType.list(DataType.cint()));
66+ when(newCodec.getJavaType()).thenReturn(LIST_OF_INT_TOKEN);
67+ when(newCodec.toString()).thenReturn("newCodec");
68+
69+ registry.register(newCodec);
70+
71+ assertThat(logs.getNext()).contains("Ignoring codec newCodec");
72+ assertThat(
73+ registry.codecFor(DataType.list(DataType.cint()), LIST_OF_INT_TOKEN)
74+ ).isNotSameAs(newCodec);
75+
76+ stopCapturingLogs(logs);
77+ }
78+
79+ private MemoryAppender startCapturingLogs() {
80+ Logger registryLogger = Logger.getLogger(CodecRegistry.class);
81+ registryLogger.setLevel(Level.WARN);
82+ MemoryAppender logs = new MemoryAppender();
83+ registryLogger.addAppender(logs);
84+ return logs;
85+ }
86+
87+ private void stopCapturingLogs(MemoryAppender logs) {
88+ Logger registryLogger = Logger.getLogger(CodecRegistry.class);
89+ registryLogger.setLevel(null);
90+ registryLogger.removeAppender(logs);
91+ }
92+}
No newline at the end of the file