summary refs log tree commit diff
path: root/synapse/storage
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-08-15 13:58:28 +0100
committerErik Johnston <erik@matrix.org>2014-08-15 13:58:28 +0100
commit114984a2361ee41005a769f6dc127c470ee08aee (patch)
treea295be465b71cbc048260b714e9fd042b4accb45 /synapse/storage
parentMerge branch 'master' of github.com:matrix-org/synapse into sql_refactor (diff)
downloadsynapse-114984a2361ee41005a769f6dc127c470ee08aee.tar.xz
Start chagning the events stream to work with the new DB schema
Diffstat (limited to 'synapse/storage')
-rw-r--r--synapse/storage/__init__.py10
-rw-r--r--synapse/storage/_base.py2
-rw-r--r--synapse/storage/roommember.py8
-rw-r--r--synapse/storage/schema/im.sql5
-rw-r--r--synapse/storage/stream.py31
5 files changed, 52 insertions, 4 deletions
diff --git a/synapse/storage/__init__.py b/synapse/storage/__init__.py
index f62cee3c39..46b9dbcbbf 100644
--- a/synapse/storage/__init__.py
+++ b/synapse/storage/__init__.py
@@ -104,7 +104,15 @@ class DataStore(RoomMemberStore, RoomStore,
 
             yield self._simple_insert("state_events", vals)
 
-            # TODO (erikj): We also need to update the current state table?
+            yield self._simple_insert(
+                "current_state_events",
+                {
+                    "event_id": event.event_id,
+                    "room_id": event.room_id,
+                    "type": event.type,
+                    "state_key": event.state_key,
+                }
+            )
 
     @defer.inlineCallbacks
     def get_current_state(self, room_id, event_type=None, state_key=""):
diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py
index c26e9a0f98..413838f798 100644
--- a/synapse/storage/_base.py
+++ b/synapse/storage/_base.py
@@ -293,7 +293,7 @@ class SQLBaseStore(object):
 
     def _parse_event_from_row(self, row_dict):
         d = copy.deepcopy({k: v for k, v in row_dict.items() if v})
-        d.update(json.loads(json.loads(row_dict["unrecognized_keys"])))
+        d.update(json.loads(row_dict["unrecognized_keys"]))
         d["content"] = json.loads(d["content"])
         del d["unrecognized_keys"]
 
diff --git a/synapse/storage/roommember.py b/synapse/storage/roommember.py
index 8c4b04f190..a0620677b9 100644
--- a/synapse/storage/roommember.py
+++ b/synapse/storage/roommember.py
@@ -63,6 +63,7 @@ class RoomMemberStore(SQLBaseStore):
             yield self._execute(None, sql, event.room_id, domain)
 
 
+    @defer.inlineCallbacks
     def get_room_member(self, user_id, room_id):
         """Retrieve the current state of a room member.
 
@@ -72,11 +73,13 @@ class RoomMemberStore(SQLBaseStore):
         Returns:
             Deferred: Results in a MembershipEvent or None.
         """
-        return self._get_members_by_dict({
+        rows = yield self._get_members_by_dict({
             "e.room_id": room_id,
             "m.user_id": user_id,
         })
 
+        defer.returnValue(rows[0] if rows else None)
+
     def get_room_members(self, room_id, membership=None):
         """Retrieve the current room member list for a room.
 
@@ -142,5 +145,8 @@ class RoomMemberStore(SQLBaseStore):
         ) % (where_clause,)
 
         rows = yield self._execute_and_decode(sql, *where_values)
+
+        logger.debug("_get_members_query Got rows %s", rows)
+
         results = [self._parse_event_from_row(r) for r in rows]
         defer.returnValue(results)
diff --git a/synapse/storage/schema/im.sql b/synapse/storage/schema/im.sql
index 9a0f2145d5..2452890ea4 100644
--- a/synapse/storage/schema/im.sql
+++ b/synapse/storage/schema/im.sql
@@ -32,7 +32,10 @@ CREATE TABLE IF NOT EXISTS state_events(
 
 CREATE TABLE IF NOT EXISTS current_state_events(
     event_id TEXT NOT NULL,
-    room_id TEXT NOT NULL
+    room_id TEXT NOT NULL,
+    type TEXT NOT NULL,
+    state_key TEXT NOT NULL,
+    CONSTRAINT uniq UNIQUE (room_id, type, state_key) ON CONFLICT REPLACE
 );
 
 CREATE TABLE IF NOT EXISTS room_memberships(
diff --git a/synapse/storage/stream.py b/synapse/storage/stream.py
index 9937239c22..c5c3770a40 100644
--- a/synapse/storage/stream.py
+++ b/synapse/storage/stream.py
@@ -34,6 +34,7 @@ class StreamStore(SQLBaseStore):
     @defer.inlineCallbacks
     def get_room_events_stream(self, user_id, from_key, to_key, room_id,
                                limit=0, with_feedback=False):
+        # TODO (erikj): Handle compressed feedback
 
         current_room_membership_sql = (
             "SELECT m.room_id FROM room_memberships as m "
@@ -69,3 +70,33 @@ class StreamStore(SQLBaseStore):
         )
 
         defer.returnValue([self._parse_event_from_row(r) for r in rows])
+
+    @defer.inlineCallbacks
+    def get_recent_events_for_room(self, room_id, limit, with_feedback=False):
+        # TODO (erikj): Handle compressed feedback
+
+        sql = (
+            "SELECT * FROM events WHERE room_id = ? "
+            "ORDER BY ordering DESC LIMIT ? "
+        )
+
+        rows = yield self._execute_and_decode(
+            sql,
+            room_id, limit
+        )
+
+        rows.reverse()  # As we selected with reverse ordering
+
+        defer.returnValue([self._parse_event_from_row(r) for r in rows])
+
+    @defer.inlineCallbacks
+    def get_room_events_max_id(self):
+        res = yield self._execute_and_decode(
+            "SELECT MAX(ordering) as m FROM events"
+        )
+
+        if not res:
+            defer.returnValue(0)
+            return
+
+        defer.returnValue(res[0]["m"])