Merge pull request #2828 from bblommers/feature/dynamodb_item_limit
DynamoDB - Implement 1MB item limit
This commit is contained in:
		
						commit
						adf741b3ff
					
				| @ -74,6 +74,9 @@ class Item(BaseModel): | |||||||
|     def __repr__(self): |     def __repr__(self): | ||||||
|         return "Item: {0}".format(self.to_json()) |         return "Item: {0}".format(self.to_json()) | ||||||
| 
 | 
 | ||||||
|  |     def size(self): | ||||||
|  |         return sum(bytesize(key) + value.size() for key, value in self.attrs.items()) | ||||||
|  | 
 | ||||||
|     def to_json(self): |     def to_json(self): | ||||||
|         attributes = {} |         attributes = {} | ||||||
|         for attribute_key, attribute in self.attrs.items(): |         for attribute_key, attribute in self.attrs.items(): | ||||||
| @ -921,6 +924,14 @@ class Table(BaseModel): | |||||||
|                     break |                     break | ||||||
| 
 | 
 | ||||||
|         last_evaluated_key = None |         last_evaluated_key = None | ||||||
|  |         size_limit = 1000000  # DynamoDB has a 1MB size limit | ||||||
|  |         item_size = sum(res.size() for res in results) | ||||||
|  |         if item_size > size_limit: | ||||||
|  |             item_size = idx = 0 | ||||||
|  |             while item_size + results[idx].size() < size_limit: | ||||||
|  |                 item_size += results[idx].size() | ||||||
|  |                 idx += 1 | ||||||
|  |             limit = min(limit, idx) if limit else idx | ||||||
|         if limit and len(results) > limit: |         if limit and len(results) > limit: | ||||||
|             results = results[:limit] |             results = results[:limit] | ||||||
|             last_evaluated_key = {self.hash_key_attr: results[-1].hash_key} |             last_evaluated_key = {self.hash_key_attr: results[-1].hash_key} | ||||||
|  | |||||||
| @ -4219,6 +4219,44 @@ def test_gsi_verify_negative_number_order(): | |||||||
|     ) |     ) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @mock_dynamodb2 | ||||||
|  | def test_dynamodb_max_1mb_limit(): | ||||||
|  |     ddb = boto3.resource("dynamodb", region_name="eu-west-1") | ||||||
|  | 
 | ||||||
|  |     table_name = "populated-mock-table" | ||||||
|  |     table = ddb.create_table( | ||||||
|  |         TableName=table_name, | ||||||
|  |         KeySchema=[ | ||||||
|  |             {"AttributeName": "partition_key", "KeyType": "HASH"}, | ||||||
|  |             {"AttributeName": "sort_key", "KeyType": "RANGE"}, | ||||||
|  |         ], | ||||||
|  |         AttributeDefinitions=[ | ||||||
|  |             {"AttributeName": "partition_key", "AttributeType": "S"}, | ||||||
|  |             {"AttributeName": "sort_key", "AttributeType": "S"}, | ||||||
|  |         ], | ||||||
|  |         BillingMode="PAY_PER_REQUEST", | ||||||
|  |     ) | ||||||
|  | 
 | ||||||
|  |     # Populate the table | ||||||
|  |     items = [ | ||||||
|  |         { | ||||||
|  |             "partition_key": "partition_key_val",  # size=30 | ||||||
|  |             "sort_key": "sort_key_value____" + str(i),  # size=30 | ||||||
|  |         } | ||||||
|  |         for i in range(10000, 29999) | ||||||
|  |     ] | ||||||
|  |     with table.batch_writer() as batch: | ||||||
|  |         for item in items: | ||||||
|  |             batch.put_item(Item=item) | ||||||
|  | 
 | ||||||
|  |     response = table.query( | ||||||
|  |         KeyConditionExpression=Key("partition_key").eq("partition_key_val") | ||||||
|  |     ) | ||||||
|  |     # We shouldn't get everything back - the total result set is well over 1MB | ||||||
|  |     len(items).should.be.greater_than(response["Count"]) | ||||||
|  |     response["LastEvaluatedKey"].shouldnt.be(None) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def assert_raise_syntax_error(client_error, token, near): | def assert_raise_syntax_error(client_error, token, near): | ||||||
|     """ |     """ | ||||||
|     Assert whether a client_error is as expected Syntax error. Syntax error looks like: `syntax_error_template` |     Assert whether a client_error is as expected Syntax error. Syntax error looks like: `syntax_error_template` | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user