Remove add_in_list_sql_clause

This commit is contained in:
Erik Johnston 2019-10-10 15:35:46 +01:00
parent 9d06fb9cb1
commit 3bc687508f
4 changed files with 25 additions and 34 deletions

View File

@ -20,7 +20,7 @@ import random
import sys import sys
import threading import threading
import time import time
from typing import Iterable, List, Tuple from typing import Iterable, Tuple
from six import PY2, iteritems, iterkeys, itervalues from six import PY2, iteritems, iterkeys, itervalues
from six.moves import builtins, intern, range from six.moves import builtins, intern, range
@ -1164,10 +1164,8 @@ class SQLBaseStore(object):
if not iterable: if not iterable:
return [] return []
clauses = [] clause, values = make_in_list_sql_clause(txn.database_engine, column, iterable)
values = [] clauses = [clause]
add_in_list_sql_clause(txn.database_engine, column, iterable, clauses, values)
for key, value in iteritems(keyvalues): for key, value in iteritems(keyvalues):
clauses.append("%s = ?" % (key,)) clauses.append("%s = ?" % (key,))
@ -1326,10 +1324,8 @@ class SQLBaseStore(object):
sql = "DELETE FROM %s" % table sql = "DELETE FROM %s" % table
clauses = [] clause, values = make_in_list_sql_clause(txn.database_engine, column, iterable)
values = [] clauses = [clause]
add_in_list_sql_clause(txn.database_engine, column, iterable, clauses, values)
for key, value in iteritems(keyvalues): for key, value in iteritems(keyvalues):
clauses.append("%s = ?" % (key,)) clauses.append("%s = ?" % (key,))
@ -1698,25 +1694,6 @@ def db_to_json(db_content):
raise raise
def add_in_list_sql_clause(
database_engine, column: str, iterable: Iterable, clauses: List[str], args: List
):
"""Adds an SQL clause to the given list of clauses/args that checks the
given column is in the iterable. c.f. `make_in_list_sql_clause`
Args:
database_engine
column: Name of the column
iterable: The values to check the column against.
clauses: A list to add the expanded clause to
args: A list of arguments that we append the args to.
"""
clause, new_args = make_in_list_sql_clause(database_engine, column, iterable)
clauses.append(clause)
args.extend(new_args)
def make_in_list_sql_clause( def make_in_list_sql_clause(
database_engine, column: str, iterable: Iterable database_engine, column: str, iterable: Iterable
) -> Tuple[str, Iterable]: ) -> Tuple[str, Iterable]:
@ -1736,7 +1713,7 @@ def make_in_list_sql_clause(
A tuple of SQL query and the args A tuple of SQL query and the args
""" """
if isinstance(database_engine, PostgresEngine): if database_engine.supports_using_any_list:
# This should hopefully be faster, but also makes postgres query # This should hopefully be faster, but also makes postgres query
# stats easier to understand. # stats easier to understand.
return "%s = ANY(?)" % (column,), [list(iterable)] return "%s = ANY(?)" % (column,), [list(iterable)]

View File

@ -79,6 +79,12 @@ class PostgresEngine(object):
""" """
return True return True
@property
def supports_using_any_list(self):
"""Do we support using `a = ANY(?)` and passing a list
"""
return True
def is_deadlock(self, error): def is_deadlock(self, error):
if isinstance(error, self.module.DatabaseError): if isinstance(error, self.module.DatabaseError):
# https://www.postgresql.org/docs/current/static/errcodes-appendix.html # https://www.postgresql.org/docs/current/static/errcodes-appendix.html

View File

@ -46,6 +46,12 @@ class Sqlite3Engine(object):
""" """
return self.module.sqlite_version_info >= (3, 15, 0) return self.module.sqlite_version_info >= (3, 15, 0)
@property
def supports_any_list(self):
"""Do we support using `a = ANY(?)` and passing a list
"""
return False
def check_database(self, txn): def check_database(self, txn):
pass pass

View File

@ -24,7 +24,7 @@ from canonicaljson import json
from twisted.internet import defer from twisted.internet import defer
from synapse.api.errors import SynapseError from synapse.api.errors import SynapseError
from synapse.storage._base import add_in_list_sql_clause from synapse.storage._base import make_in_list_sql_clause
from synapse.storage.engines import PostgresEngine, Sqlite3Engine from synapse.storage.engines import PostgresEngine, Sqlite3Engine
from .background_updates import BackgroundUpdateStore from .background_updates import BackgroundUpdateStore
@ -386,9 +386,10 @@ class SearchStore(SearchBackgroundUpdateStore):
# Make sure we don't explode because the person is in too many rooms. # Make sure we don't explode because the person is in too many rooms.
# We filter the results below regardless. # We filter the results below regardless.
if len(room_ids) < 500: if len(room_ids) < 500:
add_in_list_sql_clause( clause, args = make_in_list_sql_clause(
self.database_engine, "room_id", room_ids, clauses, args self.database_engine, "room_id", room_ids
) )
clauses = [clause]
local_clauses = [] local_clauses = []
for key in keys: for key in keys:
@ -494,9 +495,10 @@ class SearchStore(SearchBackgroundUpdateStore):
# Make sure we don't explode because the person is in too many rooms. # Make sure we don't explode because the person is in too many rooms.
# We filter the results below regardless. # We filter the results below regardless.
if len(room_ids) < 500: if len(room_ids) < 500:
add_in_list_sql_clause( clause, args = make_in_list_sql_clause(
self.database_engine, "room_id", room_ids, clauses, args self.database_engine, "room_id", room_ids
) )
clauses = [clause]
local_clauses = [] local_clauses = []
for key in keys: for key in keys: