Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Copy link
Member

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?

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) {
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The 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:

case EQ:
  return AttributeValueComparator.equal(columnValue, comparisonAttributeValue);

Also minor but I might shorten comparisonAttributeValue to just comparisonValue.

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;

Expand Down
Loading