-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented rudimentary query for DynamoDB. #11
base: master
Are you sure you want to change the base?
Changes from all commits
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,25 @@ 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) | ||
.withCount(match.size()) | ||
.withConsumedCapacity(new ConsumedCapacity() | ||
.withTableName(tableName) | ||
.withCapacityUnits(CAPACITY_UNITS) | ||
.withTable(new Capacity() | ||
.withCapacityUnits(CAPACITY_UNITS))); | ||
} | ||
|
||
return queryResult; | ||
|
@@ -728,6 +741,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) { | ||
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; | ||
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; | ||
|
||
|
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?