Stop reading from event_edges.room_id. (#12914)

event_edges.room_id is implied by the event id, so there is no need to join on the room id.
This commit is contained in:
Richard van der Hoff 2022-05-31 13:51:49 +01:00 committed by GitHub
parent 1e453053cb
commit 5e17922ef7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 28 deletions

1
changelog.d/12914.misc Normal file
View File

@ -0,0 +1 @@
Preparation for database schema simplifications: stop reading from `event_edges.room_id`.

View File

@ -1318,17 +1318,14 @@ class EventFederationWorkerStore(SignatureWorkerStore, EventsWorkerStore, SQLBas
query = ( query = (
"SELECT prev_event_id FROM event_edges " "SELECT prev_event_id FROM event_edges "
"WHERE room_id = ? AND event_id = ? AND is_state = ? " "WHERE event_id = ? AND NOT is_state "
"LIMIT ?" "LIMIT ?"
) )
while front and len(event_results) < limit: while front and len(event_results) < limit:
new_front = set() new_front = set()
for event_id in front: for event_id in front:
txn.execute( txn.execute(query, (event_id, limit - len(event_results)))
query, (room_id, event_id, False, limit - len(event_results))
)
new_results = {t[0] for t in txn} - seen_events new_results = {t[0] for t in txn} - seen_events
new_front |= new_results new_front |= new_results

View File

@ -1928,23 +1928,6 @@ class EventsWorkerStore(SQLBaseStore):
LIMIT 1 LIMIT 1
""" """
# Check to see whether the event in question is already referenced
# by another event. If we don't see any edges, we're next to a
# forward gap.
forward_edge_query = """
SELECT 1 FROM event_edges
/* Check to make sure the event referencing our event in question is not rejected */
LEFT JOIN rejections ON event_edges.event_id = rejections.event_id
WHERE
event_edges.room_id = ?
AND event_edges.prev_event_id = ?
/* It's not a valid edge if the event referencing our event in
* question is rejected.
*/
AND rejections.event_id IS NULL
LIMIT 1
"""
# We consider any forward extremity as the latest in the room and # We consider any forward extremity as the latest in the room and
# not a forward gap. # not a forward gap.
# #
@ -1954,16 +1937,30 @@ class EventsWorkerStore(SQLBaseStore):
# is useless. The new latest messages will just be federated as # is useless. The new latest messages will just be federated as
# usual. # usual.
txn.execute(forward_extremity_query, (event.room_id, event.event_id)) txn.execute(forward_extremity_query, (event.room_id, event.event_id))
forward_extremities = txn.fetchall() if txn.fetchone():
if len(forward_extremities):
return False return False
# Check to see whether the event in question is already referenced
# by another event. If we don't see any edges, we're next to a
# forward gap.
forward_edge_query = """
SELECT 1 FROM event_edges
/* Check to make sure the event referencing our event in question is not rejected */
LEFT JOIN rejections ON event_edges.event_id = rejections.event_id
WHERE
event_edges.prev_event_id = ?
/* It's not a valid edge if the event referencing our event in
* question is rejected.
*/
AND rejections.event_id IS NULL
LIMIT 1
"""
# If there are no forward edges to the event in question (another # If there are no forward edges to the event in question (another
# event hasn't referenced this event in their prev_events), then we # event hasn't referenced this event in their prev_events), then we
# assume there is a forward gap in the history. # assume there is a forward gap in the history.
txn.execute(forward_edge_query, (event.room_id, event.event_id)) txn.execute(forward_edge_query, (event.event_id,))
forward_edges = txn.fetchall() if not txn.fetchone():
if not len(forward_edges):
return True return True
return False return False

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
SCHEMA_VERSION = 70 # remember to update the list below when updating SCHEMA_VERSION = 71 # remember to update the list below when updating
"""Represents the expectations made by the codebase about the database schema """Represents the expectations made by the codebase about the database schema
This should be incremented whenever the codebase changes its requirements on the This should be incremented whenever the codebase changes its requirements on the
@ -67,6 +67,9 @@ Changes in SCHEMA_VERSION = 69:
Changes in SCHEMA_VERSION = 70: Changes in SCHEMA_VERSION = 70:
- event_reference_hashes is no longer written to. - event_reference_hashes is no longer written to.
Changes in SCHEMA_VERSION = 71:
- event_edges.room_id is no longer read from.
""" """