use of org.apache.gora.rethinkdb.query.RethinkDBQuery 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);
}
}
use of org.apache.gora.rethinkdb.query.RethinkDBQuery 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;
}
}
}
Aggregations