Search in sources :

Example 1 with OrientDBQuery

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);
    }
}
Also used : OrientDBQuery(org.apache.gora.orientdb.query.OrientDBQuery) OrientDBResult(org.apache.gora.orientdb.query.OrientDBResult) GoraException(org.apache.gora.util.GoraException) PartitionQueryImpl(org.apache.gora.query.impl.PartitionQueryImpl) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with OrientDBQuery

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);
        }
    }
}
Also used : Delete(com.gitub.raymanrt.orientqb.delete.Delete) HashMap(java.util.HashMap) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OrientDBQuery(org.apache.gora.orientdb.query.OrientDBQuery) GoraException(org.apache.gora.util.GoraException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with OrientDBQuery

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 });
        }
    }
}
Also used : OrientDBQuery(org.apache.gora.orientdb.query.OrientDBQuery) GoraException(org.apache.gora.util.GoraException) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)3 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 IOException (java.io.IOException)3 OrientDBQuery (org.apache.gora.orientdb.query.OrientDBQuery)3 GoraException (org.apache.gora.util.GoraException)3 Delete (com.gitub.raymanrt.orientqb.delete.Delete)1 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)1 HashMap (java.util.HashMap)1 OrientDBResult (org.apache.gora.orientdb.query.OrientDBResult)1 PartitionQueryImpl (org.apache.gora.query.impl.PartitionQueryImpl)1