Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 44bb3f0

Browse files
committed
Remove create_event fallback for room_versions
See https://github.com/matrix-org/synapse/pull/10245/files#r677641879
1 parent 465b3d8 commit 44bb3f0

File tree

2 files changed

+16
-139
lines changed

2 files changed

+16
-139
lines changed

synapse/storage/databases/main/events_worker.py

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from synapse.replication.tcp.streams import BackfillStream
5252
from synapse.replication.tcp.streams.events import EventsStream
5353
from synapse.storage._base import SQLBaseStore, db_to_json, make_in_list_sql_clause
54-
from synapse.storage.database import DatabasePool, LoggingTransaction
54+
from synapse.storage.database import DatabasePool
5555
from synapse.storage.engines import PostgresEngine
5656
from synapse.storage.util.id_generators import MultiWriterIdGenerator, StreamIdGenerator
5757
from synapse.storage.util.sequence import build_sequence_generator
@@ -207,82 +207,6 @@ async def get_received_ts(self, event_id: str) -> Optional[int]:
207207
desc="get_received_ts",
208208
)
209209

210-
# Inform mypy that if allow_none is False (the default) then get_event
211-
# always returns an EventBase.
212-
@overload
213-
def get_event_txn(
214-
self,
215-
event_id: str,
216-
allow_rejected: bool = False,
217-
allow_none: Literal[False] = False,
218-
) -> EventBase:
219-
...
220-
221-
@overload
222-
def get_event_txn(
223-
self,
224-
event_id: str,
225-
allow_rejected: bool = False,
226-
allow_none: Literal[True] = False,
227-
) -> Optional[EventBase]:
228-
...
229-
230-
def get_event_txn(
231-
self,
232-
txn: LoggingTransaction,
233-
event_id: str,
234-
allow_rejected: bool = False,
235-
allow_none: bool = False,
236-
) -> Optional[EventBase]:
237-
"""Get an event from the database by event_id.
238-
239-
Args:
240-
txn: Transaction object
241-
242-
event_id: The event_id of the event to fetch
243-
244-
get_prev_content: If True and event is a state event,
245-
include the previous states content in the unsigned field.
246-
247-
allow_rejected: If True, return rejected events. Otherwise,
248-
behave as per allow_none.
249-
250-
allow_none: If True, return None if no event found, if
251-
False throw a NotFoundError
252-
253-
check_room_id: if not None, check the room of the found event.
254-
If there is a mismatch, behave as per allow_none.
255-
256-
Returns:
257-
The event, or None if the event was not found and allow_none=True
258-
259-
260-
Raises:
261-
NotFoundError: if the event_id was not found and allow_none=False
262-
"""
263-
event_map = self._fetch_event_rows(txn, [event_id])
264-
event_info = event_map[event_id]
265-
if event_info is None and not allow_none:
266-
raise NotFoundError("Could not find event %s" % (event_id,))
267-
268-
rejected_reason = event_info["rejected_reason"]
269-
if not allow_rejected and rejected_reason:
270-
return
271-
272-
d = db_to_json(event_info["json"])
273-
internal_metadata = db_to_json(event_info["internal_metadata"])
274-
room_version_id = event_info["room_version_id"]
275-
room_version = KNOWN_ROOM_VERSIONS.get(room_version_id)
276-
277-
event = make_event_from_dict(
278-
event_dict=d,
279-
room_version=room_version,
280-
internal_metadata_dict=internal_metadata,
281-
rejected_reason=rejected_reason,
282-
)
283-
284-
return event
285-
286210
# Inform mypy that if allow_none is False (the default) then get_event
287211
# always returns an EventBase.
288212
@overload

synapse/storage/databases/main/state.py

Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ def __init__(self, database: DatabasePool, db_conn, hs):
5858

