Use events_to_purge table rather than token

pull/3221/head
Erik Johnston 2018-05-15 15:48:40 +01:00
parent 8cbbfd16fb
commit 37dbee6490
1 changed files with 34 additions and 15 deletions

View File

@ -1871,6 +1871,10 @@ class EventsStore(EventsWorkerStore):
"CREATE INDEX events_to_purge_should_delete" "CREATE INDEX events_to_purge_should_delete"
" ON events_to_purge(should_delete)", " ON events_to_purge(should_delete)",
) )
txn.execute(
"CREATE INDEX events_to_purge_id"
" ON events_to_purge(event_id)",
)
# First ensure that we're not about to delete all the forward extremeties # First ensure that we're not about to delete all the forward extremeties
txn.execute( txn.execute(
@ -1927,9 +1931,8 @@ class EventsStore(EventsWorkerStore):
txn.execute( txn.execute(
"SELECT DISTINCT e.event_id FROM events_to_purge AS e" "SELECT DISTINCT e.event_id FROM events_to_purge AS e"
" INNER JOIN event_edges AS ed ON e.event_id = ed.prev_event_id" " INNER JOIN event_edges AS ed ON e.event_id = ed.prev_event_id"
" INNER JOIN events AS e2 ON e2.event_id = ed.event_id" " LEFT JOIN events_to_purge AS ep2 ON ed.event_id = ep2.event_id"
" WHERE e2.topological_ordering >= ?", " WHERE ep2.event_id IS NULL",
(topological_ordering, )
) )
new_backwards_extrems = txn.fetchall() new_backwards_extrems = txn.fetchall()
@ -1953,16 +1956,22 @@ class EventsStore(EventsWorkerStore):
# Get all state groups that are only referenced by events that are # Get all state groups that are only referenced by events that are
# to be deleted. # to be deleted.
txn.execute( # This works by first getting state groups that we may want to delete,
"SELECT state_group FROM event_to_state_groups" # joining against event_to_state_groups to get events that use that
" INNER JOIN events USING (event_id)" # state group, then left joining against events_to_purge again. Any
" WHERE state_group IN (" # state group where the left join produce *no nulls* are referenced
" SELECT DISTINCT state_group FROM events_to_purge" # only by events that are going to be purged.
" INNER JOIN event_to_state_groups USING (event_id)" txn.execute("""
" )" SELECT state_group FROM
" GROUP BY state_group HAVING MAX(topological_ordering) < ?", (
(topological_ordering, ) SELECT DISTINCT state_group FROM events_to_purge
) INNER JOIN event_to_state_groups USING (event_id)
) AS sp
INNER JOIN event_to_state_groups USING (state_group)
LEFT JOIN events_to_purge AS ep USING (event_id)
GROUP BY state_group
HAVING SUM(CASE WHEN ep.event_id IS NULL THEN 1 ELSE 0 END) = 0
""")
state_rows = txn.fetchall() state_rows = txn.fetchall()
logger.info("[purge] found %i redundant state groups", len(state_rows)) logger.info("[purge] found %i redundant state groups", len(state_rows))
@ -2109,10 +2118,20 @@ class EventsStore(EventsWorkerStore):
# #
# So, let's stick it at the end so that we don't block event # So, let's stick it at the end so that we don't block event
# persistence. # persistence.
logger.info("[purge] updating room_depth") txn.execute("""
SELECT COALESCE(MIN(depth), 0)
FROM event_backward_extremities AS eb
INNER JOIN event_edges AS eg ON eg.prev_event_id = eb.event_id
INNER JOIN events AS e ON e.event_id = eg.event_id
WHERE eb.room_id = ?
""", (room_id,))
min_depth, = txn.fetchone()
logger.info("[purge] updating room_depth to %d", min_depth)
txn.execute( txn.execute(
"UPDATE room_depth SET min_depth = ? WHERE room_id = ?", "UPDATE room_depth SET min_depth = ? WHERE room_id = ?",
(topological_ordering, room_id,) (min_depth, room_id,)
) )
# finally, drop the temp table. this will commit the txn in sqlite, # finally, drop the temp table. this will commit the txn in sqlite,