use of org.apache.gora.util.GoraException in project gora by apache.
the class DynamoDBNativeStore method get.
@Override
public /**
* Gets the object with the specific key
* @throws IOException
*/
T get(K key) throws GoraException {
T object = null;
try {
Object rangeKey;
rangeKey = getRangeKeyFromKey(key);
Object hashKey = getHashFromKey(key);
if (hashKey != null) {
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
if (rangeKey != null)
object = mapper.load(persistentClass, hashKey, rangeKey);
else
object = mapper.load(persistentClass, hashKey);
return object;
} else {
throw new GoraException("Error while retrieving keys from object: " + key.toString());
}
} catch (GoraException e) {
throw e;
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class DynamoDBNativeStore method deleteByQuery.
/**
* Deletes items using a specific query
*
* @param query matching records to this query will be deleted
* @return
*/
@Override
@SuppressWarnings("unchecked")
public long deleteByQuery(Query<K, T> query) throws GoraException {
// TODO verify whether or not we are deleting a whole row
// String[] fields = getFieldsToQuery(query.getFields());
// find whether all fields are queried, which means that complete
// rows will be deleted
// boolean isAllFields = Arrays.equals(fields
// , getBeanFactory().getCachedPersistent().getFields());
ArrayList<T> deletes = null;
try {
Result<K, T> result = execute(query);
deletes = new ArrayList<T>();
while (result.next()) {
T resultObj = result.get();
deletes.add(resultObj);
@SuppressWarnings("rawtypes") DynamoDBKey dKey = new DynamoDBKey();
dKey.setHashKey(getHashFromObj(resultObj));
dKey.setRangeKey(getRangeKeyFromObj(resultObj));
delete((K) dKey);
}
} catch (GoraException e) {
// If it is a GoraException we assume it is already logged
throw e;
} catch (Exception e) {
throw new GoraException(e);
}
return deletes.size();
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class DynamoDBNativeStore method put.
/**
* Puts an object identified by a key
*
* @param key
* @param obj
*/
@Override
public void put(K key, T obj) throws GoraException {
try {
Object hashKey = getHashKey(key, obj);
Object rangeKey = getRangeKey(key, obj);
if (hashKey != null) {
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
if (rangeKey != null) {
mapper.load(persistentClass, hashKey, rangeKey);
} else {
mapper.load(persistentClass, hashKey);
}
mapper.save(obj);
} else
throw new GoraException("No HashKey found in Key nor in Object.");
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class DynamoDBStore method readMapping.
/**
* Reads the schema file and converts it into a data structure to be used
*
* @return DynamoDBMapping Object containing all necessary information to
* create tables
* @throws IOException
*/
@SuppressWarnings("unchecked")
private DynamoDBMapping readMapping(String filename) throws IOException {
DynamoDBMappingBuilder mappingBuilder = new DynamoDBMappingBuilder();
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(getClass().getClassLoader().getResourceAsStream(filename));
if (doc == null || doc.getRootElement() == null)
throw new GoraException("Unable to load " + MAPPING_FILE + ". Please check its existance!");
Element root = doc.getRootElement();
List<Element> tableElements = root.getChildren("table");
boolean keys = false;
for (Element tableElement : tableElements) {
String tableName = tableElement.getAttributeValue("name");
long readCapacUnits = Long.parseLong(tableElement.getAttributeValue("readcunit"));
long writeCapacUnits = Long.parseLong(tableElement.getAttributeValue("writecunit"));
mappingBuilder.setProvisionedThroughput(tableName, readCapacUnits, writeCapacUnits);
LOG.debug("Basic table properties have been set: Name, and Provisioned throughput.");
// Retrieving attributes
List<Element> fieldElements = tableElement.getChildren("attribute");
for (Element fieldElement : fieldElements) {
String key = fieldElement.getAttributeValue("key");
String attributeName = fieldElement.getAttributeValue("name");
String attributeType = fieldElement.getAttributeValue("type");
mappingBuilder.addAttribute(tableName, attributeName, attributeType);
// Retrieving key's features
if (key != null) {
mappingBuilder.setKeySchema(tableName, attributeName, key);
keys = true;
}
}
LOG.debug("Attributes for table '" + tableName + "' have been read.");
if (!keys)
LOG.warn("Keys for table '" + tableName + "' have NOT been set.");
}
} catch (IOException ex) {
LOG.error("Error while performing xml mapping.", ex.getMessage());
throw new IOException(ex);
} catch (Exception ex) {
LOG.error("Error while performing xml mapping.", ex.getMessage());
throw new RuntimeException(ex);
}
return mappingBuilder.build();
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class DynamoDBStore method schemaExists.
/**
* Verifies if the specified schemas exist
*
* @return
*/
@Override
public boolean schemaExists() throws GoraException {
try {
LOG.info("Verifying schemas.");
TableDescription success = null;
if (getDynamoDbMapping().getTables().isEmpty())
throw new IllegalStateException("There are not tables defined.");
if (getPreferredSchema() == null) {
LOG.debug("Verifying schemas");
if (getDynamoDbMapping().getTables().isEmpty())
throw new IllegalStateException("There are not tables defined.");
// read the mapping object
for (String tableName : getDynamoDbMapping().getTables().keySet()) {
success = getTableSchema(tableName);
if (success == null)
return false;
}
} else {
LOG.info("Verifying schema " + preferredSchema);
success = getTableSchema(preferredSchema);
}
LOG.info("Finished verifying schemas.");
return (success != null) ? true : false;
} catch (Exception e) {
throw new GoraException(e);
}
}
Aggregations