diff options
Diffstat (limited to 'src/com/p4square/grow')
| -rw-r--r-- | src/com/p4square/grow/GrowProcessComponent.java | 7 | ||||
| -rw-r--r-- | src/com/p4square/grow/backend/dynamo/DbTool.java | 58 | 
2 files changed, 64 insertions, 1 deletions
| 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"); | 
