'Update any outliers with new event info.
This turns outliers into ex-outliers (unless the new event was
rejected).
Args:
txn (twisted.enterprise.adbapi.Connection): db connection
events_and_contexts (list[(EventBase, EventContext)]): events
we are persisting
Returns:
list[(EventBase, EventContext)] new list, without events which
are already in the events table.'

def _update_outliers_txn(self, txn, events_and_contexts):

    txn.execute(('SELECT    event_id,    outlier    FROM    events    WHERE    event_id    in    (%s)' % (','.join((['?'] * len(events_and_contexts))),)), [event.event_id for (event, _) in events_and_contexts])
    have_persisted = {event_id: outlier for (event_id, outlier) in txn}
    to_remove = set()
    for (event, context) in events_and_contexts:
        if (event.event_id not in have_persisted):
            continue
        to_remove.add(event)
        if context.rejected:
            continue
        outlier_persisted = have_persisted[event.event_id]
        if ((not event.internal_metadata.is_outlier()) and outlier_persisted):
            try:
                self._store_mult_state_groups_txn(txn, ((event, context),))
            except Exception:
                logger.exception('')
                raise
            metadata_json = encode_json(event.internal_metadata.get_dict()).decode('UTF-8')
            sql = 'UPDATE    event_json    SET    internal_metadata    =    ?    WHERE    event_id    =    ?'
            txn.execute(sql, (metadata_json, event.event_id))
            stream_order = event.internal_metadata.stream_ordering
            state_group_id = context.state_group
            self._simple_insert_txn(txn, table='ex_outlier_stream', values={'event_stream_ordering': stream_order, 'event_id': event.event_id, 'state_group': state_group_id})
            sql = 'UPDATE    events    SET    outlier    =    ?    WHERE    event_id    =    ?'
            txn.execute(sql, (False, event.event_id))
            self._update_backward_extremeties(txn, [event])
    return [ec for ec in events_and_contexts if (ec[0] not in to_remove)]
