add batch get

This commit is contained in:
Steve Pulec 2013-03-14 00:26:55 -04:00
parent 0398da2c8d
commit cc23453d77
2 changed files with 64 additions and 2 deletions

View File

@ -155,6 +155,29 @@ class DynamoHandler(object):
er = 'com.amazonaws.dynamodb.v20111205#ResourceNotFoundException'
return self.error(er)
def BatchGetItem(self, uri, body, headers):
table_batches = body['RequestItems']
results = {
"Responses": {
"UnprocessedKeys": {}
}
}
for table_name, table_request in table_batches.iteritems():
items = []
keys = table_request['Keys']
attributes_to_get = table_request.get('AttributesToGet')
for key in keys:
hash_key = value_from_dynamo_type(key["HashKeyElement"])
range_key = value_from_dynamo_type(key.get("RangeKeyElement"))
item = dynamodb_backend.get_item(table_name, hash_key, range_key)
if item:
item_describe = item.describe_attrs(attributes_to_get)
items.append(item_describe)
results["Responses"][table_name] = {"Items": items, "ConsumedCapacityUnits": 1}
return json.dumps(results)
def Query(self, uri, body, headers):
name = body['TableName']
hash_key = body['HashKeyValue'].values()[0]

View File

@ -338,5 +338,44 @@ def test_write_batch():
table.item_count.should.equal(1)
# Batch read
# Batch write
@mock_dynamodb
def test_batch_read():
conn = boto.connect_dynamodb()
table = create_table(conn)
item_data = {
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User A',
'ReceivedTime': '12/9/2011 11:36:03 PM',
}
item = table.new_item(
hash_key='the-key',
range_key='456',
attrs=item_data,
)
item.put()
item = table.new_item(
hash_key='the-key',
range_key='123',
attrs=item_data,
)
item.put()
item_data = {
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User B',
'ReceivedTime': '12/9/2011 11:36:03 PM',
'Ids': {1, 2, 3},
'PK': 7,
}
item = table.new_item(
hash_key='another-key',
range_key='789',
attrs=item_data,
)
item.put()
items = table.batch_get_item([('the-key', '123'), ('another-key', '789')])
count = len([item for item in items])
count.should.equal(2)