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

Commit f901f8b

Browse files
authored
Require Collections as the parameters for simple_* methods. (#11580)
Instead of Iterable since the generators are not allowed due to the potential for their re-use.
1 parent 323151b commit f901f8b

File tree

3 files changed

+12
-40
lines changed

3 files changed

+12
-40
lines changed

changelog.d/11580.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add some safety checks that storage functions are used correctly.

synapse/storage/database.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from synapse.storage.background_updates import BackgroundUpdater
5656
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine, Sqlite3Engine
5757
from synapse.storage.types import Connection, Cursor
58+
from synapse.util.iterutils import batch_iter
5859

5960
if TYPE_CHECKING:
6061
from synapse.server import HomeServer
@@ -986,7 +987,7 @@ async def simple_insert_many_values(
986987
self,
987988
table: str,
988989
keys: Collection[str],
989-
values: Iterable[Iterable[Any]],
990+
values: Collection[Collection[Any]],
990991
desc: str,
991992
) -> None:
992993
"""Executes an INSERT query on the named table.
@@ -1427,7 +1428,7 @@ async def simple_select_one(
14271428
self,
14281429
table: str,
14291430
keyvalues: Dict[str, Any],
1430-
retcols: Iterable[str],
1431+
retcols: Collection[str],
14311432
allow_none: Literal[False] = False,
14321433
desc: str = "simple_select_one",
14331434
) -> Dict[str, Any]:
@@ -1438,7 +1439,7 @@ async def simple_select_one(
14381439
self,
14391440
table: str,
14401441
keyvalues: Dict[str, Any],
1441-
retcols: Iterable[str],
1442+
retcols: Collection[str],
14421443
allow_none: Literal[True] = True,
14431444
desc: str = "simple_select_one",
14441445
) -> Optional[Dict[str, Any]]:
@@ -1448,7 +1449,7 @@ async def simple_select_one(
14481449
self,
14491450
table: str,
14501451
keyvalues: Dict[str, Any],
1451-
retcols: Iterable[str],
1452+
retcols: Collection[str],
14521453
allow_none: bool = False,
14531454
desc: str = "simple_select_one",
14541455
) -> Optional[Dict[str, Any]]:
@@ -1618,7 +1619,7 @@ async def simple_select_list(
16181619
self,
16191620
table: str,
16201621
keyvalues: Optional[Dict[str, Any]],
1621-
retcols: Iterable[str],
1622+
retcols: Collection[str],
16221623
desc: str = "simple_select_list",
16231624
) -> List[Dict[str, Any]]:
16241625
"""Executes a SELECT query on the named table, which may return zero or
@@ -1681,7 +1682,7 @@ async def simple_select_many_batch(
16811682
table: str,
16821683
column: str,
16831684
iterable: Iterable[Any],
1684-
retcols: Iterable[str],
1685+
retcols: Collection[str],
16851686
keyvalues: Optional[Dict[str, Any]] = None,
16861687
desc: str = "simple_select_many_batch",
16871688
batch_size: int = 100,
@@ -1704,16 +1705,7 @@ async def simple_select_many_batch(
17041705

17051706
results: List[Dict[str, Any]] = []
17061707

1707-
if not iterable:
1708-
return results
1709-
1710-
# iterables can not be sliced, so convert it to a list first
1711-
it_list = list(iterable)
1712-
1713-
chunks = [
1714-
it_list[i : i + batch_size] for i in range(0, len(it_list), batch_size)
1715-
]
1716-
for chunk in chunks:
1708+
for chunk in batch_iter(iterable, batch_size):
17171709
rows = await self.runInteraction(
17181710
desc,
17191711
self.simple_select_many_txn,
@@ -1853,7 +1845,7 @@ def simple_select_one_txn(
18531845
txn: LoggingTransaction,
18541846
table: str,
18551847
keyvalues: Dict[str, Any],
1856-
retcols: Iterable[str],
1848+
retcols: Collection[str],
18571849
allow_none: bool = False,
18581850
) -> Optional[Dict[str, Any]]:
18591851
select_sql = "SELECT %s FROM %s WHERE %s" % (
@@ -2146,7 +2138,7 @@ async def simple_search_list(
21462138
table: str,
21472139
term: Optional[str],
21482140
col: str,
2149-
retcols: Iterable[str],
2141+
retcols: Collection[str],
21502142
desc="simple_search_list",
21512143
) -> Optional[List[Dict[str, Any]]]:
21522144
"""Executes a SELECT query on the named table, which may return zero or

synapse/storage/databases/main/pusher.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from synapse.storage.util.id_generators import StreamIdGenerator
2323
from synapse.types import JsonDict
2424
from synapse.util import json_encoder
25-
from synapse.util.caches.descriptors import cached, cachedList
25+
from synapse.util.caches.descriptors import cached
2626

2727
if TYPE_CHECKING:
2828
from synapse.server import HomeServer
@@ -196,27 +196,6 @@ async def get_if_user_has_pusher(self, user_id: str):
196196
# This only exists for the cachedList decorator
197197
raise NotImplementedError()
198198

199-
@cachedList(
200-
cached_method_name="get_if_user_has_pusher",
201-
list_name="user_ids",
202-
num_args=1,
203-
)
204-
async def get_if_users_have_pushers(
205-
self, user_ids: Iterable[str]
206-
) -> Dict[str, bool]:
207-
rows = await self.db_pool.simple_select_many_batch(
208-
table="pushers",
209-
column="user_name",
210-
iterable=user_ids,
211-
retcols=["user_name"],
212-
desc="get_if_users_have_pushers",
213-
)
214-
215-
result = {user_id: False for user_id in user_ids}
216-
result.update({r["user_name"]: True for r in rows})
217-
218-
return result
219-
220199
async def update_pusher_last_stream_ordering(
221200
self, app_id, pushkey, user_id, last_stream_ordering
222201
) -> None:

0 commit comments

Comments
 (0)