5959
async def get_room_version(self, room_id: str) -> RoomVersion:
6060
"""Get the room_version of a given room
61-
6261
Raises:
6362
NotFoundError: if the room is unknown
64-
6563
UnsupportedRoomVersionError: if the room uses an unknown room version.
6664
Typically this happens if support for the room's version has been
6765
removed from Synapse.
@@ -76,14 +74,11 @@ def get_room_version_txn(
7674
self, txn: LoggingTransaction, room_id: str
7775
) -> RoomVersion:
7876
"""Get the room_version of a given room
79-
8077
Args:
8178
txn: Transaction object
8279
room_id: The room_id of the room you are trying to get the version for
83-
8480
Raises:
8581
NotFoundError: if the room is unknown
86-
8782
UnsupportedRoomVersionError: if the room uses an unknown room version.
8883
Typically this happens if support for the room's version has been
8984
removed from Synapse.
@@ -102,7 +97,6 @@ def get_room_version_txn(
10297
@cached(max_entries=10000)
10398
async def get_room_version_id(self, room_id: str) -> str:
10499
"""Get the room_version of a given room
105-
106100
Raises:
107101
NotFoundError: if the room is unknown
108102
"""
@@ -114,11 +108,9 @@ async def get_room_version_id(self, room_id: str) -> str:
114108

115109
def get_room_version_id_txn(self, txn: LoggingTransaction, room_id: str) -> str:
116110
"""Get the room_version of a given room
117-
118111
Args:
119112
txn: Transaction object
120113
room_id: The room_id of the room you are trying to get the version for
121-
122114
Raises:
123115
NotFoundError: if the room is unknown
124116
"""
@@ -138,12 +130,10 @@ def get_room_version_id_txn(self, txn: LoggingTransaction, room_id: str) -> str:
138130
allow_none=True,
139131
)
140132

141-
if room_version is not None:
142-
return room_version
133+
if room_version is None:
134+
raise NotFoundError("Could not room_version for %s" % (room_id,))
143135

144-
# Retrieve the room's create event
145-
create_event = self.get_create_event_for_room_txn(txn, room_id)
146-
return create_event.content.get("room_version", "1")
136+
return room_version
147137

148138
async def get_room_predecessor(self, room_id: str) -> Optional[dict]:
149139
"""Get the predecessor of an upgraded room if it exists.
@@ -188,37 +178,15 @@ async def get_create_event_for_room(self, room_id: str) -> EventBase:
188178
Raises:
189179
NotFoundError if the room is unknown
190180
"""
191-
return await self.db_pool.runInteraction(
192-
"get_create_event_for_room_txn",
193-
self.get_create_event_for_room_txn,
194-
room_id,
195-
)
196-
197-
def get_create_event_for_room_txn(
198-
self, txn: LoggingTransaction, room_id: str
199-
) -> EventBase:
200-
"""Get the create state event for a room.
201-
202-
Args:
203-
txn: Transaction object
204-
room_id: The room ID.
205-
206-
Returns:
207-
The room creation event.
208-
209-
Raises:
210-
NotFoundError if the room is unknown
211-
"""
212-
213-
state_ids = self.get_current_state_ids_txn(txn, room_id)
181+
state_ids = await self.get_current_state_ids(room_id)
214182
create_id = state_ids.get((EventTypes.Create, ""))
215183

216184
# If we can't find the create event, assume we've hit a dead end
217185
if not create_id:
218186
raise NotFoundError("Unknown room %s" % (room_id,))
219187

220188
# Retrieve the room's create event and return
221-
create_event = self.get_event_txn(txn, create_id)
189+
create_event = await self.get_event(create_id)
222190
return create_event
223191

224192
@cached(max_entries=100000, iterable=True)
@@ -233,35 +201,20 @@ async def get_current_state_ids(self, room_id: str) -> StateMap[str]:
233201
The current state of the room.
234202
"""
235203

236-
return await self.db_pool.runInteraction(
237-
"get_current_state_ids_txn",
238-
self.get_current_state_ids_txn,
239-
room_id,
240-
)
241-
242-
def get_current_state_ids_txn(
243-
self, txn: LoggingTransaction, room_id: str
244-
) -> StateMap[str]:
245-
"""Get the current state event ids for a room based on the
246-
current_state_events table.
204+
def _get_current_state_ids_txn(txn):
205+
txn.execute(
206+
"""SELECT type, state_key, event_id FROM current_state_events
207+
WHERE room_id = ?
208+
""",
209+
(room_id,),
210+
)
247211

248-
Args:
249-
txn: Transaction object
250-
room_id: The room to get the state IDs of.
212+
return {(intern_string(r[0]), intern_string(r[1])): r[2] for r in txn}
251213

252-
Returns:
253-
The current state of the room.
254-
"""
255-
256-
txn.execute(
257-
"""SELECT type, state_key, event_id FROM current_state_events
258-
WHERE room_id = ?
259-
""",
260-
(room_id,),
214+
return await self.db_pool.runInteraction(
215+
"get_current_state_ids", _get_current_state_ids_txn
261216
)
262217

263-
return {(intern_string(r[0]), intern_string(r[1])): r[2] for r in txn}
264-
265218
# FIXME: how should this be cached?
266219
async def get_filtered_current_state_ids(
267220
self, room_id: str, state_filter: Optional[StateFilter] = None

0 commit comments

Comments
 (0)