-
Notifications
You must be signed in to change notification settings - Fork 13
Implemented rudimentary query for DynamoDB. #11
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import com.amazonaws.services.dynamodbv2.model.BatchWriteItemRequest; | ||
| import com.amazonaws.services.dynamodbv2.model.BatchWriteItemResult; | ||
| import com.amazonaws.services.dynamodbv2.model.Capacity; | ||
| import com.amazonaws.services.dynamodbv2.model.ComparisonOperator; | ||
| import com.amazonaws.services.dynamodbv2.model.Condition; | ||
| import com.amazonaws.services.dynamodbv2.model.ConsumedCapacity; | ||
| import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; | ||
|
|
@@ -360,8 +361,6 @@ public GetItemResult getItem(GetItemRequest getItemRequest) { | |
| throw new ResourceNotFoundException("Resource " + tableName + " not found."); | ||
| } | ||
|
|
||
| List<Map<String, AttributeValue>> rows = table.getRows(); | ||
|
|
||
| Map<String, AttributeValue> match = table.find(key, attributesToGet); | ||
|
|
||
| GetItemResult getItemResult = null; | ||
|
|
@@ -481,11 +480,24 @@ public PutItemResult putItem(String tableName, | |
| public QueryResult query(QueryRequest queryRequest) { | ||
| shutdownThrowsException(); | ||
|
|
||
| QueryResult queryResult = new QueryResult() | ||
| .withCount(new Integer(0)); | ||
| String tableName = queryRequest.getTableName(); | ||
| Map<String, Condition> keyConditions = queryRequest.getKeyConditions(); | ||
| List<String> attributesToGet = queryRequest.getAttributesToGet(); | ||
|
|
||
| if (true) { | ||
| throw new InternalServerErrorException("Operation not supported."); | ||
| Table table = dynamoDB.get(tableName); | ||
|
|
||
| List<Map<String, AttributeValue>> match = table.query(keyConditions, attributesToGet); | ||
|
|
||
| QueryResult queryResult = null; | ||
|
|
||
| if (match != null) { | ||
| queryResult = new QueryResult() | ||
| .withItems(match) | ||
| .withConsumedCapacity(new ConsumedCapacity() | ||
| .withTableName(tableName) | ||
| .withCapacityUnits(CAPACITY_UNITS) | ||
| .withTable(new Capacity() | ||
| .withCapacityUnits(CAPACITY_UNITS))); | ||
| } | ||
|
|
||
| return queryResult; | ||
|
|
@@ -728,6 +740,62 @@ public List<Map<String, AttributeValue>> find(List<Map<String, AttributeValue>> | |
| return matches; | ||
| } | ||
|
|
||
| public List<Map<String, AttributeValue>> query(Map<String, Condition> keyConditions, | ||
| List<String> attributesToGet) { | ||
| List<Map<String, AttributeValue>> matches = new ArrayList<Map<String, AttributeValue>>(); | ||
|
|
||
| List<Map<String, AttributeValue>> rows = this.getRows(); | ||
|
|
||
| for (Map<String, AttributeValue> row : rows) { | ||
| boolean match = true; | ||
|
|
||
| for (Entry<String, Condition> keyCondition : keyConditions.entrySet()) { | ||
| String columnName = keyCondition.getKey(); | ||
| Condition condition = keyCondition.getValue(); | ||
|
|
||
| AttributeValue columnValue = row.get(columnName); | ||
|
|
||
| boolean comparisonMatch = comparisonMatch(columnValue, condition); | ||
| match &= comparisonMatch; | ||
| } | ||
|
|
||
| if (match) { | ||
| matches.add(row); | ||
| } | ||
| } | ||
|
|
||
| return matches; | ||
| } | ||
|
|
||
| private boolean comparisonMatch(AttributeValue columnValue, Condition condition) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe make this method static and move it into AttributeValueComparator? |
||
| boolean comparisonMatch = false; | ||
|
|
||
| ComparisonOperator comparisonOperator = ComparisonOperator.fromValue(condition.getComparisonOperator()); | ||
| AttributeValue comparisonAttributeValue = condition.getAttributeValueList().get(0); | ||
|
|
||
| switch (comparisonOperator) { | ||
| case EQ: | ||
| comparisonMatch = AttributeValueComparator.equal(columnValue, comparisonAttributeValue); | ||
| break; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of having the comparisonMatch local variable, and a break line in each case, you could do: Also minor but I might shorten |
||
| case LT: | ||
| comparisonMatch = AttributeValueComparator.lessThan(columnValue, comparisonAttributeValue); | ||
| break; | ||
| case GT: | ||
| comparisonMatch = AttributeValueComparator.greaterThan(columnValue, comparisonAttributeValue); | ||
| break; | ||
| case LE: | ||
| comparisonMatch = AttributeValueComparator.lessThanEqual(columnValue, comparisonAttributeValue); | ||
| break; | ||
| case GE: | ||
| comparisonMatch = AttributeValueComparator.greaterThanEqual(columnValue, comparisonAttributeValue); | ||
| break; | ||
| default: | ||
| throw new InternalServerErrorException("Operation not supported."); | ||
| } | ||
|
|
||
| return comparisonMatch; | ||
| } | ||
|
|
||
| public Map<String, AttributeValue> delete(Map<String, AttributeValue> key) { | ||
| Map<String, AttributeValue> match = null; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| package com.bizo.awsstubs.services.dynamodb2; | ||
|
|
||
| import com.amazonaws.services.dynamodbv2.model.AttributeValue; | ||
|
|
||
| public class AttributeValueComparator { | ||
|
|
||
| private AttributeValueComparator() { | ||
| } | ||
|
|
||
| // attribute1 == attribute2 | ||
| public static boolean equal(AttributeValue attribute1, AttributeValue attribute2) { | ||
|
||
| if (attribute1 == attribute2) | ||
|
||
| return true; | ||
| if ((attribute1 == null) || (attribute2 == null)) | ||
| return false; | ||
|
|
||
| if (attribute2.getS() == null ^ attribute1.getS() == null) | ||
| return false; | ||
| if (attribute2.getS() != null && attribute2.getS().equals(attribute1.getS()) == false) | ||
| return false; | ||
| if (attribute2.getN() == null ^ attribute1.getN() == null) | ||
| return false; | ||
| if (attribute2.getN() != null && attribute2.getN().equals(attribute1.getN()) == false) | ||
| return false; | ||
| if (attribute2.getB() == null ^ attribute1.getB() == null) | ||
| return false; | ||
| if (attribute2.getB() != null && attribute2.getB().equals(attribute1.getB()) == false) | ||
| return false; | ||
| if (attribute2.getSS() == null ^ attribute1.getSS() == null) | ||
| return false; | ||
| if (attribute2.getSS() != null && attribute2.getSS().equals(attribute1.getSS()) == false) | ||
| return false; | ||
| if (attribute2.getNS() == null ^ attribute1.getNS() == null) | ||
| return false; | ||
| if (attribute2.getNS() != null && attribute2.getNS().equals(attribute1.getNS()) == false) | ||
| return false; | ||
| if (attribute2.getBS() == null ^ attribute1.getBS() == null) | ||
| return false; | ||
| if (attribute2.getBS() != null && attribute2.getBS().equals(attribute1.getBS()) == false) | ||
| return false; | ||
| if (attribute2.getM() == null ^ attribute1.getM() == null) | ||
| return false; | ||
| if (attribute2.getM() != null && attribute2.getM().equals(attribute1.getM()) == false) | ||
| return false; | ||
| if (attribute2.getL() == null ^ attribute1.getL() == null) | ||
| return false; | ||
| if (attribute2.getL() != null && attribute2.getL().equals(attribute1.getL()) == false) | ||
| return false; | ||
| if (attribute2.getNULL() == null ^ attribute1.getNULL() == null) | ||
| return false; | ||
| if (attribute2.getNULL() != null && attribute2.getNULL().equals(attribute1.getNULL()) == false) | ||
| return false; | ||
| if (attribute2.getBOOL() == null ^ attribute1.getBOOL() == null) | ||
| return false; | ||
| if (attribute2.getBOOL() != null && attribute2.getBOOL().equals(attribute1.getBOOL()) == false) | ||
| return false; | ||
| return true; | ||
| } | ||
|
|
||
| // attribute1 < attribute2 | ||
| public static boolean lessThan(AttributeValue attribute1, AttributeValue attribute2) { | ||
| if ((attribute1 == null) || (attribute2 == null)) | ||
| throw new NullAttributeValueException("Both AttributeValue instances must be non-null."); | ||
| if (attribute1 == attribute2) | ||
| return false; | ||
|
|
||
| if (attribute2.getS() == null ^ attribute1.getS() == null) | ||
| return false; | ||
| if (attribute2.getS() != null && attribute2.getS().compareTo(attribute1.getS()) < 0) | ||
| return false; | ||
| if (attribute2.getN() == null ^ attribute1.getN() == null) | ||
| return false; | ||
| if (attribute2.getN() != null && attribute2.getN().compareTo(attribute1.getN()) < 0) | ||
| return false; | ||
| if (attribute2.getB() == null ^ attribute1.getB() == null) | ||
| return false; | ||
| if (attribute2.getB() != null && attribute2.getB().compareTo(attribute1.getB()) < 0) | ||
| return false; | ||
| // if (attribute2.getSS() == null ^ attribute1.getSS() == null) | ||
| // return false; | ||
| // if (attribute2.getSS() != null && attribute2.getSS().compareTo(attribute1.getSS()) < 0) | ||
| // return false; | ||
| // if (attribute2.getNS() == null ^ attribute1.getNS() == null) | ||
| // return false; | ||
| // if (attribute2.getNS() != null && attribute2.getNS().compareTo(attribute1.getNS()) < 0) | ||
| // return false; | ||
| // if (attribute2.getBS() == null ^ attribute1.getBS() == null) | ||
| // return false; | ||
| // if (attribute2.getBS() != null && attribute2.getBS().compareTo(attribute1.getBS()) < 0) | ||
| // return false; | ||
| // if (attribute2.getM() == null ^ attribute1.getM() == null) | ||
| // return false; | ||
| // if (attribute2.getM() != null && attribute2.getM().compareTo(attribute1.getM()) < 0) | ||
| // return false; | ||
| // if (attribute2.getL() == null ^ attribute1.getL() == null) | ||
| // return false; | ||
| // if (attribute2.getL() != null && attribute2.getL().compareTo(attribute1.getL()) < 0) | ||
| // return false; | ||
| if (attribute2.getNULL() == null ^ attribute1.getNULL() == null) | ||
| return false; | ||
| if (attribute2.getNULL() != null && attribute2.getNULL().compareTo(attribute1.getNULL()) < 0) | ||
| return false; | ||
| if (attribute2.getBOOL() == null ^ attribute1.getBOOL() == null) | ||
| return false; | ||
| if (attribute2.getBOOL() != null && attribute2.getBOOL().compareTo(attribute1.getBOOL()) < 0) | ||
| return false; | ||
| return true; | ||
| } | ||
|
|
||
| // attribute1 > attribute2 | ||
| public static boolean greaterThan(AttributeValue attribute1, AttributeValue attribute2) { | ||
| if ((attribute1 == null) || (attribute2 == null)) | ||
| throw new NullAttributeValueException("Both AttributeValue instances must be non-null."); | ||
| if (attribute1 == attribute2) | ||
| return false; | ||
|
|
||
| if (attribute2.getS() == null ^ attribute1.getS() == null) | ||
| return false; | ||
| if (attribute2.getS() != null && attribute2.getS().compareTo(attribute1.getS()) > 0) | ||
| return false; | ||
| if (attribute2.getN() == null ^ attribute1.getN() == null) | ||
| return false; | ||
| if (attribute2.getN() != null && attribute2.getN().compareTo(attribute1.getN()) > 0) | ||
| return false; | ||
| if (attribute2.getB() == null ^ attribute1.getB() == null) | ||
| return false; | ||
| if (attribute2.getB() != null && attribute2.getB().compareTo(attribute1.getB()) > 0) | ||
| return false; | ||
| // if (attribute2.getSS() == null ^ attribute1.getSS() == null) | ||
| // return false; | ||
| // if (attribute2.getSS() != null && attribute2.getSS().compareTo(attribute1.getSS()) > 0) | ||
| // return false; | ||
| // if (attribute2.getNS() == null ^ attribute1.getNS() == null) | ||
| // return false; | ||
| // if (attribute2.getNS() != null && attribute2.getNS().compareTo(attribute1.getNS()) > 0) | ||
| // return false; | ||
| // if (attribute2.getBS() == null ^ attribute1.getBS() == null) | ||
| // return false; | ||
| // if (attribute2.getBS() != null && attribute2.getBS().compareTo(attribute1.getBS()) > 0) | ||
| // return false; | ||
| // if (attribute2.getM() == null ^ attribute1.getM() == null) | ||
| // return false; | ||
| // if (attribute2.getM() != null && attribute2.getM().compareTo(attribute1.getM()) > 0) | ||
| // return false; | ||
| // if (attribute2.getL() == null ^ attribute1.getL() == null) | ||
| // return false; | ||
| // if (attribute2.getL() != null && attribute2.getL().compareTo(attribute1.getL()) > 0) | ||
| // return false; | ||
| if (attribute2.getNULL() == null ^ attribute1.getNULL() == null) | ||
| return false; | ||
| if (attribute2.getNULL() != null && attribute2.getNULL().compareTo(attribute1.getNULL()) > 0) | ||
| return false; | ||
| if (attribute2.getBOOL() == null ^ attribute1.getBOOL() == null) | ||
| return false; | ||
| if (attribute2.getBOOL() != null && attribute2.getBOOL().compareTo(attribute1.getBOOL()) > 0) | ||
| return false; | ||
| return true; | ||
| } | ||
|
|
||
| // attribute1 <= attribute2 | ||
| public static boolean lessThanEqual(AttributeValue attribute1, AttributeValue attribute2) { | ||
| if (attribute1 == attribute2) | ||
| return true; | ||
| if ((attribute1 == null) || (attribute2 == null)) | ||
| throw new NullAttributeValueException("Both AttributeValue instances must be non-null."); | ||
|
|
||
| if (attribute2.getS() == null ^ attribute1.getS() == null) | ||
| return false; | ||
| if (attribute2.getS() != null && attribute2.getS().compareTo(attribute1.getS()) <= 0) | ||
| return false; | ||
| if (attribute2.getN() == null ^ attribute1.getN() == null) | ||
| return false; | ||
| if (attribute2.getN() != null && attribute2.getN().compareTo(attribute1.getN()) <= 0) | ||
| return false; | ||
| if (attribute2.getB() == null ^ attribute1.getB() == null) | ||
| return false; | ||
| if (attribute2.getB() != null && attribute2.getB().compareTo(attribute1.getB()) <= 0) | ||
| return false; | ||
| // if (attribute2.getSS() == null ^ attribute1.getSS() == null) | ||
| // return false; | ||
| // if (attribute2.getSS() != null && attribute2.getSS().compareTo(attribute1.getSS()) <= 0) | ||
| // return false; | ||
| // if (attribute2.getNS() == null ^ attribute1.getNS() == null) | ||
| // return false; | ||
| // if (attribute2.getNS() != null && attribute2.getNS().compareTo(attribute1.getNS()) <= 0) | ||
| // return false; | ||
| // if (attribute2.getBS() == null ^ attribute1.getBS() == null) | ||
| // return false; | ||
| // if (attribute2.getBS() != null && attribute2.getBS().compareTo(attribute1.getBS()) <= 0) | ||
| // return false; | ||
| // if (attribute2.getM() == null ^ attribute1.getM() == null) | ||
| // return false; | ||
| // if (attribute2.getM() != null && attribute2.getM().compareTo(attribute1.getM()) <= 0) | ||
| // return false; | ||
| // if (attribute2.getL() == null ^ attribute1.getL() == null) | ||
| // return false; | ||
| // if (attribute2.getL() != null && attribute2.getL().compareTo(attribute1.getL()) <= 0) | ||
| // return false; | ||
| if (attribute2.getNULL() == null ^ attribute1.getNULL() == null) | ||
| return false; | ||
| if (attribute2.getNULL() != null && attribute2.getNULL().compareTo(attribute1.getNULL()) <= 0) | ||
| return false; | ||
| if (attribute2.getBOOL() == null ^ attribute1.getBOOL() == null) | ||
| return false; | ||
| if (attribute2.getBOOL() != null && attribute2.getBOOL().compareTo(attribute1.getBOOL()) <= 0) | ||
| return false; | ||
| return true; | ||
| } | ||
|
|
||
| // attribute1 >= attribute2 | ||
| public static boolean greaterThanEqual(AttributeValue attribute1, AttributeValue attribute2) { | ||
| if (attribute1 == attribute2) | ||
| return true; | ||
| if ((attribute1 == null) || (attribute2 == null)) | ||
| throw new NullAttributeValueException("Both AttributeValue instances must be non-null."); | ||
|
|
||
| if (attribute2.getS() == null ^ attribute1.getS() == null) | ||
| return false; | ||
| if (attribute2.getS() != null && attribute2.getS().compareTo(attribute1.getS()) >= 0) | ||
| return false; | ||
| if (attribute2.getN() == null ^ attribute1.getN() == null) | ||
| return false; | ||
| if (attribute2.getN() != null && attribute2.getN().compareTo(attribute1.getN()) >= 0) | ||
| return false; | ||
| if (attribute2.getB() == null ^ attribute1.getB() == null) | ||
| return false; | ||
| if (attribute2.getB() != null && attribute2.getB().compareTo(attribute1.getB()) >= 0) | ||
| return false; | ||
| // if (attribute2.getSS() == null ^ attribute1.getSS() == null) | ||
| // return false; | ||
| // if (attribute2.getSS() != null && attribute2.getSS().compareTo(attribute1.getSS()) >= 0) | ||
| // return false; | ||
| // if (attribute2.getNS() == null ^ attribute1.getNS() == null) | ||
| // return false; | ||
| // if (attribute2.getNS() != null && attribute2.getNS().compareTo(attribute1.getNS()) >= 0) | ||
| // return false; | ||
| // if (attribute2.getBS() == null ^ attribute1.getBS() == null) | ||
| // return false; | ||
| // if (attribute2.getBS() != null && attribute2.getBS().compareTo(attribute1.getBS()) >= 0) | ||
| // return false; | ||
| // if (attribute2.getM() == null ^ attribute1.getM() == null) | ||
| // return false; | ||
| // if (attribute2.getM() != null && attribute2.getM().compareTo(attribute1.getM()) >= 0) | ||
| // return false; | ||
| // if (attribute2.getL() == null ^ attribute1.getL() == null) | ||
| // return false; | ||
| // if (attribute2.getL() != null && attribute2.getL().compareTo(attribute1.getL()) >= 0) | ||
| // return false; | ||
| if (attribute2.getNULL() == null ^ attribute1.getNULL() == null) | ||
| return false; | ||
| if (attribute2.getNULL() != null && attribute2.getNULL().compareTo(attribute1.getNULL()) >= 0) | ||
| return false; | ||
| if (attribute2.getBOOL() == null ^ attribute1.getBOOL() == null) | ||
| return false; | ||
| if (attribute2.getBOOL() != null && attribute2.getBOOL().compareTo(attribute1.getBOOL()) >= 0) | ||
| return false; | ||
| return true; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.bizo.awsstubs.services.dynamodb2; | ||
|
|
||
| public class NullAttributeValueException extends NullPointerException { | ||
|
|
||
| public NullAttributeValueException() { | ||
| super(); | ||
| } | ||
|
|
||
| public NullAttributeValueException(String message) { | ||
| super(message); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this query overload part of the interface? If so, please add an @OverRide, otherwise make it private?