This repository was archived by the owner on Sep 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Implemented rudimentary query for DynamoDB. #11
Open
git-rzanelli
wants to merge
4
commits into
bizo:master
Choose a base branch
from
git-rzanelli:20160715_Implement-rudimentary-query-for-DynamoDB
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?