use of org.apache.gora.persistency.impl.PersistentBase in project gora by apache.
the class CassandraSuperColumn method getSuperValue.
private Object getSuperValue(Field field, Schema fieldSchema, Type type) {
Object value = null;
switch(type) {
case ARRAY:
List<Object> array = new ArrayList<>();
for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
Object memberValue = fromByteBuffer(fieldSchema.getElementType(), hColumn.getValue());
// int i = IntegerSerializer().get().fromByteBuffer(hColumn.getName());
array.add(memberValue);
}
value = array;
break;
case MAP:
Map<CharSequence, Object> map = new HashMap<>();
for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
CharSequence mapKey = CharSequenceSerializer.get().fromByteBuffer(hColumn.getName());
if (!mapKey.toString().contains(CassandraStore.UNION_COL_SUFIX)) {
Object memberValue = null;
// We need detect real type for UNION Fields
if (fieldSchema.getValueType().getType().equals(Type.UNION)) {
HColumn<ByteBuffer, ByteBuffer> cc = getUnionTypeColumn(mapKey + CassandraStore.UNION_COL_SUFIX, this.hSuperColumn.getColumns());
Integer unionIndex = getUnionIndex(cc);
Schema realSchema = fieldSchema.getValueType().getTypes().get(unionIndex);
memberValue = fromByteBuffer(realSchema, hColumn.getValue());
} else {
memberValue = fromByteBuffer(fieldSchema.getValueType(), hColumn.getValue());
}
map.put(mapKey, memberValue);
}
}
value = map;
break;
case RECORD:
String fullName = fieldSchema.getFullName();
Class<?> claz = null;
try {
claz = Class.forName(fullName);
} catch (ClassNotFoundException cnfe) {
LOG.warn("Unable to load class " + fullName, cnfe);
break;
}
try {
value = claz.newInstance();
} catch (InstantiationException ie) {
LOG.warn("Instantiation error", ie);
break;
} catch (IllegalAccessException iae) {
LOG.warn("Illegal access error", iae);
break;
}
// we updated the value instance, now update its members
if (value instanceof PersistentBase) {
PersistentBase record = (PersistentBase) value;
for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
String memberName = StringSerializer.get().fromByteBuffer(hColumn.getName());
if (memberName == null || memberName.length() == 0) {
LOG.warn("member name is null or empty.");
continue;
}
if (!memberName.contains(CassandraStore.UNION_COL_SUFIX)) {
Field memberField = fieldSchema.getField(memberName);
Schema memberSchema = memberField.schema();
Type memberType = memberSchema.getType();
CassandraSubColumn cassandraColumn = new CassandraSubColumn();
cassandraColumn.setField(memberField);
cassandraColumn.setValue(hColumn);
if (memberType.equals(Type.UNION)) {
HColumn<ByteBuffer, ByteBuffer> hc = getUnionTypeColumn(memberField.name() + CassandraStore.UNION_COL_SUFIX, this.hSuperColumn.getColumns().toArray());
Integer unionIndex = getUnionIndex(hc);
cassandraColumn.setUnionType(unionIndex);
}
record.put(record.getSchema().getField(memberName).pos(), cassandraColumn.getValue());
}
}
}
break;
case UNION:
int schemaPos = this.getUnionType();
Schema unioSchema = fieldSchema.getTypes().get(schemaPos);
Type unionType = unioSchema.getType();
value = getSuperValue(field, unioSchema, unionType);
break;
default:
Object memberValue = null;
// Using for UnionIndex of Union type field get value. UnionIndex always Integer.
for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
memberValue = fromByteBuffer(fieldSchema, hColumn.getValue());
}
value = memberValue;
LOG.warn("Type: " + type.name() + " not supported for field: " + field.name());
}
return value;
}
use of org.apache.gora.persistency.impl.PersistentBase in project gora by apache.
the class CouchDBStore method fromCouchDBRecord.
private Object fromCouchDBRecord(final Schema fieldSchema, final String docf, final Object value) {
final Object innerValue = ((Map) value).get(docf);
if (innerValue == null) {
return null;
}
Class<?> clazz = null;
try {
clazz = ClassLoadingUtils.loadClass(fieldSchema.getFullName());
} catch (ClassNotFoundException e) {
LOG.debug(e.getMessage());
}
final PersistentBase record = (PersistentBase) new BeanFactoryImpl(keyClass, clazz).newPersistent();
for (Field recField : fieldSchema.getFields()) {
Schema innerSchema = recField.schema();
record.put(recField.pos(), fromDBObject(innerSchema, recField, recField.name(), innerValue));
}
return record;
}
use of org.apache.gora.persistency.impl.PersistentBase in project gora by apache.
the class CouchDBStore method recordToCouchDB.
private Map<String, Object> recordToCouchDB(final Schema fieldSchema, final Object fieldValue) {
final PersistentBase persistent = (PersistentBase) fieldValue;
final Map<String, Object> newMap = new LinkedHashMap<>();
if (persistent != null) {
for (Field member : fieldSchema.getFields()) {
Schema memberSchema = member.schema();
Object memberValue = persistent.get(member.pos());
newMap.put(member.name(), toDBObject(memberSchema, memberValue));
}
return newMap;
}
return null;
}
use of org.apache.gora.persistency.impl.PersistentBase in project gora by apache.
the class MongoStore method fromMongoRecord.
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object fromMongoRecord(final Schema fieldSchema, final String docf, final DBObject rec) {
Object result;
BSONDecorator innerBson = new BSONDecorator(rec);
Class<?> clazz = null;
try {
clazz = ClassLoadingUtils.loadClass(fieldSchema.getFullName());
} catch (ClassNotFoundException e) {
}
PersistentBase record = (PersistentBase) new BeanFactoryImpl(keyClass, clazz).newPersistent();
for (Field recField : fieldSchema.getFields()) {
Schema innerSchema = recField.schema();
DocumentFieldType innerStoreType = mapping.getDocumentFieldType(innerSchema.getName());
String innerDocField = mapping.getDocumentField(recField.name()) != null ? mapping.getDocumentField(recField.name()) : recField.name();
String fieldPath = docf + "." + innerDocField;
LOG.debug("Load from DBObject (RECORD), field:{}, schemaType:{}, docField:{}, storeType:{}", new Object[] { recField.name(), innerSchema.getType(), fieldPath, innerStoreType });
record.put(recField.pos(), fromDBObject(innerSchema, innerStoreType, recField, innerDocField, innerBson));
}
result = record;
return result;
}
use of org.apache.gora.persistency.impl.PersistentBase in project gora by apache.
the class MongoStore method recordToMongo.
private BasicDBObject recordToMongo(final String docf, final Schema fieldSchema, final Object value) {
BasicDBObject record = new BasicDBObject();
for (Field member : fieldSchema.getFields()) {
Object innerValue = ((PersistentBase) value).get(member.pos());
String innerDoc = mapping.getDocumentField(member.name());
Type innerType = member.schema().getType();
DocumentFieldType innerStoreType = mapping.getDocumentFieldType(innerDoc);
LOG.debug("Transform value to DBObject (RECORD), docField:{}, schemaType:{}, storeType:{}", new Object[] { member.name(), member.schema().getType(), innerStoreType });
record.put(member.name(), toDBObject(docf, member.schema(), innerType, innerStoreType, innerValue));
}
return record;
}
Aggregations