Copy-pasta QueryLatestEventsAndState code

This commit is contained in:
Neil Alexander 2020-08-27 09:48:43 +01:00
parent 1cea9f7a1c
commit 36ae29be81
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
4 changed files with 17 additions and 39 deletions

View file

@ -85,7 +85,6 @@ type roomStatements struct {
updateLatestEventNIDsStmt *sql.Stmt updateLatestEventNIDsStmt *sql.Stmt
selectRoomVersionForRoomIDStmt *sql.Stmt selectRoomVersionForRoomIDStmt *sql.Stmt
selectRoomVersionForRoomNIDStmt *sql.Stmt selectRoomVersionForRoomNIDStmt *sql.Stmt
selectStateSnapshotNIDStmt *sql.Stmt
} }
func NewPostgresRoomsTable(db *sql.DB) (tables.Rooms, error) { func NewPostgresRoomsTable(db *sql.DB) (tables.Rooms, error) {
@ -102,7 +101,6 @@ func NewPostgresRoomsTable(db *sql.DB) (tables.Rooms, error) {
{&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL}, {&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL},
{&s.selectRoomVersionForRoomIDStmt, selectRoomVersionForRoomIDSQL}, {&s.selectRoomVersionForRoomIDStmt, selectRoomVersionForRoomIDSQL},
{&s.selectRoomVersionForRoomNIDStmt, selectRoomVersionForRoomNIDSQL}, {&s.selectRoomVersionForRoomNIDStmt, selectRoomVersionForRoomNIDSQL},
{&s.selectStateSnapshotNIDStmt, selectStateSnapshotNIDSQL},
}.Prepare(db) }.Prepare(db)
} }
@ -201,14 +199,3 @@ func (s *roomStatements) SelectRoomVersionForRoomNID(
} }
return roomVersion, err 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
}

View file

@ -177,22 +177,27 @@ func (d *Database) SnapshotNIDFromEventID(
func (d *Database) StateForRoomID( func (d *Database) StateForRoomID(
ctx context.Context, roomID string, ctx context.Context, roomID string,
) ([]types.StateEntry, error) { ) ([]types.StateEntry, error) {
stateSnapshotNID, err := d.RoomsTable.SelectStateSnapshotNID(ctx, roomID) roomNID, err := d.RoomNIDExcludingStubs(ctx, roomID)
if err != nil || stateSnapshotNID == 0 { if err != nil {
// the room doesn't exist or it doesn't have state return nil, err
}
if roomNID == 0 {
return nil, nil return nil, nil
} }
stateBlockLists, err := d.StateSnapshotTable.BulkSelectStateBlockNIDs(ctx, []types.StateSnapshotNID{stateSnapshotNID}) _, stateSnapshotNID, _, err := d.LatestEventIDs(ctx, roomNID)
if err != nil { if err != nil || stateSnapshotNID == 0 {
return nil, fmt.Errorf("d.StateSnapshotTable.BulkSelectStateBlockNIDs: %w", err) return nil, err
} }
stateBlockNIDs := []types.StateBlockNID{}
for _, stateBlockList := range stateBlockLists { stateBlockNIDLists, err := d.StateBlockNIDs(ctx, []types.StateSnapshotNID{stateSnapshotNID})
stateBlockNIDs = append(stateBlockNIDs, stateBlockList.StateBlockNIDs...)
}
stateEventLists, err := d.StateBlockTable.BulkSelectStateBlockEntries(ctx, stateBlockNIDs)
if err != nil { 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{} stateEventNIDs := []types.StateEntry{}
for _, stateEventList := range stateEventLists { for _, stateEventList := range stateEventLists {

View file

@ -76,7 +76,6 @@ type roomStatements struct {
updateLatestEventNIDsStmt *sql.Stmt updateLatestEventNIDsStmt *sql.Stmt
selectRoomVersionForRoomIDStmt *sql.Stmt selectRoomVersionForRoomIDStmt *sql.Stmt
selectRoomVersionForRoomNIDStmt *sql.Stmt selectRoomVersionForRoomNIDStmt *sql.Stmt
selectStateSnapshotNIDStmt *sql.Stmt
} }
func NewSqliteRoomsTable(db *sql.DB) (tables.Rooms, error) { func NewSqliteRoomsTable(db *sql.DB) (tables.Rooms, error) {
@ -95,7 +94,6 @@ func NewSqliteRoomsTable(db *sql.DB) (tables.Rooms, error) {
{&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL}, {&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL},
{&s.selectRoomVersionForRoomIDStmt, selectRoomVersionForRoomIDSQL}, {&s.selectRoomVersionForRoomIDStmt, selectRoomVersionForRoomIDSQL},
{&s.selectRoomVersionForRoomNIDStmt, selectRoomVersionForRoomNIDSQL}, {&s.selectRoomVersionForRoomNIDStmt, selectRoomVersionForRoomNIDSQL},
{&s.selectStateSnapshotNIDStmt, selectStateSnapshotNIDSQL},
}.Prepare(db) }.Prepare(db)
} }
@ -200,14 +198,3 @@ func (s *roomStatements) SelectRoomVersionForRoomNID(
} }
return roomVersion, err 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
}

View file

@ -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 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) SelectRoomVersionForRoomID(ctx context.Context, txn *sql.Tx, roomID string) (gomatrixserverlib.RoomVersion, error)
SelectRoomVersionForRoomNID(ctx context.Context, roomNID types.RoomNID) (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 { type Transactions interface {