Example usage for org.apache.cassandra.db.marshal UserType fieldName

List of usage examples for org.apache.cassandra.db.marshal UserType fieldName

Introduction

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

Prototype

public FieldIdentifier fieldName(int i) 

Source Link

Usage

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

/**
 * Serialize a cassandra typed object.// www .  jav a2s . c  om
 * List of list converted to List, see https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html
 * @param ksName
 * @param cfName
 * @param type
 * @param name
 * @param value
 * @param mapper
 * @return
 * @throws SyntaxException
 * @throws ConfigurationException
 * @throws IOException 
 * @throws JsonMappingException 
 * @throws JsonGenerationException 
 */
public static ByteBuffer serializeType(final String ksName, final String cfName, final AbstractType type,
        final String name, final Object value, final Mapper mapper) throws SyntaxException,
        ConfigurationException, JsonGenerationException, JsonMappingException, IOException {
    if (value == null) {
        return null;
    }
    if (type instanceof UserType) {
        UserType udt = (UserType) type;
        ByteBuffer[] components = new ByteBuffer[udt.size()];
        int i = 0;

        if (GEO_POINT_TYPE.equals(ByteBufferUtil.string(udt.name))) {
            GeoPoint geoPoint = new GeoPoint();
            if (value instanceof String) {
                // parse from string lat,lon (ex: "41.12,-71.34") or geohash (ex:"drm3btev3e86")
                geoPoint.resetFromString((String) value);
            } else {
                // parse from lat, lon fields as map
                Map<String, Object> mapValue = (Map<String, Object>) value;
                geoPoint.reset((Double) mapValue.get(GeoPointFieldMapper.Names.LAT),
                        (Double) mapValue.get(GeoPointFieldMapper.Names.LON));
            }
            components[i++] = serializeType(ksName, cfName, udt.fieldType(0), GeoPointFieldMapper.Names.LAT,
                    geoPoint.lat(), null);
            components[i++] = serializeType(ksName, cfName, udt.fieldType(1), GeoPointFieldMapper.Names.LON,
                    geoPoint.lon(), null);
        } else if (COMPLETION_TYPE.equals(ByteBufferUtil.string(udt.name))) {
            // input list<text>, output text, weight int, payload text
            Map<String, Object> mapValue = (Map<String, Object>) value;
            components[i++] = serializeType(ksName, cfName, udt.fieldType(0), Fields.CONTENT_FIELD_NAME_INPUT,
                    mapValue.get(Fields.CONTENT_FIELD_NAME_INPUT), null);
            components[i++] = serializeType(ksName, cfName, udt.fieldType(1), Fields.CONTENT_FIELD_NAME_OUTPUT,
                    mapValue.get(Fields.CONTENT_FIELD_NAME_OUTPUT), null);
            components[i++] = serializeType(ksName, cfName, udt.fieldType(2), Fields.CONTENT_FIELD_NAME_WEIGHT,
                    new Long((Integer) mapValue.get(Fields.CONTENT_FIELD_NAME_WEIGHT)), null);
            components[i++] = serializeType(ksName, cfName, udt.fieldType(3), Fields.CONTENT_FIELD_NAME_PAYLOAD,
                    stringify(mapValue.get(Fields.CONTENT_FIELD_NAME_PAYLOAD)), null);
        } else {
            Map<String, Object> mapValue = (Map<String, Object>) value;
            for (int j = 0; j < udt.size(); j++) {
                String subName = UTF8Type.instance.compose(udt.fieldName(j));
                AbstractType<?> subType = udt.fieldType(j);
                Object subValue = mapValue.get(subName);
                Mapper subMapper = (mapper instanceof ObjectMapper) ? ((ObjectMapper) mapper).getMapper(subName)
                        : null;
                components[i++] = serializeType(ksName, cfName, subType, subName, subValue, subMapper);
            }
        }
        return TupleType.buildValue(components);
    } else if (type instanceof MapType) {
        MapType mapType = InternalCassandraClusterService.getMapType(ksName, cfName, name);
        MapSerializer serializer = mapType.getSerializer();
        Map map = (Map) value;
        List<ByteBuffer> buffers = serializer.serializeValues((Map) value);
        return CollectionSerializer.pack(buffers, map.size(), Server.VERSION_3);
    } else if (type instanceof CollectionType) {
        AbstractType elementType = (type instanceof ListType) ? ((ListType) type).getElementsType()
                : ((SetType) type).getElementsType();

        if (elementType instanceof UserType
                && InternalCassandraClusterService.GEO_POINT_TYPE
                        .equals(ByteBufferUtil.string(((UserType) elementType).name))
                && value instanceof List && ((List) value).get(0) instanceof Double) {
            // geo_point as array of double lon,lat like [1.2, 1.3]
            UserType udt = (UserType) elementType;
            List<Double> values = (List<Double>) value;
            ByteBuffer[] elements = new ByteBuffer[] {
                    serializeType(ksName, cfName, udt.fieldType(0), GeoPointFieldMapper.Names.LAT,
                            values.get(1), null),
                    serializeType(ksName, cfName, udt.fieldType(1), GeoPointFieldMapper.Names.LON,
                            values.get(0), null) };
            ByteBuffer geo_point = TupleType.buildValue(elements);
            return CollectionSerializer.pack(ImmutableList.of(geo_point), 1, Server.VERSION_3);
        }

        if (value instanceof Collection) {
            // list of elementType
            List<ByteBuffer> elements = new ArrayList<ByteBuffer>();
            for (Object v : flattenCollection((Collection) value)) {
                ByteBuffer bb = serializeType(ksName, cfName, elementType, name, v, mapper);
                elements.add(bb);
            }
            return CollectionSerializer.pack(elements, elements.size(), Server.VERSION_3);
        } else {
            // singleton list
            ByteBuffer bb = serializeType(ksName, cfName, elementType, name, value, mapper);
            return CollectionSerializer.pack(ImmutableList.of(bb), 1, Server.VERSION_3);
        }
    } else {
        // Native cassandra type, encoded with mapper if available.
        if (mapper != null && mapper instanceof FieldMapper) {
            return type.decompose(value((FieldMapper) mapper, value));
        }
        return type.decompose(value);
    }
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

public static Object deserialize(AbstractType<?> type, ByteBuffer bb, Mapper mapper)
        throws CharacterCodingException {
    if (type instanceof UserType) {
        UserType udt = (UserType) type;
        Map<String, Object> mapValue = new HashMap<String, Object>();
        ByteBuffer[] components = udt.split(bb);

        if (GEO_POINT_TYPE.equals(ByteBufferUtil.string(udt.name))) {
            if (components[0] != null)
                mapValue.put(GeoPointFieldMapper.Names.LAT, deserialize(udt.type(0), components[0], null));
            if (components[1] != null)
                mapValue.put(GeoPointFieldMapper.Names.LON, deserialize(udt.type(1), components[1], null));
        } else {/*w  ww .j  ava 2 s . c o m*/
            for (int i = 0; i < components.length; i++) {
                String fieldName = UTF8Type.instance.compose(udt.fieldName(i));
                AbstractType<?> ctype = udt.type(i);
                Mapper subMapper = null;
                if (mapper != null && mapper instanceof ObjectMapper)
                    subMapper = ((ObjectMapper) mapper).getMapper(fieldName);
                Object value = (components[i] == null) ? null : deserialize(ctype, components[i], subMapper);
                mapValue.put(fieldName, value);
            }
        }
        return mapValue;
    } else if (type instanceof ListType) {
        ListType<?> ltype = (ListType<?>) type;
        ByteBuffer input = bb.duplicate();
        int size = CollectionSerializer.readCollectionSize(input, Server.VERSION_3);
        List list = new ArrayList(size);
        for (int i = 0; i < size; i++) {
            list.add(deserialize(ltype.getElementsType(),
                    CollectionSerializer.readValue(input, Server.VERSION_3), mapper));
        }
        if (input.hasRemaining())
            throw new MarshalException("Unexpected extraneous bytes after map value");
        return list;
    } else if (type instanceof SetType) {
        SetType<?> ltype = (SetType<?>) type;
        ByteBuffer input = bb.duplicate();
        int size = CollectionSerializer.readCollectionSize(input, Server.VERSION_3);
        Set set = new HashSet(size);
        for (int i = 0; i < size; i++) {
            set.add(deserialize(ltype.getElementsType(),
                    CollectionSerializer.readValue(input, Server.VERSION_3), mapper));
        }
        if (input.hasRemaining())
            throw new MarshalException("Unexpected extraneous bytes after map value");
        return set;
    } else if (type instanceof MapType) {
        MapType<?, ?> ltype = (MapType<?, ?>) type;
        ByteBuffer input = bb.duplicate();
        int size = CollectionSerializer.readCollectionSize(input, Server.VERSION_3);
        Map map = new LinkedHashMap(size);
        for (int i = 0; i < size; i++) {
            ByteBuffer kbb = CollectionSerializer.readValue(input, Server.VERSION_3);
            ByteBuffer vbb = CollectionSerializer.readValue(input, Server.VERSION_3);
            String key = (String) ltype.getKeysType().compose(kbb);
            Mapper subMapper = null;
            if (mapper != null) {
                assert mapper instanceof ObjectMapper : "Expecting an object mapper for MapType";
                subMapper = ((ObjectMapper) mapper).getMapper(key);
            }
            map.put(key, deserialize(ltype.getValuesType(), vbb, subMapper));
        }
        if (input.hasRemaining())
            throw new MarshalException("Unexpected extraneous bytes after map value");
        return map;
    } else {
        Object value = type.compose(bb);
        if (mapper != null && mapper instanceof FieldMapper) {
            return ((FieldMapper) mapper).fieldType().valueForSearch(value);
        }
        return value;
    }
}

From source file:org.elasticsearch.cassandra.cluster.InternalCassandraClusterService.java

License:Apache License

public ByteBuffer serialize(final String ksName, final String cfName, final AbstractType type,
        final String name, final Object value, final Mapper mapper)
        throws SyntaxException, ConfigurationException {
    if (value == null)
        return null;

    if (type instanceof UserType) {
        List<ByteBuffer> components = new ArrayList<ByteBuffer>();
        UserType udt = (UserType) type;
        Map<String, Object> mapValue = (Map<String, Object>) value;
        for (int j = 0; j < udt.size(); j++) {
            String subName = UTF8Type.instance.compose(udt.fieldName(j));
            AbstractType<?> subType = udt.fieldType(j);
            Mapper subMapper = ((ObjectMapper) mapper).getMapper(subName);
            Object subValue = mapValue.get(subName);
            components.add(serialize(ksName, cfName, subType, subName, subValue, subMapper));
        }//w w w  . j  a  va2 s  .c o m
        return TupleType.buildValue(components.toArray(new ByteBuffer[components.size()]));
    } else if (type instanceof ListType) {
        if (!(value instanceof List)) {
            if (value instanceof Map) {
                // build a singleton list of UDT 
                return type.decompose(ImmutableList
                        .of(serializeMapAsUDT(ksName, cfName, name, (Map<String, Object>) value, mapper)));
            } else {
                return type.decompose(ImmutableList.of(value((FieldMapper) mapper, value)));
            }
        } else {
            return type.decompose(value);
        }
    } else {
        // Native cassandra type,
        return type.decompose(value((FieldMapper) mapper, value));
    }
}

From source file:org.elasticsearch.cassandra.ElasticSecondaryIndex.java

License:Apache License

public static Object deserialize(AbstractType<?> type, ByteBuffer bb) {
    if (type instanceof UserType) {
        UserType utype = (UserType) type;
        Map<String, Object> mapValue = new HashMap<String, Object>();
        ByteBuffer[] components = utype.split(bb);
        for (int i = 0; i < components.length; i++) {
            String fieldName = UTF8Type.instance.compose(utype.fieldName(i));
            AbstractType<?> ctype = utype.type(i);
            Object value = (components[i] == null) ? null : deserialize(ctype, components[i]);
            mapValue.put(fieldName, value);
        }/*ww w. ja va2s  . c om*/
        return mapValue;
    } else if (type instanceof ListType) {
        ListType ltype = (ListType) type;
        ByteBuffer input = bb.duplicate();
        int size = CollectionSerializer.readCollectionSize(input, Server.VERSION_3);
        List list = new ArrayList(size);
        for (int i = 0; i < size; i++) {
            list.add(deserialize(ltype.getElementsType(),
                    CollectionSerializer.readValue(input, Server.VERSION_3)));
        }
        if (input.hasRemaining())
            throw new MarshalException("Unexpected extraneous bytes after map value");
        return list;
    } else if (type instanceof SetType) {
        SetType ltype = (SetType) type;
        ByteBuffer input = bb.duplicate();
        int size = CollectionSerializer.readCollectionSize(input, Server.VERSION_3);
        Set set = new HashSet(size);
        for (int i = 0; i < size; i++) {
            set.add(deserialize(ltype.getElementsType(),
                    CollectionSerializer.readValue(input, Server.VERSION_3)));
        }
        if (input.hasRemaining())
            throw new MarshalException("Unexpected extraneous bytes after map value");
        return set;
    } else if (type instanceof MapType) {
        MapType ltype = (MapType) type;
        ByteBuffer input = bb.duplicate();
        int size = CollectionSerializer.readCollectionSize(input, Server.VERSION_3);
        Map map = new LinkedHashMap(size);
        for (int i = 0; i < size; i++) {
            ByteBuffer kbb = CollectionSerializer.readValue(input, Server.VERSION_3);
            ByteBuffer vbb = CollectionSerializer.readValue(input, Server.VERSION_3);
            String key = (String) ltype.getKeysType().compose(kbb);
            map.put(key, deserialize(ltype.getValuesType(), vbb));
        }
        if (input.hasRemaining())
            throw new MarshalException("Unexpected extraneous bytes after map value");
        return map;
    } else {
        return type.compose(bb);
    }
}