Search in sources :

Example 1 with MapObject

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

Example 2 with MapObject

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

Example 3 with MapObject

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);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) MapObject(com.rethinkdb.model.MapObject) MapObject(com.rethinkdb.model.MapObject) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 4 with MapObject

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

Example 5 with MapObject

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 });
        }
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) MapObject(com.rethinkdb.model.MapObject) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

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