From 36ae29be81d8a7104d1ad936e614137ea295c768 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 27 Aug 2020 09:48:43 +0100 Subject: [PATCH] Copy-pasta QueryLatestEventsAndState code --- roomserver/storage/postgres/rooms_table.go | 13 ---------- roomserver/storage/shared/storage.go | 29 +++++++++++++--------- roomserver/storage/sqlite3/rooms_table.go | 13 ---------- roomserver/storage/tables/interface.go | 1 - 4 files changed, 17 insertions(+), 39 deletions(-) diff --git a/roomserver/storage/postgres/rooms_table.go b/roomserver/storage/postgres/rooms_table.go index baf9e1d83..24cee9c56 100644 --- a/roomserver/storage/postgres/rooms_table.go +++ b/roomserver/storage/postgres/rooms_table.go @@ -85,7 +85,6 @@ type roomStatements struct { updateLatestEventNIDsStmt *sql.Stmt selectRoomVersionForRoomIDStmt *sql.Stmt selectRoomVersionForRoomNIDStmt *sql.Stmt - selectStateSnapshotNIDStmt *sql.Stmt } func NewPostgresRoomsTable(db *sql.DB) (tables.Rooms, error) { @@ -102,7 +101,6 @@ func NewPostgresRoomsTable(db *sql.DB) (tables.Rooms, error) { {&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL}, {&s.selectRoomVersionForRoomIDStmt, selectRoomVersionForRoomIDSQL}, {&s.selectRoomVersionForRoomNIDStmt, selectRoomVersionForRoomNIDSQL}, - {&s.selectStateSnapshotNIDStmt, selectStateSnapshotNIDSQL}, }.Prepare(db) } @@ -201,14 +199,3 @@ func (s *roomStatements) SelectRoomVersionForRoomNID( } return roomVersion, err } - -func (s *roomStatements) SelectStateSnapshotNID( - ctx context.Context, roomID string, -) (types.StateSnapshotNID, error) { - var stateSnapshotNID types.StateSnapshotNID - err := s.selectStateSnapshotNIDStmt.QueryRowContext(ctx, roomID).Scan(&stateSnapshotNID) - if err == sql.ErrNoRows { - return 0, errors.New("room not found") - } - return stateSnapshotNID, err -} diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index a2c86ea2d..3e8b063fc 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -177,22 +177,27 @@ func (d *Database) SnapshotNIDFromEventID( func (d *Database) StateForRoomID( ctx context.Context, roomID string, ) ([]types.StateEntry, error) { - stateSnapshotNID, err := d.RoomsTable.SelectStateSnapshotNID(ctx, roomID) - if err != nil || stateSnapshotNID == 0 { - // the room doesn't exist or it doesn't have state + roomNID, err := d.RoomNIDExcludingStubs(ctx, roomID) + if err != nil { + return nil, err + } + if roomNID == 0 { return nil, nil } - stateBlockLists, err := d.StateSnapshotTable.BulkSelectStateBlockNIDs(ctx, []types.StateSnapshotNID{stateSnapshotNID}) - if err != nil { - return nil, fmt.Errorf("d.StateSnapshotTable.BulkSelectStateBlockNIDs: %w", err) + _, stateSnapshotNID, _, err := d.LatestEventIDs(ctx, roomNID) + if err != nil || stateSnapshotNID == 0 { + return nil, err } - stateBlockNIDs := []types.StateBlockNID{} - for _, stateBlockList := range stateBlockLists { - stateBlockNIDs = append(stateBlockNIDs, stateBlockList.StateBlockNIDs...) - } - stateEventLists, err := d.StateBlockTable.BulkSelectStateBlockEntries(ctx, stateBlockNIDs) + + stateBlockNIDLists, err := d.StateBlockNIDs(ctx, []types.StateSnapshotNID{stateSnapshotNID}) if err != nil { - return nil, fmt.Errorf("d.StateBlockTable.BulkSelectStateBlockEntries: %w", err) + return nil, err + } + // We've asked for exactly one snapshot from the db so we should have exactly one entry in the result. + stateBlockNIDList := stateBlockNIDLists[0] + stateEventLists, err := d.StateEntries(ctx, stateBlockNIDList.StateBlockNIDs) + if err != nil { + return nil, err } stateEventNIDs := []types.StateEntry{} for _, stateEventList := range stateEventLists { diff --git a/roomserver/storage/sqlite3/rooms_table.go b/roomserver/storage/sqlite3/rooms_table.go index 7e14efca4..712f177c2 100644 --- a/roomserver/storage/sqlite3/rooms_table.go +++ b/roomserver/storage/sqlite3/rooms_table.go @@ -76,7 +76,6 @@ type roomStatements struct { updateLatestEventNIDsStmt *sql.Stmt selectRoomVersionForRoomIDStmt *sql.Stmt selectRoomVersionForRoomNIDStmt *sql.Stmt - selectStateSnapshotNIDStmt *sql.Stmt } func NewSqliteRoomsTable(db *sql.DB) (tables.Rooms, error) { @@ -95,7 +94,6 @@ func NewSqliteRoomsTable(db *sql.DB) (tables.Rooms, error) { {&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL}, {&s.selectRoomVersionForRoomIDStmt, selectRoomVersionForRoomIDSQL}, {&s.selectRoomVersionForRoomNIDStmt, selectRoomVersionForRoomNIDSQL}, - {&s.selectStateSnapshotNIDStmt, selectStateSnapshotNIDSQL}, }.Prepare(db) } @@ -200,14 +198,3 @@ func (s *roomStatements) SelectRoomVersionForRoomNID( } return roomVersion, err } - -func (s *roomStatements) SelectStateSnapshotNID( - ctx context.Context, roomID string, -) (types.StateSnapshotNID, error) { - var stateSnapshotNID types.StateSnapshotNID - err := s.selectStateSnapshotNIDStmt.QueryRowContext(ctx, roomID).Scan(&stateSnapshotNID) - if err == sql.ErrNoRows { - return 0, errors.New("room not found") - } - return stateSnapshotNID, err -} diff --git a/roomserver/storage/tables/interface.go b/roomserver/storage/tables/interface.go index ba0da321f..47c12c2ca 100644 --- a/roomserver/storage/tables/interface.go +++ b/roomserver/storage/tables/interface.go @@ -65,7 +65,6 @@ 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) - SelectStateSnapshotNID(ctx context.Context, roomID string) (types.StateSnapshotNID, error) } type Transactions interface {