Example usage for org.apache.cassandra.db.marshal SetType getInstance

List of usage examples for org.apache.cassandra.db.marshal SetType getInstance

Introduction

In this page you can find the example usage for org.apache.cassandra.db.marshal SetType getInstance.

Prototype

public static SetType<?> getInstance(TypeParser parser) throws ConfigurationException, SyntaxException 

Source Link

Usage

From source file:clojurewerkz.cassaforte.Codec.java

License:Apache License

private static AbstractType getCodecInternal(DataType type) {
    switch (type.getName()) {
    case ASCII://from  w w  w  .  j  a va 2s. c om
        return AsciiType.instance;
    case BIGINT:
        return LongType.instance;
    case BLOB:
        return BytesType.instance;
    case BOOLEAN:
        return BooleanType.instance;
    case COUNTER:
        return CounterColumnType.instance;
    case DECIMAL:
        return DecimalType.instance;
    case DOUBLE:
        return DoubleType.instance;
    case FLOAT:
        return FloatType.instance;
    case INET:
        return InetAddressType.instance;
    case INT:
        return Int32Type.instance;
    case TEXT:
        return UTF8Type.instance;
    case TIMESTAMP:
        return DateType.instance;
    case UUID:
        return UUIDType.instance;
    case VARCHAR:
        return UTF8Type.instance;
    case VARINT:
        return IntegerType.instance;
    case TIMEUUID:
        return TimeUUIDType.instance;
    case LIST:
        return ListType.getInstance(getCodec(type.getTypeArguments().get(0)));
    case SET:
        return SetType.getInstance(getCodec(type.getTypeArguments().get(0)));
    case MAP:
        return MapType.getInstance(getCodec(type.getTypeArguments().get(0)),
                getCodec(type.getTypeArguments().get(1)));
    default:
        throw new RuntimeException("Unknown type");
    }
}

From source file:com.datastax.driver.core.Codec.java

License:Apache License

private static AbstractType<?> getCodecInternal(DataType type) {
    switch (type.getName()) {
    case ASCII:/*  ww w.  j  ava  2 s  .c  o m*/
        return AsciiType.instance;
    case BIGINT:
        return LongType.instance;
    case BLOB:
        return BytesType.instance;
    case BOOLEAN:
        return BooleanType.instance;
    case COUNTER:
        return CounterColumnType.instance;
    case DECIMAL:
        return DecimalType.instance;
    case DOUBLE:
        return DoubleType.instance;
    case FLOAT:
        return FloatType.instance;
    case INET:
        return InetAddressType.instance;
    case INT:
        return Int32Type.instance;
    case TEXT:
        return UTF8Type.instance;
    case TIMESTAMP:
        return DateType.instance;
    case UUID:
        return UUIDType.instance;
    case VARCHAR:
        return UTF8Type.instance;
    case VARINT:
        return IntegerType.instance;
    case TIMEUUID:
        return TimeUUIDType.instance;
    case LIST:
        return ListType.getInstance(getCodec(type.getTypeArguments().get(0)));
    case SET:
        return SetType.getInstance(getCodec(type.getTypeArguments().get(0)));
    case MAP:
        return MapType.getInstance(getCodec(type.getTypeArguments().get(0)),
                getCodec(type.getTypeArguments().get(1)));
    // We don't interpret custom values in any way
    case CUSTOM:
        return BytesType.instance;
    default:
        throw new RuntimeException("Unknown type");
    }
}

From source file:com.impetus.client.cassandra.datahandler.CassandraDataHandlerBase.java

License:Apache License

/**
 * Sets the element collection.//from w w w.  j  a va 2s  .  c o m
 * 
 * @param entity
 *            the entity
 * @param thriftColumnValue
 *            the thrift column value
 * @param metaModel
 *            the meta model
 * @param attribute
 *            the attribute
 * @return the object
 */
