kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5390190f38563e509e3b2ce30d4a7a9cd008f5aa5d2a48d9253ce5001ce99b09 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-11-16 14:53 +01:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-17 14:52 +01:00 |
| Parents | 01c1288c6b3c |
5 files changed
modified changelog/README.md +1 −0
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | - [new feature] JAVA-606: Codec support for Java 8. |
| 20 | 20 | - [new feature] JAVA-565: Codec support for Java arrays. |
| 21 | 21 | - [new feature] JAVA-605: Codec support for Java enums. |
| 22 | +- [bug] JAVA-884: Fix UDT mapper to process fields in the correct order. | |
| 22 | 23 | |
| 23 | 24 | Merged from 2.1 branch: |
| 24 | 25 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +20 −11
| @@ -18,13 +18,11 @@ | ||
| 18 | 18 | import java.lang.annotation.Annotation; |
| 19 | 19 | import java.lang.reflect.Field; |
| 20 | 20 | import java.lang.reflect.*; |
| 21 | -import java.util.ArrayList; | |
| 22 | -import java.util.Collections; | |
| 23 | -import java.util.Comparator; | |
| 24 | -import java.util.List; | |
| 21 | +import java.util.*; | |
| 25 | 22 | import java.util.concurrent.atomic.AtomicInteger; |
| 26 | 23 | |
| 27 | 24 | import com.google.common.base.Strings; |
| 25 | +import com.google.common.collect.Maps; | |
| 28 | 26 | import com.google.common.reflect.TypeToken; |
| 29 | 27 | |
| 30 | 28 | import com.datastax.driver.core.ConsistencyLevel; |
| @@ -106,12 +104,22 @@ | ||
| 106 | 104 | validateOrder(pks, "@PartitionKey"); |
| 107 | 105 | validateOrder(ccs, "@ClusteringColumn"); |
| 108 | 106 | |
| 109 | - mapper.addColumns(convert(pks, factory, mapper.entityClass, mappingManager, columnCounter), | |
| 110 | - convert(ccs, factory, mapper.entityClass, mappingManager, columnCounter), | |
| 111 | - convert(rgs, factory, mapper.entityClass, mappingManager, columnCounter)); | |
| 107 | + mapper.addColumns(createColumnMappers(pks, factory, mapper.entityClass, mappingManager, columnCounter), | |
| 108 | + createColumnMappers(ccs, factory, mapper.entityClass, mappingManager, columnCounter), | |
| 109 | + createColumnMappers(rgs, factory, mapper.entityClass, mappingManager, columnCounter)); | |
| 112 | 110 | return mapper; |
| 113 | 111 | } |
| 114 | 112 | |
| 113 | + private static <T> List<ColumnMapper<T>> createColumnMappers(List<Field> fields, EntityMapper.Factory factory, Class<T> klass, MappingManager mappingManager, AtomicInteger columnCounter) { | |
| 114 | + List<ColumnMapper<T>> mappers = new ArrayList<ColumnMapper<T>>(fields.size()); | |
| 115 | + for (int i = 0; i < fields.size(); i++) { | |
| 116 | + Field field = fields.get(i); | |
| 117 | + int pos = position(field); | |
| 118 | + mappers.add(factory.createColumnMapper(klass, field, pos < 0 ? i : pos, mappingManager, columnCounter)); | |
| 119 | + } | |
| 120 | + return mappers; | |
| 121 | + } | |
| 122 | + | |
| 115 | 123 | public static <T> MappedUDTCodec<T> parseUDT(Class<T> udtClass, EntityMapper.Factory factory, MappingManager mappingManager) { |
| 116 | 124 | UDT udt = AnnotationChecks.getTypeAnnotation(UDT.class, udtClass); |
| 117 | 125 | |
| @@ -153,16 +161,17 @@ | ||
| 153 | 161 | break; |
| 154 | 162 | } |
| 155 | 163 | } |
| 156 | - List<ColumnMapper<T>> columnMappers = convert(columns, factory, udtClass, mappingManager, null); | |
| 164 | + Map<String, ColumnMapper<T>> columnMappers = createFieldMappers(columns, factory, udtClass, mappingManager, null); | |
| 157 | 165 | return new MappedUDTCodec<T>(userType, udtClass, columnMappers, mappingManager); |
| 158 | 166 | } |
| 159 | 167 | |
| 160 | - private static <T> List<ColumnMapper<T>> convert(List<Field> fields, EntityMapper.Factory factory, Class<T> klass, MappingManager mappingManager, AtomicInteger columnCounter) { | |
| 161 | - List<ColumnMapper<T>> mappers = new ArrayList<ColumnMapper<T>>(fields.size()); | |
| 168 | + private static <T> Map<String, ColumnMapper<T>> createFieldMappers(List<Field> fields, EntityMapper.Factory factory, Class<T> klass, MappingManager mappingManager, AtomicInteger columnCounter) { | |
| 169 | + Map<String, ColumnMapper<T>> mappers = Maps.newHashMapWithExpectedSize(fields.size()); | |
| 162 | 170 | for (int i = 0; i < fields.size(); i++) { |
| 163 | 171 | Field field = fields.get(i); |
| 164 | 172 | int pos = position(field); |
| 165 | - mappers.add(factory.createColumnMapper(klass, field, pos < 0 ? i : pos, mappingManager, columnCounter)); | |
| 173 | + ColumnMapper<T> mapper = factory.createColumnMapper(klass, field, pos < 0 ? i : pos, mappingManager, columnCounter); | |
| 174 | + mappers.put(mapper.getColumnName(), mapper); | |
| 166 | 175 | } |
| 167 | 176 | return mappers; |
| 168 | 177 | } |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ColumnMapper.java +2 −3
| @@ -20,10 +20,9 @@ | ||
| 20 | 20 | |
| 21 | 21 | import com.google.common.reflect.TypeToken; |
| 22 | 22 | |
| 23 | +import com.datastax.driver.core.Metadata; | |
| 23 | 24 | import com.datastax.driver.core.TypeCodec; |
| 24 | 25 | |
| 25 | -import static com.datastax.driver.core.querybuilder.QueryBuilder.quote; | |
| 26 | - | |
| 27 | 26 | abstract class ColumnMapper<T> { |
| 28 | 27 | |
| 29 | 28 | public enum Kind {PARTITION_KEY, CLUSTERING_COLUMN, REGULAR, COMPUTED}; |
| @@ -57,7 +56,7 @@ | ||
| 57 | 56 | public String getColumnName() { |
| 58 | 57 | return kind == Kind.COMPUTED |
| 59 | 58 | ? columnName |
| 60 | - : quote(columnName); | |
| 59 | + : Metadata.quote(columnName); | |
| 61 | 60 | } |
| 62 | 61 | |
| 63 | 62 | public String getAlias() { |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MappedUDTCodec.java +38 −49
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.nio.ByteBuffer; |
| 19 | 19 | import java.util.List; |
| 20 | +import java.util.Map; | |
| 20 | 21 | |
| 21 | 22 | import com.google.common.collect.Lists; |
| 22 | 23 | |
| @@ -26,13 +27,13 @@ | ||
| 26 | 27 | /** |
| 27 | 28 | * Serializes a class annotated with {@code @UDT} to the corresponding CQL user-defined type. |
| 28 | 29 | */ |
| 29 | -class MappedUDTCodec<T> extends TypeCodec<T> { | |
| 30 | +class MappedUDTCodec<T> extends TypeCodec.AbstractUDTCodec<T> { | |
| 30 | 31 | private final UserType cqlUserType; |
| 31 | 32 | private final Class<T> udtClass; |
| 32 | - private final List<ColumnMapper<T>> columnMappers; | |
| 33 | + private final Map<String, ColumnMapper<T>> columnMappers; | |
| 33 | 34 | private final CodecRegistry codecRegistry; |
| 34 | 35 | |
| 35 | - public MappedUDTCodec(UserType cqlUserType, Class<T> udtClass, List<ColumnMapper<T>> columnMappers, MappingManager mappingManager) { | |
| 36 | + public MappedUDTCodec(UserType cqlUserType, Class<T> udtClass, Map<String, ColumnMapper<T>> columnMappers, MappingManager mappingManager) { | |
| 36 | 37 | super(cqlUserType, udtClass); |
| 37 | 38 | this.cqlUserType = cqlUserType; |
| 38 | 39 | this.udtClass = udtClass; |
| @@ -41,70 +42,58 @@ | ||
| 41 | 42 | } |
| 42 | 43 | |
| 43 | 44 | @Override |
| 44 | - public ByteBuffer serialize(T sourceObject, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 45 | - if (sourceObject == null) | |
| 46 | - return null; | |
| 47 | - | |
| 48 | - int size = 0; | |
| 49 | - | |
| 50 | - List<ByteBuffer> serializedFields = Lists.newArrayList(); | |
| 51 | - for (ColumnMapper<T> cm : columnMappers) { | |
| 52 | - Object value = cm.getValue(sourceObject); | |
| 53 | - TypeCodec<Object> codec = cm.getCustomCodec(); | |
| 54 | - if (codec == null) | |
| 55 | - codec = codecRegistry.codecFor(cqlUserType.getFieldType(cm.getColumnName()), cm.getJavaType()); | |
| 56 | - ByteBuffer serializedField = codec.serialize(value, protocolVersion); | |
| 57 | - size += 4 + (serializedField == null ? 0 : serializedField.remaining()); | |
| 58 | - serializedFields.add(serializedField); | |
| 59 | - } | |
| 60 | - | |
| 61 | - ByteBuffer result = ByteBuffer.allocate(size); | |
| 62 | - for (ByteBuffer bb : serializedFields) { | |
| 63 | - if (bb == null) { | |
| 64 | - result.putInt(-1); | |
| 65 | - } else { | |
| 66 | - result.putInt(bb.remaining()); | |
| 67 | - result.put(bb.duplicate()); | |
| 68 | - } | |
| 45 | + protected T newInstance() { | |
| 46 | + try { | |
| 47 | + return udtClass.newInstance(); | |
| 48 | + } catch (Exception e) { | |
| 49 | + throw new IllegalArgumentException("Error creating instance of @UDT-annotated class " + udtClass, e); | |
| 69 | 50 | } |
| 70 | - return (ByteBuffer)result.flip(); | |
| 71 | 51 | } |
| 72 | 52 | |
| 73 | 53 | @Override |
| 74 | - public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 75 | - if (bytes == null || bytes.remaining() == 0) | |
| 54 | + protected ByteBuffer serializeField(T source, String fieldName, ProtocolVersion protocolVersion) { | |
| 55 | + // The parent class passes lowercase names unquoted, but in our internal map of mappers they are always quoted | |
| 56 | + if (!fieldName.startsWith("\"")) | |
| 57 | + fieldName = Metadata.quote(fieldName); | |
| 58 | + | |
| 59 | + ColumnMapper<T> columnMapper = columnMappers.get(fieldName); | |
| 60 | + | |
| 61 | + if (columnMapper == null) | |
| 76 | 62 | return null; |
| 77 | - ByteBuffer input = bytes.duplicate(); | |
| 78 | 63 | |
| 79 | - T targetObject = newInstance(); | |
| 64 | + Object value = columnMapper.getValue(source); | |
| 80 | 65 | |
| 81 | - for (ColumnMapper<T> cm : columnMappers) { | |
| 82 | - int size = input.getInt(); | |
| 83 | - ByteBuffer serialized = (size < 0) ? null : CodecUtils.readBytes(input, size); | |
| 84 | - TypeCodec<Object> codec = cm.getCustomCodec(); | |
| 85 | - if (codec == null) | |
| 86 | - codec = codecRegistry.codecFor(cqlUserType.getFieldType(cm.getColumnName()), cm.getJavaType()); | |
| 87 | - cm.setValue(targetObject, codec.deserialize(serialized, protocolVersion)); | |
| 88 | - } | |
| 66 | + TypeCodec<Object> codec = columnMapper.getCustomCodec(); | |
| 67 | + if (codec == null) | |
| 68 | + codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType()); | |
| 89 | 69 | |
| 90 | - return targetObject; | |
| 70 | + return codec.serialize(value, protocolVersion); | |
| 91 | 71 | } |
| 92 | 72 | |
| 93 | - private T newInstance() { | |
| 94 | - try { | |
| 95 | - return udtClass.newInstance(); | |
| 96 | - } catch (Exception e) { | |
| 97 | - throw new IllegalArgumentException("Error creating instance of @UDT-annotated class " + udtClass, e); | |
| 73 | + @Override | |
| 74 | + protected T deserializeAndSetField(ByteBuffer input, T target, String fieldName, ProtocolVersion protocolVersion) { | |
| 75 | + if (!fieldName.startsWith("\"")) | |
| 76 | + fieldName = Metadata.quote(fieldName); | |
| 77 | + | |
| 78 | + ColumnMapper<T> columnMapper = columnMappers.get(fieldName); | |
| 79 | + if (columnMapper != null) { | |
| 80 | + TypeCodec<Object> codec = columnMapper.getCustomCodec(); | |
| 81 | + if (codec == null) | |
| 82 | + codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType()); | |
| 83 | + columnMapper.setValue(target, codec.deserialize(input, protocolVersion)); | |
| 98 | 84 | } |
| 85 | + return target; | |
| 99 | 86 | } |
| 100 | 87 | |
| 101 | 88 | @Override |
| 102 | - public T parse(String value) throws InvalidTypeException { | |
| 89 | + protected String formatField(T source, String fieldName) { | |
| 90 | + // This codec implementation is internal-use only, and its format method is never used | |
| 103 | 91 | throw new UnsupportedOperationException(); |
| 104 | 92 | } |
| 105 | 93 | |
| 106 | 94 | @Override |
| 107 | - public String format(T value) throws InvalidTypeException { | |
| 95 | + protected T parseAndSetField(String input, T target, String fieldName) { | |
| 96 | + // This codec implementation is internal-use only, and its parse method is never used | |
| 108 | 97 | throw new UnsupportedOperationException(); |
| 109 | 98 | } |
| 110 | 99 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java +37 −2
| @@ -113,6 +113,8 @@ | ||
| 113 | 113 | |
| 114 | 114 | @Override |
| 115 | 115 | public boolean equals(Object other) { |
| 116 | + if (this == other) | |
| 117 | + return true; | |
| 116 | 118 | if (other instanceof User) { |
| 117 | 119 | User that = (User) other; |
| 118 | 120 | return Objects.equal(this.userId, that.userId) && |
| @@ -122,6 +124,21 @@ | ||
| 122 | 124 | } |
| 123 | 125 | return false; |
| 124 | 126 | } |
| 127 | + | |
| 128 | + @Override | |
| 129 | + public int hashCode() { | |
| 130 | + return Objects.hashCode(this.userId, this.name, this.mainAddress, this.otherAddresses); | |
| 131 | + } | |
| 132 | + | |
| 133 | + @Override | |
| 134 | + public String toString() { | |
| 135 | + return Objects.toStringHelper(User.class) | |
| 136 | + .add("userId", userId) | |
| 137 | + .add("name", name) | |
| 138 | + .add("mainAddress", mainAddress) | |
| 139 | + .add("otherAddresses", otherAddresses) | |
| 140 | + .toString(); | |
| 141 | + } | |
| 125 | 142 | } |
| 126 | 143 | |
| 127 | 144 | @UDT(name = "address") |
| @@ -130,11 +147,12 @@ | ||
| 130 | 147 | // Dummy constant to test that static fields are properly ignored |
| 131 | 148 | public static final int FOO = 1; |
| 132 | 149 | |
| 133 | - private String street; | |
| 134 | - | |
| 135 | 150 | @Field // not strictly required, but we want to check that the annotation works without a name |
| 136 | 151 | private String city; |
| 137 | 152 | |
| 153 | + // Declared out of order compared to the UDT definition, to make sure that we serialize fields in the correct order (JAVA-884) | |
| 154 | + private String street; | |
| 155 | + | |
| 138 | 156 | @Field(name = "ZIP_code", caseSensitive = true) |
| 139 | 157 | private int zipCode; |
| 140 | 158 | |
| @@ -187,6 +205,8 @@ | ||
| 187 | 205 | |
| 188 | 206 | @Override |
| 189 | 207 | public boolean equals(Object other) { |
| 208 | + if (this == other) | |
| 209 | + return true; | |
| 190 | 210 | if (other instanceof Address) { |
| 191 | 211 | Address that = (Address) other; |
| 192 | 212 | return Objects.equal(this.street, that.street) && |
| @@ -196,6 +216,21 @@ | ||
| 196 | 216 | } |
| 197 | 217 | return false; |
| 198 | 218 | } |
| 219 | + | |
| 220 | + @Override | |
| 221 | + public int hashCode() { | |
| 222 | + return Objects.hashCode(this.street, this.city, this.zipCode, this.phones); | |
| 223 | + } | |
| 224 | + | |
| 225 | + @Override | |
| 226 | + public String toString() { | |
| 227 | + return Objects.toStringHelper(Address.class) | |
| 228 | + .add("street", street) | |
| 229 | + .add("city", city) | |
| 230 | + .add("zip", zipCode) | |
| 231 | + .add("phones", phones) | |
| 232 | + .toString(); | |
| 233 | + } | |
| 199 | 234 | } |
| 200 | 235 | |
| 201 | 236 | @Accessor |