Example #1
0
  /**
   * @param datatype the db datatype
   * @param udtValue the udt value
   * @param fieldtype1 the field 1 type
   * @param fieldtype2 the field 2 type
   * @param fieldname the fieldname
   * @return the mapped value or <code>null</code>
   */
  public <T> Object fromUdtValue(
      DataType datatype,
      UDTValue udtValue,
      Class<?> fieldtype1,
      Class<?> fieldtype2,
      String fieldname) {
    final CodecRegistry codecRegistry = getCodecRegistry();

    // build-in type
    if (isBuildInType(datatype)) {
      final TypeCodec<T> typeCodec = codecRegistry.codecFor(datatype);

      try {
        if (udtValue.isNull(fieldname)) return null;
        return typeCodec.deserialize(udtValue.getBytesUnsafe(fieldname), protocolVersion);
      } catch (IllegalArgumentException ex) {
        return null;
      }

      // udt collection
    } else if (datatype.isCollection()) {

      // set
      if (DataType.Name.SET == datatype.getName()) {
        return fromUdtValues(
            datatype.getTypeArguments().get(0),
            ImmutableSet.copyOf(udtValue.getSet(fieldname, UDTValue.class)),
            fieldtype2);

        // list
      } else if (DataType.Name.LIST == datatype.getName()) {
        return fromUdtValues(
            datatype.getTypeArguments().get(0),
            ImmutableList.copyOf(udtValue.getList(fieldname, UDTValue.class)),
            fieldtype2);

        // map
      } else {
        if (isBuildInType(datatype.getTypeArguments().get(0))) {
          return fromUdtValues(
              datatype.getTypeArguments().get(0),
              datatype.getTypeArguments().get(1),
              ImmutableMap.<Object, Object>copyOf(
                  udtValue.getMap(fieldname, fieldtype1, UDTValue.class)),
              fieldtype1,
              fieldtype2);

        } else if (isBuildInType(datatype.getTypeArguments().get(1))) {
          return fromUdtValues(
              datatype.getTypeArguments().get(0),
              datatype.getTypeArguments().get(1),
              ImmutableMap.<Object, Object>copyOf(
                  udtValue.getMap(fieldname, UDTValue.class, fieldtype2)),
              fieldtype1,
              fieldtype2);

        } else {
          return fromUdtValues(
              datatype.getTypeArguments().get(0),
              datatype.getTypeArguments().get(1),
              ImmutableMap.<Object, Object>copyOf(
                  udtValue.getMap(fieldname, UDTValue.class, UDTValue.class)),
              fieldtype1,
              fieldtype2);
        }
      }

      // udt
    } else {
      return fromUdtValue(datatype, udtValue, fieldtype1);
    }
  }
Example #2
0
 /**
  * Deserialize a whole ByteBuffer into an object
  *
  * @param dataType
  * @param byteBuffer
  * @return
  */
 public <T> T deserialize(DataType dataType, ByteBuffer byteBuffer) {
   final CodecRegistry codecRegistry = getCodecRegistry();
   final TypeCodec<T> typeCodec = codecRegistry.codecFor(dataType);
   return typeCodec.deserialize(byteBuffer, protocolVersion);
 }
Example #3
0
 /**
  * Serialize a field using the Codec for the value itself
  *
  * @param value
  * @return
  */
 public <T> ByteBuffer serialize(T value) {
   final CodecRegistry codecRegistry = getCodecRegistry();
   final TypeCodec<T> typeCodec = codecRegistry.codecFor(value);
   return typeCodec.serialize((T) value, protocolVersion);
 }
Example #4
0
 /**
  * jwestra: 3.x API change deserialize a single field in a UDTValue map
  *
  * @param dataType
  * @param udtValue
  * @param fieldname
  * @return
  */
 public <T> T deserialize(DataType dataType, UDTValue udtValue, String fieldname) {
   final CodecRegistry codecRegistry = getCodecRegistry();
   final TypeCodec<T> typeCodec = codecRegistry.codecFor(dataType);
   return typeCodec.deserialize(udtValue.getBytesUnsafe(fieldname), protocolVersion);
 }
Example #5
0
 /**
  * Serialize a field using the data type passed.
  *
  * @param dataType
  * @param value
  * @return
  */
 @SuppressWarnings("unchecked")
 public <T> ByteBuffer serialize(DataType dataType, Object value) {
   final CodecRegistry codecRegistry = getCodecRegistry();
   final TypeCodec<T> typeCodec = codecRegistry.codecFor(dataType);
   return typeCodec.serialize((T) value, protocolVersion);
 }