Search in sources :

Example 11 with MapObject

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;
}
Also used : Utf8(org.apache.avro.util.Utf8) MapObject(com.rethinkdb.model.MapObject) MapObject(com.rethinkdb.model.MapObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with MapObject

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;
        }
    }
}
Also used : RethinkDBQuery(org.apache.gora.rethinkdb.query.RethinkDBQuery) MapObject(com.rethinkdb.model.MapObject) MapObject(com.rethinkdb.model.MapObject) ReqlExpr(com.rethinkdb.gen.ast.ReqlExpr)

Example 13 with MapObject

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;
    }
}
Also used : HashMap(java.util.HashMap) MapObject(com.rethinkdb.model.MapObject) HashMap(java.util.HashMap) Map(java.util.Map) MapObject(com.rethinkdb.model.MapObject)

Example 14 with MapObject

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;
}
Also used : PersistentBase(org.apache.gora.persistency.impl.PersistentBase) Schema(org.apache.avro.Schema) MapObject(com.rethinkdb.model.MapObject) MapObject(com.rethinkdb.model.MapObject)

Aggregations

MapObject (com.rethinkdb.model.MapObject)14 Schema (org.apache.avro.Schema)5 GoraException (org.apache.gora.util.GoraException)5 IOException (java.io.IOException)4 Map (java.util.Map)4 HashMap (java.util.HashMap)3 ReqlExpr (com.rethinkdb.gen.ast.ReqlExpr)2 List (java.util.List)2 Utf8 (org.apache.avro.util.Utf8)2 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)2 RethinkDBQuery (org.apache.gora.rethinkdb.query.RethinkDBQuery)2 ReqlDriverCompileError (com.rethinkdb.gen.exc.ReqlDriverCompileError)1 Arguments (com.rethinkdb.model.Arguments)1 ReqlLambda (com.rethinkdb.model.ReqlLambda)1 LocalDateTime (java.time.LocalDateTime)1 OffsetDateTime (java.time.OffsetDateTime)1 ZoneId (java.time.ZoneId)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ArrayList (java.util.ArrayList)1