aboutsummaryrefslogtreecommitdiff
path: root/roomserver/storage/sqlite3/events_table.go
diff options
context:
space:
mode:
Diffstat (limited to 'roomserver/storage/sqlite3/events_table.go')
-rw-r--r--roomserver/storage/sqlite3/events_table.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/roomserver/storage/sqlite3/events_table.go b/roomserver/storage/sqlite3/events_table.go
index 2c269bce..26401e45 100644
--- a/roomserver/storage/sqlite3/events_table.go
+++ b/roomserver/storage/sqlite3/events_table.go
@@ -44,6 +44,14 @@ const eventsSchema = `
auth_event_nids TEXT NOT NULL DEFAULT '[]',
is_rejected BOOLEAN NOT NULL DEFAULT FALSE
);
+
+-- Create an index which helps in resolving membership events (event_type_nid = 5) - (used for history visibility)
+CREATE INDEX IF NOT EXISTS roomserver_events_memberships_idx ON roomserver_events (room_nid, event_state_key_nid) WHERE (event_type_nid = 5);
+
+-- The following indexes are used by bulkSelectStateEventByNIDSQL
+CREATE INDEX IF NOT EXISTS roomserver_event_event_type_nid_idx ON roomserver_events (event_type_nid);
+CREATE INDEX IF NOT EXISTS roomserver_event_state_key_nid_idx ON roomserver_events (event_state_key_nid);
+
`
const insertEventSQL = `
@@ -120,6 +128,8 @@ const selectRoomNIDsForEventNIDsSQL = "" +
const selectEventRejectedSQL = "" +
"SELECT is_rejected FROM roomserver_events WHERE room_nid = $1 AND event_id = $2"
+const selectRoomsWithEventTypeNIDSQL = `SELECT DISTINCT room_nid FROM roomserver_events WHERE event_type_nid = $1`
+
type eventStatements struct {
db *sql.DB
insertEventStmt *sql.Stmt
@@ -135,6 +145,7 @@ type eventStatements struct {
bulkSelectStateAtEventAndReferenceStmt *sql.Stmt
bulkSelectEventIDStmt *sql.Stmt
selectEventRejectedStmt *sql.Stmt
+ selectRoomsWithEventTypeNIDStmt *sql.Stmt
//bulkSelectEventNIDStmt *sql.Stmt
//bulkSelectUnsentEventNIDStmt *sql.Stmt
//selectRoomNIDsForEventNIDsStmt *sql.Stmt
@@ -192,6 +203,7 @@ func PrepareEventsTable(db *sql.DB) (tables.Events, error) {
//{&s.bulkSelectUnsentEventNIDStmt, bulkSelectUnsentEventNIDSQL},
//{&s.selectRoomNIDForEventNIDStmt, selectRoomNIDForEventNIDSQL},
{&s.selectEventRejectedStmt, selectEventRejectedSQL},
+ {&s.selectRoomsWithEventTypeNIDStmt, selectRoomsWithEventTypeNIDSQL},
}.Prepare(db)
}
@@ -682,3 +694,25 @@ func (s *eventStatements) SelectEventRejected(
err = stmt.QueryRowContext(ctx, roomNID, eventID).Scan(&rejected)
return
}
+
+func (s *eventStatements) SelectRoomsWithEventTypeNID(
+ ctx context.Context, txn *sql.Tx, eventTypeNID types.EventTypeNID,
+) ([]types.RoomNID, error) {
+ stmt := sqlutil.TxStmt(txn, s.selectRoomsWithEventTypeNIDStmt)
+ rows, err := stmt.QueryContext(ctx, eventTypeNID)
+ defer internal.CloseAndLogIfError(ctx, rows, "SelectRoomsWithEventTypeNID: rows.close() failed")
+ if err != nil {
+ return nil, err
+ }
+
+ var roomNIDs []types.RoomNID
+ var roomNID types.RoomNID
+ for rows.Next() {
+ if err := rows.Scan(&roomNID); err != nil {
+ return nil, err
+ }
+ roomNIDs = append(roomNIDs, roomNID)
+ }
+
+ return roomNIDs, rows.Err()
+}