private Object setElementCollection(Object entity, Object thriftColumnValue, MetamodelImpl metaModel,
        Attribute attribute) {
    String cqlColumnMetadata = null;
    Map<ByteBuffer, String> schemaTypes = this.clientBase.getCqlMetadata().getValue_types();
    for (Map.Entry<ByteBuffer, String> schemaType : schemaTypes.entrySet()) {

        String key = UTF8Serializer.instance.deserialize((schemaType.getKey()));
        if (key.equals(((AbstractAttribute) attribute).getJavaMember().getName())) {
            cqlColumnMetadata = schemaType.getValue();
        }
    }

    Field field = (Field) ((AbstractAttribute) attribute).getJavaMember();
    Class embeddedClass = ((AbstractAttribute) attribute).getBindableJavaType();

    if (List.class.isAssignableFrom(((Field) attribute.getJavaMember()).getType())) {
        ListType listType = null;
        try {
            listType = ListType.getInstance(new TypeParser(
                    cqlColumnMetadata.substring(cqlColumnMetadata.indexOf("("), cqlColumnMetadata.length())));
        } catch (ConfigurationException | SyntaxException e) {
            log.error(e.getMessage());
            throw new KunderaException("Error while getting instance of ListType " + e);
        }
        return setElementCollectionList(listType, ByteBuffer.wrap((byte[]) thriftColumnValue), entity, field,
                metaModel, embeddedClass, true);

    } else if (Set.class.isAssignableFrom(((Field) attribute.getJavaMember()).getType())) {
        SetType setType = null;
        try {
            setType = SetType.getInstance(new TypeParser(
                    cqlColumnMetadata.substring(cqlColumnMetadata.indexOf("("), cqlColumnMetadata.length())));
        } catch (ConfigurationException | SyntaxException e) {
            log.error(e.getMessage());
            throw new KunderaException("Error while getting instance of SetType " + e);
        }
        return setElementCollectionSet(setType, ByteBuffer.wrap((byte[]) thriftColumnValue), entity, field,
                metaModel, embeddedClass, true);

    } else if (Map.class.isAssignableFrom(((Field) attribute.getJavaMember()).getType())) {
        MapType mapType = null;
        try {
            mapType = MapType.getInstance(new TypeParser(
                    cqlColumnMetadata.substring(cqlColumnMetadata.indexOf("("), cqlColumnMetadata.length())));
        } catch (ConfigurationException | SyntaxException e) {
            log.error(e.getMessage());
            throw new KunderaException("Error while getting instance of MapType " + e);
        }
        return setElementCollectionMap(mapType, ByteBuffer.wrap((byte[]) thriftColumnValue), entity, field,
                metaModel, embeddedClass, true);
    }

    return entity;

}

From source file:com.stratio.deep.cassandra.util.CassandraUtils.java

License:Apache License

/**
 * Returns an instance of the Cassandra validator that matches the provided object.
 *
 * @param obj the object to use to resolve the cassandra marshaller.
 * @param <T> the generic object type.
 * @return an instance of the Cassandra validator that matches the provided object.
 * @throws com.stratio.deep.commons.exception.DeepGenericException if no validator can be found for the specified object.
 *//*from   w w w . ja va  2  s .  c  o  m*/
public static <T> AbstractType<?> marshallerInstance(T obj) {

    AbstractType<?> abstractType = null;

    if (obj != null) {
        abstractType = MAP_JAVA_TYPE_TO_ABSTRACT_TYPE.get(obj.getClass());

        if (obj instanceof UUID) {
            UUID uuid = (UUID) obj;

            if (uuid.version() == 1) {
                abstractType = TimeUUIDType.instance;

            } else {
                abstractType = UUIDType.instance;
            }
        }

        if (abstractType == null) {
            //LIST Case
            if (List.class.isAssignableFrom(obj.getClass())) {

                List list = (List) obj;
                if (!list.isEmpty()) {
                    abstractType = ListType.getInstance(marshallerInstance(list.get(0)));
                }

            }
            // SET Case
            else if (Set.class.isAssignableFrom(obj.getClass())) {
                Set set = (Set) obj;
                if (!set.isEmpty()) {
                    java.util.Iterator i = set.iterator();
                    Object o = i.next();
                    abstractType = SetType.getInstance(marshallerInstance(o));
                }
            }
            // MAP Case
            else if (Map.class.isAssignableFrom(obj.getClass())) {
                Set set = ((Map) obj).keySet();
                if (!set.isEmpty()) {
                    java.util.Iterator i = set.iterator();
                    Object o = i.next();
                    abstractType = MapType.getInstance(marshallerInstance(o),
                            marshallerInstance(((Map) obj).get(o)));

                }

            }
        }

    }

    if (abstractType == null) {
        throw new DeepGenericException("parameter class " + obj.getClass().getCanonicalName()
                + " does not have a" + " Cassandra marshaller");
    }

    return abstractType;
}

From source file:info.archinnov.achilles.entity.operations.CQLNativeQueryMapperTest.java

License:Apache License

@Test
public void should_map_rows_with_set() throws Exception {
    Set<String> followers = new HashSet<String>();

    ColumnIdentifier iden1 = new ColumnIdentifier(UTF8Type.instance.decompose("followers"), UTF8Type.instance);
    ColumnSpecification spec1 = new ColumnSpecification("keyspace", "followers", iden1,
            SetType.getInstance(UTF8Type.instance));

    def1 = Whitebox.invokeMethod(Definition.class, "fromTransportSpecification", spec1);

    when(row.getColumnDefinitions()).thenReturn(columnDefs);
    when(columnDefs.iterator()).thenReturn(Arrays.asList(def1).iterator());

    when(row.getSet("followers", String.class)).thenReturn(followers);
    List<Map<String, Object>> result = mapper.mapRows(Arrays.asList(row));

    assertThat(result).hasSize(1);/*www  .ja va 2 s.  c o m*/
    Map<String, Object> line = result.get(0);

    assertThat(line).hasSize(1);
    assertThat(line.get("followers")).isSameAs(followers);
}

From source file:kina.entity.CellTest.java

License:Apache License

@Test
public void testCellInstantiationForCollections() throws UnknownHostException, NoSuchFieldException {
    CommonsTestEntity te = new CommonsTestEntity();

    Set<String> emails = new HashSet<>(
            Arrays.asList("DelfinaMarino@superrito.com", "GabyCasasVeliz@superrito.com"));
    List<String> phones = Arrays.asList("401-477-8301", "209-845-8841");

    te.setEmails(emails);//from  w  w w  . j a v a  2 s.c o  m
    te.setPhones(phones);
    Map<UUID, Integer> map = new HashMap<>();
    map.put(UUID.fromString("0E175996-1D5C-4CD1-A0C2-8F4EFAF59F37"), 3211);
    map.put(UUID.fromString("CFC4B1ED-A188-4541-A25B-77098D89A555"), 3212);
    map.put(UUID.fromString("A0C6954F-E576-44C8-94B3-89C9A52BBC7E"), 3213);
    te.setUuid2id(map);

    CassandraCell c1 = (CassandraCell) CassandraCell.create(te,
            CommonsTestEntity.class.getDeclaredField("emails"));

    assertNotNull(c1);
    assertEquals(c1.getCellName(), "emails");
    assertEquals(SetType.getInstance(UTF8Type.instance).compose(c1.getDecomposedCellValue()), emails);
    assertTrue(c1.marshallerClassName().equals(SetType.class.getCanonicalName()));
    assertEquals(c1.getValueType(), Set.class);

    CassandraCell c2 = (CassandraCell) CassandraCell.create(te,
            CommonsTestEntity.class.getDeclaredField("phones"));

    assertNotNull(c2);
    assertEquals(c2.getCellName(), "phones");
    assertEquals(ListType.getInstance(UTF8Type.instance).compose(c2.getDecomposedCellValue()), phones);
    assertTrue(c2.marshallerClassName().equals(ListType.class.getCanonicalName()));
    assertEquals(c2.getValueType(), List.class);

    CassandraCell c3 = (CassandraCell) CassandraCell.create(te,
            CommonsTestEntity.class.getDeclaredField("uuid2id"));

    assertNotNull(c3);
    assertEquals(c3.getCellName(), "uuid2id");
    assertEquals(
            MapType.getInstance(UUIDType.instance, Int32Type.instance).compose(c3.getDecomposedCellValue()),
            map);
    assertTrue(c3.marshallerClassName().equals(MapType.class.getCanonicalName()));
    assertEquals(c3.getValueType(), Map.class);
}

From source file:kina.entity.CellValidator.java

License:Apache License

/**
 * Generates the cassandra marshaller ({@link org.apache.cassandra.db.marshal.AbstractType}) for this CellValidator.
 *
 * @return an instance of the cassandra marshaller for this CellValidator.
 *//*from  w ww  .ja v a 2 s  .co m*/
public AbstractType<?> getAbstractType() {
    if (abstractType != null) {
        return abstractType;
    }

    try {

        if (validatorKind == Kind.NOT_A_COLLECTION) {
            abstractType = AnnotationUtils.MAP_ABSTRACT_TYPE_CLASS_TO_ABSTRACT_TYPE
                    .get(forName(validatorClassName));

        } else {
            Iterator<String> types = validatorTypes.iterator();

            switch (validatorKind) {
            case SET:
                CQL3Type cql3Type = CQL3Type.Native.valueOf(types.next().toUpperCase());
                abstractType = SetType.getInstance(cql3Type.getType());
                break;
            case LIST:
                cql3Type = CQL3Type.Native.valueOf(types.next().toUpperCase());
                abstractType = ListType.getInstance(cql3Type.getType());
                break;
            case MAP:
                cql3Type = CQL3Type.Native.valueOf(types.next().toUpperCase());
                CQL3Type cql3Type2 = CQL3Type.Native.valueOf(types.next().toUpperCase());
                abstractType = MapType.getInstance(cql3Type.getType(), cql3Type2.getType());
                break;
            default:
                throw new GenericException("Cannot determine collection kind for " + validatorKind);

            }
        }

    } catch (ClassNotFoundException e) {
        throw new GenericException(e);
    }

    return abstractType;
}

From source file:kina.entity.CellValidatorTest.java

License:Apache License

public void testDataTypeSetInstantiation() {
    DataType type = DataType.set(DataType.text());

    CellValidator cv = cellValidator(type);
    assertNotNull(cv);//from   w w  w. java 2s.  co  m
    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));
}