Example usage for org.apache.cassandra.db.marshal TupleType buildValue

List of usage examples for org.apache.cassandra.db.marshal TupleType buildValue

Introduction

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

Prototype

public static ByteBuffer buildValue(ByteBuffer[] components) 

Source Link

Usage

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

License:Apache License

/**
 * Serialize a cassandra typed object.// w  w  w. ja  v  a  2s.c  o m
 * 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.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;//from  w ww .java  2s  . c om
        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));
        }
        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.cluster.InternalCassandraClusterService.java

License:Apache License

public ByteBuffer serializeMapAsMap(final String ksName, final String cfName, final String fieldName,
        final Map<String, Object> source, final ObjectMapper mapper)
        throws SyntaxException, ConfigurationException {
    MapType mapType = getMapType(ksName, cfName, fieldName);
    MapSerializer serializer = mapType.getSerializer();
    List<ByteBuffer> buffers = serializer.serializeValues(source);
    return TupleType.buildValue(buffers.toArray(new ByteBuffer[buffers.size()]));
}

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

License:Apache License

public ByteBuffer serializeMapAsUDT(final String ksName, final String cfName, final String fieldName,
        final Map<String, Object> source, final Mapper mapper) throws SyntaxException, ConfigurationException {
    List<ByteBuffer> components = new ArrayList<ByteBuffer>();

    if (mapper instanceof GeoPointFieldMapper) {
        components.add(DoubleType.instance.decompose((Double) source.get("lat")));
        components.add(DoubleType.instance.decompose((Double) source.get("lon")));
    } else if (mapper instanceof ObjectMapper) {
        Pair<List<String>, List<String>> udtInfo = getUDTInfo(ksName, cfName + '_' + fieldName);
        if (udtInfo == null) {
            throw new ConfigurationException(
                    "User Defined Type not found:" + ksName + "." + cfName + '_' + fieldName);
        }// w  w  w  . j  ava 2s  . co m
        for (int i = 0; i < udtInfo.left.size(); i++) {
            String fname = udtInfo.left.get(i);
            AbstractType type = TypeParser.parse(udtInfo.right.get(i));
            Object field_value = source.get(fname);
            components.add(serialize(ksName, cfName, type, fieldName + '_' + fname, field_value,
                    ((ObjectMapper) mapper).getMapper(fname)));
        }
    }
    return TupleType.buildValue(components.toArray(new ByteBuffer[components.size()]));
}