summary refs log tree commit diff
path: root/synapse/storage
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2017-05-31 15:23:49 +0100
committerErik Johnston <erik@matrix.org>2017-05-31 15:23:49 +0100
commit5d79d728f5f38463171f0d063713905f8cb9faec (patch)
tree67741a257e1eb8706dc41dd48f6c5bfdd7a0f6e3 /synapse/storage
parentPull max id from correct table (diff)
downloadsynapse-5d79d728f5f38463171f0d063713905f8cb9faec.tar.xz
Split out directory and search tables
Diffstat (limited to 'synapse/storage')
-rw-r--r--synapse/storage/schema/delta/42/user_dir.py25
-rw-r--r--synapse/storage/user_directory.py60
2 files changed, 56 insertions, 29 deletions
diff --git a/synapse/storage/schema/delta/42/user_dir.py b/synapse/storage/schema/delta/42/user_dir.py
index 95a7a79fd3..7e32662928 100644
--- a/synapse/storage/schema/delta/42/user_dir.py
+++ b/synapse/storage/schema/delta/42/user_dir.py
@@ -28,30 +28,33 @@ CREATE TABLE user_directory_stream_pos (
 );
 
 INSERT INTO user_directory_stream_pos (stream_id) VALUES (null);
-"""
-
 
-POSTGRES_TABLE = """
 CREATE TABLE user_directory (
     user_id TEXT NOT NULL,
     room_id TEXT NOT NULL,  -- A room_id that we know is public
     display_name TEXT,
-    avatar_url TEXT,
-    vector tsvector
+    avatar_url TEXT
 );
 
-CREATE INDEX user_directory_fts_idx ON user_directory USING gin(vector);
 CREATE INDEX user_directory_room_idx ON user_directory(room_id);
 CREATE INDEX user_directory_user_idx ON user_directory(user_id);
 """
 
 
-SQLITE_TABLE = """
-CREATE VIRTUAL TABLE user_directory
-    USING fts4 ( user_id, room_id, display_name, avatar_url, value );
+POSTGRES_TABLE = """
+CREATE TABLE user_directory_search (
+    user_id TEXT NOT NULL,
+    vector tsvector
+);
 
-CREATE INDEX user_directory_room_idx ON user_directory(room_id);
-CREATE INDEX user_directory_user_idx ON user_directory(user_id);
+CREATE INDEX user_directory_search_fts_idx ON user_directory_search USING gin(vector);
+CREATE INDEX user_directory_search_user_idx ON user_directory_search(user_id);
+"""
+
+
+SQLITE_TABLE = """
+CREATE VIRTUAL TABLE user_directory_search
+    USING fts4 ( user_id, value );
 """
 
 
