add batch write
This commit is contained in:
parent
3e6f802a20
commit
0398da2c8d
@ -2,7 +2,7 @@ import json
|
|||||||
|
|
||||||
from moto.core.utils import headers_to_dict
|
from moto.core.utils import headers_to_dict
|
||||||
from .models import dynamodb_backend
|
from .models import dynamodb_backend
|
||||||
from .utils import values_from_dynamo_types
|
from .utils import value_from_dynamo_type, values_from_dynamo_types
|
||||||
|
|
||||||
|
|
||||||
class DynamoHandler(object):
|
class DynamoHandler(object):
|
||||||
@ -110,6 +110,37 @@ class DynamoHandler(object):
|
|||||||
item_dict['ConsumedCapacityUnits'] = 1
|
item_dict['ConsumedCapacityUnits'] = 1
|
||||||
return json.dumps(item_dict)
|
return json.dumps(item_dict)
|
||||||
|
|
||||||
|
def BatchWriteItem(self, uri, body, headers):
|
||||||
|
table_batches = body['RequestItems']
|
||||||
|
|
||||||
|
for table_name, table_requests in table_batches.iteritems():
|
||||||
|
for table_request in table_requests:
|
||||||
|
request_type = table_request.keys()[0]
|
||||||
|
request = table_request.values()[0]
|
||||||
|
|
||||||
|
if request_type == 'PutRequest':
|
||||||
|
item = request['Item']
|
||||||
|
dynamodb_backend.put_item(table_name, item)
|
||||||
|
elif request_type == 'DeleteRequest':
|
||||||
|
key = request['Key']
|
||||||
|
hash_key = value_from_dynamo_type(key['HashKeyElement'])
|
||||||
|
range_key = value_from_dynamo_type(key.get('RangeKeyElement'))
|
||||||
|
item = dynamodb_backend.delete_item(table_name, hash_key, range_key)
|
||||||
|
|
||||||
|
response = {
|
||||||
|
"Responses": {
|
||||||
|
"Thread": {
|
||||||
|
"ConsumedCapacityUnits": 1.0
|
||||||
|
},
|
||||||
|
"Reply": {
|
||||||
|
"ConsumedCapacityUnits": 1.0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"UnprocessedItems": {}
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.dumps(response)
|
||||||
|
|
||||||
def GetItem(self, uri, body, headers):
|
def GetItem(self, uri, body, headers):
|
||||||
name = body['TableName']
|
name = body['TableName']
|
||||||
hash_key = body['Key']['HashKeyElement'].values()[0]
|
hash_key = body['Key']['HashKeyElement'].values()[0]
|
||||||
|
@ -15,6 +15,7 @@ def value_from_dynamo_type(dynamo_type):
|
|||||||
# TODO eventually this should be smarted to actually read the type of
|
# TODO eventually this should be smarted to actually read the type of
|
||||||
the attribute
|
the attribute
|
||||||
"""
|
"""
|
||||||
|
if dynamo_type:
|
||||||
return dynamo_type.values()[0]
|
return dynamo_type.values()[0]
|
||||||
|
|
||||||
|
|
||||||
|
@ -294,5 +294,49 @@ def test_scan():
|
|||||||
results.response['Items'].should.have.length_of(1)
|
results.response['Items'].should.have.length_of(1)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_dynamodb
|
||||||
|
def test_write_batch():
|
||||||
|
conn = boto.connect_dynamodb()
|
||||||
|
table = create_table(conn)
|
||||||
|
|
||||||
|
batch_list = conn.new_batch_write_list()
|
||||||
|
|
||||||
|
items = []
|
||||||
|
items.append(table.new_item(
|
||||||
|
hash_key='the-key',
|
||||||
|
range_key='123',
|
||||||
|
attrs={
|
||||||
|
'Body': 'http://url_to_lolcat.gif',
|
||||||
|
'SentBy': 'User A',
|
||||||
|
'ReceivedTime': '12/9/2011 11:36:03 PM',
|
||||||
|
},
|
||||||
|
))
|
||||||
|
|
||||||
|
items.append(table.new_item(
|
||||||
|
hash_key='the-key',
|
||||||
|
range_key='789',
|
||||||
|
attrs={
|
||||||
|
'Body': 'http://url_to_lolcat.gif',
|
||||||
|
'SentBy': 'User B',
|
||||||
|
'ReceivedTime': '12/9/2011 11:36:03 PM',
|
||||||
|
'Ids': {1, 2, 3},
|
||||||
|
'PK': 7,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
|
||||||
|
batch_list.add_batch(table, puts=items)
|
||||||
|
conn.batch_write_item(batch_list)
|
||||||
|
|
||||||
|
table.refresh()
|
||||||
|
table.item_count.should.equal(2)
|
||||||
|
|
||||||
|
batch_list = conn.new_batch_write_list()
|
||||||
|
batch_list.add_batch(table, deletes=[('the-key', '789')])
|
||||||
|
conn.batch_write_item(batch_list)
|
||||||
|
|
||||||
|
table.refresh()
|
||||||
|
table.item_count.should.equal(1)
|
||||||
|
|
||||||
|
|
||||||
# Batch read
|
# Batch read
|
||||||
# Batch write
|
# Batch write
|
||||||
|
Loading…
Reference in New Issue
Block a user