下面列出了com.datastax.driver.core.DataType#Name ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets field value as an object having Cassandra compatible type.
* This it could be stored directly into Cassandra without any conversions.
*
* @param obj Object instance.
* @param serializer {@link org.apache.ignite.cache.store.cassandra.serializer.Serializer} to use.
* @return Object to store in Cassandra table column.
*/
public Object getValueFromObject(Object obj, Serializer serializer) {
Object val = accessor.getValue(obj);
if (val == null)
return null;
DataType.Name cassandraType = PropertyMappingHelper.getCassandraType(val.getClass());
if (cassandraType != null)
return val;
if (serializer == null) {
throw new IllegalStateException("Can't serialize value from object '" +
val.getClass().getName() + "' field '" + name + "', cause there is no BLOB serializer specified");
}
return serializer.serialize(val);
}
private DataType.Name validateName(DataType dataType) {
final DataType.Name name = dataType.getName();
if (DataTypeHelper.isNumber(name)) {
return name;
}
throw new IllegalArgumentException("Datatype " + dataType + " not a number");
}
/**
* Initializes field info from property descriptor.
*
* @param accessor {@link PojoFieldAccessor} accessor.
*/
private void init(PojoFieldAccessor accessor) {
DataType.Name cassandraType = PropertyMappingHelper.getCassandraType(accessor.getFieldType());
cassandraType = cassandraType == null ? DataType.Name.BLOB : cassandraType;
this.colDDL = "\"" + col + "\" " + cassandraType.toString();
this.accessor = accessor;
}
/**
* Checks if two Java classes are Cassandra compatible - mapped to the same Cassandra type.
*
* @param type1 First type.
* @param type2 Second type.
* @return {@code true} if classes are compatible and {@code false} if not.
*/
public static boolean isCassandraCompatibleTypes(Class type1, Class type2) {
if (type1 == null || type2 == null)
return false;
DataType.Name t1 = PropertyMappingHelper.getCassandraType(type1);
DataType.Name t2 = PropertyMappingHelper.getCassandraType(type2);
return t1 != null && t2 != null && t1.equals(t2);
}
DataType.Name checkType(int i, DataType.Name name1, DataType.Name name2) {
DataType defined = getType(i);
if (name1 != defined.getName() && name2 != defined.getName())
throw new InvalidTypeException(String.format("Column %s is of type %s", getName(i), defined));
return defined.getName();
}
DataType.Name checkType(int i, DataType.Name name1, DataType.Name name2, DataType.Name name3) {
DataType defined = getType(i);
if (name1 != defined.getName() && name2 != defined.getName() && name3 != defined.getName())
throw new InvalidTypeException(String.format("Column %s is of type %s", getName(i), defined));
return defined.getName();
}
/**
* Generates a CellValidator for a generic instance of an object.
* We need the actual instance in order to differentiate between an UUID and a TimeUUID.
*
* @param obj an instance to use to build the new CellValidator.
* @param <T> the generic type of the provided object instance.
* @return a new CellValidator associated to the provided object.
*/
public static <T> CellValidator cellValidator(T obj) {
if (obj == null) {
return null;
}
Kind kind = Kind.objectToKind(obj);
AbstractType<?> tAbstractType = CassandraUtils.marshallerInstance(obj);
String validatorClassName = tAbstractType.getClass().getCanonicalName();
Collection<String> validatorTypes = null;
DataType.Name cqlTypeName = MAP_JAVA_TYPE_TO_DATA_TYPE_NAME.get(validatorClassName);// tAbstractType.get
return new CellValidator(validatorClassName, kind, validatorTypes, cqlTypeName);
}
/**
* private constructor.
*/
private CellValidator(String validatorClassName, Kind validatorKind, Collection<String> validatorTypes,
DataType.Name cqlTypeName) {
this.validatorClassName = validatorClassName != null ? validatorClassName : DEFAULT_VALIDATOR_CLASSNAME;
this.validatorKind = validatorKind;
this.validatorTypes = validatorTypes;
this.cqlTypeName = cqlTypeName;
}
private DataType.Name validateName(DataType dataType) {
final DataType.Name name = dataType.getName();
if (DataTypeHelper.isNumber(name)) {
return name;
}
throw new IllegalArgumentException("Datatype " + dataType + " not a number");
}
private DataType.Name validateName(DataType dataType) {
final DataType.Name name = dataType.getName();
if (DataTypeHelper.isNumber(name)) {
return name;
}
throw new IllegalArgumentException("Datatype " + dataType + " not a number");
}
private DataType.Name validateName(DataType dataType) {
final DataType.Name name = dataType.getName();
if (DataTypeHelper.isNumber(name)) {
return name;
}
throw new IllegalArgumentException("Datatype " + dataType + " not a number");
}
@Test
public void testLegacyCachingOptions() throws Exception{
final CassandraFig cassandraFig = mock(CassandraFig.class);
when(cassandraFig.getVersion()).thenReturn("2.0");
Map<String, DataType.Name> columns = new HashMap<>();
columns.put("key", DataType.Name.BLOB);
columns.put("column1", DataType.Name.TEXT);
columns.put("value", DataType.Name.BLOB);
List<String> partitionKeys = new ArrayList<>();
partitionKeys.add("key");
List<String> columnKeys = new ArrayList<>();
columnKeys.add("column1");
Map<String, String> clusteringOrder = new HashMap<>();
clusteringOrder.put("column1", "DESC");
TableDefinitionImpl table1 = new TableDefinitionImpl( cassandraFig.getApplicationKeyspace(),
CQLUtils.quote("table1"),
partitionKeys,
columnKeys,
columns,
TableDefinitionImpl.CacheOption.KEYS,
clusteringOrder
);
String createCQL = table1.getTableCQL(cassandraFig, TableDefinition.ACTION.CREATE);
logger.info(createCQL);
assertTrue(
createCQL.contains( "\"keys_only\"" ) &&
!createCQL.contains( "'keys':'ALL'" )
);
}
private ODataDataType getDataType(DataType.Name dataTypeName) {
ODataDataType dataType;
switch (dataTypeName) {
case ASCII:
/* fall through */
case TEXT:
/* fall through */
case VARCHAR:
/* fall through */
case TIMEUUID:
dataType = ODataDataType.STRING;
break;
case UUID:
dataType = ODataDataType.GUID;
break;
case BIGINT:
/* fall through */
case VARINT:
/* fall through */
case COUNTER:
dataType = ODataDataType.INT64;
break;
case BLOB:
dataType = ODataDataType.BINARY;
break;
case BOOLEAN:
dataType = ODataDataType.BOOLEAN;
break;
case DECIMAL:
/* fall through */
case FLOAT:
dataType = ODataDataType.DECIMAL;
break;
case DOUBLE:
dataType = ODataDataType.DOUBLE;
break;
case INT:
dataType = ODataDataType.INT32;
break;
case TIMESTAMP:
dataType = ODataDataType.DATE_TIMEOFFSET;
break;
case TIME:
dataType = ODataDataType.TIMEOFDAY;
break;
case DATE:
dataType = ODataDataType.DATE;
break;
default:
dataType = ODataDataType.STRING;
break;
}
return dataType;
}
/**
* @return the original CQL3 type name (if known, null otherwise)
*/
public DataType.Name getCqlTypeName() {
return cqlTypeName;
}
void checkType(int i, DataType.Name name) {
DataType defined = getType(i);
if (name != defined.getName())
throw new InvalidTypeException(String.format("Column %s is of type %s", getName(i), defined));
}
@Test
public void testTableCQL() throws Exception {
Map<String, DataType.Name> columns = new HashMap<>();
columns.put("key", DataType.Name.BLOB);
columns.put("column1", DataType.Name.TEXT);
columns.put("value", DataType.Name.BLOB);
List<String> partitionKeys = new ArrayList<>();
partitionKeys.add("key");
List<String> columnKeys = new ArrayList<>();
columnKeys.add("column1");
Map<String, String> clusteringOrder = new HashMap<>();
clusteringOrder.put("column1", "DESC");
TableDefinitionImpl table1 = new TableDefinitionImpl( cassandraFig.getApplicationKeyspace(),
CQLUtils.quote("table1"),
partitionKeys,
columnKeys,
columns,
TableDefinitionImpl.CacheOption.KEYS,
clusteringOrder
);
String createCQL = table1.getTableCQL(cassandraFig, TableDefinition.ACTION.CREATE);
String updateCQL = table1.getTableCQL(cassandraFig, TableDefinition.ACTION.UPDATE);
assertTrue(
createCQL.contains(CQLUtils.CREATE_TABLE ) &&
!createCQL.contains( CQLUtils.ALTER_TABLE ) &&
createCQL.contains( DataType.Name.BLOB.toString() ) &&
createCQL.contains( DataType.Name.TEXT.toString() )
);
assertTrue(
updateCQL.contains( CQLUtils.ALTER_TABLE ) &&
!updateCQL.contains( CQLUtils.CREATE_TABLE ) &&
!updateCQL.contains( DataType.Name.BLOB.toString() ) &&
!updateCQL.contains( DataType.Name.TEXT.toString() )
);
logger.info(createCQL);
logger.info(updateCQL);
}
@Test
public void testCachingOptions() throws Exception {
final CassandraFig cassandraFig = mock(CassandraFig.class);
when(cassandraFig.getVersion()).thenReturn("2.1");
Map<String, DataType.Name> columns = new HashMap<>();
columns.put("key", DataType.Name.BLOB);
columns.put("column1", DataType.Name.TEXT);
columns.put("value", DataType.Name.BLOB);
List<String> partitionKeys = new ArrayList<>();
partitionKeys.add("key");
List<String> columnKeys = new ArrayList<>();
columnKeys.add("column1");
Map<String, String> clusteringOrder = new HashMap<>();
clusteringOrder.put("column1", "DESC");
TableDefinitionImpl table1 = new TableDefinitionImpl( cassandraFig.getApplicationKeyspace(),
CQLUtils.quote("table1"),
partitionKeys,
columnKeys,
columns,
TableDefinitionImpl.CacheOption.KEYS,
clusteringOrder
);
String createCQL = table1.getTableCQL(cassandraFig, TableDefinition.ACTION.CREATE);
logger.info(createCQL);
assertTrue(
createCQL.contains( "'keys':'ALL'" ) &&
!createCQL.contains( "\"keys_only\"" )
);
}
private DataType.Name validateName(DataType dataType) {
final DataType.Name name = dataType.getName();
if (DataTypeHelper.isNumber(name)) {
return name;
}
throw new IllegalArgumentException("Datatype " + dataType + " not a number");
}
private DataType.Name validateName(DataType dataType) {
final DataType.Name name = dataType.getName();
if (DataTypeHelper.isNumber(name)) {
return name;
}
throw new IllegalArgumentException("Datatype " + dataType + " not a number");
}
/**
* Maps Cassandra type to specified Java type.
*
* @param clazz java class.
*
* @return Cassandra type.
*/
public static DataType.Name getCassandraType(Class clazz) {
return JAVA_TO_CASSANDRA_MAPPING.get(clazz);
}