@Override public List<LegacyQueueMessage> getMessages(int limit, Class klass) { createQueueIfNecessary(); List<LegacyQueueMessage> messages = new ArrayList<>(); List<QueueMessage> qakkaMessages = queueMessageManager.getNextMessages( scope.getName(), limit ); for ( QueueMessage qakkaMessage : qakkaMessages ) { Object body; try { ByteBuffer messageData = queueMessageManager.getMessageData( qakkaMessage.getMessageId() ); ByteBuffer bb = (ByteBuffer)DataType.blob().deserialize( messageData, ProtocolVersion.NEWEST_SUPPORTED ); ByteArrayInputStream bais = new ByteArrayInputStream( bb.array() ); ObjectInputStream ios = new ObjectInputStream( bais ); body = ios.readObject(); } catch (Throwable t) { throw new QakkaRuntimeException( "Error de-serializing object", t ); } LegacyQueueMessage legacyQueueMessage = new LegacyQueueMessage( qakkaMessage.getQueueMessageId().toString(), null, // handle body, null); // type messages.add( legacyQueueMessage ); } return messages; }
protected static String getPrimitiveAvroTypeFromCassandraType(DataType dataType) { // Map types from Cassandra to Avro where possible if (dataType.equals(DataType.ascii()) || dataType.equals(DataType.text()) || dataType.equals(DataType.varchar()) // Nonstandard types represented by this processor as a string || dataType.equals(DataType.timestamp()) || dataType.equals(DataType.timeuuid()) || dataType.equals(DataType.uuid()) || dataType.equals(DataType.inet()) || dataType.equals(DataType.varint())) { return "string"; } else if (dataType.equals(DataType.cboolean())) { return "boolean"; } else if (dataType.equals(DataType.cint())) { return "int"; } else if (dataType.equals(DataType.bigint()) || dataType.equals(DataType.counter())) { return "long"; } else if (dataType.equals(DataType.cfloat())) { return "float"; } else if (dataType.equals(DataType.cdouble())) { return "double"; } else if (dataType.equals(DataType.blob())) { return "bytes"; } else { throw new IllegalArgumentException("createSchema: Unknown Cassandra data type " + dataType.getName() + " cannot be converted to Avro type"); } }
protected static Object getCassandraObject(Row row, int i, DataType dataType) { if (dataType.equals(DataType.blob())) { return row.getBytes(i);
statement.setDouble(paramIndex, (double) typeCodec.parse(paramValue)); } else if (mainType.equals(DataType.blob())) { statement.setBytes(paramIndex, (ByteBuffer) typeCodec.parse(paramValue));
private BlobCodec() { super(DataType.blob(), ByteBuffer.class); }
@Override DataType getTokenType() { return DataType.blob(); }
public OPPTokenIntegrationTest() { super(DataType.blob(), false); }
public OPPTokenVnodeIntegrationTest() { super(DataType.blob(), true); }
protected String parameterValueAsString(Object value) { String valueStr; if (value == null) { valueStr = "NULL"; } else { CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); TypeCodec<Object> codec = codecRegistry.codecFor(value); int maxParameterValueLength = this.maxParameterValueLength; if (codec.cqlType.equals(DataType.blob()) && maxParameterValueLength != -1) { // prevent large blobs from being converted to strings ByteBuffer buf = (ByteBuffer) value; int maxBufferLength = Math.max(2, (maxParameterValueLength - 2) / 2); boolean bufferTooLarge = buf.remaining() > maxBufferLength; if (bufferTooLarge) { value = (ByteBuffer) buf.duplicate().limit(maxBufferLength); } valueStr = codec.format(value); if (bufferTooLarge) { valueStr = valueStr + TRUNCATED_OUTPUT; } } else { valueStr = codec.format(value); if (maxParameterValueLength != -1 && valueStr.length() > maxParameterValueLength) { valueStr = valueStr.substring(0, maxParameterValueLength) + TRUNCATED_OUTPUT; } } } return valueStr; }
TypeCodec<Object> codec = codecRegistry.codecFor(type); int maxParameterValueLength = this.maxParameterValueLength; if (type.equals(DataType.blob()) && maxParameterValueLength != -1) {
.addClusteringColumn("c", DataType.cint()); for (int i = 0; i < colCount; i++) { create.addColumn("col" + i, DataType.blob());
"t", false, Arrays.asList(new UserType.Field("a", DataType.blob())), protocolVersion, codecRegistry);
@Test(groups = "unit") public void should_create_map_codec() { CollectionType cqlType = map(cint(), list(varchar())); TypeToken<Map<Integer, List<String>>> javaType = mapOf(of(Integer.class), listOf(String.class)); assertThat(new CodecRegistry().codecFor(cqlType)) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(cqlType, javaType)) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(singletonMap(42, singletonList("foo")))) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(cqlType, singletonMap(42, singletonList("foo")))) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(new HashMap<Integer, List<String>>())) .isNotNull() // empty collections are mapped to blob codec if no CQL type provided .accepts(map(blob(), blob())) .accepts(mapOf(ByteBuffer.class, ByteBuffer.class)); assertThat(new CodecRegistry().codecFor(cqlType, new HashMap<Integer, List<String>>())) .isNotNull() .accepts(cqlType) .accepts(javaType); }
@DataProvider public static Object[][] cqlAndValue() { return new Object[][] { {DataType.blob(), ByteBuffer.allocate(0), TypeCodec.blob()}, {DataType.cboolean(), true, TypeCodec.cboolean()}, {DataType.smallint(), (short) 42, TypeCodec.smallInt()}, {DataType.tinyint(), (byte) 42, TypeCodec.tinyInt()}, {DataType.cint(), 42, TypeCodec.cint()}, {DataType.bigint(), 42L, TypeCodec.bigint()}, {DataType.counter(), 42L, TypeCodec.counter()}, {DataType.cdouble(), 42D, TypeCodec.cdouble()}, {DataType.cfloat(), 42F, TypeCodec.cfloat()}, {DataType.varint(), new BigInteger("1234"), TypeCodec.varint()}, {DataType.decimal(), new BigDecimal("123.45"), TypeCodec.decimal()}, {DataType.varchar(), "foo", TypeCodec.varchar()}, {DataType.ascii(), "foo", TypeCodec.ascii()}, {DataType.timestamp(), new Date(42), TypeCodec.timestamp()}, {DataType.date(), LocalDate.fromDaysSinceEpoch(42), TypeCodec.date()}, {DataType.time(), 42L, TypeCodec.time()}, {DataType.uuid(), UUID.randomUUID(), TypeCodec.uuid()}, {DataType.timeuuid(), UUID.randomUUID(), TypeCodec.timeUUID()}, {DataType.inet(), mock(InetAddress.class), TypeCodec.inet()}, {DataType.duration(), Duration.from("1mo2d3h"), TypeCodec.duration()} }; }
@Test(groups = "unit") public void should_create_list_codec() { CollectionType cqlType = list(cint()); TypeToken<List<Integer>> javaType = listOf(Integer.class); assertThat(new CodecRegistry().codecFor(cqlType)) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(cqlType, javaType)) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(singletonList(42))) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(cqlType, singletonList(42))) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(new ArrayList<Integer>())) .isNotNull() // empty collections are mapped to blob codec if no CQL type provided .accepts(list(blob())) .accepts(listOf(ByteBuffer.class)); assertThat(new CodecRegistry().codecFor(cqlType, new ArrayList<Integer>())) .isNotNull() .accepts(cqlType) .accepts(javaType); }
@Test(groups = "unit") public void should_create_set_codec() { CollectionType cqlType = set(cint()); TypeToken<Set<Integer>> javaType = setOf(Integer.class); assertThat(new CodecRegistry().codecFor(cqlType)) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(cqlType, javaType)) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(singleton(42))) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(cqlType, singleton(42))) .isNotNull() .accepts(cqlType) .accepts(javaType); assertThat(new CodecRegistry().codecFor(new HashSet<Integer>())) .isNotNull() // empty collections are mapped to blob codec if no CQL type provided .accepts(set(blob())) .accepts(setOf(ByteBuffer.class)); assertThat(new CodecRegistry().codecFor(cqlType, new HashSet<Integer>())) .isNotNull() .accepts(cqlType) .accepts(javaType); }
@DataProvider public static Object[][] cql() { return new Object[][] { {DataType.blob(), TypeCodec.blob()}, {DataType.cboolean(), TypeCodec.cboolean()}, {DataType.smallint(), TypeCodec.smallInt()}, {DataType.tinyint(), TypeCodec.tinyInt()}, {DataType.cint(), TypeCodec.cint()}, {DataType.bigint(), TypeCodec.bigint()}, {DataType.counter(), TypeCodec.counter()}, {DataType.cdouble(), TypeCodec.cdouble()}, {DataType.cfloat(), TypeCodec.cfloat()}, {DataType.varint(), TypeCodec.varint()}, {DataType.decimal(), TypeCodec.decimal()}, {DataType.varchar(), TypeCodec.varchar()}, {DataType.ascii(), TypeCodec.ascii()}, {DataType.timestamp(), TypeCodec.timestamp()}, {DataType.date(), TypeCodec.date()}, {DataType.time(), TypeCodec.time()}, {DataType.uuid(), TypeCodec.uuid()}, {DataType.timeuuid(), TypeCodec.timeUUID()}, {DataType.inet(), TypeCodec.inet()}, {DataType.duration(), TypeCodec.duration()} }; }
@DataProvider public static Object[][] cqlAndJava() { return new Object[][] { {DataType.blob(), ByteBuffer.class, TypeCodec.blob()}, {DataType.cboolean(), Boolean.class, TypeCodec.cboolean()}, {DataType.smallint(), Short.class, TypeCodec.smallInt()}, {DataType.tinyint(), Byte.class, TypeCodec.tinyInt()}, {DataType.cint(), Integer.class, TypeCodec.cint()}, {DataType.bigint(), Long.class, TypeCodec.bigint()}, {DataType.counter(), Long.class, TypeCodec.counter()}, {DataType.cdouble(), Double.class, TypeCodec.cdouble()}, {DataType.cfloat(), Float.class, TypeCodec.cfloat()}, {DataType.varint(), BigInteger.class, TypeCodec.varint()}, {DataType.decimal(), BigDecimal.class, TypeCodec.decimal()}, {DataType.varchar(), String.class, TypeCodec.varchar()}, {DataType.ascii(), String.class, TypeCodec.ascii()}, {DataType.timestamp(), Date.class, TypeCodec.timestamp()}, {DataType.date(), LocalDate.class, TypeCodec.date()}, {DataType.time(), Long.class, TypeCodec.time()}, {DataType.uuid(), UUID.class, TypeCodec.uuid()}, {DataType.timeuuid(), UUID.class, TypeCodec.timeUUID()}, {DataType.inet(), InetAddress.class, TypeCodec.inet()}, {DataType.duration(), Duration.class, TypeCodec.duration()} }; }
@Test(groups = "short") public void should_parse_native_types() { assertThat(parse("ascii", cluster(), null, null, null, false, false)).isEqualTo(ascii()); assertThat(parse("bigint", cluster(), null, null, null, false, false)).isEqualTo(bigint()); assertThat(parse("blob", cluster(), null, null, null, false, false)).isEqualTo(blob()); assertThat(parse("boolean", cluster(), null, null, null, false, false)).isEqualTo(cboolean()); assertThat(parse("counter", cluster(), null, null, null, false, false)).isEqualTo(counter()); assertThat(parse("decimal", cluster(), null, null, null, false, false)).isEqualTo(decimal()); assertThat(parse("double", cluster(), null, null, null, false, false)).isEqualTo(cdouble()); assertThat(parse("float", cluster(), null, null, null, false, false)).isEqualTo(cfloat()); assertThat(parse("inet", cluster(), null, null, null, false, false)).isEqualTo(inet()); assertThat(parse("int", cluster(), null, null, null, false, false)).isEqualTo(cint()); assertThat(parse("text", cluster(), null, null, null, false, false)).isEqualTo(text()); assertThat(parse("varchar", cluster(), null, null, null, false, false)).isEqualTo(varchar()); assertThat(parse("timestamp", cluster(), null, null, null, false, false)) .isEqualTo(timestamp()); assertThat(parse("date", cluster(), null, null, null, false, false)).isEqualTo(date()); assertThat(parse("time", cluster(), null, null, null, false, false)).isEqualTo(time()); assertThat(parse("uuid", cluster(), null, null, null, false, false)).isEqualTo(uuid()); assertThat(parse("varint", cluster(), null, null, null, false, false)).isEqualTo(varint()); assertThat(parse("timeuuid", cluster(), null, null, null, false, false)).isEqualTo(timeuuid()); assertThat(parse("tinyint", cluster(), null, null, null, false, false)).isEqualTo(tinyint()); assertThat(parse("smallint", cluster(), null, null, null, false, false)).isEqualTo(smallint()); }