private TableColumn<ConnectionData, String> buildColumn(String name, int columnWidth, Function<ConnectionData, String> extractor) { TableColumn<ConnectionData, String> column = new TableColumn<>(); Label columnLabel = new Label(name); column.setCellFactory(cellFactory(TypeCodec.varchar())); column.setCellValueFactory(param -> { String field = extractor.apply(param.getValue()); return new SimpleObjectProperty<>(field); }); column.setOnEditCommit(event -> log.info("edit")); column.setGraphic(columnLabel); column.setMinWidth(columnWidth); return column; }
private static void initializeUDTs(Session session) { Schema.ensureExists(DEFAULT_KEYSPACE + "_udts", session); MappingManager mapping = new MappingManager(session); // The UDTs are hardcoded against the zipkin keyspace. // If a different keyspace is being used the codecs must be re-applied to this different keyspace TypeCodec<TraceIdUDT> traceIdCodec = mapping.udtCodec(TraceIdUDT.class); TypeCodec<EndpointUDT> endpointCodec = mapping.udtCodec(EndpointUDT.class); TypeCodec<AnnotationUDT> annoCodec = mapping.udtCodec(AnnotationUDT.class); TypeCodec<BinaryAnnotationUDT> bAnnoCodec = mapping.udtCodec(BinaryAnnotationUDT.class); KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace()); session.getCluster().getConfiguration().getCodecRegistry() .register( new TypeCodecImpl(keyspace.getUserType("trace_id"), TraceIdUDT.class, traceIdCodec)) .register( new TypeCodecImpl(keyspace.getUserType("endpoint"), EndpointUDT.class, endpointCodec)) .register( new TypeCodecImpl(keyspace.getUserType("annotation"), AnnotationUDT.class, annoCodec)) .register( new TypeCodecImpl(keyspace.getUserType("binary_annotation"), BinaryAnnotationUDT.class, bAnnoCodec)); }
private String verifyNextRow(ResultSet rs) { String rowid = null; for (int col=0; col < 137; col++) { Row row = rs.one(); assertNotNull(row); if (rowid == null) { rowid = row.get("rowid", TypeCodec.varchar()); } else { assertEquals(row.get("rowid", TypeCodec.varchar()), rowid); } assertEquals((long) row.get("col", TypeCodec.bigint()), col); assertEquals( row.get("data", TypeCodec.blob()).asIntBuffer().get(), col); } return rowid; }
private void registerCodecs() { complexTypeCodecs = getCodecsForUserDefinedTypes(); if (complexTypeCodecs != null) { CodecRegistry registry = cluster.getConfiguration().getCodecRegistry(); if (cluster.getConfiguration().getProtocolOptions().getProtocolVersion().toInt() < 4) { LOG.error("Custom codecs are not supported for protocol version < 4"); throw new RuntimeException("Custom codecs are not supported for protocol version < 4"); } for (String typeCodecStr : complexTypeCodecs.keySet()) { TypeCodec codec = complexTypeCodecs.get(typeCodecStr); registry.register(codec); userDefinedTypesClass.put(typeCodecStr, codec.getJavaType().getRawType()); } } else { complexTypeCodecs = new HashMap<>(); } }
@Override public Map<String, TypeCodec> getCodecsForUserDefinedTypes() { Map<String, TypeCodec> allCodecs = new HashMap<>(); CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); UserType addressType = cluster.getMetadata().getKeyspace(getConnectionStateManager().getKeyspaceName()) .getUserType("address"); TypeCodec<UDTValue> addressTypeCodec = codecRegistry.codecFor(addressType); AddressCodec addressCodec = new AddressCodec(addressTypeCodec, Address.class); allCodecs.put("currentaddress", addressCodec); UserType userFullNameType = cluster.getMetadata().getKeyspace(getConnectionStateManager().getKeyspaceName()) .getUserType("fullname"); TypeCodec<UDTValue> userFullNameTypeCodec = codecRegistry.codecFor(userFullNameType); FullNameCodec fullNameCodec = new FullNameCodec(userFullNameTypeCodec, FullName.class); allCodecs.put("username", fullNameCodec); return allCodecs; }
public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> cellFactory( TypeCodec<T> codec) { return TextFieldTableCell.forTableColumn(new StringConverter<T>() { @Override public String toString(T object) { return codec.format(object); } @Override public T fromString(String string) { return codec.parse(string); } }); }
@Override public boolean test(DataObject data) { TypeCodec<?> typeCodec = getColumnCodec(data.get(getField())); Object expected = typeCodec.parse(getValue()); Object actual = data.get(getField()); if (expected instanceof Comparable && actual instanceof Comparable) { //noinspection unchecked return Comparable.class.cast(actual).compareTo(expected) != 0; } else { return expected != actual; } }
@Override public boolean test(DataObject data) { TypeCodec<?> typeCodec = getColumnCodec(data.get(getField())); Object expected = typeCodec.parse(getValue()); Object actual = data.get(getField()); if (expected instanceof Comparable && actual instanceof Comparable) { //noinspection unchecked return Comparable.class.cast(actual).compareTo(expected) < 0; } else { return false; } }
@Override public boolean test(DataObject data) { TypeCodec<?> typeCodec = getColumnCodec(data.get(getField())); Object expected = typeCodec.parse(getValue()); Object actual = data.get(getField()); if (expected instanceof Comparable && actual instanceof Comparable) { //noinspection unchecked return Comparable.class.cast(actual).compareTo(expected) <= 0; } else { return false; } }
@Override public boolean test(DataObject data) { TypeCodec<?> typeCodec = getColumnCodec(data.get(getField())); Object expected = typeCodec.parse(getValue()); Object actual = data.get(getField()); if (expected instanceof Comparable && actual instanceof Comparable) { //noinspection unchecked return Comparable.class.cast(actual).compareTo(expected) >= 0; } else { return false; } }
@Override public boolean test(DataObject data) { TypeCodec<?> typeCodec = getColumnCodec(data.get(getField())); Object expected = typeCodec.parse(getValue()); Object actual = data.get(getField()); if (expected instanceof Comparable && actual instanceof Comparable) { //noinspection unchecked return Comparable.class.cast(actual).compareTo(expected) > 0; } else { return false; } }
private void registerCodecIfNotFound(CodecRegistry registry, TypeCodec<?> codec) { try { registry.codecFor(codec.getCqlType(), codec.getJavaType()); } catch (CodecNotFoundException e) { registry.register(codec); } }
static TypeCodec<Object>[] codecsFor(DataType[] dataType) { TypeCodec<Object>[] codecs = new TypeCodec[dataType.length]; for (int i = 0; i < dataType.length; i++) codecs[i] = codecFor(dataType[i]); return codecs; }
/** * Construct an array containing the Java classes for the given Java Driver {@link com.datastax.driver.core.DataType}s. * * @param dataTypes array with UDF argument types * @param calledOnNullInput whether to allow {@code null} as an argument value * @return array of same size with UDF arguments */ public static Class<?>[] javaTypes(TypeCodec<Object>[] dataTypes, boolean calledOnNullInput) { Class<?>[] paramTypes = new Class[dataTypes.length]; for (int i = 0; i < paramTypes.length; i++) { Class<?> clazz = asJavaClass(dataTypes[i]); if (!calledOnNullInput) { // only care about classes that can be used in a data type if (clazz == Integer.class) clazz = int.class; else if (clazz == Long.class) clazz = long.class; else if (clazz == Byte.class) clazz = byte.class; else if (clazz == Short.class) clazz = short.class; else if (clazz == Float.class) clazz = float.class; else if (clazz == Double.class) clazz = double.class; else if (clazz == Boolean.class) clazz = boolean.class; } paramTypes[i] = clazz; } return paramTypes; }
public static ByteBuffer serialize(TypeCodec<?> codec, int protocolVersion, Object value) { if (!codec.getJavaType().getRawType().isAssignableFrom(value.getClass())) throw new InvalidTypeException("Invalid value for CQL type " + codec.getCqlType().getName().toString()); return ((TypeCodec)codec).serialize(value, ProtocolVersion.fromInt(protocolVersion)); }
static void initializeThread() { // Get the TypeCodec stuff in Java Driver initialized. // This is to get the classes loaded outside of the restricted sandbox's security context of a UDF. TypeCodec.inet().format(InetAddress.getLoopbackAddress()); TypeCodec.ascii().format(""); }
/** * 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); }
public CassandraEnumCodec(Class<E> enumClass, Map<Integer, E> enumValueMap) { this(TypeCodec.cint(), enumClass, enumValueMap); }
public CassandraEnumCodec(TypeCodec<Integer> innerCodec, Class<E> enumClass, Map<Integer, E> enumValueMap) { super(innerCodec.getCqlType(), enumClass); this.enumValueMap = enumValueMap; this.innerCodec = innerCodec; }
TypeCodec<?> getColumnCodec(Object actual) { return getCodecRegistry().codecFor(actual); }
@Override public boolean test(DataObject data) { TypeCodec<?> typeCodec = getColumnCodec(data.get(getField())); Object typedValue = typeCodec.parse(getValue()); return Objects.equals(typedValue, data.get(getField())); }
public TypeCodecImpl(DataType cqlType, Class<T> javaClass, TypeCodec<T> codec) { super(cqlType, javaClass); this.codec = codec; }
public UDTCodec(TypeCodec<UDTValue> innerCodec, Class<T> javaType) { super(innerCodec.getCqlType(), javaType); this.innerCodec = innerCodec; this.userType = (UserType) innerCodec.getCqlType(); this.javaType = javaType; }
public UDTCodec(final Cluster cluster, final String keySpace, final String userType, Class<T> javaType) { this(TypeCodec.userType(cluster.getMetadata().getKeyspace(keySpace).getUserType(userType)), javaType); }
public AddressCodec(TypeCodec<UDTValue> innerCodec, Class<Address> javaType) { super(innerCodec.getCqlType(), javaType); this.innerCodec = innerCodec; this.userType = (UserType)innerCodec.getCqlType(); }
@Override public Map<String, TypeCodec> getCodecsForUserDefinedTypes() { return null; }
public FullNameCodec(TypeCodec<UDTValue> innerCodec, Class<FullName> javaType) { super(innerCodec.getCqlType(), javaType); this.innerCodec = innerCodec; this.userType = (UserType)innerCodec.getCqlType(); }
public UUIDAsStringCodec() { super(TypeCodec.uuid(), String.class); }
public LocalDateAsDateCodec() { super(TypeCodec.date(), Date.class); }
public TimeUUIDAsStringCodec() { super(TypeCodec.timeUUID(), String.class); }
static TypeCodec<Object> codecFor(DataType dataType) { return codecRegistry.codecFor(dataType); }
public static Object deserialize(TypeCodec<?> codec, int protocolVersion, ByteBuffer value) { return codec.deserialize(value, ProtocolVersion.fromInt(protocolVersion)); }
public static Class<?> asJavaClass(TypeCodec<?> codec) { return codec.getJavaType().getRawType(); }
protected JavaUDF(TypeCodec<Object> returnCodec, TypeCodec<Object>[] argCodecs) { this.returnCodec = returnCodec; this.argCodecs = argCodecs; }
protected static Object compose(TypeCodec<Object>[] codecs, int protocolVersion, int argIndex, ByteBuffer value) { return value == null ? null : UDHelper.deserialize(codecs[argIndex], protocolVersion, value); }
protected static ByteBuffer decompose(TypeCodec<Object> codec, int protocolVersion, Object value) { return value == null ? null : UDHelper.serialize(codec, protocolVersion, value); }
@Override public <T> T get(int i, TypeCodec<T> typeCodec) { return row.get(i, typeCodec); }
@Override public <T> T get(String s, TypeCodec<T> typeCodec) { return row.get(s, typeCodec); }
public ClassWithInitializer3(TypeCodec<Object> returnDataType, TypeCodec<Object>[] argDataTypes) { super(returnDataType, argDataTypes); }