Search in sources :

Example 76 with GoraException

use of org.apache.gora.util.GoraException 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 77 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class OrientDBStore method createSchema.

/**
 * {@inheritDoc}
 * Create a new class of OrientDB documents if necessary. Enforce specified schema over the document class.   *
 */
@Override
public void createSchema() throws GoraException {
    if (schemaExists()) {
        return;
    }
    try (ODatabaseDocumentTx schemaTx = connectionPool.acquire()) {
        schemaTx.activateOnCurrentThread();
        OClass documentClass = schemaTx.getMetadata().getSchema().createClass(orientDBMapping.getDocumentClass());
        documentClass.createProperty("_id", OType.getTypeByClass(super.getKeyClass())).createIndex(OClass.INDEX_TYPE.UNIQUE);
        for (String docField : orientDBMapping.getDocumentFields()) {
            documentClass.createProperty(docField, OType.valueOf(orientDBMapping.getDocumentFieldType(docField).name()));
        }
        schemaTx.getMetadata().getSchema().reload();
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 78 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class OrientDBStore method deleteSchema.

/**
 * {@inheritDoc}
 * Deletes enforced schema over OrientDB Document class.
 */
@Override
public void deleteSchema() throws GoraException {
    try (ODatabaseDocumentTx schemaTx = connectionPool.acquire()) {
        schemaTx.activateOnCurrentThread();
        schemaTx.getMetadata().getSchema().dropClass(orientDBMapping.getDocumentClass());
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 79 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class OrientDBStore method convertDocFieldToAvroUnion.

private Object convertDocFieldToAvroUnion(final Schema fieldSchema, final OrientDBMapping.DocumentFieldType storeType, final Schema.Field field, final String docf, final ODocument doc) throws GoraException {
    Object result;
    Schema.Type type0 = fieldSchema.getTypes().get(0).getType();
    Schema.Type type1 = fieldSchema.getTypes().get(1).getType();
    if (!type0.equals(type1) && (type0.equals(Schema.Type.NULL) || type1.equals(Schema.Type.NULL))) {
        Schema innerSchema = null;
        if (type0.equals(Schema.Type.NULL)) {
            innerSchema = fieldSchema.getTypes().get(1);
        } else {
            innerSchema = fieldSchema.getTypes().get(0);
        }
        LOG.debug("Load from ODocument (UNION), schemaType:{}, docField:{}, storeType:{}", new Object[] { innerSchema.getType(), docf, storeType });
        result = convertDocFieldToAvroField(innerSchema, storeType, field, docf, doc);
    } else {
        throw new GoraException("OrientDBStore only supports Union of two types field.");
    }
    return result;
}
Also used : GoraException(org.apache.gora.util.GoraException) Schema(org.apache.avro.Schema)

Example 80 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class SolrStore method delete.

@Override
public boolean delete(K key) throws GoraException {
    String keyField = mapping.getPrimaryKey();
    try {
        UpdateResponse rsp = server.deleteByQuery(keyField + ":" + escapeQueryKey(key.toString()));
        server.commit();
        LOG.info(rsp.toString());
        return true;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) GoraException(org.apache.gora.util.GoraException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Aggregations

GoraException (org.apache.gora.util.GoraException)174 IOException (java.io.IOException)119 ArrayList (java.util.ArrayList)30 Schema (org.apache.avro.Schema)25 SQLException (java.sql.SQLException)17 HashMap (java.util.HashMap)17 InvocationTargetException (java.lang.reflect.InvocationTargetException)13 PreparedStatement (java.sql.PreparedStatement)11 Map (java.util.Map)11 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)9 SolrServerException (org.apache.solr.client.solrj.SolrServerException)9 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)8 AccumuloException (org.apache.accumulo.core.client.AccumuloException)8 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)8 Field (org.apache.avro.Schema.Field)8 KuduException (org.apache.kudu.client.KuduException)8 ResultSet (com.datastax.driver.core.ResultSet)7 SimpleStatement (com.datastax.driver.core.SimpleStatement)7 SAXBuilder (org.jdom.input.SAXBuilder)7 InputStream (java.io.InputStream)6