Search in sources :

Example 6 with MapObject

use of com.rethinkdb.model.MapObject in project MantaroBot by Mantaro.

the class Util method toReqlAst.

@SuppressWarnings("unchecked")
private static ReqlAst toReqlAst(Object val, int remainingDepth) {
    if (remainingDepth <= 0) {
        throw new ReqlDriverCompileError("Recursion limit reached converting to ReqlAst");
    }
    if (val instanceof ReqlAst) {
        return (ReqlAst) val;
    }
    if (val instanceof Object[]) {
        Arguments innerValues = new Arguments();
        for (Object innerValue : Arrays.asList((Object[]) val)) {
            innerValues.add(toReqlAst(innerValue, remainingDepth - 1));
        }
        return new MakeArray(innerValues, null);
    }
    if (val instanceof List) {
        Arguments innerValues = new Arguments();
        for (Object innerValue : (List) val) {
            innerValues.add(toReqlAst(innerValue, remainingDepth - 1));
        }
        return new MakeArray(innerValues, null);
    }
    if (val instanceof Map) {
        Map<String, ReqlAst> obj = new MapObject<>();
        for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) val).entrySet()) {
            if (!(entry.getKey() instanceof String)) {
                throw new ReqlDriverCompileError("Object keys can only be strings");
            }
            obj.put((String) entry.getKey(), toReqlAst(entry.getValue()));
        }
        return MakeObj.fromMap(obj);
    }
    if (val instanceof ReqlLambda) {
        return Func.fromLambda((ReqlLambda) val);
    }
    final DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
    if (val instanceof LocalDateTime) {
        ZoneId zid = ZoneId.systemDefault();
        DateTimeFormatter fmt2 = fmt.withZone(zid);
        return Iso8601.fromString(((LocalDateTime) val).format(fmt2));
    }
    if (val instanceof ZonedDateTime) {
        return Iso8601.fromString(((ZonedDateTime) val).format(fmt));
    }
    if (val instanceof OffsetDateTime) {
        return Iso8601.fromString(((OffsetDateTime) val).format(fmt));
    }
    if (val instanceof Number || val instanceof Boolean || val instanceof String) {
        return new Datum(val);
    }
    if (val == null) {
        return new Datum(null);
    }
    // val is a non-null POJO, let's introspect its public properties
    return toReqlAst(toMap(val));
}
Also used : LocalDateTime(java.time.LocalDateTime) ReqlDriverCompileError(com.rethinkdb.gen.exc.ReqlDriverCompileError) ZoneId(java.time.ZoneId) Arguments(com.rethinkdb.model.Arguments) ReqlLambda(com.rethinkdb.model.ReqlLambda) ZonedDateTime(java.time.ZonedDateTime) OffsetDateTime(java.time.OffsetDateTime) MapObject(com.rethinkdb.model.MapObject) List(java.util.List) Map(java.util.Map) MapObject(com.rethinkdb.model.MapObject) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 7 with MapObject

use of com.rethinkdb.model.MapObject in project gora by apache.

the class RethinkDBStore method convertAvroBeanToRethinkDBDoc.

private Object convertAvroBeanToRethinkDBDoc(final Schema fieldSchema, final MapObject<String, Object> doc) throws GoraException {
    Object result;
    Class<?> clazz = null;
    try {
        clazz = ClassLoadingUtils.loadClass(fieldSchema.getFullName());
    } catch (Exception e) {
        throw new GoraException(e);
    }
    PersistentBase record = (PersistentBase) new BeanFactoryImpl(keyClass, clazz).newPersistent();
    for (Schema.Field recField : fieldSchema.getFields()) {
        Schema innerSchema = recField.schema();
        RethinkDBMapping.DocumentFieldType innerStoreType = rethinkDBMapping.getDocumentFieldType(recField.name());
        String innerDocField = rethinkDBMapping.getDocumentField(recField.name()) != null ? rethinkDBMapping.getDocumentField(recField.name()) : recField.name();
        LOG.debug("Load from ODocument (RECORD), field:{}, schemaType:{}, docField:{}, storeType:{}", new Object[] { recField.name(), innerSchema.getType(), innerDocField, innerStoreType });
        record.put(recField.pos(), convertDocFieldToAvroField(innerSchema, innerStoreType, recField, innerDocField, doc));
    }
    result = record;
    return result;
}
Also used : GoraException(org.apache.gora.util.GoraException) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) Schema(org.apache.avro.Schema) MapObject(com.rethinkdb.model.MapObject) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 8 with MapObject

use of com.rethinkdb.model.MapObject in project gora by apache.

the class RethinkDBStore method convertDocFieldToAvroMap.

