use of com.rethinkdb.model.MapObject in project gora by apache.
the class RethinkDBStore method convertRethinkDBDocToAvroBean.
public T convertRethinkDBDocToAvroBean(final MapObject<String, Object> obj, final String[] fields) throws GoraException {
T persistent = newPersistent();
String[] dbFields = getFieldsToQuery(fields);
for (String f : dbFields) {
String docf = rethinkDBMapping.getDocumentField(f);
if (docf == null || !obj.containsKey(docf))
continue;
RethinkDBMapping.DocumentFieldType storeType = rethinkDBMapping.getDocumentFieldType(docf);
Schema.Field field = fieldMap.get(f);
Schema fieldSchema = field.schema();
LOG.debug("Load from ODocument, field:{}, schemaType:{}, docField:{}, storeType:{}", new Object[] { field.name(), fieldSchema.getType(), docf, storeType });
Object result = convertDocFieldToAvroField(fieldSchema, storeType, field, docf, obj);
persistent.put(field.pos(), result);
}
persistent.clearDirty();
return persistent;
}
use of com.rethinkdb.model.MapObject in project gora by apache.
the class RethinkDBStore method convertAvroBeanToRethinkDBDocument.
private MapObject<String, Object> convertAvroBeanToRethinkDBDocument(final K key, final T persistent) {
MapObject<String, Object> result = new MapObject();
for (Schema.Field f : persistent.getSchema().getFields()) {
if (persistent.isDirty(f.pos()) && (persistent.get(f.pos()) != null)) {
String docf = rethinkDBMapping.getDocumentField(f.name());
Object value = persistent.get(f.pos());
RethinkDBMapping.DocumentFieldType storeType = rethinkDBMapping.getDocumentFieldType(docf);
LOG.debug("Transform value to ODocument, docField:{}, schemaType:{}, storeType:{}", new Object[] { docf, f.schema().getType(), storeType });
Object o = convertAvroFieldToRethinkDBField(docf, f.schema(), f.schema().getType(), storeType, value);
result.put(docf, o);
}
}
result.put("id", key.toString());
return result;
}
use of com.rethinkdb.model.MapObject in project gora by apache.
the class RethinkDBStore method get.
/**
* {@inheritDoc}
*/
@Override
public T get(K key, String[] fields) throws GoraException {
try {
boolean isExists = r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).getAll(key).count().run(connection, Boolean.class).first();
if (isExists) {
String[] dbFields = getFieldsToQuery(fields);
MapObject<String, Object> document = r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).get(key).run(connection, MapObject.class).first();
return convertRethinkDBDocToAvroBean(document, dbFields);
} else {
return null;
}
} catch (Exception e) {
throw new GoraException(e);
}
}
use of com.rethinkdb.model.MapObject in project gora by apache.
the class RethinkDBStore method convertDocFieldToAvroUnion.
private Object convertDocFieldToAvroUnion(final Schema fieldSchema, final RethinkDBMapping.DocumentFieldType storeType, final Schema.Field field, final String docf, final MapObject<String, Object> doc) throws GoraException {
Object result;
Schema.Type type0 = fieldSchema.getTypes().get(0).getType();
Schema.Type type1 = fieldSchema.getTypes().get(1).getType();
if (!type0.equals(type1) && (type0.equals(Schema.Type.NULL) || type1.equals(Schema.Type.NULL))) {
Schema innerSchema = null;
if (type0.equals(Schema.Type.NULL)) {
innerSchema = fieldSchema.getTypes().get(1);
} else {
innerSchema = fieldSchema.getTypes().get(0);
}
LOG.debug("Load from ODocument (UNION), schemaType:{}, docField:{}, storeType:{}", new Object[] { innerSchema.getType(), docf, storeType });
result = convertDocFieldToAvroField(innerSchema, storeType, field, docf, doc);
} else {
throw new GoraException("RethinkDBStore only supports Union of two types field.");
}
return result;
}
use of com.rethinkdb.model.MapObject in project gora by apache.
the class RethinkDBStore method put.
/**
* {@inheritDoc}
*/
@Override
public void put(K key, T val) throws GoraException {
if (val.isDirty()) {
try {
boolean isExists = r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).getAll(key).count().run(connection, Boolean.class).first();
if (!isExists) {
MapObject<String, Object> document = convertAvroBeanToRethinkDBDocument(key, val);
r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).insert(document).run(connection);
} else {
MapObject<String, Object> document = convertAvroBeanToRethinkDBDocument(key, val);
r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).get(key).replace(document).run(connection);
}
} catch (Exception e) {
throw new GoraException(e);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.info("Ignored putting persistent bean {} in the store as it is neither " + "new, neither dirty.", new Object[] { val });
}
}
}
Aggregations