Merge pull request #483 from matrix-org/erikj/bulk_get_push_rules

bulk_get_push_rules should handle empty lists
pull/485/head
Erik Johnston 2016-01-13 12:35:11 +00:00
commit 4399684582
1 changed files with 13 additions and 21 deletions

View File

@ -57,43 +57,35 @@ class PushRuleStore(SQLBaseStore):
@defer.inlineCallbacks @defer.inlineCallbacks
def bulk_get_push_rules(self, user_ids): def bulk_get_push_rules(self, user_ids):
if not user_ids:
defer.returnValue({})
batch_size = 100 batch_size = 100
def f(txn, user_ids_to_fetch): def f(txn, user_ids_to_fetch):
sql = ( sql = (
"SELECT " + "SELECT pr.*"
",".join("pr."+x for x in PushRuleTable.fields) + " FROM push_rules as pr "
" FROM " + PushRuleTable.table_name + " pr " + " LEFT JOIN push_rules_enable as pre "
" LEFT JOIN " + PushRuleEnableTable.table_name + " pre " + " ON pr.user_name = pre.user_name and pr.rule_id = pre.rule_id "
" ON pr.user_name = pre.user_name and pr.rule_id = pre.rule_id " + " WHERE pr.user_name "
" WHERE pr.user_name " +
" IN (" + ",".join("?" for _ in user_ids_to_fetch) + ")" " IN (" + ",".join("?" for _ in user_ids_to_fetch) + ")"
" AND (pre.enabled is null or pre.enabled = 1)" " AND (pre.enabled is null or pre.enabled = 1)"
" ORDER BY pr.user_name, pr.priority_class DESC, pr.priority DESC" " ORDER BY pr.user_name, pr.priority_class DESC, pr.priority DESC"
) )
txn.execute(sql, user_ids_to_fetch) txn.execute(sql, user_ids_to_fetch)
return txn.fetchall() return self.cursor_to_dict(txn)
results = {} results = {}
batch_start = 0 chunks = [user_ids[i:i+batch_size] for i in xrange(0, len(user_ids), batch_size)]
while batch_start < len(user_ids): for batch_user_ids in chunks:
batch_end = min(len(user_ids), batch_size)
batch_user_ids = user_ids[batch_start:batch_end]
batch_start = batch_end
rows = yield self.runInteraction( rows = yield self.runInteraction(
"bulk_get_push_rules", f, batch_user_ids "bulk_get_push_rules", f, batch_user_ids
) )
for r in rows: for row in rows:
rawdict = { results.setdefault(row['user_name'], []).append(row)
PushRuleTable.fields[i]: r[i] for i in range(len(r))
}
if rawdict['user_name'] not in results:
results[rawdict['user_name']] = []
results[rawdict['user_name']].append(rawdict)
defer.returnValue(results) defer.returnValue(results)
@defer.inlineCallbacks @defer.inlineCallbacks