summaryrefslogtreecommitdiff
path: root/boto/dynamodb/batch.py
diff options
context:
space:
mode:
authorMitch Garnaat <mitch@garnaat.com>2012-05-15 18:59:46 -0700
committerMitch Garnaat <mitch@garnaat.com>2012-05-15 18:59:46 -0700
commit1aa1133e8502ea6c95e49ac34681569df5ace46b (patch)
treea25397b20d7d409131ccfbfce5d4dca23d7e52a4 /boto/dynamodb/batch.py
parent911f42b97fdccbc55e160ec323df0cad6fe64c6b (diff)
parent6588ea270bfc9e0bb4d17263b72ee8b5255545c5 (diff)
downloadboto-2.4.0.tar.gz
Merge branch 'release-2.4.0'2.4.0
Diffstat (limited to 'boto/dynamodb/batch.py')
-rw-r--r--boto/dynamodb/batch.py142
1 files changed, 134 insertions, 8 deletions
diff --git a/boto/dynamodb/batch.py b/boto/dynamodb/batch.py
index cce32b62..2e76b92a 100644
--- a/boto/dynamodb/batch.py
+++ b/boto/dynamodb/batch.py
@@ -15,14 +15,17 @@
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
-# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
+
class Batch(object):
"""
+ Used to construct a BatchGet request.
+
:ivar table: The Table object from which the item is retrieved.
:ivar keys: A list of scalar or tuple values. Each element in the
@@ -31,8 +34,10 @@ class Batch(object):
list should be a tuple consisting of (hash_key, range_key). If
the schema for the table contains only a HashKey, each element
in the list should be a scalar value of the appropriate type
- for the table schema.
-
+ for the table schema. NOTE: The maximum number of items that
+ can be retrieved for a single operation is 100. Also, the
+ number of items retrieved is constrained by a 1 MB size limit.
+
:ivar attributes_to_get: A list of attribute names.
If supplied, only the specified attribute names will
be returned. Otherwise, all attributes will be returned.
@@ -42,7 +47,74 @@ class Batch(object):
self.table = table
self.keys = keys
self.attributes_to_get = attributes_to_get
-
+
+ def to_dict(self):
+ """
+ Convert the Batch object into the format required for Layer1.
+ """
+ batch_dict = {}
+ key_list = []
+ for key in self.keys:
+ if isinstance(key, tuple):
+ hash_key, range_key = key
+ else:
+ hash_key = key
+ range_key = None
+ k = self.table.layer2.build_key_from_values(self.table.schema,
+ hash_key, range_key)
+ key_list.append(k)
+ batch_dict['Keys'] = key_list
+ if self.attributes_to_get:
+ batch_dict['AttributesToGet'] = self.attributes_to_get
+ return batch_dict
+
+class BatchWrite(object):
+ """
+ Used to construct a BatchWrite request. Each BatchWrite object
+ represents a collection of PutItem and DeleteItem requests for
+ a single Table.
+
+ :ivar table: The Table object from which the item is retrieved.
+
+ :ivar puts: A list of :class:`boto.dynamodb.item.Item` objects
+ that you want to write to DynamoDB.
+
+ :ivar deletes: A list of scalar or tuple values. Each element in the
+ list represents one Item to delete. If the schema for the
+ table has both a HashKey and a RangeKey, each element in the
+ list should be a tuple consisting of (hash_key, range_key). If
+ the schema for the table contains only a HashKey, each element
+ in the list should be a scalar value of the appropriate type
+ for the table schema.
+ """
+
+ def __init__(self, table, puts=None, deletes=None):
+ self.table = table
+ self.puts = puts or []
+ self.deletes = deletes or []
+
+ def to_dict(self):
+ """
+ Convert the Batch object into the format required for Layer1.
+ """
+ op_list = []
+ for item in self.puts:
+ d = {'Item': self.table.layer2.dynamize_item(item)}
+ d = {'PutRequest': d}
+ op_list.append(d)
+ for key in self.deletes:
+ if isinstance(key, tuple):
+ hash_key, range_key = key
+ else:
+ hash_key = key
+ range_key = None
+ k = self.table.layer2.build_key_from_values(self.table.schema,
+ hash_key, range_key)
+ d = {'Key': k}
+ op_list.append({'DeleteRequest': d})
+ return (self.table.name, op_list)
+
+
class BatchList(list):
"""
A subclass of a list object that contains a collection of
@@ -56,7 +128,7 @@ class BatchList(list):
def add_batch(self, table, keys, attributes_to_get=None):
"""
Add a Batch to this BatchList.
-
+
:type table: :class:`boto.dynamodb.table.Table`
:param table: The Table object in which the items are contained.
@@ -67,8 +139,10 @@ class BatchList(list):
list should be a tuple consisting of (hash_key, range_key). If
the schema for the table contains only a HashKey, each element
in the list should be a scalar value of the appropriate type
- for the table schema.
-
+ for the table schema. NOTE: The maximum number of items that
+ can be retrieved for a single operation is 100. Also, the
+ number of items retrieved is constrained by a 1 MB size limit.
+
:type attributes_to_get: list
:param attributes_to_get: A list of attribute names.
If supplied, only the specified attribute names will
@@ -79,5 +153,57 @@ class BatchList(list):
def submit(self):
return self.layer2.batch_get_item(self)
-
+ def to_dict(self):
+ """
+ Convert a BatchList object into format required for Layer1.
+ """
+ d = {}
+ for batch in self:
+ d[batch.table.name] = batch.to_dict()
+ return d
+
+class BatchWriteList(list):
+ """
+ A subclass of a list object that contains a collection of
+ :class:`boto.dynamodb.batch.BatchWrite` objects.
+ """
+
+ def __init__(self, layer2):
+ list.__init__(self)
+ self.layer2 = layer2
+
+ def add_batch(self, table, puts=None, deletes=None):
+ """
+ Add a BatchWrite to this BatchWriteList.
+
+ :type table: :class:`boto.dynamodb.table.Table`
+ :param table: The Table object in which the items are contained.
+
+ :type puts: list of :class:`boto.dynamodb.item.Item` objects
+ :param puts: A list of items that you want to write to DynamoDB.
+
+ :type deletes: A list
+ :param deletes: A list of scalar or tuple values. Each element
+ in the list represents one Item to delete. If the schema
+ for the table has both a HashKey and a RangeKey, each
+ element in the list should be a tuple consisting of
+ (hash_key, range_key). If the schema for the table
+ contains only a HashKey, each element in the list should
+ be a scalar value of the appropriate type for the table
+ schema.
+ """
+ self.append(BatchWrite(table, puts, deletes))
+
+ def submit(self):
+ return self.layer2.batch_write_item(self)
+
+ def to_dict(self):
+ """
+ Convert a BatchWriteList object into format required for Layer1.
+ """
+ d = {}
+ for batch in self:
+ table_name, batch_dict = batch.to_dict()
+ d[table_name] = batch_dict
+ return d