← Back to team overview

launchpad-dev team mailing list archive

batching with DecoratedResultSet

 

Apparently, I even reviewed a branch once that used
DecoratedResultSet, but I never thought much about it. It turns out
that there are situations when converting a page to use batches, that
DecoratedResultSet can make life easier. If the model is providing a
modified result set as a list, batching won't provide an immediate
speed improvement, since it will be slicing a list instead of the
result set, so the query won't get limited to the batch size.

For example, if you start of with this method returning a list:

  def getResults(self):
      results = store.find((Person, SQL('(SELECT COUNT(*)...'))
      return [
          dict(person=person, membership_count=membership_count)
          for person, membership_count in results
          ]

You can turn it into a DecoratedResultSet as:

  def getResults(self):
      result = store.find((Person, SQL('(SELECT COUNT(*)...'))
      def decorator(row):
          person, membership_count = row
          return dict(person=person, membership_count=membership_count)
      return DecoratedResultSet(results, decorator)


-Edwin