/** * Method in charge of compiling a specific table using a key schema and a set of attributes * @param pTableNameTable name * @param pKeySchemaKey schema used * @param pItemsList of items belonging to a specific table */ private void compile(String pTableName, KeySchema pKeySchema, List<Map<String, String>> pItems){ // TODO define where the generated will go try { startFile(pTableName, pTableName); setHeaders(null); line(0, ""); line(0, "@DynamoDBTable(tableName = \"" + pTableName + "\")"); line(0, "public class " + pTableName + " implements Persistent {"); setKeyAttributes(pKeySchema, 2); setKeyMethods(pKeySchema, 2); setItems(pItems, 2); setDefaultMethods(2); line(0, "}"); out.flush(); out.close(); } catch (IOException e) { log.error("Error while compiling table " + pTableName); e.printStackTrace(); } }
/** * Creates key getters and setters * @param pKeySchemaThe key schema for a specific table * @param pIdenNumber of spaces used for indentation * @throws IOException */ private void setKeyMethods(KeySchema pKeySchema, int pIden) throws IOException{ KeySchemaElement hashKey = pKeySchema.getHashKeyElement(); KeySchemaElement rangeKey = pKeySchema.getRangeKeyElement(); StringBuilder strBuilder = new StringBuilder(); // hash key if(hashKey != null){ strBuilder.append("@DynamoDBHashKey(attributeName=\"" + hashKey.getAttributeName() + "\") \n"); strBuilder.append(" public String getHashKey() { return " + hashKey.getAttributeName() + "; } \n"); strBuilder.append(" public void setHashKey(" + (hashKey.getAttributeType().equals("S")?"String ":"double ")); strBuilder.append("p" + camelCasify(hashKey.getAttributeName()) + "){ this." + hashKey.getAttributeName()); strBuilder.append(" = p" + camelCasify(hashKey.getAttributeName()) + "; }"); line(pIden, strBuilder.toString()); } strBuilder.delete(0, strBuilder.length()); // range key if(rangeKey != null){ strBuilder.append("@DynamoDBRangeKey(attributeName=\"" + rangeKey.getAttributeName() + "\") \n"); strBuilder.append(" public String getRangeKey() { return " + rangeKey.getAttributeName() + "; } \n"); strBuilder.append(" public void setRangeKey(" + (rangeKey.getAttributeType().equals("S")?"String ":"double ")); strBuilder.append("p" + camelCasify(rangeKey.getAttributeName()) + "){ this." + rangeKey.getAttributeName()); strBuilder.append(" = p" + camelCasify(rangeKey.getAttributeName()) + "; }"); line(pIden, strBuilder.toString()); } line(0, ""); }
/** * Creates the key attributes within the generated class * @param pKeySchemaKey schema * @param pIdenNumber of spaces used for indentation * @throws IOException */ private void setKeyAttributes(KeySchema pKeySchema, int pIden) throws IOException{ KeySchemaElement hashKey = pKeySchema.getHashKeyElement(); KeySchemaElement rangeKey = pKeySchema.getRangeKeyElement(); StringBuilder strBuilder = new StringBuilder(); // hash key if(hashKey != null){ strBuilder.append("private " + (hashKey.getAttributeType().equals("S")?"String ":"double ")); strBuilder.append(hashKey.getAttributeName() + ";"); line(pIden, strBuilder.toString()); } strBuilder.delete(0, strBuilder.length()); // range key if(rangeKey != null){ strBuilder.append("private " + (rangeKey.getAttributeType().equals("S")?"String ":"double ")); strBuilder.append(rangeKey.getAttributeName() + ";"); line(pIden, strBuilder.toString()); } line(0, ""); }
/** * Verifies is a table has a key schema defined * @param tableName Table name to determine which key schema to obtain * @return */ private boolean verifyKeySchema(String tableName){ KeySchema kSchema = tablesToKeySchemas.get(tableName); if (kSchema == null) return false; KeySchemaElement rangeKey = kSchema.getRangeKeyElement(); KeySchemaElement hashKey = kSchema.getHashKeyElement(); // A range key must have a hash key as well if (rangeKey != null){ if (hashKey != null) return true; else return false; } // A hash key may exist by itself if (hashKey != null) return true; return false; }
/** * Creates the table in DynamoDB. The hash+range key is: * * Hash (String) | Range (String) * File Path File Name * Example: s3n://netflix/data test.xml * * The table also includes one attribute for epoch (time created), but * that is only defined during the put operation (see add()). * */ private void createTable() { log.info("Creating table in DynamoDB: " + tableName); CreateTableRequest createRequest = new CreateTableRequest(); createRequest.withTableName(tableName); //Key KeySchemaElement pathKey = new KeySchemaElement().withAttributeName(HASH_KEY).withAttributeType(ScalarAttributeType.S); KeySchemaElement fileKey = new KeySchemaElement().withAttributeName(RANGE_KEY).withAttributeType(ScalarAttributeType.S); KeySchema schema = new KeySchema(); schema.setHashKeyElement(pathKey); schema.setRangeKeyElement(fileKey); createRequest.setKeySchema(schema); //Throughput ProvisionedThroughput tp = new ProvisionedThroughput(); tp.setReadCapacityUnits(readUnits); tp.setWriteCapacityUnits(writeUnits); createRequest.setProvisionedThroughput(tp); db.createTable(createRequest); }
/** * Constructor for DynamoDBMapping * @param tables Tables mapped. * @param tablesToKeySchemas KeySchemas used within tables mapped. * @param provisionedThroughput Provisioned throughput used within tables mapped. */ public DynamoDBMapping(Map<String, List<Map<String, String>>> tables, Map<String, KeySchema> tablesToKeySchemas, Map<String, ProvisionedThroughput> provisionedThroughput) { this.tablesToItems = tables; this.tablesToKeySchemas = tablesToKeySchemas; this.tablesToPrTh = provisionedThroughput; }
/** * Builds the necessary requests to create tables * @param tableName * @param keySchema * @param proThrou * @return */ private CreateTableRequest getCreateTableRequest(String tableName, KeySchema keySchema, ProvisionedThroughput proThrou){ CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.setTableName(tableName); createTableRequest.setKeySchema(keySchema); createTableRequest.setProvisionedThroughput(proThrou); return createTableRequest; }
@Test public void testMissingPrimaryKey() throws IOException, InterruptedException { String tableName = "mortar_test_foo_table"; String awsAccessKeyId = "XXXXXXXXXXXXX"; String awsSecretKey = "YYYYYYYYYYYYYY"; ResourceSchema schema = new ResourceSchema(Utils.getSchemaFromString("my_field:int")); // mock dynamo client AmazonDynamoDBClient dynamo = mock(AmazonDynamoDBClient.class); DescribeTableResult describeResult = new DescribeTableResult() .withTable( new TableDescription() .withProvisionedThroughput( new ProvisionedThroughputDescription().withWriteCapacityUnits(50L)) .withKeySchema(new KeySchema() .withHashKeyElement(new KeySchemaElement() .withAttributeName("not_the_key_you_will_find") .withAttributeType(ScalarAttributeType.N)))); when(dynamo.describeTable(any(DescribeTableRequest.class))).thenReturn(describeResult); DynamoDBStorage storage = new DynamoDBStorage(tableName, awsAccessKeyId, awsSecretKey, dynamo, null); try { storage.checkSchema(schema); Assert.fail("Expected schema validation to fail"); } catch(IOException e) { Assert.assertTrue("Expected " + e.getMessage() + " to contain hash msg", e.getMessage().contains("hash primary key")); } }
/** * Gets key schema * @return */ public KeySchema getKeySchema(){ return keySchema; }
/** * Sets query key schema used for queying * @param pKeySchema */ public void setKeySchema(KeySchema pKeySchema){ this.keySchema = pKeySchema; }
/** * Gets the key schema from a specific table * @param tableName Table name to determine which key schema to get * @return */ public KeySchema getKeySchema(String tableName) { return tablesToKeySchemas.get(tableName); }