| 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.nio.ByteBuffer; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import com.google.common.collect.Lists; |
| 22 | |
| 23 | import com.datastax.driver.core.*; |
| 24 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 25 | |
| 26 | /** |
| 27 | * Serializes a class annotated with {@code @UDT} to the corresponding CQL user-defined type. |
| 28 | */ |
| 29 | class MappedUDTCodec<T> extends TypeCodec<T> { |
| 30 | private final UserType cqlUserType; |
| 31 | private final Class<T> udtClass; |
| 32 | private final List<ColumnMapper<T>> columnMappers; |
| 33 | private final CodecRegistry codecRegistry; |
| 34 | |
| 35 | public MappedUDTCodec(UserType cqlUserType, Class<T> udtClass, List<ColumnMapper<T>> columnMappers, MappingManager mappingManager) { |
| 36 | super(cqlUserType, udtClass); |
| 37 | this.cqlUserType = cqlUserType; |
| 38 | this.udtClass = udtClass; |
| 39 | this.columnMappers = columnMappers; |
| 40 | this.codecRegistry = mappingManager.getSession().getCluster().getConfiguration().getCodecRegistry(); |
| 41 | } |
| 42 | |
| 43 | @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.getPivotType()); |
| 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 | } |
| 69 | } |
| 70 | return (ByteBuffer)result.flip(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 75 | if (bytes == null || bytes.remaining() == 0) |
| 76 | return null; |
| 77 | ByteBuffer input = bytes.duplicate(); |
| 78 | |
| 79 | T targetObject = newInstance(); |
| 80 | |
| 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.getPivotType()); |
| 87 | cm.setValue(targetObject, codec.deserialize(serialized, protocolVersion)); |
| 88 | } |
| 89 | |
| 90 | return targetObject; |
| 91 | } |
| 92 | |
| 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); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public T parse(String value) throws InvalidTypeException { |
| 103 | throw new UnsupportedOperationException(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public String format(T value) throws InvalidTypeException { |
| 108 | throw new UnsupportedOperationException(); |
| 109 | } |
| 110 | } |