diff --git a/synapse/storage/user_directory.py b/synapse/storage/user_directory.py
index 011c711ec1..b1957cb873 100644
--- a/synapse/storage/user_directory.py
+++ b/synapse/storage/user_directory.py
@@ -61,9 +61,8 @@ class UserDirectoryStore(SQLBaseStore):
             # We weight the loclpart most highly, then display name and finally
             # server name
             sql = """
-                INSERT INTO user_directory
-                    (user_id, room_id, display_name, avatar_url, vector)
-                VALUES (?,?,?,?,
+                INSERT INTO user_directory_search(user_id, vector)
+                VALUES (?,
                     setweight(to_tsvector('english', ?), 'A')
                     || setweight(to_tsvector('english', ?), 'D')
                     || setweight(to_tsvector('english', COALESCE(?, '')), 'B')
@@ -71,21 +70,19 @@ class UserDirectoryStore(SQLBaseStore):
             """
             args = (
                 (
-                    user_id, room_id, p.display_name, p.avatar_url,
-                    get_localpart_from_id(user_id), get_domain_from_id(user_id),
-                    p.display_name,
+                    user_id, get_localpart_from_id(user_id), get_domain_from_id(user_id),
+                    profile.display_name,
                 )
-                for user_id, p in users_with_profile.iteritems()
+                for user_id, profile in users_with_profile.iteritems()
             )
         elif isinstance(self.database_engine, Sqlite3Engine):
             sql = """
-                INSERT INTO user_directory
-                    (user_id, room_id, display_name, avatar_url, value)
-                VALUES (?,?,?,?,?)
+                INSERT INTO user_directory_search(user_id, value)
+                VALUES (?,?)
             """
             args = (
                 (
-                    user_id, room_id, p.display_name, p.avatar_url,
+                    user_id,
                     "%s %s" % (user_id, p.display_name,) if p.display_name else user_id
                 )
                 for user_id, p in users_with_profile.iteritems()
@@ -96,6 +93,19 @@ class UserDirectoryStore(SQLBaseStore):
 
         def _add_profiles_to_user_dir_txn(txn):
             txn.executemany(sql, args)
+            self._simple_insert_many_txn(
+                txn,
+                table="user_directory",
+                values=[
+                    {
+                        "user_id": user_id,
+                        "room_id": room_id,
+                        "display_name": profile.display_name,
+                        "avatar_url": profile.avatar_url,
+                    }
+                    for user_id, profile in users_with_profile.iteritems()
+                ]
+            )
             for user_id in users_with_profile:
                 txn.call_after(
                     self.get_user_in_directory.invalidate, (user_id,)
@@ -117,12 +127,23 @@ class UserDirectoryStore(SQLBaseStore):
 
     @defer.inlineCallbacks
     def remove_from_user_dir(self, user_id):
-        yield self._simple_delete(
-            table="user_directory",
-            keyvalues={"user_id": user_id},
-            desc="remove_from_user_dir",
+        def _remove_from_user_dir_txn(txn):
+            self._simple_delete_txn(
+                txn,
+                table="user_directory",
+                keyvalues={"user_id": user_id},
+            )
+            self._simple_delete_txn(
+                txn,
+                table="user_directory_search",
+                keyvalues={"user_id": user_id},
+            )
+            txn.call_after(
+                self.get_user_in_directory.invalidate, (user_id,)
+            )
+        return self.runInteraction(
+            "remove_from_user_dir", _remove_from_user_dir_txn,
         )
-        self.get_user_in_directory.invalidate((user_id,))
 
     def get_users_in_dir_due_to_room(self, room_id):
         """Get all user_ids that are in the room directory becuase they're
@@ -150,6 +171,7 @@ class UserDirectoryStore(SQLBaseStore):
         """
         def _delete_all_from_user_dir_txn(txn):
             txn.execute("DELETE FROM user_directory")
+            txn.execute("DELETE FROM user_directory_search")
             txn.call_after(self.get_user_in_directory.invalidate_all)
         return self.runInteraction(
             "delete_all_from_user_dir", _delete_all_from_user_dir_txn
@@ -225,7 +247,8 @@ class UserDirectoryStore(SQLBaseStore):
         if isinstance(self.database_engine, PostgresEngine):
             sql = """
                 SELECT user_id, display_name, avatar_url
-                FROM user_directory
+                FROM user_directory_search
+                INNER JOIN user_directory USING (user_id)
                 WHERE vector @@ plainto_tsquery('english', ?)
                 ORDER BY ts_rank_cd(vector, plainto_tsquery('english', ?)) DESC
                 LIMIT ?
@@ -234,7 +257,8 @@ class UserDirectoryStore(SQLBaseStore):
         elif isinstance(self.database_engine, Sqlite3Engine):
             sql = """
                 SELECT user_id, display_name, avatar_url
-                FROM user_directory
+                FROM user_directory_search
+                INNER JOIN user_directory USING (user_id)
                 WHERE value MATCH ?
                 ORDER BY rank(matchinfo(user_directory)) DESC
                 LIMIT ?