use of org.apache.gora.orientdb.query.OrientDBQuery in project gora by apache.
the class OrientDBStore method execute.
/**
* {@inheritDoc}
*/
@Override
public Result<K, T> execute(Query<K, T> query) throws GoraException {
String[] fields = getFieldsToQuery(query.getFields());
OrientDBQuery dataStoreQuery;
if (query instanceof OrientDBQuery) {
dataStoreQuery = ((OrientDBQuery) query);
} else {
dataStoreQuery = (OrientDBQuery) ((PartitionQueryImpl<K, T>) query).getBaseQuery();
}
dataStoreQuery.populateOrientDBQuery(orientDBMapping, fields, getFields());
try (ODatabaseDocumentTx selectTx = connectionPool.acquire()) {
selectTx.activateOnCurrentThread();
OConcurrentResultSet<ODocument> result = selectTx.command(dataStoreQuery.getOrientDBQuery()).execute(dataStoreQuery.getParams());
result.setLimit((int) query.getLimit());
return new OrientDBResult<K, T>(this, query, result);
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.orientdb.query.OrientDBQuery in project gora by apache.
the class OrientDBStore method deleteByQuery.
/**
* {@inheritDoc}
*/
@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
Delete delete = new Delete();
delete.from(orientDBMapping.getDocumentClass());
Map<String, Object> params = new HashMap<String, Object>();
if (query.getFields() == null || (query.getFields().length == getFields().length)) {
if (query.getStartKey() != null) {
delete.where(projection("_id").ge(Parameter.parameter("start")));
params.put("start", query.getStartKey());
}
if (query.getEndKey() != null) {
delete.where(projection("_id").le(Parameter.parameter("end")));
params.put("end", query.getEndKey());
}
OCommandSQL dbQuery = new OCommandSQL(delete.toString().replace("DELETE", "DELETE FROM"));
try (ODatabaseDocumentTx deleteTx = connectionPool.acquire()) {
deleteTx.activateOnCurrentThread();
int deleteCount;
if (params.isEmpty()) {
deleteCount = deleteTx.command(dbQuery).execute();
} else {
deleteCount = deleteTx.command(dbQuery).execute(params);
}
if (deleteCount > 0) {
return deleteCount;
} else {
return 0;
}
} catch (Exception e) {
throw new GoraException(e);
}
} else {
OrientDBQuery<K, T> dataStoreQuery = new OrientDBQuery<>(this);
dataStoreQuery.setStartKey(query.getStartKey());
dataStoreQuery.setEndKey(query.getEndKey());
dataStoreQuery.populateOrientDBQuery(orientDBMapping, getFieldsToQuery(null), getFields());
try (ODatabaseDocumentTx selectTx = connectionPool.acquire()) {
selectTx.activateOnCurrentThread();
List<ODocument> result = selectTx.command(dataStoreQuery.getOrientDBQuery()).execute(dataStoreQuery.getParams());
if (result != null && result.isEmpty()) {
return 0;
} else {
for (ODocument doc : result) {
for (String docField : query.getFields()) {
if (doc.containsField(orientDBMapping.getDocumentField(docField))) {
doc.removeField(orientDBMapping.getDocumentField(docField));
}
}
doc.save();
}
return result.size();
}
} catch (Exception e) {
throw new GoraException(e);
}
}
}
use of org.apache.gora.orientdb.query.OrientDBQuery in project gora by apache.
the class OrientDBStore method put.
/**
* {@inheritDoc}
*/
@Override
public void put(K key, T val) throws GoraException {
if (val.isDirty()) {
OrientDBQuery<K, T> dataStoreQuery = new OrientDBQuery<>(this);
dataStoreQuery.setStartKey(key);
dataStoreQuery.setEndKey(key);
dataStoreQuery.populateOrientDBQuery(orientDBMapping, getFieldsToQuery(null), getFields());
try (ODatabaseDocumentTx selectTx = connectionPool.acquire()) {
selectTx.activateOnCurrentThread();
// TODO : further optimize for queries to separate cases update / insert == get rid of select all query
// TODO : for update
List<ODocument> result = selectTx.command(dataStoreQuery.getOrientDBQuery()).execute(dataStoreQuery.getParams());
if (result.size() == 1) {
ODocument document = updateOrientDocFromAvroBean(key, val, result.get(0));
docBatch.add(document);
} else {
ODocument document = convertAvroBeanToOrientDoc(key, val);
docBatch.add(document);
}
} 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 });
}
}
}
Aggregations