org.apache.cassandra.db.marshal.ListType Java Examples

The following examples show how to use org.apache.cassandra.db.marshal.ListType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraTypeConverterTest.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@Test
public void testList() {
    // list of ints
    // test non-frozen
    DataType listType = DataType.list(DataType.cint(), false);
    AbstractType<?> convertedType = CassandraTypeConverter.convert(listType);

    ListType<?> expectedType = ListType.getInstance(Int32Type.instance, true);
    Assert.assertEquals(expectedType, convertedType);

    // test frozen
    listType = DataType.list(DataType.cint(), true);
    convertedType = CassandraTypeConverter.convert(listType);
    expectedType = ListType.getInstance(Int32Type.instance, false);
    Assert.assertEquals(expectedType, convertedType);
    Assert.assertTrue("Expected convertedType to be frozen", convertedType.isFrozenCollection());
}
 
Example #2
Source File: CassandraTypeDeserializerTest.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@Test
public void testListType() {
    List<Integer> expectedList = new ArrayList<>();
    expectedList.add(1);
    expectedList.add(3);
    expectedList.add(5);

    // non-frozen
    ListType<Integer> nonFrozenListType = ListType.getInstance(Int32Type.instance, true);
    ByteBuffer serializedList = nonFrozenListType.decompose(expectedList);
    Object deserializedList = CassandraTypeDeserializer.deserialize(nonFrozenListType, serializedList);
    Assert.assertEquals(expectedList, deserializedList);

    // frozen
    ListType<Integer> frozenListType = ListType.getInstance(Int32Type.instance, false);
    serializedList = frozenListType.decompose(expectedList);
    deserializedList = CassandraTypeDeserializer.deserialize(frozenListType, serializedList);
    Assert.assertEquals(expectedList, deserializedList);
}
 
