protected static DataType getPrimitiveDataTypeFromString(String dataTypeName) { Set<DataType> primitiveTypes = DataType.allPrimitiveTypes(); for (DataType primitiveType : primitiveTypes) { if (primitiveType.toString().equals(dataTypeName)) { return primitiveType; } } return null; }
/** * Returns a set of all primitive types supported by the given protocolVersion. * * <p>Primitive types are defined as the types that don't have type arguments (that is excluding * lists, sets, and maps, tuples and udts). * * @param protocolVersion protocol version to get types for. * @return returns a set of all the primitive types for the given protocolVersion. */ static Set<DataType> allPrimitiveTypes(final ProtocolVersion protocolVersion) { return Sets.filter( DataType.allPrimitiveTypes(), new Predicate<DataType>() { @Override public boolean apply(DataType dataType) { return protocolVersion.compareTo(dataType.getName().minProtocolVersion) >= 0; } }); }
@Test(groups = "unit") public void parseNativeTest() { for (DataType dt : DataType.allPrimitiveTypes()) { if (exclude(dt)) continue; for (TestValue value : primitiveTestValues(dt)) assertThat(codecRegistry.codecFor(dt).parse(value.cqlInputString)) .as("Parsing input %s to a %s", value.cqlInputString, dt) .isEqualTo(value.javaObject); } }
@Test(groups = "unit") public void formatNativeTest() { for (DataType dt : DataType.allPrimitiveTypes()) { if (exclude(dt)) continue; for (TestValue value : primitiveTestValues(dt)) assertThat(codecRegistry.codecFor(dt).format(value.javaObject)) .as("Formatting a %s expecting %s", dt, value.cqlOutputString) .isEqualTo(value.cqlOutputString); } }
public void serializeDeserializeTest(ProtocolVersion version) { for (DataType dt : DataType.allPrimitiveTypes()) { if (exclude(dt)) continue; Object value = TestUtils.getFixedValue(dt); TypeCodec<Object> codec = codecRegistry.codecFor(dt); assertEquals(codec.deserialize(codec.serialize(value, version), version), value); } TypeCodec<Long> codec = codecRegistry.codecFor(DataType.bigint()); try { ByteBuffer badValue = ByteBuffer.allocate(4); codec.deserialize(badValue, version); fail("This should not have worked"); } catch (InvalidTypeException e) { /* That's what we want */ } }
public static DataType getPrimitiveDataTypeFromString(String dataTypeName) { Set<DataType> primitiveTypes = DataType.allPrimitiveTypes(); for (DataType primitiveType : primitiveTypes) { if (primitiveType.toString().equals(dataTypeName)) { return primitiveType; } } return null; }
/** * Returns a {@link Set} containing all Cassandra primitive types. * * @param codecRegistry the Cassandra codec registry. * @return the set of Cassandra primitive types. */ private static Set<Class<?>> getCassandraPrimitiveTypes(CodecRegistry codecRegistry) { return DataType.allPrimitiveTypes().stream() .map(codecRegistry::codecFor) .map(TypeCodec::getJavaType) .map(TypeToken::getRawType) .collect(Collectors.toSet()); }
/** * @return the map between {@link Name} and {@link DataType}. */ private static Map<Name, DataType> nameToDataType() { Map<Name, DataType> nameToDataType = new HashMap<>(16); DataType.allPrimitiveTypes().forEach(dataType -> nameToDataType.put(dataType.getName(), dataType)); nameToDataType.put(Name.VARCHAR, DataType.varchar()); nameToDataType.put(Name.TEXT, DataType.text()); return nameToDataType; }
static boolean isBuildInType(DataType dataType) { if (dataType.isCollection()) { for (DataType type : dataType.getTypeArguments()) { if (!isBuildInType(type)) { return false; } } return true; } else { return DataType.allPrimitiveTypes().contains(dataType) || (TupleType.class.isAssignableFrom(dataType.getClass())); } }
static boolean isBuildInType(DataType dataType) { if (dataType.isCollection()) { for (DataType type : dataType.getTypeArguments()) { if (!isBuildInType(type)) { return false; } } return true; } else { return DataType.allPrimitiveTypes().contains(dataType); } }
/** * @return the map between {@link Class} and {@link DataType}. * @param codecRegistry the Cassandra codec registry. * @param primitiveWrappers map of primitive to wrapper type. */ private static Map<Class<?>, DataType> classToDataType(CodecRegistry codecRegistry, Map<Class<?>, Class<?>> primitiveWrappers) { Map<Class<?>, DataType> classToDataType = new HashMap<>(16); DataType.allPrimitiveTypes().forEach(dataType -> { Class<?> javaType = codecRegistry.codecFor(dataType).getJavaType().getRawType(); classToDataType.put(javaType, dataType); Optional.ofNullable(primitiveWrappers.get(javaType)) .ifPresent(primitiveType -> classToDataType.put(primitiveType, dataType)); }); // override String to text DataType as String is used multiple times classToDataType.put(String.class, DataType.text()); // map Long to bigint as counter columns (last type aver multiple overrides) are a special use case // so map it to a more common type by default classToDataType.put(Long.class, DataType.bigint()); classToDataType.put(long.class, DataType.bigint()); // override UUID to timeuuid as regular uuid as the favored default classToDataType.put(UUID.class, DataType.uuid()); // override LocalTime to time as time columns are mapped to long by default classToDataType.put(LocalTime.class, DataType.time()); return classToDataType; }
/** * Returns a set of all primitive types supported by the given protocolVersion. * * <p>Primitive types are defined as the types that don't have type arguments (that is excluding * lists, sets, and maps, tuples and udts). * * @param protocolVersion protocol version to get types for. * @return returns a set of all the primitive types for the given protocolVersion. */ static Set<DataType> allPrimitiveTypes(final ProtocolVersion protocolVersion) { return Sets.filter( DataType.allPrimitiveTypes(), new Predicate<DataType>() { @Override public boolean apply(DataType dataType) { return protocolVersion.compareTo(dataType.getName().minProtocolVersion) >= 0; } }); }
@Test(groups = "unit") public void parseNativeTest() { for (DataType dt : DataType.allPrimitiveTypes()) { if (exclude(dt)) continue; for (TestValue value : primitiveTestValues(dt)) assertThat(codecRegistry.codecFor(dt).parse(value.cqlInputString)) .as("Parsing input %s to a %s", value.cqlInputString, dt) .isEqualTo(value.javaObject); } }
@Test(groups = "unit") public void formatNativeTest() { for (DataType dt : DataType.allPrimitiveTypes()) { if (exclude(dt)) continue; for (TestValue value : primitiveTestValues(dt)) assertThat(codecRegistry.codecFor(dt).format(value.javaObject)) .as("Formatting a %s expecting %s", dt, value.cqlOutputString) .isEqualTo(value.cqlOutputString); } }
public void serializeDeserializeTest(ProtocolVersion version) { for (DataType dt : DataType.allPrimitiveTypes()) { if (exclude(dt)) continue; Object value = TestUtils.getFixedValue(dt); TypeCodec<Object> codec = codecRegistry.codecFor(dt); assertEquals(codec.deserialize(codec.serialize(value, version), version), value); } TypeCodec<Long> codec = codecRegistry.codecFor(DataType.bigint()); try { ByteBuffer badValue = ByteBuffer.allocate(4); codec.deserialize(badValue, version); fail("This should not have worked"); } catch (InvalidTypeException e) { /* That's what we want */ } }