* @throws IOException
*/
@SuppressWarnings("unchecked")
private DynamoDBMapping readMapping() throws IOException {
DynamoDBMappingBuilder mappingBuilder = new DynamoDBMappingBuilder();
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(getClass().getClassLoader().getResourceAsStream(MAPPING_FILE));
Element root = doc.getRootElement();
List<Element> tableElements = root.getChildren("table");
for(Element tableElement : tableElements) {
String tableName = tableElement.getAttributeValue("name");
long readCapacUnits = Long.parseLong(tableElement.getAttributeValue("readcunit"));
long writeCapacUnits = Long.parseLong(tableElement.getAttributeValue("writecunit"));
mappingBuilder.setTableName(tableName);
mappingBuilder.setProvisionedThroughput(tableName, readCapacUnits, writeCapacUnits);
LOG.debug("Basic table properties have been set: Name, and Provisioned throughput.");
// Retrieving key's features
List<Element> fieldElements = tableElement.getChildren("key");
for(Element fieldElement : fieldElements) {
String keyName = fieldElement.getAttributeValue("name");
String keyType = fieldElement.getAttributeValue("type");
String keyAttrType = fieldElement.getAttributeValue("att-type");
if(keyType.equals("hash"))
mappingBuilder.setHashKeySchema(tableName, keyName, keyAttrType);
else if(keyType.equals("hashrange"))
mappingBuilder.setHashRangeKeySchema(tableName, keyName, keyAttrType);
}
LOG.debug("Table key schemas have been set.");
// Retrieving attributes
fieldElements = tableElement.getChildren("attribute");
for(Element fieldElement : fieldElements) {
String attributeName = fieldElement.getAttributeValue("name");
String attributeType = fieldElement.getAttributeValue("type");
mappingBuilder.addAttribute(tableName, attributeName, attributeType, 0);
}
LOG.debug("Table attributes have been read.");
}
} catch(IOException ex) {
LOG.error("Error while performing xml mapping.");
ex.printStackTrace();
throw ex;
} catch(Exception ex) {
LOG.error("Error while performing xml mapping.");
ex.printStackTrace();
throw new IOException(ex);
}
return mappingBuilder.build();
}