Example #3
Source File: CellValidatorTest.java    From deep-spark with Apache License 2.0 6 votes vote down vote up
public void testValidatorClassToKind() {
    assertEquals(Kind.validatorClassToKind(null), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(TimeUUIDType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(UTF8Type.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(Int32Type.class), Kind.NOT_A_COLLECTION);

    assertEquals(Kind.validatorClassToKind(BooleanType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(TimestampType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(DecimalType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(LongType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(DoubleType.class), Kind.NOT_A_COLLECTION);

    assertEquals(Kind.validatorClassToKind(FloatType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(InetAddressType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(IntegerType.class), Kind.NOT_A_COLLECTION);
    assertEquals(Kind.validatorClassToKind(UUIDType.class), Kind.NOT_A_COLLECTION);

    assertEquals(Kind.validatorClassToKind(SetType.class), Kind.SET);
    assertEquals(Kind.validatorClassToKind(ListType.class), Kind.LIST);
    assertEquals(Kind.validatorClassToKind(MapType.class), Kind.MAP);
}
 
Example #4
Source File: ColumnMapper.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Returns {@code true} if the specified Cassandra type/marshaller is supported, {@code false} otherwise.
 *
 * @param type A Cassandra type/marshaller.
 * @return {@code true} if the specified Cassandra type/marshaller is supported, {@code false} otherwise.
 */
public boolean supports(final AbstractType<?> type) {
    AbstractType<?> checkedType = type;
    if (type.isCollection()) {
        if (type instanceof MapType<?, ?>) {
            checkedType = ((MapType<?, ?>) type).getValuesType();
        } else if (type instanceof ListType<?>) {
            checkedType = ((ListType<?>) type).getElementsType();
        } else if (type instanceof SetType) {
            checkedType = ((SetType<?>) type).getElementsType();
        }
    }

    if (type instanceof ReversedType) {
        ReversedType reversedType = (ReversedType) type;
        checkedType = reversedType.baseType;
    }

    for (AbstractType<?> n : supportedTypes) {
        if (checkedType.getClass() == n.getClass()) {
            return true;
        }
    }
    return false;
}
 
Example #5
Source File: Lists.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static Value fromSerialized(ByteBuffer value, ListType type, int version) throws InvalidRequestException
{
    try
    {
        // Collections have this small hack that validate cannot be called on a serialized object,
        // but compose does the validation (so we're fine).
        List<?> l = (List<?>)type.getSerializer().deserializeForNativeProtocol(value, version);
        List<ByteBuffer> elements = new ArrayList<ByteBuffer>(l.size());
        for (Object element : l)
            // elements can be null in lists that represent a set of IN values
            elements.add(element == null ? null : type.getElementsType().decompose(element));
        return new Value(elements);
    }
    catch (MarshalException e)
    {
        throw new InvalidRequestException(e.getMessage());
    }
}
 
Example #6
Source File: ListTypeDeserializer.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
@Override
public SchemaBuilder getSchemaBuilder(AbstractType<?> abstractType) {
    ListType<?> listType = (ListType<?>) abstractType;
    AbstractType<?> elementsType = listType.getElementsType();
    Schema innerSchema = CassandraTypeDeserializer.getSchemaBuilder(elementsType).build();
    return SchemaBuilder.array(innerSchema);
}
 
Example #7
Source File: ListTypeConverter.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
@Override
public ListType convert(DataType dataType) {
    // list only has one inner type.
    DataType innerDataType = dataType.getTypeArguments().get(0);
    AbstractType<?> innerAbstractType = CassandraTypeConverter.convert(innerDataType);
    return ListType.getInstance(innerAbstractType, !dataType.isFrozen());
}
 
Example #8
Source File: CellValidator.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * private constructor.
 *
 * @param type a {@link com.datastax.driver.core.DataType} coming from the underlying Java driver.
 */
private CellValidator(DataType type) {
    if (type == null) {
        throw new DeepInstantiationException("input DataType cannot be null");
    }

    cqlTypeName = type.getName();

    if (!type.isCollection()) {
        validatorClassName = MAP_JAVA_TYPE_TO_ABSTRACT_TYPE.get(type.asJavaClass()).getClass()
                .getName();
    } else {
        validatorTypes = new ArrayList<>();

        for (DataType dataType : type.getTypeArguments()) {
            validatorTypes.add(dataType.toString());
        }

        switch (type.getName()) {
        case SET:
            validatorKind = Kind.SET;
            validatorClassName = SetType.class.getName();
            break;
        case LIST:
            validatorKind = Kind.LIST;
            validatorClassName = ListType.class.getName();
            break;
        case MAP:
            validatorKind = Kind.MAP;
            validatorClassName = MapType.class.getName();
            break;
        default:
            throw new DeepGenericException("Cannot determine collection type for " + type.getName());
        }

        validatorTypes = unmodifiableCollection(validatorTypes);
    }
}
 
Example #9
Source File: CellValidatorTest.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
public void testDataTypeListInstantiation() {
    try {
        assertNull(cellValidator((DataType) null));
        fail();
    } catch (Exception e) {
        //ok
    }

    DataType type = DataType.list(DataType.timeuuid());

    CellValidator cv = cellValidator(type);
    assertNotNull(cv);
    assertEquals(cv.getValidatorClassName(), ListType.class.getName());
    assertNotNull(cv.getValidatorTypes());
    assertEquals(cv.validatorKind(), Kind.LIST);
    assertEquals(cv.getValidatorTypes().size(), 1);
    assertEquals(cv.getValidatorTypes().iterator().next(), "timeuuid");
    assertEquals(DataType.Name.LIST, cv.getCqlTypeName());

    try {
        Collection<String> types = cv.getValidatorTypes();
        types.add("test");
        fail("Validator types collection must be inmutable");
    } catch (Exception ex) {
        // ok
    }

    //        assertNotNull(cv.getAbstractType());
    //        assertEquals(cv.getAbstractType(), ListType.getInstance(TimeUUIDType.instance));
}
 
Example #10
Source File: FunctionCall.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static Term.Terminal makeTerminal(Function fun, ByteBuffer result, int version) throws InvalidRequestException
{
    if (!(fun.returnType() instanceof CollectionType))
        return new Constants.Value(result);

    switch (((CollectionType)fun.returnType()).kind)
    {
        case LIST: return Lists.Value.fromSerialized(result, (ListType)fun.returnType(), version);
        case SET:  return Sets.Value.fromSerialized(result, (SetType)fun.returnType(), version);
        case MAP:  return Maps.Value.fromSerialized(result, (MapType)fun.returnType(), version);
    }
    throw new AssertionError();
}
 
Example #11
Source File: Lists.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void validateAssignableTo(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
    if (!(receiver.type instanceof ListType))
        throw new InvalidRequestException(String.format("Invalid list literal for %s of type %s", receiver.name, receiver.type.asCQL3Type()));

    ColumnSpecification valueSpec = Lists.valueSpecOf(receiver);
    for (Term.Raw rt : elements)
    {
        if (!rt.isAssignableTo(keyspace, valueSpec))
            throw new InvalidRequestException(String.format("Invalid list literal for %s: value %s is not of type %s", receiver.name, rt, valueSpec.type.asCQL3Type()));
    }
}
 
Example #12
Source File: Lists.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public boolean equals(ListType lt, Value v)
{
    if (elements.size() != v.elements.size())
        return false;

    for (int i = 0; i < elements.size(); i++)
        if (lt.getElementsType().compare(elements.get(i), v.elements.get(i)) != 0)
            return false;

    return true;
}
 
Example #13
Source File: Lists.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Lists(String name, Generator valueType, GeneratorConfig config)
{
    super(ListType.getInstance(valueType.type, true), config, name, List.class);
    this.valueType = valueType;
    buffer = new Object[(int) sizeDistribution.maxValue()];
}
 
Example #14
Source File: Lists.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Value bind(QueryOptions options) throws InvalidRequestException
{
    ByteBuffer value = options.getValues().get(bindIndex);
    return value == null ? null : Value.fromSerialized(value, (ListType)receiver.type, options.getProtocolVersion());
}
 
Example #15
Source File: Lists.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static ColumnSpecification valueSpecOf(ColumnSpecification column)
{
    return new ColumnSpecification(column.ksName, column.cfName, new ColumnIdentifier("value(" + column.name + ")", true), ((ListType)column.type).getElementsType());
}
 
Example #16
Source File: AbstractMarker.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private static ColumnSpecification makeInReceiver(ColumnSpecification receiver)
{
    ColumnIdentifier inName = new ColumnIdentifier("in(" + receiver.name + ")", true);
    return new ColumnSpecification(receiver.ksName, receiver.cfName, inName, ListType.getInstance(receiver.type, false));
}
 
Example #17
Source File: CellValidator.java    From deep-spark with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the Kind associated to the provided Cassandra validator class.
 * <p/>
 * <p>
 * To be more specific, returns:
 * <ul>
 * <li>NOT_A_COLLECTION: when the provided type is not a set a list or a map</li>
 * <li>MAP: when the provided validator is MapType.class</li>
 * <li>LIST: when the provided validator is ListType.class</li>
 * <li>SET: when the provided validator is SetType.class</li>
 * </ul>
 * </p>
 *
 * @param type the marshaller Class object.
 * @return the Kind associated to the provided marhaller Class object.
 */
public static Kind validatorClassToKind(Class<? extends AbstractType> type) {
    if (type == null) {
        return NOT_A_COLLECTION;
    }

    if (type.equals(SetType.class)) {
        return SET;
    } else if (type.equals(ListType.class)) {
        return LIST;
    } else if (type.equals(MapType.class)) {
        return MAP;
    } else {
        return NOT_A_COLLECTION;
    }
}