use of org.apache.gora.persistency.impl.DirtyListWrapper in project gora by apache.
the class HBaseStore method setField.
private void setField(Result result, T persistent, HBaseColumn col, Field field, Schema fieldSchema) throws IOException {
switch(fieldSchema.getType()) {
case UNION:
int index = getResolvedUnionIndex(fieldSchema);
if (index > 1) {
// if more than 2 type in union, deserialize directly for now
byte[] val = result.getValue(col.getFamily(), col.getQualifier());
if (val == null) {
return;
}
setField(persistent, field, val);
} else {
Schema resolvedSchema = fieldSchema.getTypes().get(index);
setField(result, persistent, col, field, resolvedSchema);
}
break;
case MAP:
if (Objects.nonNull(col.getQualifier())) {
byte[] val = result.getValue(col.getFamily(), col.getQualifier());
if (val == null) {
return;
}
setField(persistent, field, val);
} else {
NavigableMap<byte[], byte[]> qualMap = result.getNoVersionMap().get(col.getFamily());
if (qualMap == null) {
return;
}
Schema valueSchema = fieldSchema.getValueType();
Map<Utf8, Object> map = new HashMap<>();
for (Entry<byte[], byte[]> e : qualMap.entrySet()) {
map.put(new Utf8(Bytes.toString(e.getKey())), fromBytes(valueSchema, e.getValue()));
}
setField(persistent, field, map);
}
break;
case ARRAY:
NavigableMap<byte[], byte[]> qualMap = result.getFamilyMap(col.getFamily());
if (qualMap == null) {
return;
}
Schema valueSchema = fieldSchema.getElementType();
ArrayList<Object> arrayList = new ArrayList<>();
DirtyListWrapper<Object> dirtyListWrapper = new DirtyListWrapper<>(arrayList);
for (Entry<byte[], byte[]> e : qualMap.entrySet()) {
dirtyListWrapper.add(fromBytes(valueSchema, e.getValue()));
}
setField(persistent, field, arrayList);
break;
default:
byte[] val = result.getValue(col.getFamily(), col.getQualifier());
if (val == null) {
return;
}
setField(persistent, field, val);
break;
}
}
use of org.apache.gora.persistency.impl.DirtyListWrapper in project gora by apache.
the class MongoStore method fromMongoList.
/* pp */
Object fromMongoList(final String docf, final Schema fieldSchema, final BSONDecorator easybson, final Field f) throws GoraException {
List<Document> list = easybson.getDBList(docf);
List<Object> rlist = new ArrayList<>();
if (list == null) {
return new DirtyListWrapper(rlist);
}
for (Object item : list) {
DocumentFieldType storeType = mapping.getDocumentFieldType(docf);
Object o = fromDocument(fieldSchema.getElementType(), storeType, f, "item", new BSONDecorator(new Document("item", item)));
rlist.add(o);
}
return new DirtyListWrapper<>(rlist);
}
use of org.apache.gora.persistency.impl.DirtyListWrapper in project gora by apache.
the class CassandraStore method addOrUpdateField.
/**
* Add a field to Cassandra according to its type.
* @param key the key of the row where the field should be added
* @param field the Avro field representing a datum
* @param schema the schema belonging to the particular Avro field
* @param value the field value
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private void addOrUpdateField(K key, Field field, Schema schema, Object value) {
Type type = schema.getType();
// checking if the value to be updated is used for saving union schema
if (!field.name().contains(CassandraStore.UNION_COL_SUFIX)) {
switch(type) {
case STRING:
case BOOLEAN:
case INT:
case LONG:
case BYTES:
case FLOAT:
case DOUBLE:
case FIXED:
this.cassandraClient.addColumn(key, field.name(), value);
break;
case RECORD:
if (value != null) {
if (value instanceof PersistentBase) {
PersistentBase persistentBase = (PersistentBase) value;
try {
byte[] byteValue = AvroSerializerUtil.serializer(persistentBase, schema);
this.cassandraClient.addColumn(key, field.name(), byteValue);
} catch (IOException e) {
LOG.warn(field.name() + " named record could not be serialized.");
}
} else {
LOG.warn("Record with value: " + value.toString() + " not supported for field: " + field.name());
}
} else {
LOG.warn("Setting content of: " + field.name() + " to null.");
String familyName = this.cassandraClient.getCassandraMapping().getFamily(field.name());
this.cassandraClient.deleteColumn(key, familyName, this.cassandraClient.toByteBuffer(field.name()));
}
break;
case MAP:
if (value != null) {
if (value instanceof Map<?, ?>) {
Map<CharSequence, Object> map = (Map<CharSequence, Object>) value;
Schema valueSchema = schema.getValueType();
Type valueType = valueSchema.getType();
if (Type.UNION.equals(valueType)) {
Map<CharSequence, Object> valueMap = new HashMap<>();
for (CharSequence mapKey : map.keySet()) {
Object mapValue = map.get(mapKey);
int valueUnionIndex = getUnionSchema(mapValue, valueSchema);
valueMap.put((mapKey + UNION_COL_SUFIX), valueUnionIndex);
valueMap.put(mapKey, mapValue);
}
map = valueMap;
}
String familyName = this.cassandraClient.getCassandraMapping().getFamily(field.name());
// If map is not super column. We using Avro serializer.
if (!this.cassandraClient.isSuper(familyName)) {
try {
byte[] byteValue = AvroSerializerUtil.serializer(map, schema);
this.cassandraClient.addColumn(key, field.name(), byteValue);
} catch (IOException e) {
LOG.warn(field.name() + " named map could not be serialized.");
}
} else {
this.cassandraClient.addStatefulHashMap(key, field.name(), map);
}
} else {
LOG.warn("Map with value: " + value.toString() + " not supported for field: " + field.name());
}
} else {
// delete map
LOG.warn("Setting content of: " + field.name() + " to null.");
this.cassandraClient.deleteStatefulHashMap(key, field.name());
}
break;
case ARRAY:
if (value != null) {
if (value instanceof DirtyListWrapper<?>) {
DirtyListWrapper fieldValue = (DirtyListWrapper<?>) value;
GenericArray valueArray = new Array(fieldValue.size(), schema);
for (int i = 0; i < fieldValue.size(); i++) {
valueArray.add(i, fieldValue.get(i));
}
this.cassandraClient.addGenericArray(key, field.name(), (GenericArray<?>) valueArray);
} else {
LOG.warn("Array with value: " + value.toString() + " not supported for field: " + field.name());
}
} else {
LOG.warn("Setting content of: " + field.name() + " to null.");
this.cassandraClient.deleteGenericArray(key, field.name());
}
break;
case UNION:
// adding union schema index
String columnName = field.name() + UNION_COL_SUFIX;
String familyName = this.cassandraClient.getCassandraMapping().getFamily(field.name());
if (value != null) {
int schemaPos = getUnionSchema(value, schema);
LOG.debug("Union with value: " + value.toString() + " at index: " + schemaPos + " supported for field: " + field.name());
this.cassandraClient.getCassandraMapping().addColumn(familyName, columnName, columnName);
if (this.cassandraClient.isSuper(familyName)) {
this.cassandraClient.addSubColumn(key, columnName, columnName, schemaPos);
} else {
this.cassandraClient.addColumn(key, columnName, schemaPos);
}
//this.cassandraClient.getCassandraMapping().addColumn(familyName, columnName, columnName);
// adding union value
Schema unionSchema = schema.getTypes().get(schemaPos);
addOrUpdateField(key, field, unionSchema, value);
//this.cassandraClient.addColumn(key, field.name(), value);
} else {
LOG.warn("Setting content of: " + field.name() + " to null.");
if (this.cassandraClient.isSuper(familyName)) {
this.cassandraClient.deleteSubColumn(key, field.name());
} else {
this.cassandraClient.deleteColumn(key, familyName, this.cassandraClient.toByteBuffer(field.name()));
}
}
break;
default:
LOG.warn("Type: " + type.name() + " not considered for field: " + field.name() + ". Please report this to dev@gora.apache.org");
}
}
}
use of org.apache.gora.persistency.impl.DirtyListWrapper in project gora by apache.
the class AvroCassandraUtils method getAvroFieldValue.
static Object getAvroFieldValue(Object value, Schema schema) {
Object result;
switch(schema.getType()) {
case MAP:
Map<String, Object> rawMap = (Map<String, Object>) value;
Map<Utf8, Object> utf8ObjectHashMap = new HashMap<>();
if (rawMap == null) {
result = new DirtyMapWrapper(utf8ObjectHashMap);
break;
}
for (Map.Entry<?, ?> e : rawMap.entrySet()) {
Schema innerSchema = schema.getValueType();
Object obj = getAvroFieldValue(e.getValue(), innerSchema);
if (e.getKey().getClass().getSimpleName().equalsIgnoreCase("Utf8")) {
utf8ObjectHashMap.put((Utf8) e.getKey(), obj);
} else {
utf8ObjectHashMap.put(new Utf8((String) e.getKey()), obj);
}
}
result = new DirtyMapWrapper<>(utf8ObjectHashMap);
break;
case ARRAY:
List<Object> rawList = (List<Object>) value;
List<Object> objectArrayList = new ArrayList<>();
if (rawList == null) {
return new DirtyListWrapper(objectArrayList);
}
for (Object item : rawList) {
Object obj = getAvroFieldValue(item, schema.getElementType());
objectArrayList.add(obj);
}
result = new DirtyListWrapper<>(objectArrayList);
break;
case RECORD:
if (value != null && ByteBuffer.class.isAssignableFrom(value.getClass())) {
ByteBuffer buffer = (ByteBuffer) value;
byte[] arr = new byte[buffer.remaining()];
buffer.get(arr);
try {
result = (PersistentBase) HBaseByteInterface.fromBytes(schema, arr);
} catch (IOException e) {
LOG.error("Error occurred while deserialize the Record. :" + e.getMessage());
result = null;
}
} else {
result = (PersistentBase) value;
}
break;
case UNION:
int index = getUnionSchema(value, schema);
Schema resolvedSchema = schema.getTypes().get(index);
result = getAvroFieldValue(value, resolvedSchema);
break;
case ENUM:
result = org.apache.gora.util.AvroUtils.getEnumValue(schema, (String) value);
break;
case BYTES:
if (ByteBuffer.class.isAssignableFrom(value.getClass())) {
result = value;
} else {
result = ByteBuffer.wrap((byte[]) value);
}
break;
case STRING:
if (value instanceof org.apache.avro.util.Utf8) {
result = value;
} else if (ByteBuffer.class.isAssignableFrom(value.getClass())) {
result = new Utf8(((ByteBuffer) value).array());
} else {
result = new Utf8((String) value);
}
break;
case INT:
if (ByteBuffer.class.isAssignableFrom(value.getClass())) {
result = ((ByteBuffer) value).getInt();
} else {
result = value;
}
break;
case FLOAT:
if (ByteBuffer.class.isAssignableFrom(value.getClass())) {
result = ((ByteBuffer) value).getFloat();
} else {
result = value;
}
break;
case DOUBLE:
if (ByteBuffer.class.isAssignableFrom(value.getClass())) {
result = ((ByteBuffer) value).getDouble();
} else {
result = value;
}
break;
case LONG:
if (ByteBuffer.class.isAssignableFrom(value.getClass())) {
result = ((ByteBuffer) value).getLong();
} else {
result = value;
}
break;
default:
result = value;
}
return result;
}
use of org.apache.gora.persistency.impl.DirtyListWrapper in project gora by apache.
the class OrientDBStore method convertDocFieldToAvroList.
private Object convertDocFieldToAvroList(final String docf, final Schema fieldSchema, final ODocument doc, final Schema.Field f, final OrientDBMapping.DocumentFieldType storeType) throws GoraException {
if (storeType == OrientDBMapping.DocumentFieldType.EMBEDDEDSET) {
OTrackedSet<Object> set = doc.field(docf);
List<Object> rlist = new ArrayList<>();
if (set == null) {
return new DirtyListWrapper(rlist);
}
for (Object item : set) {
Object o = convertDocFieldToAvroField(fieldSchema.getElementType(), storeType, f, "item", new ODocument("item", item));
rlist.add(o);
}
return new DirtyListWrapper<>(rlist);
} else {
OTrackedList<Object> list = doc.field(docf);
List<Object> rlist = new ArrayList<>();
if (list == null) {
return new DirtyListWrapper(rlist);
}
for (Object item : list) {
Object o = convertDocFieldToAvroField(fieldSchema.getElementType(), storeType, f, "item", new ODocument("item", item));
rlist.add(o);
}
return new DirtyListWrapper<>(rlist);
}
}
Aggregations