List of usage examples for org.apache.cassandra.db.marshal UserType type
public AbstractType<?> type(int i)
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 . ja v a2 s. com*/ 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.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); }// w w w. jav a 2 s. co m 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); } }