Change the way we implement get_events to be less sucky

This commit is contained in:
Erik Johnston 2014-12-09 13:35:26 +00:00
parent 90d022441f
commit aa3f66cf7f
2 changed files with 22 additions and 36 deletions

View file

@ -119,25 +119,15 @@ class DataStore(RoomMemberStore, RoomStore,
@defer.inlineCallbacks @defer.inlineCallbacks
def get_event(self, event_id, allow_none=False): def get_event(self, event_id, allow_none=False):
events_dict = yield self._simple_select_one( events = yield self._get_events([event_id])
"events",
{"event_id": event_id},
[
"event_id",
"type",
"room_id",
"content",
"unrecognized_keys",
"depth",
],
allow_none=allow_none,
)
if not events_dict: if not events:
defer.returnValue(None) if allow_none:
defer.returnValue(None)
else:
raise RuntimeError("Could not find event %s" % (event_id,))
event = yield self._parse_events([events_dict]) defer.returnValue(events[0])
defer.returnValue(event[0])
@log_function @log_function
def _persist_event_txn(self, txn, event, context, backfilled, def _persist_event_txn(self, txn, event, context, backfilled,

View file

@ -461,32 +461,18 @@ class SQLBaseStore(object):
**d **d
) )
def _get_events_txn(self, txn, event_ids): def _get_events(self, event_ids):
# FIXME (erikj): This should be batched?
sql = "SELECT * FROM events WHERE event_id = ? ORDER BY rowid asc"
event_rows = []
for e_id in event_ids:
c = txn.execute(sql, (e_id,))
event_rows.extend(self.cursor_to_dict(c))
return self._parse_events_txn(txn, event_rows)
def _parse_events(self, rows):
return self.runInteraction( return self.runInteraction(
"_parse_events", self._parse_events_txn, rows "_get_events", self._get_events_txn, event_ids
) )
def _parse_events_txn(self, txn, rows): def _get_events_txn(self, txn, event_ids):
event_ids = [r["event_id"] for r in rows]
events = [] events = []
for event_id in event_ids: for e_id in event_ids:
js = self._simple_select_one_onecol_txn( js = self._simple_select_one_onecol_txn(
txn, txn,
table="event_json", table="event_json",
keyvalues={"event_id": event_id}, keyvalues={"event_id": e_id},
retcol="json", retcol="json",
allow_none=True, allow_none=True,
) )
@ -516,6 +502,16 @@ class SQLBaseStore(object):
return events return events
def _parse_events(self, rows):
return self.runInteraction(
"_parse_events", self._parse_events_txn, rows
)
def _parse_events_txn(self, txn, rows):
event_ids = [r["event_id"] for r in rows]
return self._get_events_txn(txn, event_ids)
def _has_been_redacted_txn(self, txn, event): def _has_been_redacted_txn(self, txn, event):
sql = "SELECT event_id FROM redactions WHERE redacts = ?" sql = "SELECT event_id FROM redactions WHERE redacts = ?"
txn.execute(sql, (event.event_id,)) txn.execute(sql, (event.event_id,))