diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2014-05-24 01:21:26 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2014-05-24 01:21:26 -0700 |
commit | 865adc8dac28310f2c2285343c168816643d171c (patch) | |
tree | 816de492443ad88ed79c891b52986c02a42e9d66 | |
parent | d90c6f8ef2c9b630a39f7e6cbf65897dc2f62011 (diff) |
Adding serverprod target and import tool.
serverprod starts the frontend locally but points it to the prod
dynamodb.
The import tool loads data into dynamodb. Each record has a hash key and
one or more attribute/value pairs. Records are separated by an empty
line. The first line of the record is the hash key. Each subsequent line
is an attribute key, followed by a space, followed by the value.
-rw-r--r-- | build.xml | 9 | ||||
-rw-r--r-- | src/com/p4square/grow/GrowProcessComponent.java | 7 | ||||
-rw-r--r-- | src/com/p4square/grow/backend/dynamo/DbTool.java | 58 |
3 files changed, 73 insertions, 1 deletions
@@ -22,6 +22,15 @@ </java> </target> + <target name="server-prod" depends="compile"> + <java classname="com.p4square.grow.GrowProcessComponent" + classpathref="classpath.run" fork="true"> + + <arg file="devfiles/grow-server.properties" /> + <arg value="serverprod" /> + </java> + </target> + <target name="frontend" depends="compile"> <java classname="com.p4square.grow.frontend.GrowFrontend" classpathref="classpath.run" fork="true"> diff --git a/src/com/p4square/grow/GrowProcessComponent.java b/src/com/p4square/grow/GrowProcessComponent.java index 7d0938e..9a7de6f 100644 --- a/src/com/p4square/grow/GrowProcessComponent.java +++ b/src/com/p4square/grow/GrowProcessComponent.java @@ -92,10 +92,15 @@ public class GrowProcessComponent extends Component { // Load an optional config file from the first argument. Config config = new Config(); config.setDomain("dev"); - if (args.length == 1) { + if (args.length >= 1) { config.updateConfig(args[0]); } + // Override domain + if (args.length == 2) { + config.setDomain(args[1]); + } + // Start the HTTP Server final GrowProcessComponent component = new GrowProcessComponent(config); component.getServers().add(Protocol.HTTP, 8085); diff --git a/src/com/p4square/grow/backend/dynamo/DbTool.java b/src/com/p4square/grow/backend/dynamo/DbTool.java index 5784f3e..c904844 100644 --- a/src/com/p4square/grow/backend/dynamo/DbTool.java +++ b/src/com/p4square/grow/backend/dynamo/DbTool.java @@ -5,6 +5,9 @@ package com.p4square.grow.backend.dynamo; import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; @@ -47,6 +50,7 @@ public class DbTool { System.out.println("\t--loadStrings <data> Load all videos and questions"); System.out.println("\t--destroy Drop all tables"); System.out.println("\t--addadmin <user> <pass> Add a backend account"); + System.out.println("\t--import <table> <file> Backfill a table"); } public static void main(String... args) { @@ -107,6 +111,9 @@ public class DbTool { } else if ("--addadmin".equals(args[offset])) { offset = addAdmin(args, ++offset); + } else if ("--import".equals(args[offset])) { + offset = importTable(args, ++offset); + } else { throw new IllegalArgumentException("Unknown command " + args[offset]); } @@ -270,6 +277,57 @@ public class DbTool { return offset; } + private static int importTable(String[] args, int offset) throws IOException { + String table = args[offset++]; + String filename = args[offset++]; + + DynamoDatabase db = getDatabase(); + + List<String> lines = Files.readAllLines(new File(filename).toPath(), + StandardCharsets.UTF_8); + + int count = 0; + + String key = null; + Map<String, String> attributes = new HashMap<>(); + for (String line : lines) { + if (line.length() == 0) { + if (attributes.size() > 0) { + db.putKey(DynamoKey.newKey(table, key), attributes); + count++; + + if (count % 50 == 0) { + System.out.printf("Imported %d records into %s...\n", count, table); + } + } + key = null; + attributes = new HashMap<>(); + continue; + } + + if (key == null) { + key = line; + continue; + } + + int space = line.indexOf(' '); + String attribute = line.substring(0, space); + String value = line.substring(space + 1); + + attributes.put(attribute, value); + } + + // Finish up the remaining attributes. + if (key != null && attributes.size() > 0) { + db.putKey(DynamoKey.newKey(table, key), attributes); + count++; + } + + System.out.printf("Imported %d records into %s.\n", count, table); + + return offset; + } + private static void insertQuestions(File baseDir) throws IOException { DynamoDatabase db = getDatabase(); File questions = new File(baseDir, "questions"); |