private Object convertDocFieldToAvroMap(final String docf, final Schema fieldSchema, final MapObject<String, Object> doc, final Schema.Field f, final RethinkDBMapping.DocumentFieldType storeType) throws GoraException {
    if (storeType == RethinkDBMapping.DocumentFieldType.MAP) {
        Map<String, Object> map = (Map<String, Object>) doc.get(docf);
        Map<Utf8, Object> rmap = new HashMap<>();
        if (map == null) {
            return new DirtyMapWrapper(rmap);
        }
        for (Map.Entry entry : map.entrySet()) {
            String mapKey = entry.getKey().toString();
            Object o = convertDocFieldToAvroField(fieldSchema.getValueType(), storeType, f, mapKey, decorateMapToODoc(map));
            rmap.put(new Utf8(mapKey), o);
        }
        return new DirtyMapWrapper<>(rmap);
    } else {
        MapObject<String, Object> innerDoc = (MapObject<String, Object>) decorateMapToODoc((Map<String, Object>) doc.get(docf));
        Map<Utf8, Object> rmap = new HashMap<>();
        if (innerDoc == null) {
            return new DirtyMapWrapper(rmap);
        }
        for (String fieldName : innerDoc.keySet()) {
            String mapKey = fieldName;
            Object o = convertDocFieldToAvroField(fieldSchema.getValueType(), storeType, f, mapKey, innerDoc);
            rmap.put(new Utf8(mapKey), o);
        }
        return new DirtyMapWrapper<>(rmap);
    }
}
Also used : DirtyMapWrapper(org.apache.gora.persistency.impl.DirtyMapWrapper) HashMap(java.util.HashMap) Utf8(org.apache.avro.util.Utf8) MapObject(com.rethinkdb.model.MapObject) HashMap(java.util.HashMap) Map(java.util.Map) MapObject(com.rethinkdb.model.MapObject)

Example 9 with MapObject

use of com.rethinkdb.model.MapObject in project gora by apache.

the class RethinkDBStore method convertDocFieldToAvroList.

private Object convertDocFieldToAvroList(final String docf, final Schema fieldSchema, final MapObject<String, Object> doc, final Schema.Field f, final RethinkDBMapping.DocumentFieldType storeType) throws GoraException {
    if (storeType == RethinkDBMapping.DocumentFieldType.LIST || storeType == null) {
        List<Object> list = (List<Object>) doc.get(docf);
        List<Object> rlist = new ArrayList<>();
        if (list == null) {
            return new DirtyListWrapper(rlist);
        }
        for (Object item : list) {
            MapObject<String, Object> innerDoc = new MapObject();
            innerDoc.put("item", item);
            Object o = convertDocFieldToAvroField(fieldSchema.getElementType(), storeType, f, "item", innerDoc);
            rlist.add(o);
        }
        return new DirtyListWrapper<>(rlist);
    }
    return null;
}
Also used : DirtyListWrapper(org.apache.gora.persistency.impl.DirtyListWrapper) ArrayList(java.util.ArrayList) MapObject(com.rethinkdb.model.MapObject) ArrayList(java.util.ArrayList) List(java.util.List) MapObject(com.rethinkdb.model.MapObject)

Example 10 with MapObject

use of com.rethinkdb.model.MapObject in project gora by apache.

the class RethinkDBStore method execute.

/**
 * {@inheritDoc}
 */
@Override
public Result<K, T> execute(Query<K, T> query) throws GoraException {
    String[] fields = getFieldsToQuery(query.getFields());
    RethinkDBQuery dataStoreQuery;
    if (query instanceof RethinkDBQuery) {
        dataStoreQuery = ((RethinkDBQuery) query);
    } else {
        dataStoreQuery = (RethinkDBQuery) ((PartitionQueryImpl<K, T>) query).getBaseQuery();
    }
    dataStoreQuery.populateRethinkDBQuery(rethinkDBMapping, rethinkDBStoreParameters, fields, getFields());
    try {
        ReqlExpr reqlExpr = dataStoreQuery.getRethinkDBDbQuery();
        com.rethinkdb.net.Result<MapObject> result = reqlExpr.run(connection, MapObject.class);
        return new RethinkDBResult<>(this, query, result);
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) PartitionQueryImpl(org.apache.gora.query.impl.PartitionQueryImpl) RethinkDBQuery(org.apache.gora.rethinkdb.query.RethinkDBQuery) RethinkDBResult(org.apache.gora.rethinkdb.query.RethinkDBResult) MapObject(com.rethinkdb.model.MapObject) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) ReqlExpr(com.rethinkdb.gen.ast.ReqlExpr)

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