docs: def cleanup_logs(connection, rconnection):
      '''
      Perform a cleanup of update logs.
      This methods analazyes what replication are configured and removes
      the update logs which we will not need in future.
      It returns the C{int} counter of performed deletes.
      '''
      source = connection.database.db_name
      statuses = yield get_replication_status(rconnection, source)
  
      counters = dict((name, max([row[0] for row in replications]))
                      for name, replications in statuses.iteritems())
      cleanup_seq = min(counters.values()) if statuses else None
      own_tag = yield connection.get_database_tag()
  
      context = dict()
      context['deletes_count'] = 0 # this is returned as a result
      context['connection'] = connection
  
      # this is cleanup for the update logs created locally
      keys = UpdateLogs.until_seq(own_tag, cleanup_seq)
      rows = yield connection.query_view(UpdateLogs, **keys)
  
      @defer.inlineCallbacks
      def clean_local(context, row):
          connection = context['connection']
          in_conflict, raw_doc = yield _check_conflict(connection, row[1])
          if not in_conflict:
              context['deletes_count'] += 1
              yield _cleanup_update_log(connection, row[2])
  
      yield itask.coiterate((clean_local(context, row) for row in rows))
  
      # this is cleanup of the update logs imported from remote partinions
      # they should be cleaned up if the document is not in conflict state
      rows = yield connection.query_view(UpdateLogs, **UpdateLogs.all())
      grouped = _group_rows(rows,
                            # (partition_tag, owner_id)
                            key=lambda row: (row[0][1], row[0][2]),
                            # (rev, doc_id)
                            value=lambda row: (row[0][3], row[2]))
  
      @defer.inlineCallbacks
      def clean_imported(context, partition_tag, owner_id, entries):
          connection = context['connection']
          if partition_tag == own_tag:
              # this is local update log, this is handeled by the code above
              return
  
          in_conflict, raw_doc = yield _check_conflict(connection, owner_id)
          last_rev = entries[-1][0]
          if (not in_conflict and
              (raw_doc.get('_deleted') or
               parse_rev(last_rev) <= parse_rev(raw_doc['_rev']))):
              for (rev, doc_id) in entries:
                  context['deletes_count'] += 1
                  yield _cleanup_update_log(connection, doc_id)
  
      yield itask.coiterate((
          clean_imported(context, partition_tag, owner_id, entries)
          for (partition_tag, owner_id), entries in grouped.iteritems()))
  
      defer.returnValue(context['deletes_count'])