Elffuss BraidHub

kikocis/java-driver public

JAVA-843: Disable frozen checks in mapper.

Backported from 3.0 branch.
Changeset4b8e8d57e2d8748d440cf1d99849a779078342253967573da342a03ca65a615f
Authorolim7t <omichallat+github@gmail.com> · 2015-07-23 23:46 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2016-02-16 17:37 +01:00
Parents05b205659999

Browse the files at this changeset

8 files changed

modified changelog/README.md +1 −0
@@ -10,6 +10,7 @@
1010 - [improvement] JAVA-664: Reduce heap consumption for TokenMap.
1111 - [improvement] JAVA-1030: Log token to replica map computation times.
1212 - [bug] JAVA-1039: Minor bugs in Event Debouncer.
13+- [improvement] JAVA-843: Disable frozen checks in mapper.
1314
1415 Merged from 2.0 branch:
1516
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationChecks.java +2 −84
@@ -15,13 +15,11 @@
1515 */
1616 package com.datastax.driver.mapping;
1717
18-import com.datastax.driver.mapping.annotations.*;
18+import com.datastax.driver.mapping.annotations.Computed;
19+import com.datastax.driver.mapping.annotations.Table;
1920
2021 import java.lang.annotation.Annotation;
2122 import java.lang.reflect.Field;
22-import java.lang.reflect.ParameterizedType;
23-import java.lang.reflect.Type;
24-import java.util.Map;
2523
2624 /**
2725 * Various checks on mapping annotations.
@@ -67,15 +65,6 @@
6765 field.getName(), classDescription,
6866 field.getDeclaringClass().getName()));
6967
70- if (field.getAnnotation(Transient.class) == null) {
71- try {
72- checkFrozenTypes(field);
73- } catch (IllegalArgumentException e) {
74- throw new IllegalArgumentException(String.format("Error while checking frozen types on field %s of %s %s: %s",
75- field.getName(), classDescription,
76- field.getDeclaringClass().getName(), e.getMessage()));
77- }
78- }
7968 checkValidComputed(field);
8069 }
8170
@@ -96,81 +85,10 @@
9685 return false;
9786 }
9887
99- static void checkFrozenTypes(Field field) {
100- Type javaType = field.getGenericType();
101- DeclaredFrozenType declaredFrozenType = getDeclaredFrozenType(field);
102- checkFrozenTypes(javaType, declaredFrozenType);
103- }
104-
10588 static void checkValidComputed(Field field) {
10689 Computed computed = field.getAnnotation(Computed.class);
10790 if (computed != null && computed.value().isEmpty()) {
10891 throw new IllegalArgumentException(String.format("Field %s: attribute 'value' of annotation @Computed is mandatory for computed fields", field.getName()));
10992 }
11093 }
111-
112- // Builds a DeclaredFrozenType hierarchy based on the @Frozen* annotations on a field.
113- private static DeclaredFrozenType getDeclaredFrozenType(Field field) {
114- Frozen frozen = field.getAnnotation(Frozen.class);
115- if (frozen != null)
116- return DeclaredFrozenType.parse(frozen.value());
117-
118- boolean frozenKey = field.getAnnotation(FrozenKey.class) != null;
119- boolean frozenValue = field.getAnnotation(FrozenValue.class) != null;
120- if (frozenKey && frozenValue)
121- return DeclaredFrozenType.FROZEN_MAP_KEY_AND_VALUE;
122- else if (frozenKey)
123- return DeclaredFrozenType.FROZEN_MAP_KEY;
124- else if (frozenValue && field.getType().equals(Map.class))
125- return DeclaredFrozenType.FROZEN_MAP_VALUE;
126- else if (frozenValue)
127- return DeclaredFrozenType.FROZEN_ELEMENT;
128- else
129- return DeclaredFrozenType.UNFROZEN_SIMPLE;
130- }
131-
132- // Traverses the Java type and CQLType hierarchies in parallel, to ensure that all
133- // Java types mapping to UDTs, tuples and nested collections are marked as frozen in the CQLType.
134- // We accept that parts of the CQLType be null, in which case the matching parts
135- // in the Java type will be considered as not frozen.
136- private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType) {
137- checkFrozenTypes(javaType, declaredFrozenType, true);
138- }
139-
140- private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType, boolean isRoot) {
141- Class<?> javaClass;
142- Type[] childrenJavaTypes;
143- if (javaType instanceof Class<?>) {
144- javaClass = (Class<?>) javaType;
145- childrenJavaTypes = null;
146- } else if (javaType instanceof ParameterizedType) {
147- ParameterizedType pt = (ParameterizedType) javaType;
148- javaClass = (Class<?>) pt.getRawType();
149- childrenJavaTypes = pt.getActualTypeArguments();
150- } else
151- throw new IllegalArgumentException("unexpected type: " + javaType);
152-
153- boolean frozen = (declaredFrozenType != null && declaredFrozenType.frozen);
154- checkValidFrozen(javaClass, isRoot, frozen);
155-
156- if (childrenJavaTypes != null) {
157- for (int i = 0; i < childrenJavaTypes.length; i++) {
158- Type childJavaType = childrenJavaTypes[i];
159- DeclaredFrozenType childDeclaredFrozenType = null;
160- if (declaredFrozenType != null && declaredFrozenType.subTypes != null && declaredFrozenType.subTypes.size() > i)
161- childDeclaredFrozenType = declaredFrozenType.subTypes.get(i);
162- checkFrozenTypes(childJavaType, childDeclaredFrozenType, false);
163- }
164- }
165- }
166-
167- private static void checkValidFrozen(Class<?> clazz, boolean isRoot, boolean isDeclaredFrozen) {
168- boolean shouldBeFrozen = (TypeMappings.mapsToCollection(clazz) && !isRoot)
169- || TypeMappings.mapsToUserTypeOrTuple(clazz);
170- if (shouldBeFrozen != isDeclaredFrozen)
171- throw new IllegalArgumentException(String.format("expected %s to be %sfrozen but was %sfrozen",
172- clazz.getSimpleName(),
173- shouldBeFrozen ? "" : "not ",
174- isDeclaredFrozen ? "" : "not "));
175- }
17694 }
deleted driver-mapping/src/main/java/com/datastax/driver/mapping/DeclaredFrozenType.java +0 −183
@@ -1,183 +0,0 @@
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.mapping;
17-
18-import com.google.common.base.Strings;
19-
20-import java.util.ArrayList;
21-import java.util.List;
22-
23-/**
24- * A tree-like structure parsed from {@code Frozen*} annotations, that indicates which
25- * types in the hierarchy were declared frozen.
26- */
27-class DeclaredFrozenType {
28- String name;
29- boolean frozen;
30- List<DeclaredFrozenType> subTypes;
31-
32- private DeclaredFrozenType(String name) {
33- this(name, false);
34- }
35-
36- private DeclaredFrozenType(String name, boolean frozen, DeclaredFrozenType... subTypes) {
37- this.name = name;
38- this.frozen = frozen;
39- for (DeclaredFrozenType subType : subTypes) {
40- if (this.subTypes == null)
41- this.subTypes = new ArrayList<DeclaredFrozenType>();
42- this.subTypes.add(subType);
43- }
44- }
45-
46- // Some constant types for the simple cases. Note that we can afford generic names because currently we don't include the name in our
47- // checks.
48- static final DeclaredFrozenType FROZEN_SIMPLE = new DeclaredFrozenType("root", true);
49- static final DeclaredFrozenType FROZEN_ELEMENT = new DeclaredFrozenType("collection", false,
50- new DeclaredFrozenType("element", true));
51- static final DeclaredFrozenType FROZEN_MAP_KEY = new DeclaredFrozenType("map", false,
52- new DeclaredFrozenType("key", true));
53- static final DeclaredFrozenType FROZEN_MAP_VALUE = new DeclaredFrozenType("map", false,
54- new DeclaredFrozenType("key", false),
55- new DeclaredFrozenType("value", true));
56- static final DeclaredFrozenType FROZEN_MAP_KEY_AND_VALUE = new DeclaredFrozenType("map", false,
57- new DeclaredFrozenType("key", true),
58- new DeclaredFrozenType("value", true));
59- static final DeclaredFrozenType UNFROZEN_SIMPLE = new DeclaredFrozenType("root");
60-
61- static DeclaredFrozenType parse(String toParse) {
62- if (Strings.isNullOrEmpty(toParse))
63- // we allow that as a shorthand
64- return FROZEN_SIMPLE;
65- else
66- return new Parser(toParse).parse();
67- }
68-
69- private static class Parser {
70- private final String toParse;
71- private int idx;
72-
73- Parser(String toParse) {
74- this.toParse = toParse;
75- }
76-
77- DeclaredFrozenType parse() {
78- skipSpaces();
79-
80- if (toParse.charAt(idx) == '"') {
81- // quoted identifier: just return everything up to the next quote
82- int endQuote = idx + 1;
83- while (endQuote < toParse.length() && toParse.charAt(endQuote) != '"')
84- endQuote += 1;
85- if (endQuote == toParse.length())
86- throw fail("could not find matching quote");
87- if (endQuote == idx + 1)
88- throw fail("empty quoted identifier");
89-
90- String name = toParse.substring(idx + 1, endQuote);
91- idx = endQuote + 1;
92- // a quoted identifier cannot be parameterized, so we can return immediately
93- return new DeclaredFrozenType(name);
94- } else {
95- // unquoted identifier: could be "simpleType", "complexType<subType1, subType2...>" or "frozen<type>"
96- int n = skipWord();
97- String nextWord = toParse.substring(idx, n).toLowerCase();
98- idx = n;
99- if ("frozen".equals(nextWord)) {
100- skipSpaces();
101- if (idx >= toParse.length() || toParse.charAt(idx) != '<')
102- throw fail("expected '<'");
103- idx += 1;
104- DeclaredFrozenType type = parse();
105- skipSpaces();
106- if (idx >= toParse.length() || toParse.charAt(idx) != '>')
107- throw fail("expected '>'");
108- idx += 1;
109- type.frozen = true;
110- return type;
111- } else {
112- DeclaredFrozenType type = new DeclaredFrozenType(nextWord);
113- skipSpaces();
114- if (idx < toParse.length() && toParse.charAt(idx) == '<') {
115- idx += 1;
116- type.subTypes = new ArrayList<DeclaredFrozenType>();
117- while (idx < toParse.length()) {
118- type.subTypes.add(parse());
119- skipSpaces();
120- if (idx >= toParse.length())
121- fail("unterminated list of subtypes");
122- else if (toParse.charAt(idx) == '>') {
123- idx += 1;
124- break;
125- } else if (toParse.charAt(idx) != ',')
126- fail("expected ','");
127- else
128- idx += 1;
129- }
130- }
131- return type;
132- }
133- }
134- }
135-
136- private void skipSpaces() {
137- while (idx < toParse.length() && isBlank(idx))
138- idx += 1;
139- }
140-
141- private int skipWord() {
142- if (idx >= toParse.length())
143- throw fail("expected type name");
144-
145- if (!isIdentStart(idx))
146- throw fail("illegal character at start of type name");
147-
148- int i = idx;
149- while (i < toParse.length() && isIdentBody(i))
150- i += 1;
151-
152- return i;
153- }
154-
155- private boolean isBlank(int i) {
156- char c = toParse.charAt(i);
157- return c == ' ' || c == '\t' || c == '\n';
158- }
159-
160- private boolean isIdentStart(int i) {
161- char c = toParse.charAt(i);
162- return isLetter(c);
163- }
164-
165- private boolean isIdentBody(int i) {
166- char c = toParse.charAt(i);
167- return isLetter(c) || isDigit(c) || c == '_';
168- }
169-
170- private boolean isLetter(char c) {
171- return (c >= 'a' && c <= 'z') ||
172- (c >= 'A' && c <= 'Z');
173- }
174-
175- private boolean isDigit(char c) {
176- return (c >= '0' && c <= '9');
177- }
178-
179- private IllegalArgumentException fail(String cause) {
180- return new IllegalArgumentException(cause + " (" + toParse + " [" + idx + "])");
181- }
182- }
183-}
modified driver-mapping/src/main/java/com/datastax/driver/mapping/annotations/Frozen.java +6 −2
@@ -26,8 +26,9 @@
2626 * Specifies that the field decorated with this annotation maps to a CQL type that is {@link DataType#isFrozen() frozen},
2727 * or contains frozen subtypes.
2828 * <p/>
29- * This annotation is purely informational at this stage, but will become useful when a schema generation feature is
30- * added to the mapper.
29+ * This annotation is purely informational at this stage, the validity of the declaration is not checked.
30+ * But will become useful when a schema generation feature is added to the mapper. Therefore it is a good idea to keep
31+ * frozen declarations up-to-date.
3132 *
3233 * @see FrozenKey
3334 * @see FrozenValue
@@ -36,6 +37,9 @@
3637 @Retention(RetentionPolicy.RUNTIME)
3738 public @interface Frozen {
3839
40+ // Implementation note: frozen annotations were previously checked at runtime, this code can be found in pre-2.1.10
41+ // versions
42+
3943 /**
4044 * Contains the full CQL type of the target column. As a convenience, this can be left out when only the top-level
4145 * type is frozen.
deleted driver-mapping/src/test/java/com/datastax/driver/mapping/AnnotationChecksTest.java +0 −96
@@ -1,96 +0,0 @@
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.mapping;
17-
18-import com.datastax.driver.mapping.annotations.Frozen;
19-import com.datastax.driver.mapping.annotations.FrozenValue;
20-import com.datastax.driver.mapping.annotations.UDT;
21-import org.testng.annotations.Test;
22-
23-import java.lang.reflect.Field;
24-import java.util.List;
25-import java.util.Map;
26-import java.util.Set;
27-
28-import static org.testng.Assert.fail;
29-
30-public class AnnotationChecksTest {
31- // Dummy UDT class:
32- @UDT(name = "user")
33- public class User {
34- }
35-
36- // Dummy fields to run our checks on:
37- String string;
38- @Frozen
39- String frozenString;
40- List<String> listOfStrings;
41- User unfrozenUser;
42- @Frozen
43- User frozenUser;
44- List<User> listOfUnfrozenUsers;
45- @FrozenValue
46- List<User> listOfFrozenUsers;
47- Map<String, Map<String, User>> deeplyNestedUnfrozenUser;
48- @Frozen("map<text, frozen<map<text, frozen<user>>>>")
49- Map<String, Map<String, User>> deeplyNestedFrozenUser;
50-
51- Map<String, List<Integer>> mapOfUnfrozenListOfInt;
52- @FrozenValue
53- Map<String, List<Integer>> mapOfFrozenListOfInt;
54- @FrozenValue
55- Map<Integer, List<Set<Integer>>> deeplyNestedUnfrozenSet;
56- @Frozen("map<int, frozen<list<set<int>>>>")
57- Map<Integer, List<Set<Integer>>> deeplyNestedUnfrozenSet2;
58- @Frozen("map<int, frozen<list<frozen<set<int>>>>>")
59- Map<Integer, List<Set<Integer>>> deeplyNestedFrozenSet;
60-
61- @Test(groups = "unit")
62- public void should_fail_if_udt_not_frozen() throws Exception {
63- checkFrozenTypesTest("string", true);
64- checkFrozenTypesTest("frozenString", false);
65- checkFrozenTypesTest("listOfStrings", true);
66- checkFrozenTypesTest("unfrozenUser", false);
67- checkFrozenTypesTest("frozenUser", true);
68- checkFrozenTypesTest("listOfUnfrozenUsers", false);
69- checkFrozenTypesTest("listOfFrozenUsers", true);
70- checkFrozenTypesTest("deeplyNestedUnfrozenUser", false);
71- checkFrozenTypesTest("deeplyNestedFrozenUser", true);
72- }
73-
74- @Test(groups = "unit")
75- public void should_fail_if_nested_collection_not_frozen() throws Exception {
76- checkFrozenTypesTest("mapOfUnfrozenListOfInt", false);
77- checkFrozenTypesTest("mapOfFrozenListOfInt", true);
78- checkFrozenTypesTest("deeplyNestedUnfrozenSet", false);
79- checkFrozenTypesTest("deeplyNestedUnfrozenSet2", false);
80- checkFrozenTypesTest("deeplyNestedFrozenSet", true);
81- }
82-
83- private void checkFrozenTypesTest(String fieldName, boolean expectSuccess) throws Exception {
84- Field field = AnnotationChecksTest.class.getDeclaredField(fieldName);
85- Exception exception = null;
86- try {
87- AnnotationChecks.checkFrozenTypes(field);
88- } catch (IllegalArgumentException e) {
89- exception = e;
90- }
91- if (expectSuccess && exception != null)
92- fail("expected check to succeed but got exception " + exception.getMessage());
93- else if (!expectSuccess && exception == null)
94- fail("expected check to fail but got no exception");
95- }
96-}
deleted driver-mapping/src/test/java/com/datastax/driver/mapping/DeclaredFrozenTypeTest.java +0 −119
@@ -1,119 +0,0 @@
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.mapping;
17-
18-import org.testng.annotations.Test;
19-
20-import static org.testng.Assert.*;
21-
22-public class DeclaredFrozenTypeTest {
23- @Test(groups = "unit")
24- public void parseSimpleTypeTest() {
25- DeclaredFrozenType type = DeclaredFrozenType.parse(" foo");
26- assertEquals(type.name, "foo");
27- assertFalse(type.frozen);
28- assertNull(type.subTypes);
29- }
30-
31- @Test(groups = "unit")
32- public void parseQuotedTypeTest() {
33- DeclaredFrozenType type = DeclaredFrozenType.parse("\"Foo bar\"");
34- assertEquals(type.name, "Foo bar");
35- assertFalse(type.frozen);
36- assertNull(type.subTypes);
37- }
38-
39- @Test(groups = "unit")
40- public void parseNestedTypeTest() {
41- // list
42- DeclaredFrozenType type = DeclaredFrozenType.parse("list<foo1>");
43- assertEquals(type.name, "list");
44- assertFalse(type.frozen);
45- assertEquals(type.subTypes.size(), 1);
46-
47- DeclaredFrozenType subType0 = type.subTypes.get(0);
48- assertEquals(subType0.name, "foo1");
49- assertFalse(subType0.frozen);
50- assertNull(subType0.subTypes);
51-
52- // map
53- type = DeclaredFrozenType.parse("map < foo , bar >");
54- assertEquals(type.name, "map");
55- assertFalse(type.frozen);
56- assertEquals(type.subTypes.size(), 2);
57-
58- subType0 = type.subTypes.get(0);
59- assertEquals(subType0.name, "foo");
60- assertFalse(subType0.frozen);
61- assertNull(subType0.subTypes);
62-
63- DeclaredFrozenType subType1 = type.subTypes.get(1);
64- assertEquals(subType1.name, "bar");
65- assertFalse(subType1.frozen);
66- assertNull(subType1.subTypes);
67-
68- }
69-
70- @Test(groups = "unit")
71- public void parseSimpleFrozenTypeTest() {
72- DeclaredFrozenType type = DeclaredFrozenType.parse("frozen<foo_1>");
73- assertEquals(type.name, "foo_1");
74- assertTrue(type.frozen);
75- assertNull(type.subTypes);
76- }
77-
78- @Test(groups = "unit")
79- public void parseNestedFrozenTypeTest() {
80- DeclaredFrozenType type = DeclaredFrozenType.parse("list<frozen<foo>>");
81- assertEquals(type.name, "list");
82- assertFalse(type.frozen);
83- assertEquals(type.subTypes.size(), 1);
84-
85- DeclaredFrozenType subType0 = type.subTypes.get(0);
86- assertEquals(subType0.name, "foo");
87- assertTrue(subType0.frozen);
88- assertNull(subType0.subTypes);
89- }
90-
91- @Test(groups = "unit")
92- public void parseDeeplyNestedTypeTest() {
93- // NB: nested collections are not allowed in C* 2.1, but might be in the future, so we want to handle that
94- DeclaredFrozenType type = DeclaredFrozenType.parse("map<text, map<text, frozen<foo>>>");
95- assertEquals(type.name, "map");
96- assertFalse(type.frozen);
97- assertEquals(type.subTypes.size(), 2);
98-
99- DeclaredFrozenType subType0 = type.subTypes.get(0);
100- assertEquals(subType0.name, "text");
101- assertFalse(subType0.frozen);
102- assertNull(subType0.subTypes);
103-
104- DeclaredFrozenType subType1 = type.subTypes.get(1);
105- assertEquals(subType1.name, "map");
106- assertFalse(subType1.frozen);
107- assertEquals(subType1.subTypes.size(), 2);
108-
109- DeclaredFrozenType subType10 = subType1.subTypes.get(0);
110- assertEquals(subType10.name, "text");
111- assertFalse(subType10.frozen);
112- assertNull(subType10.subTypes);
113-
114- DeclaredFrozenType subType11 = subType1.subTypes.get(1);
115- assertEquals(subType11.name, "foo");
116- assertTrue(subType11.frozen);
117- assertNull(subType11.subTypes);
118- }
119-}
modified manual/object_mapper/creating/README.md +6 −3
@@ -217,9 +217,12 @@
217217 types and all [user types](#mapping-user-types) previously defined is
218218 the database.
219219
220-Collection fields must be annotated to indicate whether they are frozen
221-or not (currently this is only for informational purposes, but the
222-mapper will check that the declared state match the rules in Cassandra).
220+Collection and UDT fields should be annotated to indicate whether they are
221+frozen. Currently this is only for informational purposes (the mapper
222+won't check that the declarations match the rules in Cassandra).
223+However it is a good idea to keep using these annotations and make sure
224+they match the schema, in anticipation for the schema generation features
225+that will be added in a future version.
223226 The default annotation is [@Frozen][frozen], [@FrozenKey][frozenkey] and
224227 [@FrozenValue][frozenvalue]` are also provided for convenience:
225228
modified upgrade_guide/README.md +10 −0
@@ -3,6 +3,16 @@
33 The purpose of this guide is to detail changes made by successive
44 versions of the Java driver.
55
6+### 2.1.10
7+
8+1. `Frozen` annotations in the mapper are no longer checked at runtime (see
9+ [JAVA-843|https://datastax-oss.atlassian.net/browse/JAVA-843] for more
10+ explanations). So they become purely informational at this stage.
11+ However it is a good idea to keep using these annotations and make sure
12+ they match the schema, in anticipation for the schema generation features
13+ that will be added in a future version.
14+
15+
616 ### 2.1.8
717
818 2.1.8 is binary-compatible with 2.1.7 but introduces a small change in the