/** * Bind Tuple to BoundStatement. * * @param bs * @param inList * @throws Exception */ public void bindTuple(Session session, BoundStatement bs, List<Object> inList) throws Exception { if (!isCollection()) { throw new Exception( "attempting to bind non-collection as collection"); } LOG.trace("bindTuple: entered with {}", inList.toString()); LOG.trace("bindTuple: first object's type {}", inList.get(0).getClass() .getCanonicalName()); TupleType tupleType = Utils.getTupleType(session.getCluster() .getMetadata(), inList); TupleValue tv = tupleType.newValue(inList.toArray(new Object[inList .size()])); for (Integer pos : getPositions()) bs.setTupleValue(pos, tv); }
private Cluster doCreateCluster(CassandraProperties properties) { Cluster cluster = Cluster.builder() .withClusterName(properties.getCluster()) .withPort(properties.getPort()) .addContactPoints(properties.getContactPoints()) .withTimestampGenerator(getTimestampGenerator()) .withPoolingOptions( //TODO some default options - move to config new PoolingOptions() .setConnectionsPerHost(HostDistance.LOCAL, 4, 4) .setConnectionsPerHost(HostDistance.REMOTE, 2, 2) .setMaxRequestsPerConnection(HostDistance.LOCAL, 1024) .setMaxRequestsPerConnection(HostDistance.REMOTE, 256) ) .build(); //almost all queries are idempotent except counter updates, so it's easier to mark them as idempotent cluster.getConfiguration().getQueryOptions().setDefaultIdempotence(true); CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); TupleType tupleType = cluster.getMetadata() .newTupleType(DataType.timestamp(), DataType.varchar()); codecRegistry.register(new ZonedDateTimeCodec(tupleType)); QueryLogger queryLogger = QueryLogger.builder() .withConstantThreshold(100) .withMaxQueryStringLength(200) .build(); cluster.register(queryLogger); return cluster; }
private static CQL3Type.Raw getCql3Type(DataType dt) throws Exception { CQL3Type.Raw type; switch (dt.getName()) { case LIST: type = CQL3Type.Raw.list(getCql3Type(dt.getTypeArguments().get(0))); break; case MAP: type = CQL3Type.Raw.map(getCql3Type(dt.getTypeArguments().get(0)), getCql3Type(dt.getTypeArguments().get(1))); break; case SET: type = CQL3Type.Raw.set(getCql3Type(dt.getTypeArguments().get(0))); break; case TUPLE: ArrayList<CQL3Type.Raw> tupleTypes = new ArrayList<CQL3Type.Raw>(); for (DataType arg : ((TupleType) dt).getComponentTypes()) { tupleTypes.add(getCql3Type(arg)); } type = CQL3Type.Raw.tuple(tupleTypes); break; case UDT: // Requires this UDT to already be loaded UserType udt = (UserType) dt; type = CQL3Type.Raw.userType(new UTName(new ColumnIdentifier(udt.getKeyspace(), true), new ColumnIdentifier(udt.getTypeName(), true))); break; default: type = CQL3Type.Raw.from( Enum.<CQL3Type.Native> valueOf(CQL3Type.Native.class, dt.getName().toString().toUpperCase())); break; } if (dt.isFrozen()) { type = CQL3Type.Raw.frozen(type); } return type; }
public static <P extends Tuple2<?, ?>> Mapper<GettableByIndexData, P> newTupleMapper(Type target, TupleType tt, DatastaxMapperFactory factory) { ConstantSourceMapperBuilder<GettableByIndexData, P, DatastaxColumnKey> builder = DatastaxUDTGetter.newFieldMapperBuilder(factory, target); List<DataType> componentTypes = tt.getComponentTypes(); for(int i = 0; i < componentTypes.size(); i++) { FieldMapperColumnDefinition<DatastaxColumnKey> identity = FieldMapperColumnDefinition.identity(); builder.addMapping(new DatastaxColumnKey("elt" + i, i, componentTypes.get(i)), identity); } return builder.mapper(); }
public static <T extends Tuple2<?, ?>> Mapper<T, SettableByIndexData> newTupleMapper(Type target, TupleType tt, MapperConfig<DatastaxColumnKey, FieldMapperColumnDefinition<DatastaxColumnKey>> config, ReflectionService reflectionService) { SettableDataMapperBuilder<T> builder = newFieldMapperBuilder(config, reflectionService, target); List<DataType> componentTypes = tt.getComponentTypes(); for(int i = 0; i < componentTypes.size(); i++) { builder.addColumn(new DatastaxColumnKey("elt" + i, i, componentTypes.get(i))); } return builder.mapper(); }
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())); } }
@SuppressWarnings("unchecked") public static <P extends Tuple2<?, ?>> Getter<GettableByIndexData, P> newInstance(DatastaxMapperFactory factory, Type target, TupleType tt, int index) { Mapper<GettableByIndexData, P> mapper = newTupleMapper(target, tt, factory); return new DatastaxTupleGetter<P>(mapper, index); }
public ConverterToTupleValueMapper(Mapper<I, TupleValue> mapper, TupleType tupleType) { this.mapper = mapper; this.tupleType = tupleType; }
/** * Creates a Cassandra TupleType based on the given list of Objects. The * TupleType can then be used to create a TupleValue. * * @param values * @return */ public static TupleType getTupleType(Metadata clusterMetaData, List<Object> values) throws IllegalArgumentException { List<DataType> dataTypes = new ArrayList<DataType>(); for (Object value : values) { LOG.trace("getTupleType: Object type = " + value.getClass().getCanonicalName()); if (value instanceof ByteBuffer) { dataTypes.add(DataType.blob()); } else if (value instanceof BigDecimal) { dataTypes.add(DataType.decimal()); } else if (value instanceof BigInteger) { dataTypes.add(DataType.varint()); } else if (value instanceof Boolean) { dataTypes.add(DataType.cboolean()); } else if (value instanceof InetAddress) { dataTypes.add(DataType.inet()); } else if (value instanceof Integer) { dataTypes.add(DataType.cint()); } else if (value instanceof Short) { dataTypes.add(DataType.smallint()); } else if (value instanceof Long) { dataTypes.add(DataType.counter()); } else if (value instanceof Float) { dataTypes.add(DataType.cfloat()); } else if (value instanceof Double) { dataTypes.add(DataType.cdouble()); } else if (value instanceof Date) { dataTypes.add(DataType.timestamp()); } else if (value instanceof String) { dataTypes.add(DataType.text()); } else { throw new IllegalArgumentException("unknown data type of " + value.getClass().getCanonicalName()); } } return clusterMetaData.newTupleType(dataTypes .toArray(new DataType[dataTypes.size()])); // return TupleType.of(dataTypes.toArray(new // DataType[dataTypes.size()])); }