use of com.rethinkdb.model.MapObject in project gora by apache.
the class RethinkDBStore method convertDocFieldToAvroField.
private Object convertDocFieldToAvroField(final Schema fieldSchema, final RethinkDBMapping.DocumentFieldType storeType, final Schema.Field field, final String docf, final MapObject<String, Object> obj) throws GoraException {
Object result = null;
switch(fieldSchema.getType()) {
case MAP:
result = convertDocFieldToAvroMap(docf, fieldSchema, obj, field, storeType);
break;
case ARRAY:
result = convertDocFieldToAvroList(docf, fieldSchema, obj, field, storeType);
break;
case RECORD:
MapObject<String, Object> record = (MapObject<String, Object>) decorateMapToODoc((Map<String, Object>) obj.get(docf));
if (record == null) {
result = null;
break;
}
result = convertAvroBeanToRethinkDBDoc(fieldSchema, record);
break;
case BOOLEAN:
result = Boolean.valueOf(obj.get(docf).toString());
break;
case DOUBLE:
result = Double.valueOf(obj.get(docf).toString());
break;
case FLOAT:
result = Float.valueOf(obj.get(docf).toString());
break;
case INT:
result = Integer.valueOf(obj.get(docf).toString());
break;
case LONG:
result = Long.valueOf(obj.get(docf).toString());
break;
case STRING:
result = new Utf8(obj.get(docf).toString());
;
break;
case ENUM:
result = AvroUtils.getEnumValue(fieldSchema, obj.get(docf).toString());
break;
case BYTES:
case FIXED:
if (!obj.containsKey(docf)) {
result = null;
break;
}
result = ByteBuffer.wrap(Base64.getDecoder().decode(obj.get(docf).toString()));
break;
case NULL:
result = null;
break;
case UNION:
result = convertDocFieldToAvroUnion(fieldSchema, storeType, field, docf, obj);
break;
default:
LOG.warn("Unable to read {}", docf);
break;
}
return result;
}
use of com.rethinkdb.model.MapObject in project gora by apache.
the class RethinkDBStore method deleteByQuery.
/**
* {@inheritDoc}
*/
@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
if (query.getFields() == null || (query.getFields().length == getFields().length)) {
String[] fields = getFieldsToQuery(query.getFields());
RethinkDBQuery dataStoreQuery = ((RethinkDBQuery) query);
dataStoreQuery.populateRethinkDBQuery(rethinkDBMapping, rethinkDBStoreParameters, fields, getFields());
ReqlExpr reqlExpr = dataStoreQuery.getRethinkDBDbQuery();
MapObject<String, Object> document = reqlExpr.delete().run(connection, MapObject.class).first();
int deleteCount = Integer.valueOf(document.get("deleted").toString());
if (deleteCount > 0) {
return deleteCount;
} else {
return 0;
}
} else {
RethinkDBQuery<K, T> dataStoreQuery = new RethinkDBQuery<>(this);
dataStoreQuery.setStartKey(query.getStartKey());
dataStoreQuery.setEndKey(query.getEndKey());
dataStoreQuery.populateRethinkDBQuery(rethinkDBMapping, rethinkDBStoreParameters, getFieldsToQuery(null), getFields());
ReqlExpr reqlExpr = dataStoreQuery.getRethinkDBDbQuery();
String[] projection = new String[query.getFields().length];
int counter = 0;
for (String k : query.getFields()) {
String dbFieldName = rethinkDBMapping.getDocumentField(k);
if (dbFieldName != null && dbFieldName.length() > 0) {
projection[counter] = dbFieldName;
counter++;
}
}
MapObject<String, Object> document = reqlExpr.replace(row -> row.without(projection)).run(connection, MapObject.class).first();
int replacedCount = Integer.valueOf(document.get("replaced").toString());
if (replacedCount > 0) {
return replacedCount;
} else {
return 0;
}
}
}
use of com.rethinkdb.model.MapObject in project gora by apache.
the class RethinkDBStore method convertAvroMapToDocField.
private Object convertAvroMapToDocField(final String docf, final Map<CharSequence, ?> value, final Schema fieldSchema, final Schema.Type fieldType, final RethinkDBMapping.DocumentFieldType storeType) {
if (storeType == RethinkDBMapping.DocumentFieldType.MAP) {
HashMap map = new HashMap<String, Object>();
if (value == null)
return map;
for (Map.Entry<CharSequence, ?> e : value.entrySet()) {
String mapKey = e.getKey().toString();
Object mapValue = e.getValue();
RethinkDBMapping.DocumentFieldType fieldStoreType = rethinkDBMapping.getDocumentFieldType(docf);
Object result = convertAvroFieldToRethinkDBField(docf, fieldSchema, fieldType, fieldStoreType, mapValue);
map.put(mapKey, result);
}
return map;
} else {
MapObject<String, Object> doc = new MapObject<String, Object>();
if (value == null)
return doc;
for (Map.Entry<CharSequence, ?> e : value.entrySet()) {
String mapKey = e.getKey().toString();
Object mapValue = e.getValue();
RethinkDBMapping.DocumentFieldType fieldStoreType = rethinkDBMapping.getDocumentFieldType(docf);
Object result = convertAvroFieldToRethinkDBField(docf, fieldSchema, fieldType, fieldStoreType, mapValue);
doc.put(mapKey, result);
}
return doc;
}
}
use of com.rethinkdb.model.MapObject in project gora by apache.
the class RethinkDBStore method convertAvroBeanToRethinkDBDocField.
private MapObject<String, Object> convertAvroBeanToRethinkDBDocField(final String docf, final Schema fieldSchema, final Object value) {
MapObject<String, Object> record = new MapObject();
for (Schema.Field member : fieldSchema.getFields()) {
Object innerValue = ((PersistentBase) value).get(member.pos());
String innerDoc = rethinkDBMapping.getDocumentField(member.name());
Schema.Type innerType = member.schema().getType();
RethinkDBMapping.DocumentFieldType innerStoreType = rethinkDBMapping.getDocumentFieldType(innerDoc);
LOG.debug("Transform value to BaseDocument , docField:{}, schemaType:{}, storeType:{}", new Object[] { member.name(), member.schema().getType(), innerStoreType });
Object fieldValue = convertAvroFieldToRethinkDBField(docf, member.schema(), innerType, innerStoreType, innerValue);
record.put(member.name(), fieldValue);
}
return record;
}
Aggregations