Address review and improve sql queries

pull/10419/head
Eric Eastwood 2021-07-14 14:49:15 -05:00
parent bc133969e4
commit 669da52ae2
2 changed files with 36 additions and 38 deletions

View File

@ -937,43 +937,40 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBas
# search. # search.
# Look for the prev_event_id connected to the given event_id # Look for the prev_event_id connected to the given event_id
query = ( query = """
"SELECT depth, prev_event_id FROM event_edges" SELECT depth, prev_event_id FROM event_edges
# Get the depth of the prev_event_id from the events table /* Get the depth of the prev_event_id from the events table */
" INNER JOIN events" INNER JOIN events
" ON prev_event_id = events.event_id" ON prev_event_id = events.event_id
# Find an event which matches the given event_id /* Find an event which matches the given event_id */
" WHERE event_edges.event_id = ?" WHERE event_edges.event_id = ?
" AND event_edges.is_state = ?" AND event_edges.is_state = ?
" LIMIT ?" LIMIT ?
) """
# Look for the "insertion" events connected to the given event_id # Look for the "insertion" events connected to the given event_id
# TODO: Do we need to worry about selecting only from the given room_id? The other query above doesn't connected_insertion_event_query = """
connected_insertion_event_query = ( SELECT e.depth, i.event_id FROM insertion_event_edges AS i
"SELECT e.depth, i.event_id FROM insertion_event_edges AS i" /* Get the depth of the insertion event from the events table */
# Get the depth of the insertion event from the events table INNER JOIN events AS e USING (event_id)
" INNER JOIN events AS e" /* Find an insertion event which points via prev_events to the given event_id */
" ON e.event_id = i.event_id" WHERE i.insertion_prev_event_id = ?
# Find an insertion event which points via prev_events to the given event_id LIMIT ?
" WHERE i.insertion_prev_event_id = ?" """
" LIMIT ?"
)
# Find any chunk connections of a given insertion event # Find any chunk connections of a given insertion event
# TODO: Do we need to worry about selecting only from the given room_id? The other query above doesn't chunk_connection_query = """
chunk_connection_query = ( SELECT e.depth, c.event_id FROM insertion_events AS i
"SELECT e.depth, c.event_id FROM insertion_events AS i" /* Find the chunk that connects to the given insertion event */
# Find the chunk that connects to the given insertion event INNER JOIN chunk_edges AS c
" INNER JOIN chunk_edges AS c" ON i.next_chunk_id = c.chunk_id
" ON i.next_chunk_id = c.chunk_id" /* Get the depth of the chunk start event from the events table */
# Get the depth of the chunk start event from the events table INNER JOIN events AS e
" INNER JOIN events AS e" ON e.event_id = c.event_id
" ON e.event_id = c.event_id" /* Find an insertion event which matches the given event_id */
# Find an insertion event which matches the given event_id WHERE i.event_id = ?
" WHERE i.event_id = ?" LIMIT ?
" LIMIT ?" """
)
# In a PriorityQueue, the lowest valued entries are retrieved first. # In a PriorityQueue, the lowest valued entries are retrieved first.
# We're using depth as the priority in the queue. # We're using depth as the priority in the queue.
@ -1008,7 +1005,7 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBas
txn.execute( txn.execute(
connected_insertion_event_query, (event_id, limit - len(event_results)) connected_insertion_event_query, (event_id, limit - len(event_results))
) )
connected_insertion_event_id_results = list(txn) connected_insertion_event_id_results = txn.fetchall()
logger.debug( logger.debug(
"_get_backfill_events: connected_insertion_event_query %s", "_get_backfill_events: connected_insertion_event_query %s",
connected_insertion_event_id_results, connected_insertion_event_id_results,
@ -1021,7 +1018,7 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBas
txn.execute( txn.execute(
chunk_connection_query, (row[1], limit - len(event_results)) chunk_connection_query, (row[1], limit - len(event_results))
) )
chunk_start_event_id_results = list(txn) chunk_start_event_id_results = txn.fetchall()
logger.debug( logger.debug(
"_get_backfill_events: chunk_start_event_id_results %s", "_get_backfill_events: chunk_start_event_id_results %s",
chunk_start_event_id_results, chunk_start_event_id_results,
@ -1031,7 +1028,7 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBas
queue.put((-row[0], row[1])) queue.put((-row[0], row[1]))
txn.execute(query, (event_id, False, limit - len(event_results))) txn.execute(query, (event_id, False, limit - len(event_results)))
prev_event_id_results = list(txn) prev_event_id_results = txn.fetchall()
logger.debug( logger.debug(
"_get_backfill_events: prev_event_ids %s", prev_event_id_results "_get_backfill_events: prev_event_ids %s", prev_event_id_results
) )

View File

@ -1760,7 +1760,8 @@ class PersistEventsStore:
txn.call_after(self.store.get_applicable_edit.invalidate, (parent_id,)) txn.call_after(self.store.get_applicable_edit.invalidate, (parent_id,))
def _handle_insertion_event(self, txn: LoggingTransaction, event: EventBase): def _handle_insertion_event(self, txn: LoggingTransaction, event: EventBase):
"""Handles keeping track of insertion events and edges/connections """Handles keeping track of insertion events and edges/connections.
Part of MSC2716.
Args: Args:
txn: The database transaction object txn: The database transaction object
@ -1805,7 +1806,7 @@ class PersistEventsStore:
def _handle_chunk_id(self, txn: LoggingTransaction, event: EventBase): def _handle_chunk_id(self, txn: LoggingTransaction, event: EventBase):
"""Handles inserting the chunk edges/connections between the event at the """Handles inserting the chunk edges/connections between the event at the
start of a chunk and an insertion event start of a chunk and an insertion event. Part of MSC2716.
Args: Args:
txn: The database transaction object txn: The database transaction object