summaryrefslogtreecommitdiff
path: root/src/com
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2014-09-25 08:11:56 -0700
committerJesse Morgan <jesse@jesterpm.net>2014-09-25 08:11:56 -0700
commit8fa4f1fb4f122ee1e27c6015479bcd883b4b0671 (patch)
tree12d5ab88b29d011af830b99f61c6cafd76612c93 /src/com
parentdd4f34e216132e3a066566daf30a6f1fc1e1b872 (diff)
Adding Support for Scan in DynamoDatabase.
Also added scan and scanf (filtered) options to DbTool.
Diffstat (limited to 'src/com')
-rw-r--r--src/com/p4square/grow/backend/dynamo/DbTool.java58
-rw-r--r--src/com/p4square/grow/backend/dynamo/DynamoDatabase.java41
2 files changed, 99 insertions, 0 deletions
diff --git a/src/com/p4square/grow/backend/dynamo/DbTool.java b/src/com/p4square/grow/backend/dynamo/DbTool.java
index 581795d..1a6d5e9 100644
--- a/src/com/p4square/grow/backend/dynamo/DbTool.java
+++ b/src/com/p4square/grow/backend/dynamo/DbTool.java
@@ -45,6 +45,8 @@ public class DbTool {
System.out.println("\t--get <table> <key> <attribute> Get a value");
System.out.println("\t--put <table> <key> <attribute> <value> Put a value");
System.out.println("\t--delete <table> <key> <attribute> Delete a value");
+ System.out.println("\t--scan <table> List all rows");
+ System.out.println("\t--scanf <table> <attribute> List all rows");
System.out.println();
System.out.println("Bootstrap Commands:");
System.out.println("\t--bootstrap <data> Create all tables and import all data");
@@ -103,6 +105,12 @@ public class DbTool {
} else if ("--delete".equals(args[offset])) {
offset = delete(args, ++offset);
+ } else if ("--scan".equals(args[offset])) {
+ offset = scan(args, ++offset);
+
+ } else if ("--scanf".equals(args[offset])) {
+ offset = scanf(args, ++offset);
+
/* Bootstrap Commands */
} else if ("--bootstrap".equals(args[offset])) {
if ("dev".equals(mConfig.getDomain())) {
@@ -221,6 +229,56 @@ public class DbTool {
return offset;
}
+ private static int scan(String[] args, int offset) {
+ String table = args[offset++];
+
+ DynamoKey key = DynamoKey.newKey(table, null);
+
+ doScan(key);
+
+ return offset;
+ }
+
+ private static int scanf(String[] args, int offset) {
+ String table = args[offset++];
+ String attribute = args[offset++];
+
+ DynamoKey key = DynamoKey.newAttributeKey(table, null, attribute);
+
+ doScan(key);
+
+ return offset;
+ }
+
+ private static void doScan(DynamoKey key) {
+ DynamoDatabase db = getDatabase();
+
+ String attributeFilter = key.getAttribute();
+
+ while (key != null) {
+ Map<DynamoKey, Map<String, String>> result = db.getAll(key);
+
+ key = null; // If there are no results, exit
+
+ for (Map.Entry<DynamoKey, Map<String, String>> entry : result.entrySet()) {
+ key = entry.getKey(); // Save the last key
+
+ for (Map.Entry<String, String> attribute : entry.getValue().entrySet()) {
+ if (attributeFilter == null || attributeFilter.equals(attribute.getKey())) {
+ String keyString = key.getHashKey();
+ if (key.getRangeKey() != null) {
+ keyString += "(" + key.getRangeKey() + ")";
+ }
+ System.out.printf("%s %s:%s\n%s\n\n",
+ key.getTable(), keyString, attribute.getKey(),
+ attribute.getValue());
+ }
+ }
+ }
+ }
+ }
+
+
private static int bootstrapTables(String[] args, int offset) {
DynamoDatabase db = getDatabase();
diff --git a/src/com/p4square/grow/backend/dynamo/DynamoDatabase.java b/src/com/p4square/grow/backend/dynamo/DynamoDatabase.java
index 0c77c49..68a165d 100644
--- a/src/com/p4square/grow/backend/dynamo/DynamoDatabase.java
+++ b/src/com/p4square/grow/backend/dynamo/DynamoDatabase.java
@@ -4,6 +4,7 @@
package com.p4square.grow.backend.dynamo;
+import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -31,6 +32,8 @@ import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.PutItemResult;
+import com.amazonaws.services.dynamodbv2.model.ScanRequest;
+import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.amazonaws.services.dynamodbv2.model.UpdateItemRequest;
import com.amazonaws.services.dynamodbv2.model.UpdateItemResult;
import com.amazonaws.services.dynamodbv2.model.UpdateTableRequest;
@@ -120,6 +123,44 @@ public class DynamoDatabase {
DeleteTableResult result = mClient.deleteTable(deleteTableRequest);
}
+ /**
+ * Get all rows from a table.
+ *
+ * The key parameter must specify a table. If hash/range key is specified,
+ * the scan will begin after that key.
+ *
+ * @param key Previous key to start with.
+ * @return An ordered map of all results.
+ */
+ public Map<DynamoKey, Map<String, String>> getAll(final DynamoKey key) {
+ ScanRequest scanRequest = new ScanRequest().withTableName(mTablePrefix + key.getTable());
+
+ if (key.getHashKey() != null) {
+ scanRequest.setExclusiveStartKey(generateKey(key));
+ }
+
+ ScanResult scanResult = mClient.scan(scanRequest);
+
+ Map<DynamoKey, Map<String, String>> result = new LinkedHashMap<>();
+ for (Map<String, AttributeValue> map : scanResult.getItems()) {
+ String id = null;
+ String range = null;
+ Map<String, String> row = new LinkedHashMap<>();
+ for (Map.Entry<String, AttributeValue> entry : map.entrySet()) {
+ if ("id".equals(entry.getKey())) {
+ id = entry.getValue().getS();
+ } else if ("range".equals(entry.getKey())) {
+ range = entry.getValue().getS();
+ } else {
+ row.put(entry.getKey(), entry.getValue().getS());
+ }
+ }
+ result.put(DynamoKey.newRangeKey(key.getTable(), id, range), row);
+ }
+
+ return result;
+ }
+
public Map<String, String> getKey(final DynamoKey key) {
GetItemRequest getItemRequest = new GetItemRequest()
.withTableName(mTablePrefix + key.getTable())