@@ -2127,13 +2127,33 @@ async def get_event_id_for_timestamp(
21272127            The closest event_id otherwise None if we can't find any event in 
21282128            the given direction. 
21292129        """ 
2130+         if  direction  ==  "b" :
2131+             # Find closest event *before* a given timestamp. We use descending 
2132+             # (which gives values largest to smallest) because we want the 
2133+             # largest possible timestamp *before* the given timestamp. 
2134+             comparison_operator  =  "<=" 
2135+             order  =  "DESC" 
2136+         else :
2137+             # Find closest event *after* a given timestamp. We use ascending 
2138+             # (which gives values smallest to largest) because we want the 
2139+             # closest possible timestamp *after* the given timestamp. 
2140+             comparison_operator  =  ">=" 
2141+             order  =  "ASC" 
21302142
2131-         sql_template  =  """ 
2143+         sql_template  =  f """
21322144            SELECT event_id FROM events 
21332145            LEFT JOIN rejections USING (event_id) 
21342146            WHERE 
2135-                 origin_server_ts %s ? 
2136-                 AND room_id = ? 
2147+                 room_id = ? 
2148+                 AND origin_server_ts { comparison_operator }  
2149+                 /** 
2150+                  * Make sure the event isn't an `outlier` because we have no way 
2151+                  * to later check whether it's next to a gap. `outliers` do not 
2152+                  * have entries in the `event_edges`, `event_forward_extremeties`, 
2153+                  * and `event_backward_extremities` tables to check against 
2154+                  * (used by `is_event_next_to_backward_gap` and `is_event_next_to_forward_gap`). 
2155+                  */ 
2156+                 AND outlier = ? /* False */ 
21372157                /* Make sure event is not rejected */ 
21382158                AND rejections.event_id IS NULL 
21392159            /** 
@@ -2143,27 +2163,14 @@ async def get_event_id_for_timestamp(
21432163             * Finally, we can tie-break based on when it was received on the server 
21442164             * (`stream_ordering`). 
21452165             */ 
2146-             ORDER BY origin_server_ts %s , depth %s , stream_ordering %s  
2166+             ORDER BY origin_server_ts { order } { order } { order }  
21472167            LIMIT 1; 
21482168        """ 
21492169
21502170        def  get_event_id_for_timestamp_txn (txn : LoggingTransaction ) ->  Optional [str ]:
2151-             if  direction  ==  "b" :
2152-                 # Find closest event *before* a given timestamp. We use descending 
2153-                 # (which gives values largest to smallest) because we want the 
2154-                 # largest possible timestamp *before* the given timestamp. 
2155-                 comparison_operator  =  "<=" 
2156-                 order  =  "DESC" 
2157-             else :
2158-                 # Find closest event *after* a given timestamp. We use ascending 
2159-                 # (which gives values smallest to largest) because we want the 
2160-                 # closest possible timestamp *after* the given timestamp. 
2161-                 comparison_operator  =  ">=" 
2162-                 order  =  "ASC" 
2163- 
21642171            txn .execute (
2165-                 sql_template   %  ( comparison_operator ,  order ,  order ,  order ) ,
2166-                 (timestamp ,  room_id ),
2172+                 sql_template ,
2173+                 (room_id ,  False ,  timestamp ),
21672174            )
21682175            row  =  txn .fetchone ()
21692176            if  row :
0 commit comments