0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-12-14 05:53:45 +01:00

MSC2326: Add background update to take previous events into account

This commit is contained in:
Brendan Abolivier 2019-10-31 14:41:28 +00:00
parent f496d25877
commit c9a1b80a74
No known key found for this signature in database
GPG key ID: 1E015C145F1916CD
2 changed files with 72 additions and 0 deletions

View file

@ -21,6 +21,7 @@ from canonicaljson import json
from twisted.internet import defer
from synapse.api.constants import LabelsField
from synapse.storage._base import make_in_list_sql_clause
from synapse.storage.background_updates import BackgroundUpdateStore
@ -85,6 +86,10 @@ class EventsBackgroundUpdatesStore(BackgroundUpdateStore):
"event_fix_redactions_bytes", self._event_fix_redactions_bytes
)
self.register_background_update_handler(
"event_store_labels", self._event_store_labels
)
@defer.inlineCallbacks
def _background_reindex_fields_sender(self, progress, batch_size):
target_min_stream_id = progress["target_min_stream_id_inclusive"]
@ -503,3 +508,53 @@ class EventsBackgroundUpdatesStore(BackgroundUpdateStore):
yield self._end_background_update("event_fix_redactions_bytes")
return 1
@defer.inlineCallbacks
def _event_store_labels(self, progress, batch_size):
"""Stores labels for events."""
last_event_id = progress.get("last_event_id", "")
def _event_store_labels_txn(txn):
txn.execute(
"""
SELECT event_id, json FROM event_json
WHERE event_id > ?
LIMIT ?
""",
(last_event_id, batch_size)
)
rows = txn.fetchall()
if not rows:
return True
for row in rows:
event_id = row["event_id"]
event_json = json.loads(row["json"])
self._simple_insert_many_txn(
txn=txn,
table="event_labels",
values=[
{
"event_id": event_id,
"label": label,
}
for label in event_json["content"].get(LabelsField)
]
)
self._background_update_progress_txn(
txn, "event_store_labels", {"last_event_id": event_id}
)
return len(rows) == batch_size
end = yield self.runInteraction(
desc="event_store_labels", func=_event_store_labels_txn
)
if end:
yield self._end_background_update("event_store_labels")
return batch_size

View file

@ -0,0 +1,17 @@
/* Copyright 2019 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
INSERT INTO background_updates (update_name, progress_json) VALUES
('event_store_labels', '{}');