mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-21 22:03:10 -06:00
Juggle around things a bit
This commit is contained in:
parent
2d08280152
commit
93f996bda3
|
|
@ -3,6 +3,7 @@ package shared
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
|
|
@ -20,25 +21,28 @@ type roomRecentEventsUpdater struct {
|
|||
func NewRoomRecentEventsUpdater(d *Database, ctx context.Context, roomNID types.RoomNID, useTxns bool) (types.RoomRecentEventsUpdater, error) {
|
||||
txn, err := d.DB.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("d.DB.Begin: %w", err)
|
||||
}
|
||||
eventNIDs, lastEventNIDSent, currentStateSnapshotNID, err :=
|
||||
d.RoomsTable.SelectLatestEventsNIDsForUpdate(ctx, txn, roomNID)
|
||||
if err != nil {
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
txn.Rollback() // nolint: errcheck
|
||||
return nil, err
|
||||
}
|
||||
stateAndRefs, err := d.EventsTable.BulkSelectStateAtEventAndReference(ctx, txn, eventNIDs)
|
||||
if err != nil {
|
||||
txn.Rollback() // nolint: errcheck
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("d.RoomsTable.SelectLatestEventsNIDsForUpdate: %w", err)
|
||||
}
|
||||
var stateAndRefs []types.StateAtEventAndReference
|
||||
var lastEventIDSent string
|
||||
if err == nil {
|
||||
stateAndRefs, err = d.EventsTable.BulkSelectStateAtEventAndReference(ctx, txn, eventNIDs)
|
||||
if err != nil {
|
||||
txn.Rollback() // nolint: errcheck
|
||||
return nil, fmt.Errorf("d.EventsTable.BulkSelectStateAtEventAndReference: %w", err)
|
||||
}
|
||||
if lastEventNIDSent != 0 {
|
||||
lastEventIDSent, err = d.EventsTable.SelectEventID(ctx, txn, lastEventNIDSent)
|
||||
if err != nil {
|
||||
txn.Rollback() // nolint: errcheck
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("d.EventsTable.SelectEventID: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !useTxns {
|
||||
|
|
|
|||
|
|
@ -357,16 +357,16 @@ func (d *Database) StoreEvent(
|
|||
err error
|
||||
)
|
||||
|
||||
err = sqlutil.WithTransaction(d.DB, func(txn *sql.Tx) error {
|
||||
if txnAndSessionID != nil {
|
||||
if err = d.TransactionsTable.InsertTransaction(
|
||||
ctx, txn, txnAndSessionID.TransactionID,
|
||||
ctx, nil, txnAndSessionID.TransactionID,
|
||||
txnAndSessionID.SessionID, event.Sender(), event.EventID(),
|
||||
); err != nil {
|
||||
return fmt.Errorf("d.TransactionsTable.InsertTransaction: %w", err)
|
||||
return 0, types.StateAtEvent{}, nil, "", fmt.Errorf("d.TransactionsTable.InsertTransaction: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = sqlutil.WithTransaction(d.DB, func(txn *sql.Tx) error {
|
||||
// TODO: Here we should aim to have two different code paths for new rooms
|
||||
// vs existing ones.
|
||||
|
||||
|
|
@ -426,6 +426,9 @@ func (d *Database) StoreEvent(
|
|||
}
|
||||
|
||||
redactionEvent, redactedEventID, err = d.handleRedactions(ctx, txn, eventNID, event)
|
||||
if err != nil {
|
||||
return fmt.Errorf("d.handleRedactions: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -456,16 +459,13 @@ func (d *Database) assignRoomNID(
|
|||
ctx context.Context, txn *sql.Tx,
|
||||
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||
) (types.RoomNID, error) {
|
||||
// Check if we already have a numeric ID in the database.
|
||||
roomNID, err := d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
||||
// Try to insert the room. If it's ineffectual then we have the
|
||||
// room in the database already.
|
||||
roomNID, err := d.RoomsTable.InsertRoomNID(ctx, txn, roomID, roomVersion)
|
||||
if err == sql.ErrNoRows {
|
||||
// We don't have a numeric ID so insert one into the database.
|
||||
roomNID, err = d.RoomsTable.InsertRoomNID(ctx, txn, roomID, roomVersion)
|
||||
if err == sql.ErrNoRows {
|
||||
// We raced with another insert so run the select again.
|
||||
// Look up the room NID that we already have.
|
||||
roomNID, err = d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
||||
}
|
||||
}
|
||||
return roomNID, err
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue