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));
}
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;
}
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);
}
}
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;
}
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);
}
}
Aggregations