Commits

Jean-Tiare Le Bigot committed eb52062 Merge

upstream merge

Comments (0)

Files changed (4)

 ---------
 
 - log all successful queries
+- add parameter ``limit`` on ``query`` method defaulting to ``None``
 - extensive documentation
 
 Known bugs - limitations

docs/api/model.rst

File contents unchanged.

dynamodb_mapper/model.py

         ]
 
     @classmethod
-    def query(cls, hash_key_value, range_key_condition=None, consistent_read=False, reverse=False):
+    def query(cls, hash_key_value, range_key_condition=None, consistent_read=False, reverse=False, limit=None):
         """Query DynamoDB for items matching the requested key criteria.
 
         You need to supply an exact hash key value, and optionally, conditions
         :param consistent_read: If False (default), an eventually consistent
             read is performed. Set to True for strongly consistent reads.
 
+        :param reverse: Ask DynamoDB to scan the ``range_key`` in the reverse
+            order. For example, if you use dates here, the more recent element
+            will be returned first. Defaults to ``False``.
+
+        :param limit: Specify the maximum number of items to read from the table.
+            Even though Boto returns a generator, it works by batchs of 1MB.
+            using this option may help to spare some read credits. Defaults to
+            ``None``
+
         :rtype: generator
         """
         table = ConnectionBorg().get_table(cls.__table__)
                 h_value,
                 range_key_condition,
                 consistent_read=consistent_read,
-                scan_index_forward=not reverse)
+                scan_index_forward=not reverse,
+                max_results=limit)
 
         dblog.debug("Queried (%s, %s) on table %s", h_value, range_key_condition, cls.__table__)
 

dynamodb_mapper/tests/test_model.py

         from boto.dynamodb import condition
         DoomMap.query(1, condition.BEGINS_WITH(u"level"), True)
 
-        m_table.query.assert_called_with(1, condition.BEGINS_WITH(u"level"), consistent_read=True, scan_index_forward=True)
+        m_table.query.assert_called_with(1, condition.BEGINS_WITH(u"level"), consistent_read=True, scan_index_forward=True, max_results=None)
 
     @mock.patch("dynamodb_mapper.model.ConnectionBorg.get_table")
-    def test_query_reverse(self, m_get_table):
+    def test_query_reverse_limit(self, m_get_table):
         m_table = m_get_table.return_value
         m_table.query.return_value = []
 
         from boto.dynamodb import condition
-        DoomMap.query(1, condition.BEGINS_WITH(u"level"), True, reverse=True)
+        DoomMap.query(1, condition.BEGINS_WITH(u"level"), True, reverse=True, limit=42)
 
-        m_table.query.assert_called_with(1, condition.BEGINS_WITH(u"level"), consistent_read=True, scan_index_forward=False)
+        m_table.query.assert_called_with(1, condition.BEGINS_WITH(u"level"), consistent_read=True, scan_index_forward=False, max_results=42)
 
     @mock.patch("dynamodb_mapper.model.ConnectionBorg.get_table")
     def test_scan(self, m_get_table):