summary refs log tree commit diff
path: root/synapse/storage/__init__.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-08-13 16:27:14 +0100
committerErik Johnston <erik@matrix.org>2014-08-13 18:03:41 +0100
commit336987bb8debec9dcdbe27d59ce3889b39f86dbb (patch)
treea2009c85c449021dcae897424f4a1b351f01ed2f /synapse/storage/__init__.py
parentConvert im schema to a 'one' table structure (diff)
downloadsynapse-336987bb8debec9dcdbe27d59ce3889b39f86dbb.tar.xz
Initial stab at refactoring the SQL tables, including rejigging some of the storage layer.
Diffstat (limited to 'synapse/storage/__init__.py')
-rw-r--r--synapse/storage/__init__.py113
1 files changed, 73 insertions, 40 deletions
diff --git a/synapse/storage/__init__.py b/synapse/storage/__init__.py
index 3c27428c08..4fcef45e93 100644
--- a/synapse/storage/__init__.py
+++ b/synapse/storage/__init__.py
@@ -46,50 +46,83 @@ class DataStore(RoomDataStore, RoomMemberStore, MessageStore, RoomStore,
         self.event_factory = hs.get_event_factory()
         self.hs = hs
 
+    @defer.inlineCallbacks
     def persist_event(self, event):
-        if event.type == MessageEvent.TYPE:
-            return self.store_message(
-                user_id=event.user_id,
-                room_id=event.room_id,
-                msg_id=event.msg_id,
-                content=json.dumps(event.content)
-            )
-        elif event.type == RoomMemberEvent.TYPE:
-            return self.store_room_member(
-                user_id=event.target_user_id,
-                sender=event.user_id,
-                room_id=event.room_id,
-                content=event.content,
-                membership=event.content["membership"]
-            )
+        if event.type == RoomMemberEvent.TYPE:
+            yield self._store_room_member(event)
         elif event.type == FeedbackEvent.TYPE:
-            return self.store_feedback(
-                room_id=event.room_id,
-                msg_id=event.msg_id,
-                msg_sender_id=event.msg_sender_id,
-                fb_sender_id=event.user_id,
-                fb_type=event.feedback_type,
-                content=json.dumps(event.content)
-            )
-        elif event.type == RoomTopicEvent.TYPE:
-            return self.store_room_data(
-                room_id=event.room_id,
-                etype=event.type,
-                state_key=event.state_key,
-                content=json.dumps(event.content)
-            )
+            yield self._store_feedback(event)
         elif event.type == RoomConfigEvent.TYPE:
-            if "visibility" in event.content:
-                visibility = event.content["visibility"]
-                return self.store_room_config(
-                    room_id=event.room_id,
-                    visibility=visibility
-                )
-
+            yield self._store_room_config(event)
+
+        self._store_event(event)
+
+    @defer.inlineCallbacks
+    def get_event(self, event_id):
+        events_dict = yield self._simple_select_one(
+            "events",
+            {"event_id": event_id},
+            [
+                "event_id",
+                "type",
+                "sender",
+                "room_id",
+                "content",
+                "unrecognized_keys"
+            ],
+        )
+
+        event = self._parse_event_from_row(events_dict)
+        defer.returnValue(event)
+
+    @defer.inlineCallbacks
+    def _store_event(self, event):
+        vals = {
+            "event_id": event.event_id,
+            "event_type", event.type,
+            "sender": event.user_id,
+            "room_id": event.room_id,
+            "content": event.content,
+        }
+
+        unrec = {k: v for k, v in event.get_full_dict() if k not in vals.keys()}
+        val["unrecognized_keys"] = unrec
+
+        yield self._simple_insert("events", vals)
+
+        if hasattr(event, "state_key"):
+            vals = {
+                "event_id": event.event_id,
+                "room_id": event.room_id,
+                "event_type": event.event_type,
+                "state_key": event.state_key,
+            }
+
+            if hasattr(event, "prev_state"):
+                vals["prev_state"] = event.prev_state
+
+            yield self._simple_insert("state_events", vals)
+
+            # TODO (erikj): We also need to update the current state table?
+
+    @defer.inlineCallbacks
+    def get_current_state(room_id, event_type=None, state_key="")
+        sql = (
+            "SELECT e.* FROM events as e"
+            "INNER JOIN current_state as c ON e.event_id = c.event_id "
+            "INNER JOIN state_events as s ON e.event_id = s.event_id "
+            "WHERE c.room_id = ? "
+        )
+
+        if event_type:
+            sql += " s.type = ? AND s.state_key = ? "
+            args = (room_id, event_type, state_key)
         else:
-            raise NotImplementedError(
-                "Don't know how to persist type=%s" % event.type
-            )
+            args = (room_id, )
+
+        results = yield self._execute_query(sql, *args)
+
+        defer.returnValue(
 
 
 def schema_path(schema):