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

The following examples show how to use org.apache.cassandra.db.marshal.SetType. 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 testSet() {
    // set of floats
    // test non-frozen
    DataType setType = DataType.set(DataType.cfloat(), false);
    AbstractType<?> convertedType = CassandraTypeConverter.convert(setType);

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

    // test frozen
    setType = DataType.set(DataType.cfloat(), true);
    convertedType = CassandraTypeConverter.convert(setType);
    expectedType = SetType.getInstance(FloatType.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 testSetType() {
    Set<Float> sourceSet = new HashSet<>();
    sourceSet.add(42F);
    sourceSet.add(123F);

    // non-frozen
    SetType<Float> nonFrozenSetType = SetType.getInstance(FloatType.instance, true);
    ByteBuffer serializedSet = nonFrozenSetType.decompose(sourceSet);
    Collection<?> deserializedSet = (Collection<?>) CassandraTypeDeserializer.deserialize(nonFrozenSetType, serializedSet);
    // order may be different in the resulting collection.
    Assert.assertTrue(sourceSet.containsAll(deserializedSet));
    Assert.assertTrue(deserializedSet.containsAll(sourceSet));

    // frozen
    SetType<Float> frozenSetType = SetType.getInstance(FloatType.instance, false);
    serializedSet = frozenSetType.decompose(sourceSet);
    deserializedSet = (Collection<?>) CassandraTypeDeserializer.deserialize(frozenSetType, serializedSet);
    Assert.assertTrue(sourceSet.containsAll(deserializedSet));
    Assert.assertTrue(deserializedSet.containsAll(sourceSet));
}
 
Example #3
Source File: CellValidatorTest.java    From deep-spark with Apache License 2.0 6 votes vote down vote up
public void testDataTypeSetInstantiation() {
    DataType type = DataType.set(DataType.text());

    CellValidator cv = cellValidator(type);
    assertNotNull(cv);
    assertEquals(cv.getValidatorClassName(), SetType.class.getName());
    assertNotNull(cv.getValidatorTypes());
    assertEquals(cv.validatorKind(), Kind.SET);
    assertEquals(cv.getValidatorTypes().size(), 1);
    assertEquals(cv.getValidatorTypes().iterator().next(), "text");
    assertEquals(DataType.Name.SET, 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(), SetType.getInstance(UTF8Type.instance));
}
 
Example #4
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 #5
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 #6
Source File: Sets.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void validateAssignableTo(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
    if (!(receiver.type instanceof SetType))
    {
        // We've parsed empty maps as a set literal to break the ambiguity so
        // handle that case now
        if ((receiver.type instanceof MapType) && elements.isEmpty())
            return;

        throw new InvalidRequestException(String.format("Invalid set literal for %s of type %s", receiver.name, receiver.type.asCQL3Type()));
    }

    ColumnSpecification valueSpec = Sets.valueSpecOf(receiver);
    for (Term.Raw rt : elements)
    {
        if (!rt.isAssignableTo(keyspace, valueSpec))
            throw new InvalidRequestException(String.format("Invalid set literal for %s: value %s is not of type %s", receiver.name, rt, valueSpec.type.asCQL3Type()));
    }
}
 
Example #7
Source File: Sets.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static Value fromSerialized(ByteBuffer value, SetType 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).
        Set<?> s = (Set<?>)type.getSerializer().deserializeForNativeProtocol(value, version);
        SortedSet<ByteBuffer> elements = new TreeSet<ByteBuffer>(type.getElementsType());
        for (Object element : s)
            elements.add(type.getElementsType().decompose(element));
        return new Value(elements);
    }
    catch (MarshalException e)
    {
        throw new InvalidRequestException(e.getMessage());
    }
}
 
Example #8
Source File: SetTypeDeserializer.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
@Override
public SchemaBuilder getSchemaBuilder(AbstractType<?> abstractType) {
    SetType<?> listType = (SetType<?>) abstractType;
    AbstractType<?> elementsType = listType.getElementsType();
    Schema innerSchema = CassandraTypeDeserializer.getSchemaBuilder(elementsType).build();
    return SchemaBuilder.array(innerSchema);
}
 
Example #9
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 #10
Source File: Sets.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public Term prepare(String keyspace, ColumnSpecification receiver) throws InvalidRequestException
{
    validateAssignableTo(keyspace, receiver);

    // We've parsed empty maps as a set literal to break the ambiguity so
    // handle that case now
    if (receiver.type instanceof MapType && elements.isEmpty())
        return new Maps.Value(Collections.<ByteBuffer, ByteBuffer>emptyMap());

    ColumnSpecification valueSpec = Sets.valueSpecOf(receiver);
    Set<Term> values = new HashSet<Term>(elements.size());
    boolean allTerminal = true;
    for (Term.Raw rt : elements)
    {
        Term t = rt.prepare(keyspace, valueSpec);

        if (t.containsBindMarker())
            throw new InvalidRequestException(String.format("Invalid set literal for %s: bind variables are not supported inside collection literals", receiver.name));

        if (t instanceof Term.NonTerminal)
            allTerminal = false;

        values.add(t);
    }
    DelayedValue value = new DelayedValue(((SetType)receiver.type).getElementsType(), values);
    return allTerminal ? value.bind(QueryOptions.DEFAULT) : value;
}
 
Example #11
Source File: Sets.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public boolean equals(SetType st, Value v)
{
    if (elements.size() != v.elements.size())
        return false;

    Iterator<ByteBuffer> thisIter = elements.iterator();
    Iterator<ByteBuffer> thatIter = v.elements.iterator();
    AbstractType elementsType = st.getElementsType();
    while (thisIter.hasNext())
        if (elementsType.compare(thisIter.next(), thatIter.next()) != 0)
            return false;

    return true;
}
 
Example #12
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 #13
Source File: Sets.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Sets(String name, Generator valueType, GeneratorConfig config)
{
    super(SetType.getInstance(valueType.type, true), config, name, Set.class);
    this.valueType = valueType;
}
 
Example #14
Source File: SetTypeConverter.java    From debezium-incubator with Apache License 2.0 4 votes vote down vote up
@Override
public SetType convert(DataType dataType) {
    AbstractType<?> innerType = CassandraTypeConverter.convert(dataType.getTypeArguments().get(0));
    innerType.getSerializer();
    return SetType.getInstance(innerType, !dataType.isFrozen());
}
 
Example #15
Source File: Sets.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, (SetType)receiver.type, options.getProtocolVersion());
}
 
Example #16
Source File: Sets.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), ((SetType)column.type).getElementsType());
}
 
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;
    }
}