List of usage examples for org.apache.cassandra.db.marshal ListType getElementsType
public AbstractType<T> getElementsType()
From source file:com.impetus.client.cassandra.datahandler.CassandraDataHandlerBase.java
License:Apache License
/** * Sets the element collection list.//from ww w .ja v a 2s.c om * * @param listType * the cql column metadata * @param thriftColumnValue * the thrift column value * @param entity * the entity * @param field * the field * @param metaModel * the meta model * @param embeddedObject * the embedded object * @return the object */ private Object setElementCollectionList(ListType listType, ByteBuffer thriftColumnValue, Object entity, Field field, MetamodelImpl metaModel, Class embeddedClass, boolean useNativeProtocol2) { ListSerializer listSerializer = listType.getSerializer(); Collection outputCollection = new ArrayList(); if (useNativeProtocol2) { outputCollection.addAll((Collection) listSerializer.deserializeForNativeProtocol(thriftColumnValue, 2)); } else { outputCollection.addAll((Collection) listSerializer.deserialize(thriftColumnValue)); } UserType usertype = (UserType) listType.getElementsType(); Collection result = new ArrayList(); Iterator collectionItems = outputCollection.iterator(); while (collectionItems.hasNext()) { Object embeddedObject = KunderaCoreUtils.createNewInstance(embeddedClass); Object value = populateEmbeddedRecursive((ByteBuffer) collectionItems.next(), usertype.allTypes(), usertype.fieldNames(), embeddedObject, metaModel); result.add(value); } PropertyAccessorHelper.set(entity, field, result); return entity; }
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;/*from w ww . j a v a 2s.c o m*/ 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 { 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); }/*from w w w . ja va 2 s.c o 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); } }