Inline simple_search_list/simple_search_list_txn. (#16434)
This only has a single use and is over abstracted. Inline it so that we can improve type hints.
This commit is contained in:
parent
b6cb610d50
commit
f1e43018b7
|
@ -0,0 +1 @@
|
||||||
|
Reduce memory allocations.
|
|
@ -842,7 +842,18 @@ class SearchUsersRestServlet(RestServlet):
|
||||||
logger.info("term: %s ", term)
|
logger.info("term: %s ", term)
|
||||||
|
|
||||||
ret = await self.store.search_users(term)
|
ret = await self.store.search_users(term)
|
||||||
return HTTPStatus.OK, ret
|
results = [
|
||||||
|
{
|
||||||
|
"name": name,
|
||||||
|
"password_hash": password_hash,
|
||||||
|
"is_guest": bool(is_guest),
|
||||||
|
"admin": bool(admin),
|
||||||
|
"user_type": user_type,
|
||||||
|
}
|
||||||
|
for name, password_hash, is_guest, admin, user_type in ret
|
||||||
|
]
|
||||||
|
|
||||||
|
return HTTPStatus.OK, results
|
||||||
|
|
||||||
|
|
||||||
class UserAdminServlet(RestServlet):
|
class UserAdminServlet(RestServlet):
|
||||||
|
|
|
@ -2476,68 +2476,6 @@ class DatabasePool:
|
||||||
|
|
||||||
return txn.fetchall()
|
return txn.fetchall()
|
||||||
|
|
||||||
async def simple_search_list(
|
|
||||||
self,
|
|
||||||
table: str,
|
|
||||||
term: Optional[str],
|
|
||||||
col: str,
|
|
||||||
retcols: Collection[str],
|
|
||||||
desc: str = "simple_search_list",
|
|
||||||
) -> Optional[List[Dict[str, Any]]]:
|
|
||||||
"""Executes a SELECT query on the named table, which may return zero or
|
|
||||||
more rows, returning the result as a list of dicts.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
table: the table name
|
|
||||||
term: term for searching the table matched to a column.
|
|
||||||
col: column to query term should be matched to
|
|
||||||
retcols: the names of the columns to return
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
A list of dictionaries or None.
|
|
||||||
"""
|
|
||||||
|
|
||||||
return await self.runInteraction(
|
|
||||||
desc,
|
|
||||||
self.simple_search_list_txn,
|
|
||||||
table,
|
|
||||||
term,
|
|
||||||
col,
|
|
||||||
retcols,
|
|
||||||
db_autocommit=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def simple_search_list_txn(
|
|
||||||
cls,
|
|
||||||
txn: LoggingTransaction,
|
|
||||||
table: str,
|
|
||||||
term: Optional[str],
|
|
||||||
col: str,
|
|
||||||
retcols: Iterable[str],
|
|
||||||
) -> Optional[List[Dict[str, Any]]]:
|
|
||||||
"""Executes a SELECT query on the named table, which may return zero or
|
|
||||||
more rows, returning the result as a list of dicts.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
txn: Transaction object
|
|
||||||
table: the table name
|
|
||||||
term: term for searching the table matched to a column.
|
|
||||||
col: column to query term should be matched to
|
|
||||||
retcols: the names of the columns to return
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
None if no term is given, otherwise a list of dictionaries.
|
|
||||||
"""
|
|
||||||
if term:
|
|
||||||
sql = "SELECT %s FROM %s WHERE %s LIKE ?" % (", ".join(retcols), table, col)
|
|
||||||
termvalues = ["%%" + term + "%%"]
|
|
||||||
txn.execute(sql, termvalues)
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
return cls.cursor_to_dict(txn)
|
|
||||||
|
|
||||||
|
|
||||||
def make_in_list_sql_clause(
|
def make_in_list_sql_clause(
|
||||||
database_engine: BaseDatabaseEngine, column: str, iterable: Collection[Any]
|
database_engine: BaseDatabaseEngine, column: str, iterable: Collection[Any]
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, List, Optional, Tuple, cast
|
from typing import TYPE_CHECKING, List, Optional, Tuple, Union, cast
|
||||||
|
|
||||||
from synapse.api.constants import Direction
|
from synapse.api.constants import Direction
|
||||||
from synapse.config.homeserver import HomeServerConfig
|
from synapse.config.homeserver import HomeServerConfig
|
||||||
|
@ -296,7 +296,11 @@ class DataStore(
|
||||||
"get_users_paginate_txn", get_users_paginate_txn
|
"get_users_paginate_txn", get_users_paginate_txn
|
||||||
)
|
)
|
||||||
|
|
||||||
async def search_users(self, term: str) -> Optional[List[JsonDict]]:
|
async def search_users(
|
||||||
|
self, term: str
|
||||||
|
) -> List[
|
||||||
|
Tuple[str, Optional[str], Union[int, bool], Union[int, bool], Optional[str]]
|
||||||
|
]:
|
||||||
"""Function to search users list for one or more users with
|
"""Function to search users list for one or more users with
|
||||||
the matched term.
|
the matched term.
|
||||||
|
|
||||||
|
@ -304,16 +308,38 @@ class DataStore(
|
||||||
term: search term
|
term: search term
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A list of dictionaries or None.
|
A list of tuples of name, password_hash, is_guest, admin, user_type or None.
|
||||||
"""
|
"""
|
||||||
return await self.db_pool.simple_search_list(
|
|
||||||
table="users",
|
def search_users(
|
||||||
term=term,
|
txn: LoggingTransaction,
|
||||||
col="name",
|
) -> List[
|
||||||
retcols=["name", "password_hash", "is_guest", "admin", "user_type"],
|
Tuple[str, Optional[str], Union[int, bool], Union[int, bool], Optional[str]]
|
||||||
desc="search_users",
|
]:
|
||||||
|
search_term = "%%" + term + "%%"
|
||||||
|
|
||||||
|
sql = """
|
||||||
|
SELECT name, password_hash, is_guest, admin, user_type
|
||||||
|
FROM users
|
||||||
|
WHERE name LIKE ?
|
||||||
|
"""
|
||||||
|
txn.execute(sql, (search_term,))
|
||||||
|
|
||||||
|
return cast(
|
||||||
|
List[
|
||||||
|
Tuple[
|
||||||
|
str,
|
||||||
|
Optional[str],
|
||||||
|
Union[int, bool],
|
||||||
|
Union[int, bool],
|
||||||
|
Optional[str],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
txn.fetchall(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return await self.db_pool.runInteraction("search_users", search_users)
|
||||||
|
|
||||||
|
|
||||||
def check_database_before_upgrade(
|
def check_database_before_upgrade(
|
||||||
cur: Cursor, database_engine: BaseDatabaseEngine, config: HomeServerConfig
|
cur: Cursor, database_engine: BaseDatabaseEngine, config: HomeServerConfig
|
||||||
|
|
Loading…
Reference in New Issue