Merge pull request #2176 from matrix-org/erikj/faster_get_joined

Make get_joined_users faster
This commit is contained in:
Erik Johnston 2017-05-05 10:59:55 +01:00 committed by GitHub
commit 66f371e8b8

View file

@ -418,11 +418,37 @@ class RoomMemberStore(SQLBaseStore):
if key[0] == EventTypes.Member if key[0] == EventTypes.Member
] ]
# We check if we have any of the member event ids in the event cache
# before we ask the DB
event_map = self._get_events_from_cache(
member_event_ids,
allow_rejected=False,
)
missing_member_event_ids = []
users_in_room = {}
for event_id in member_event_ids:
ev_entry = event_map.get(event_id)
if ev_entry:
if ev_entry.event.membership == Membership.JOIN:
users_in_room[to_ascii(ev_entry.event.state_key)] = ProfileInfo(
display_name=to_ascii(
ev_entry.event.content.get("displayname", None)
),
avatar_url=to_ascii(
ev_entry.event.content.get("avatar_url", None)
),
)
else:
missing_member_event_ids.append(event_id)
if missing_member_event_ids:
rows = yield self._simple_select_many_batch( rows = yield self._simple_select_many_batch(
table="room_memberships", table="room_memberships",
column="event_id", column="event_id",
iterable=member_event_ids, iterable=missing_member_event_ids,
retcols=['user_id', 'display_name', 'avatar_url'], retcols=('user_id', 'display_name', 'avatar_url',),
keyvalues={ keyvalues={
"membership": Membership.JOIN, "membership": Membership.JOIN,
}, },
@ -430,13 +456,13 @@ class RoomMemberStore(SQLBaseStore):
desc="_get_joined_users_from_context", desc="_get_joined_users_from_context",
) )
users_in_room = { users_in_room.update({
to_ascii(row["user_id"]): ProfileInfo( to_ascii(row["user_id"]): ProfileInfo(
avatar_url=to_ascii(row["avatar_url"]), avatar_url=to_ascii(row["avatar_url"]),
display_name=to_ascii(row["display_name"]), display_name=to_ascii(row["display_name"]),
) )
for row in rows for row in rows
} })
if event is not None and event.type == EventTypes.Member: if event is not None and event.type == EventTypes.Member:
if event.membership == Membership.JOIN: if event.membership == Membership.JOIN: