'Get all the new events that have arrived at the server either as
new events or as backfilled events'

@cached(num_args=5, max_entries=10)
def get_all_new_events(self, last_backfill_id, last_forward_id, current_backfill_id, current_forward_id, limit):

    have_backfill_events = (last_backfill_id != current_backfill_id)
    have_forward_events = (last_forward_id != current_forward_id)
    if ((not have_backfill_events) and (not have_forward_events)):
        return defer.succeed(AllNewEventsResult([], [], [], [], []))
    def get_all_new_events_txn(txn):
        sql = 'SELECT    e.stream_ordering,    e.event_id,    e.room_id,    e.type,    state_key,    redacts    FROM    events    AS    e    LEFT    JOIN    redactions    USING    (event_id)    LEFT    JOIN    state_events    USING    (event_id)    WHERE    ?    <    stream_ordering    AND    stream_ordering    <=    ?    ORDER    BY    stream_ordering    ASC    LIMIT    ?'
        if have_forward_events:
            txn.execute(sql, (last_forward_id, current_forward_id, limit))
            new_forward_events = txn.fetchall()
            if (len(new_forward_events) == limit):
                upper_bound = new_forward_events[(-1)][0]
            else:
                upper_bound = current_forward_id
            sql = 'SELECT    event_stream_ordering,    event_id,    state_group    FROM    ex_outlier_stream    WHERE    ?    >    event_stream_ordering    AND    event_stream_ordering    >=    ?    ORDER    BY    event_stream_ordering    DESC'
            txn.execute(sql, (last_forward_id, upper_bound))
            forward_ex_outliers = txn.fetchall()
        else:
            new_forward_events = []
            forward_ex_outliers = []
        sql = 'SELECT    -e.stream_ordering,    e.event_id,    e.room_id,    e.type,    state_key,    redacts    FROM    events    AS    e    LEFT    JOIN    redactions    USING    (event_id)    LEFT    JOIN    state_events    USING    (event_id)    WHERE    ?    >    stream_ordering    AND    stream_ordering    >=    ?    ORDER    BY    stream_ordering    DESC    LIMIT    ?'
        if have_backfill_events:
            txn.execute(sql, ((- last_backfill_id), (- current_backfill_id), limit))
            new_backfill_events = txn.fetchall()
            if (len(new_backfill_events) == limit):
                upper_bound = new_backfill_events[(-1)][0]
            else:
                upper_bound = current_backfill_id
            sql = 'SELECT    -event_stream_ordering,    event_id,    state_group    FROM    ex_outlier_stream    WHERE    ?    >    event_stream_ordering    AND    event_stream_ordering    >=    ?    ORDER    BY    event_stream_ordering    DESC'
            txn.execute(sql, ((- last_backfill_id), (- upper_bound)))
            backward_ex_outliers = txn.fetchall()
        else:
            new_backfill_events = []
            backward_ex_outliers = []
        return AllNewEventsResult(new_forward_events, new_backfill_events, forward_ex_outliers, backward_ex_outliers)
    return self.runInteraction('get_all_new_events', get_all_new_events_txn)
