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