aboutsummaryrefslogtreecommitdiff
path: root/roomserver/storage
diff options
context:
space:
mode:
authorKegsay <kegan@matrix.org>2020-09-01 12:40:49 +0100
committerGitHub <noreply@github.com>2020-09-01 12:40:49 +0100
commit6d79f043541f522207f298d0f585c01e74e1177d (patch)
tree7509c9f1407bd9592de75f1b76d199cc9a2c9839 /roomserver/storage
parent0ab5bccd11eea6063968c60fbdf5b36ade22da81 (diff)
Add RoomInfo metadata struct (#1367)
* Add RoomInfo struct * Remove RoomNID and replace with RoomInfo * Bugfix and remove another needless query * nil guard
Diffstat (limited to 'roomserver/storage')
-rw-r--r--roomserver/storage/interface.go10
-rw-r--r--roomserver/storage/postgres/rooms_table.go18
-rw-r--r--roomserver/storage/shared/storage.go32
-rw-r--r--roomserver/storage/sqlite3/rooms_table.go25
-rw-r--r--roomserver/storage/tables/interface.go1
5 files changed, 50 insertions, 36 deletions
diff --git a/roomserver/storage/interface.go b/roomserver/storage/interface.go
index 545885f7..5f641614 100644
--- a/roomserver/storage/interface.go
+++ b/roomserver/storage/interface.go
@@ -26,6 +26,8 @@ import (
type Database interface {
// Do we support processing input events for more than one room at a time?
SupportsConcurrentRoomInputs() bool
+ // RoomInfo returns room information for the given room ID, or nil if there is no room.
+ RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error)
// Store the room state at an event in the database
AddState(
ctx context.Context,
@@ -94,14 +96,6 @@ type Database interface {
// This is used to determine if the room event is processed/processing already.
// Returns an empty string if no such event exists.
GetTransactionEventID(ctx context.Context, transactionID string, sessionID int64, userID string) (string, error)
- // Look up the numeric ID for the room.
- // Returns 0 if the room doesn't exists.
- // Returns an error if there was a problem talking to the database.
- RoomNID(ctx context.Context, roomID string) (types.RoomNID, error)
- // RoomNIDExcludingStubs is a special variation of RoomNID that will return 0 as if the room
- // does not exist if the room has no latest events. This can happen when we've received an
- // invite over federation for a room that we don't know anything else about yet.
- RoomNIDExcludingStubs(ctx context.Context, roomID string) (types.RoomNID, error)
// Look up event references for the latest events in the room and the current state snapshot.
// Returns the latest events, the current state and the maximum depth of the latest events plus 1.
// Returns an error if there was a problem talking to the database.
diff --git a/roomserver/storage/postgres/rooms_table.go b/roomserver/storage/postgres/rooms_table.go
index 8e00cfdb..691c04ba 100644
--- a/roomserver/storage/postgres/rooms_table.go
+++ b/roomserver/storage/postgres/rooms_table.go
@@ -74,6 +74,9 @@ const selectRoomVersionForRoomIDSQL = "" +
const selectRoomVersionForRoomNIDSQL = "" +
"SELECT room_version FROM roomserver_rooms WHERE room_nid = $1"
+const selectRoomInfoSQL = "" +
+ "SELECT room_version, room_nid, state_snapshot_nid, latest_event_nids FROM roomserver_rooms WHERE room_id = $1"
+
type roomStatements struct {
insertRoomNIDStmt *sql.Stmt
selectRoomNIDStmt *sql.Stmt
@@ -82,6 +85,7 @@ type roomStatements struct {
updateLatestEventNIDsStmt *sql.Stmt
selectRoomVersionForRoomIDStmt *sql.Stmt
selectRoomVersionForRoomNIDStmt *sql.Stmt
+ selectRoomInfoStmt *sql.Stmt
}
func NewPostgresRoomsTable(db *sql.DB) (tables.Rooms, error) {
@@ -98,6 +102,7 @@ func NewPostgresRoomsTable(db *sql.DB) (tables.Rooms, error) {
{&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL},
{&s.selectRoomVersionForRoomIDStmt, selectRoomVersionForRoomIDSQL},
{&s.selectRoomVersionForRoomNIDStmt, selectRoomVersionForRoomNIDSQL},
+ {&s.selectRoomInfoStmt, selectRoomInfoSQL},
}.Prepare(db)
}
@@ -111,6 +116,19 @@ func (s *roomStatements) InsertRoomNID(
return types.RoomNID(roomNID), err
}
+func (s *roomStatements) SelectRoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error) {
+ var info types.RoomInfo
+ var latestNIDs pq.Int64Array
+ err := s.selectRoomInfoStmt.QueryRowContext(ctx, roomID).Scan(
+ &info.RoomVersion, &info.RoomNID, &info.StateSnapshotNID, &latestNIDs,
+ )
+ if err == sql.ErrNoRows {
+ return nil, nil
+ }
+ info.IsStub = len(latestNIDs) == 0
+ return &info, err
+}
+
func (s *roomStatements) SelectRoomNID(
ctx context.Context, txn *sql.Tx, roomID string,
) (types.RoomNID, error) {
diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go
index 50ab5dde..4af61be8 100644
--- a/roomserver/storage/shared/storage.go
+++ b/roomserver/storage/shared/storage.go
@@ -120,6 +120,10 @@ func (d *Database) StateEntriesForTuples(
)
}
+func (d *Database) RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error) {
+ return d.RoomsTable.SelectRoomInfo(ctx, roomID)
+}
+
func (d *Database) AddState(
ctx context.Context,
roomNID types.RoomNID,
@@ -194,34 +198,6 @@ func (d *Database) EventsFromIDs(ctx context.Context, eventIDs []string) ([]type
return d.Events(ctx, nids)
}
-func (d *Database) RoomNID(ctx context.Context, roomID string) (types.RoomNID, error) {
- if nid, ok := d.Cache.GetRoomServerRoomNID(roomID); ok {
- return nid, nil
- }
- roomNID, err := d.RoomsTable.SelectRoomNID(ctx, nil, roomID)
- if err == sql.ErrNoRows {
- return 0, nil
- }
- d.Cache.StoreRoomServerRoomNID(roomID, roomNID)
- return roomNID, err
-}
-
-func (d *Database) RoomNIDExcludingStubs(ctx context.Context, roomID string) (roomNID types.RoomNID, err error) {
- roomNID, err = d.RoomNID(ctx, roomID)
- if err != nil {
- return
- }
- latestEvents, _, err := d.RoomsTable.SelectLatestEventNIDs(ctx, nil, roomNID)
- if err != nil {
- return
- }
- if len(latestEvents) == 0 {
- roomNID = 0
- return
- }
- return
-}
-
func (d *Database) LatestEventIDs(
ctx context.Context, roomNID types.RoomNID,
) (references []gomatrixserverlib.EventReference, currentStateSnapshotNID types.StateSnapshotNID, depth int64, err error) {
diff --git a/roomserver/storage/sqlite3/rooms_table.go b/roomserver/storage/sqlite3/rooms_table.go
index 6541cc0c..fc1bcf22 100644
--- a/roomserver/storage/sqlite3/rooms_table.go
+++ b/roomserver/storage/sqlite3/rooms_table.go
@@ -64,6 +64,9 @@ const selectRoomVersionForRoomIDSQL = "" +
const selectRoomVersionForRoomNIDSQL = "" +
"SELECT room_version FROM roomserver_rooms WHERE room_nid = $1"
+const selectRoomInfoSQL = "" +
+ "SELECT room_version, room_nid, state_snapshot_nid, latest_event_nids FROM roomserver_rooms WHERE room_id = $1"
+
type roomStatements struct {
db *sql.DB
insertRoomNIDStmt *sql.Stmt
@@ -73,6 +76,7 @@ type roomStatements struct {
updateLatestEventNIDsStmt *sql.Stmt
selectRoomVersionForRoomIDStmt *sql.Stmt
selectRoomVersionForRoomNIDStmt *sql.Stmt
+ selectRoomInfoStmt *sql.Stmt
}
func NewSqliteRoomsTable(db *sql.DB) (tables.Rooms, error) {
@@ -91,9 +95,30 @@ func NewSqliteRoomsTable(db *sql.DB) (tables.Rooms, error) {
{&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL},
{&s.selectRoomVersionForRoomIDStmt, selectRoomVersionForRoomIDSQL},
{&s.selectRoomVersionForRoomNIDStmt, selectRoomVersionForRoomNIDSQL},
+ {&s.selectRoomInfoStmt, selectRoomInfoSQL},
}.Prepare(db)
}
+func (s *roomStatements) SelectRoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error) {
+ var info types.RoomInfo
+ var latestNIDsJSON string
+ err := s.selectRoomInfoStmt.QueryRowContext(ctx, roomID).Scan(
+ &info.RoomVersion, &info.RoomNID, &info.StateSnapshotNID, &latestNIDsJSON,
+ )
+ if err != nil {
+ if err == sql.ErrNoRows {
+ return nil, nil
+ }
+ return nil, err
+ }
+ var latestNIDs []int64
+ if err = json.Unmarshal([]byte(latestNIDsJSON), &latestNIDs); err != nil {
+ return nil, err
+ }
+ info.IsStub = len(latestNIDs) == 0
+ return &info, err
+}
+
func (s *roomStatements) InsertRoomNID(
ctx context.Context, txn *sql.Tx,
roomID string, roomVersion gomatrixserverlib.RoomVersion,
diff --git a/roomserver/storage/tables/interface.go b/roomserver/storage/tables/interface.go
index 47c12c2c..ca9159d0 100644
--- a/roomserver/storage/tables/interface.go
+++ b/roomserver/storage/tables/interface.go
@@ -65,6 +65,7 @@ type Rooms interface {
UpdateLatestEventNIDs(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, eventNIDs []types.EventNID, lastEventSentNID types.EventNID, stateSnapshotNID types.StateSnapshotNID) error
SelectRoomVersionForRoomID(ctx context.Context, txn *sql.Tx, roomID string) (gomatrixserverlib.RoomVersion, error)
SelectRoomVersionForRoomNID(ctx context.Context, roomNID types.RoomNID) (gomatrixserverlib.RoomVersion, error)
+ SelectRoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error)
}
type Transactions interface {