Elffuss BraidHub

kikocis/java-driver public

Disable frozen checks in mapper (JAVA-843)
Changeset35b04c2122898d95e42e197f04c02bde5ea5ae75f15b4cc0756b401d48f8717f
Authorolim7t <omichallat+github@gmail.com> · 2015-07-23 23:46 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-07-24 22:03 +02:00
Parentsf1f8f4e3bbe9

Browse the files at this changeset

8 files changed

modified changelog/README.md +1 −0
@@ -8,6 +8,7 @@
88 - [bug] TypeCodec.getDataTypeFor() does not handle LocalDate instances (JAVA-818)
99 - [improvement] Make ResultSet#fetchMoreResult return a
1010 ListenableFuture<ResultSet> (JAVA-836)
11+- [improvement] Disable frozen checks in mapper (JAVA-843)
1112
1213 Merged from 2.1 branch:
1314
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationChecks.java +0 −80
@@ -66,15 +66,6 @@
6666 field.getName(), classDescription,
6767 field.getDeclaringClass().getName()));
6868
69- if (field.getAnnotation(Transient.class) == null) {
70- try {
71- checkFrozenTypes(field);
72- } catch (IllegalArgumentException e) {
73- throw new IllegalArgumentException(String.format("Error while checking frozen types on field %s of %s %s: %s",
74- field.getName(), classDescription,
75- field.getDeclaringClass().getName(), e.getMessage()));
76- }
77- }
7869 checkValidComputed(field);
7970 }
8071
@@ -95,81 +86,10 @@
9586 return false;
9687 }
9788
98- static void checkFrozenTypes(Field field) {
99- Type javaType = field.getGenericType();
100- DeclaredFrozenType declaredFrozenType = getDeclaredFrozenType(field);
101- checkFrozenTypes(javaType, declaredFrozenType);
102- }
103-
10489 static void checkValidComputed(Field field) {
10590 Computed computed = field.getAnnotation(Computed.class);
10691 if (computed != null && computed.value().isEmpty()){
10792 throw new IllegalArgumentException(String.format("Field %s: attribute 'value' of annotation @Computed is mandatory for computed fields", field.getName()));
10893 }
10994 }
110-
111- // Builds a DeclaredFrozenType hierarchy based on the @Frozen* annotations on a field.
112- private static DeclaredFrozenType getDeclaredFrozenType(Field field) {
113- Frozen frozen = field.getAnnotation(Frozen.class);
114- if (frozen != null)
115- return DeclaredFrozenType.parse(frozen.value());
116-
117- boolean frozenKey = field.getAnnotation(FrozenKey.class) != null;
118- boolean frozenValue = field.getAnnotation(FrozenValue.class) != null;
119- if (frozenKey && frozenValue)
120- return DeclaredFrozenType.FROZEN_MAP_KEY_AND_VALUE;
121- else if (frozenKey)
122- return DeclaredFrozenType.FROZEN_MAP_KEY;
123- else if (frozenValue && field.getType().equals(Map.class))
124- return DeclaredFrozenType.FROZEN_MAP_VALUE;
125- else if (frozenValue)
126- return DeclaredFrozenType.FROZEN_ELEMENT;
127- else
128- return DeclaredFrozenType.UNFROZEN_SIMPLE;
129- }
130-
131- // Traverses the Java type and CQLType hierarchies in parallel, to ensure that all
132- // Java types mapping to UDTs, tuples and nested collections are marked as frozen in the CQLType.
133- // We accept that parts of the CQLType be null, in which case the matching parts
134- // in the Java type will be considered as not frozen.
135- private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType) {
136- checkFrozenTypes(javaType, declaredFrozenType, true);
137- }
138-
139- private static void checkFrozenTypes(Type javaType, DeclaredFrozenType declaredFrozenType, boolean isRoot) {
140- Class<?> javaClass;
141- Type[] childrenJavaTypes;
142- if (javaType instanceof Class<?>) {
143- javaClass = (Class<?>)javaType;
144- childrenJavaTypes = null;
145- } else if (javaType instanceof ParameterizedType){
146- ParameterizedType pt = (ParameterizedType)javaType;
147- javaClass = (Class<?>)pt.getRawType();
148- childrenJavaTypes = pt.getActualTypeArguments();
149- } else
150- throw new IllegalArgumentException("unexpected type: " + javaType);
151-
152- boolean frozen = (declaredFrozenType != null && declaredFrozenType.frozen);
153- checkValidFrozen(javaClass, isRoot, frozen);
154-
155- if (childrenJavaTypes != null) {
156- for (int i = 0; i < childrenJavaTypes.length; i++) {
157- Type childJavaType = childrenJavaTypes[i];
158- DeclaredFrozenType childDeclaredFrozenType = null;
159- if (declaredFrozenType != null && declaredFrozenType.subTypes != null && declaredFrozenType.subTypes.size() > i)
160- childDeclaredFrozenType = declaredFrozenType.subTypes.get(i);
161- checkFrozenTypes(childJavaType, childDeclaredFrozenType, false);
162- }
163- }
164- }
165-
166- private static void checkValidFrozen(Class<?> clazz, boolean isRoot, boolean isDeclaredFrozen) {
167- boolean shouldBeFrozen = (TypeMappings.mapsToCollection(clazz) && !isRoot)
168- || TypeMappings.mapsToUserTypeOrTuple(clazz);
169- if (shouldBeFrozen != isDeclaredFrozen)
170- throw new IllegalArgumentException(String.format("expected %s to be %sfrozen but was %sfrozen",
171- clazz.getSimpleName(),
172- shouldBeFrozen ? "" : "not ",
173- isDeclaredFrozen ? "" : "not "));
174- }
17595 }
deleted driver-mapping/src/main/java/com/datastax/driver/mapping/DeclaredFrozenType.java +0 −185
@@ -1,185 +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 java.util.ArrayList;
19-import java.util.List;
20-
21-import com.datastax.driver.mapping.annotations.Frozen;
22-import com.google.common.base.Strings;
23-
24-/**
25- * A tree-like structure parsed from {@code Frozen*} annotations, that indicates which
26- * types in the hierarchy were declared frozen.
27- */
28-class DeclaredFrozenType {
29- String name;
30- boolean frozen;
31- List<DeclaredFrozenType> subTypes;
32-
33- private DeclaredFrozenType(String name) {
34- this(name, false);
35- }
36-
37- private DeclaredFrozenType(String name, boolean frozen, DeclaredFrozenType... subTypes) {
38- this.name = name;
39- this.frozen = frozen;
40- for (DeclaredFrozenType subType : subTypes) {
41- if (this.subTypes == null)
42- this.subTypes = new ArrayList<DeclaredFrozenType>();
43- this.subTypes.add(subType);
44- }
45- }
46-
47- // Some constant types for the simple cases. Note that we can afford generic names because currently we don't include the name in our
48- // checks.
49- static final DeclaredFrozenType FROZEN_SIMPLE = new DeclaredFrozenType("root", true);
50- static final DeclaredFrozenType FROZEN_ELEMENT = new DeclaredFrozenType("collection", false,
51- new DeclaredFrozenType("element", true));
52- static final DeclaredFrozenType FROZEN_MAP_KEY = new DeclaredFrozenType("map", false,
53- new DeclaredFrozenType("key", true));
54- static final DeclaredFrozenType FROZEN_MAP_VALUE = new DeclaredFrozenType("map", false,
55- new DeclaredFrozenType("key", false),
56- new DeclaredFrozenType("value", true));
57- static final DeclaredFrozenType FROZEN_MAP_KEY_AND_VALUE = new DeclaredFrozenType("map", false,
58- new DeclaredFrozenType("key", true),
59- new DeclaredFrozenType("value", true));
60- static final DeclaredFrozenType UNFROZEN_SIMPLE = new DeclaredFrozenType("root");
61-
62- static DeclaredFrozenType parse(String toParse) {
63- if (Strings.isNullOrEmpty(toParse))
64- // we allow that as a shorthand
65- return FROZEN_SIMPLE;
66- else
67- return new Parser(toParse).parse();
68- }
69-
70- private static class Parser {
71- private final String toParse;
72- private int idx;
73-
74- Parser(String toParse) {
75- this.toParse = toParse;
76- }
77-
78- DeclaredFrozenType parse() {
79- skipSpaces();
80-
81- if (toParse.charAt(idx) == '"') {
82- // quoted identifier: just return everything up to the next quote
83- int endQuote = idx + 1;
84- while (endQuote < toParse.length() && toParse.charAt(endQuote) != '"')
85- endQuote += 1;
86- if (endQuote == toParse.length())
87- throw fail("could not find matching quote");
88- if (endQuote == idx + 1)
89- throw fail("empty quoted identifier");
90-
91- String name = toParse.substring(idx + 1, endQuote);
92- idx = endQuote + 1;
93- // a quoted identifier cannot be parameterized, so we can return immediately
94- return new DeclaredFrozenType(name);
95- } else {
96- // unquoted identifier: could be "simpleType", "complexType<subType1, subType2...>" or "frozen<type>"
97- int n = skipWord();
98- String nextWord = toParse.substring(idx, n).toLowerCase();
99- idx = n;
100- if ("frozen".equals(nextWord)) {
101- skipSpaces();
102- if (idx >= toParse.length() || toParse.charAt(idx) != '<')
103- throw fail("expected '<'");
104- idx += 1;
105- DeclaredFrozenType type = parse();
106- skipSpaces();
107- if (idx >= toParse.length() || toParse.charAt(idx) != '>')
108- throw fail("expected '>'");
109- idx += 1;
110- type.frozen = true;
111- return type;
112- } else {
113- DeclaredFrozenType type = new DeclaredFrozenType(nextWord);
114- skipSpaces();
115- if (idx < toParse.length() && toParse.charAt(idx) == '<') {
116- idx += 1;
117- type.subTypes = new ArrayList<DeclaredFrozenType>();
118- while (idx < toParse.length()) {
119- type.subTypes.add(parse());
120- skipSpaces();
121- if (idx >= toParse.length())
122- fail("unterminated list of subtypes");
123- else if (toParse.charAt(idx) == '>') {
124- idx += 1;
125- break;
126- }
127- else if (toParse.charAt(idx) != ',')
128- fail("expected ','");
129- else
130- idx += 1;
131- }
132- }
133- return type;
134- }
135- }
136- }
137-
138- private void skipSpaces() {
139- while (idx < toParse.length() && isBlank(idx))
140- idx += 1;
141- }
142-
143- private int skipWord() {
144- if (idx >= toParse.length())
145- throw fail("expected type name");
146-
147- if (!isIdentStart(idx))
148- throw fail("illegal character at start of type name");
149-
150- int i = idx;
151- while (i < toParse.length() && isIdentBody(i))
152- i += 1;
153-
154- return i;
155- }
156-
157- private boolean isBlank(int i) {
158- char c = toParse.charAt(i);
159- return c == ' ' || c == '\t' || c == '\n';
160- }
161-
162- private boolean isIdentStart(int i) {
163- char c = toParse.charAt(i);
164- return isLetter(c);
165- }
166-
167- private boolean isIdentBody(int i) {
168- char c = toParse.charAt(i);
169- return isLetter(c) || isDigit(c) || c == '_';
170- }
171-
172- private boolean isLetter(char c) {
173- return (c >= 'a' && c <= 'z') ||
174- (c >= 'A' && c <= 'Z');
175- }
176-
177- private boolean isDigit(char c) {
178- return (c >= '0' && c <= '9');
179- }
180-
181- private IllegalArgumentException fail(String cause) {
182- return new IllegalArgumentException(cause + " (" + toParse + " [" + idx + "])");
183- }
184- }
185-}
modified driver-mapping/src/main/java/com/datastax/driver/mapping/annotations/Frozen.java +5 −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,8 @@
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 version 2.1.7
41+
3942 /**
4043 * Contains the full CQL type of the target column. As a convenience, this can be left out when only the top-level
4144 * type is frozen.
deleted driver-mapping/src/test/java/com/datastax/driver/mapping/AnnotationChecksTest.java +0 −97
@@ -1,97 +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 java.lang.reflect.Field;
19-import java.util.List;
20-import java.util.Map;
21-import java.util.Set;
22-
23-import com.datastax.driver.mapping.annotations.Frozen;
24-import com.datastax.driver.mapping.annotations.FrozenValue;
25-import com.datastax.driver.mapping.annotations.UDT;
26-
27-import org.testng.annotations.Test;
28-
29-import static org.testng.Assert.fail;
30-
31-public class AnnotationChecksTest {
32- // Dummy UDT class:
33- @UDT(name = "user")
34- public class User {
35- }
36-
37- // Dummy fields to run our checks on:
38- String string;
39- @Frozen
40- String frozenString;
41- List<String> listOfStrings;
42- User unfrozenUser;
43- @Frozen
44- User frozenUser;
45- List<User> listOfUnfrozenUsers;
46- @FrozenValue
47- List<User> listOfFrozenUsers;
48- Map<String, Map<String, User>> deeplyNestedUnfrozenUser;
49- @Frozen("map<text, frozen<map<text, frozen<user>>>>")
50- Map<String, Map<String, User>> deeplyNestedFrozenUser;
51-
52- Map<String, List<Integer>> mapOfUnfrozenListOfInt;
53- @FrozenValue
54- Map<String, List<Integer>> mapOfFrozenListOfInt;
55- @FrozenValue
56- Map<Integer, List<Set<Integer>>> deeplyNestedUnfrozenSet;
57- @Frozen("map<int, frozen<list<set<int>>>>")
58- Map<Integer, List<Set<Integer>>> deeplyNestedUnfrozenSet2;
59- @Frozen("map<int, frozen<list<frozen<set<int>>>>>")
60- Map<Integer, List<Set<Integer>>> deeplyNestedFrozenSet;
61-
62- @Test(groups = "unit")
63- public void should_fail_if_udt_not_frozen() throws Exception {
64- checkFrozenTypesTest("string", true);
65- checkFrozenTypesTest("frozenString", false);
66- checkFrozenTypesTest("listOfStrings", true);
67- checkFrozenTypesTest("unfrozenUser", false);
68- checkFrozenTypesTest("frozenUser", true);
69- checkFrozenTypesTest("listOfUnfrozenUsers", false);
70- checkFrozenTypesTest("listOfFrozenUsers", true);
71- checkFrozenTypesTest("deeplyNestedUnfrozenUser", false);
72- checkFrozenTypesTest("deeplyNestedFrozenUser", true);
73- }
74-
75- @Test(groups = "unit")
76- public void should_fail_if_nested_collection_not_frozen() throws Exception {
77- checkFrozenTypesTest("mapOfUnfrozenListOfInt", false);
78- checkFrozenTypesTest("mapOfFrozenListOfInt", true);
79- checkFrozenTypesTest("deeplyNestedUnfrozenSet", false);
80- checkFrozenTypesTest("deeplyNestedUnfrozenSet2", false);
81- checkFrozenTypesTest("deeplyNestedFrozenSet", true);
82- }
83-
84- private void checkFrozenTypesTest(String fieldName, boolean expectSuccess) throws Exception {
85- Field field = AnnotationChecksTest.class.getDeclaredField(fieldName);
86- Exception exception = null;
87- try {
88- AnnotationChecks.checkFrozenTypes(field);
89- } catch (IllegalArgumentException e) {
90- exception = e;
91- }
92- if (expectSuccess && exception != null)
93- fail("expected check to succeed but got exception " + exception.getMessage());
94- else if (!expectSuccess && exception == null)
95- fail("expected check to fail but got no exception");
96- }
97-}
deleted driver-mapping/src/test/java/com/datastax/driver/mapping/DeclaredFrozenTypeTest.java +0 −118
@@ -1,118 +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-import static org.testng.Assert.*;
20-
21-public class DeclaredFrozenTypeTest {
22- @Test(groups = "unit")
23- public void parseSimpleTypeTest() {
24- DeclaredFrozenType type = DeclaredFrozenType.parse(" foo");
25- assertEquals(type.name, "foo");
26- assertFalse(type.frozen);
27- assertNull(type.subTypes);
28- }
29-
30- @Test(groups = "unit")
31- public void parseQuotedTypeTest() {
32- DeclaredFrozenType type = DeclaredFrozenType.parse("\"Foo bar\"");
33- assertEquals(type.name, "Foo bar");
34- assertFalse(type.frozen);
35- assertNull(type.subTypes);
36- }
37-
38- @Test(groups = "unit")
39- public void parseNestedTypeTest() {
40- // list
41- DeclaredFrozenType type = DeclaredFrozenType.parse("list<foo1>");
42- assertEquals(type.name, "list");
43- assertFalse(type.frozen);
44- assertEquals(type.subTypes.size(), 1);
45-
46- DeclaredFrozenType subType0 = type.subTypes.get(0);
47- assertEquals(subType0.name, "foo1");
48- assertFalse(subType0.frozen);
49- assertNull(subType0.subTypes);
50-
51- // map
52- type = DeclaredFrozenType.parse("map < foo , bar >");
53- assertEquals(type.name, "map");
54- assertFalse(type.frozen);
55- assertEquals(type.subTypes.size(), 2);
56-
57- subType0 = type.subTypes.get(0);
58- assertEquals(subType0.name, "foo");
59- assertFalse(subType0.frozen);
60- assertNull(subType0.subTypes);
61-
62- DeclaredFrozenType subType1 = type.subTypes.get(1);
63- assertEquals(subType1.name, "bar");
64- assertFalse(subType1.frozen);
65- assertNull(subType1.subTypes);
66-
67- }
68-
69- @Test(groups = "unit")
70- public void parseSimpleFrozenTypeTest() {
71- DeclaredFrozenType type = DeclaredFrozenType.parse("frozen<foo_1>");
72- assertEquals(type.name, "foo_1");
73- assertTrue(type.frozen);
74- assertNull(type.subTypes);
75- }
76-
77- @Test(groups = "unit")
78- public void parseNestedFrozenTypeTest() {
79- DeclaredFrozenType type = DeclaredFrozenType.parse("list<frozen<foo>>");
80- assertEquals(type.name, "list");
81- assertFalse(type.frozen);
82- assertEquals(type.subTypes.size(), 1);
83-
84- DeclaredFrozenType subType0 = type.subTypes.get(0);
85- assertEquals(subType0.name, "foo");
86- assertTrue(subType0.frozen);
87- assertNull(subType0.subTypes);
88- }
89-
90- @Test(groups = "unit")
91- public void parseDeeplyNestedTypeTest() {
92- // NB: nested collections are not allowed in C* 2.1, but might be in the future, so we want to handle that
93- DeclaredFrozenType type = DeclaredFrozenType.parse("map<text, map<text, frozen<foo>>>");
94- assertEquals(type.name, "map");
95- assertFalse(type.frozen);
96- assertEquals(type.subTypes.size(), 2);
97-
98- DeclaredFrozenType subType0 = type.subTypes.get(0);
99- assertEquals(subType0.name, "text");
100- assertFalse(subType0.frozen);
101- assertNull(subType0.subTypes);
102-
103- DeclaredFrozenType subType1 = type.subTypes.get(1);
104- assertEquals(subType1.name, "map");
105- assertFalse(subType1.frozen);
106- assertEquals(subType1.subTypes.size(), 2);
107-
108- DeclaredFrozenType subType10 = subType1.subTypes.get(0);
109- assertEquals(subType10.name, "text");
110- assertFalse(subType10.frozen);
111- assertNull(subType10.subTypes);
112-
113- DeclaredFrozenType subType11 = subType1.subTypes.get(1);
114- assertEquals(subType11.name, "foo");
115- assertTrue(subType11.frozen);
116- assertNull(subType11.subTypes);
117- }
118-}
modified features/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 +7 −0
@@ -130,6 +130,13 @@
130130 query to process all pages (see `AsyncResultSetTest` in the sources for an
131131 example).
132132
133+14. `Frozen` annotations in the mapper are no longer checked at runtime (see
134+ [JAVA-843|https://datastax-oss.atlassian.net/browse/JAVA-843] for more
135+ explanations). So they become purely informational at this stage.
136+ However it is a good idea to keep using these annotations and make sure
137+ they match the schema, in anticipation for the schema generation features
138+ that will be added in a future version.
139+
133140
134141 ### 2.1.7
135142