mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 10:33:11 -06:00
Remove PrevEvents()
This commit is contained in:
parent
0489d16f95
commit
a9dffcb23a
|
|
@ -57,7 +57,7 @@ const insertPreviousEventSQL = "" +
|
||||||
// This should only be done while holding a "FOR UPDATE" lock on the row in the rooms table for this room.
|
// This should only be done while holding a "FOR UPDATE" lock on the row in the rooms table for this room.
|
||||||
const selectPreviousEventExistsSQL = "" +
|
const selectPreviousEventExistsSQL = "" +
|
||||||
"SELECT 1 FROM roomserver_previous_events" +
|
"SELECT 1 FROM roomserver_previous_events" +
|
||||||
" WHERE previous_event_id = $1 AND previous_reference_sha256 = $2"
|
" WHERE previous_event_id = $1"
|
||||||
|
|
||||||
type previousEventStatements struct {
|
type previousEventStatements struct {
|
||||||
insertPreviousEventStmt *sql.Stmt
|
insertPreviousEventStmt *sql.Stmt
|
||||||
|
|
@ -82,12 +82,11 @@ func (s *previousEventStatements) InsertPreviousEvent(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
txn *sql.Tx,
|
txn *sql.Tx,
|
||||||
previousEventID string,
|
previousEventID string,
|
||||||
previousEventReferenceSHA256 []byte,
|
|
||||||
eventNID types.EventNID,
|
eventNID types.EventNID,
|
||||||
) error {
|
) error {
|
||||||
stmt := sqlutil.TxStmt(txn, s.insertPreviousEventStmt)
|
stmt := sqlutil.TxStmt(txn, s.insertPreviousEventStmt)
|
||||||
_, err := stmt.ExecContext(
|
_, err := stmt.ExecContext(
|
||||||
ctx, previousEventID, previousEventReferenceSHA256, int64(eventNID),
|
ctx, previousEventID, []byte(""), int64(eventNID),
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -95,9 +94,9 @@ func (s *previousEventStatements) InsertPreviousEvent(
|
||||||
// Check if the event reference exists
|
// Check if the event reference exists
|
||||||
// Returns sql.ErrNoRows if the event reference doesn't exist.
|
// Returns sql.ErrNoRows if the event reference doesn't exist.
|
||||||
func (s *previousEventStatements) SelectPreviousEventExists(
|
func (s *previousEventStatements) SelectPreviousEventExists(
|
||||||
ctx context.Context, txn *sql.Tx, eventID string, eventReferenceSHA256 []byte,
|
ctx context.Context, txn *sql.Tx, eventID string,
|
||||||
) error {
|
) error {
|
||||||
var ok int64
|
var ok int64
|
||||||
stmt := sqlutil.TxStmt(txn, s.selectPreviousEventExistsStmt)
|
stmt := sqlutil.TxStmt(txn, s.selectPreviousEventExistsStmt)
|
||||||
return stmt.QueryRowContext(ctx, eventID, eventReferenceSHA256).Scan(&ok)
|
return stmt.QueryRowContext(ctx, eventID).Scan(&ok)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,7 @@ func (u *RoomUpdater) CurrentStateSnapshotNID() types.StateSnapshotNID {
|
||||||
func (u *RoomUpdater) StorePreviousEvents(eventNID types.EventNID, previousEventReferences []gomatrixserverlib.EventReference) error {
|
func (u *RoomUpdater) StorePreviousEvents(eventNID types.EventNID, previousEventReferences []gomatrixserverlib.EventReference) error {
|
||||||
return u.d.Writer.Do(u.d.DB, u.txn, func(txn *sql.Tx) error {
|
return u.d.Writer.Do(u.d.DB, u.txn, func(txn *sql.Tx) error {
|
||||||
for _, ref := range previousEventReferences {
|
for _, ref := range previousEventReferences {
|
||||||
if err := u.d.PrevEventsTable.InsertPreviousEvent(u.ctx, txn, ref.EventID, ref.EventSHA256, eventNID); err != nil {
|
if err := u.d.PrevEventsTable.InsertPreviousEvent(u.ctx, txn, ref.EventID, eventNID); err != nil {
|
||||||
return fmt.Errorf("u.d.PrevEventsTable.InsertPreviousEvent: %w", err)
|
return fmt.Errorf("u.d.PrevEventsTable.InsertPreviousEvent: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -204,7 +204,7 @@ func (u *RoomUpdater) EventsFromIDs(ctx context.Context, roomInfo *types.RoomInf
|
||||||
|
|
||||||
// IsReferenced implements types.RoomRecentEventsUpdater
|
// IsReferenced implements types.RoomRecentEventsUpdater
|
||||||
func (u *RoomUpdater) IsReferenced(eventReference gomatrixserverlib.EventReference) (bool, error) {
|
func (u *RoomUpdater) IsReferenced(eventReference gomatrixserverlib.EventReference) (bool, error) {
|
||||||
err := u.d.PrevEventsTable.SelectPreviousEventExists(u.ctx, u.txn, eventReference.EventID, eventReference.EventSHA256)
|
err := u.d.PrevEventsTable.SelectPreviousEventExists(u.ctx, u.txn, eventReference.EventID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -762,7 +762,7 @@ func (d *EventDatabase) StoreEvent(
|
||||||
return fmt.Errorf("d.EventJSONTable.InsertEventJSON: %w", err)
|
return fmt.Errorf("d.EventJSONTable.InsertEventJSON: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if prevEvents := event.PrevEvents(); len(prevEvents) > 0 {
|
if prevEvents := event.PrevEventIDs(); len(prevEvents) > 0 {
|
||||||
// Create an updater - NB: on sqlite this WILL create a txn as we are directly calling the shared DB form of
|
// Create an updater - NB: on sqlite this WILL create a txn as we are directly calling the shared DB form of
|
||||||
// GetLatestEventsForUpdate - not via the SQLiteDatabase form which has `nil` txns. This
|
// GetLatestEventsForUpdate - not via the SQLiteDatabase form which has `nil` txns. This
|
||||||
// function only does SELECTs though so the created txn (at this point) is just a read txn like
|
// function only does SELECTs though so the created txn (at this point) is just a read txn like
|
||||||
|
|
@ -770,8 +770,8 @@ func (d *EventDatabase) StoreEvent(
|
||||||
// to do writes however then this will need to go inside `Writer.Do`.
|
// to do writes however then this will need to go inside `Writer.Do`.
|
||||||
|
|
||||||
// The following is a copy of RoomUpdater.StorePreviousEvents
|
// The following is a copy of RoomUpdater.StorePreviousEvents
|
||||||
for _, ref := range prevEvents {
|
for _, eventID := range prevEvents {
|
||||||
if err = d.PrevEventsTable.InsertPreviousEvent(ctx, txn, ref.EventID, ref.EventSHA256, eventNID); err != nil {
|
if err = d.PrevEventsTable.InsertPreviousEvent(ctx, txn, eventID, eventNID); err != nil {
|
||||||
return fmt.Errorf("u.d.PrevEventsTable.InsertPreviousEvent: %w", err)
|
return fmt.Errorf("u.d.PrevEventsTable.InsertPreviousEvent: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,20 +47,20 @@ const previousEventSchema = `
|
||||||
// The lock is necessary to avoid data races when checking whether an event is already referenced by another event.
|
// The lock is necessary to avoid data races when checking whether an event is already referenced by another event.
|
||||||
const insertPreviousEventSQL = `
|
const insertPreviousEventSQL = `
|
||||||
INSERT OR REPLACE INTO roomserver_previous_events
|
INSERT OR REPLACE INTO roomserver_previous_events
|
||||||
(previous_event_id, previous_reference_sha256, event_nids)
|
(previous_event_id, event_nids)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2)
|
||||||
`
|
`
|
||||||
|
|
||||||
const selectPreviousEventNIDsSQL = `
|
const selectPreviousEventNIDsSQL = `
|
||||||
SELECT event_nids FROM roomserver_previous_events
|
SELECT event_nids FROM roomserver_previous_events
|
||||||
WHERE previous_event_id = $1 AND previous_reference_sha256 = $2
|
WHERE previous_event_id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
// Check if the event is referenced by another event in the table.
|
// Check if the event is referenced by another event in the table.
|
||||||
// This should only be done while holding a "FOR UPDATE" lock on the row in the rooms table for this room.
|
// This should only be done while holding a "FOR UPDATE" lock on the row in the rooms table for this room.
|
||||||
const selectPreviousEventExistsSQL = `
|
const selectPreviousEventExistsSQL = `
|
||||||
SELECT 1 FROM roomserver_previous_events
|
SELECT 1 FROM roomserver_previous_events
|
||||||
WHERE previous_event_id = $1 AND previous_reference_sha256 = $2
|
WHERE previous_event_id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
type previousEventStatements struct {
|
type previousEventStatements struct {
|
||||||
|
|
@ -91,13 +91,12 @@ func (s *previousEventStatements) InsertPreviousEvent(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
txn *sql.Tx,
|
txn *sql.Tx,
|
||||||
previousEventID string,
|
previousEventID string,
|
||||||
previousEventReferenceSHA256 []byte,
|
|
||||||
eventNID types.EventNID,
|
eventNID types.EventNID,
|
||||||
) error {
|
) error {
|
||||||
var eventNIDs string
|
var eventNIDs string
|
||||||
eventNIDAsString := fmt.Sprintf("%d", eventNID)
|
eventNIDAsString := fmt.Sprintf("%d", eventNID)
|
||||||
selectStmt := sqlutil.TxStmt(txn, s.selectPreviousEventExistsStmt)
|
selectStmt := sqlutil.TxStmt(txn, s.selectPreviousEventExistsStmt)
|
||||||
err := selectStmt.QueryRowContext(ctx, previousEventID, previousEventReferenceSHA256).Scan(&eventNIDs)
|
err := selectStmt.QueryRowContext(ctx, previousEventID).Scan(&eventNIDs)
|
||||||
if err != nil && err != sql.ErrNoRows {
|
if err != nil && err != sql.ErrNoRows {
|
||||||
return fmt.Errorf("selectStmt.QueryRowContext.Scan: %w", err)
|
return fmt.Errorf("selectStmt.QueryRowContext.Scan: %w", err)
|
||||||
}
|
}
|
||||||
|
|
@ -115,7 +114,7 @@ func (s *previousEventStatements) InsertPreviousEvent(
|
||||||
}
|
}
|
||||||
insertStmt := sqlutil.TxStmt(txn, s.insertPreviousEventStmt)
|
insertStmt := sqlutil.TxStmt(txn, s.insertPreviousEventStmt)
|
||||||
_, err = insertStmt.ExecContext(
|
_, err = insertStmt.ExecContext(
|
||||||
ctx, previousEventID, previousEventReferenceSHA256, eventNIDs,
|
ctx, previousEventID, eventNIDs,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -123,9 +122,9 @@ func (s *previousEventStatements) InsertPreviousEvent(
|
||||||
// Check if the event reference exists
|
// Check if the event reference exists
|
||||||
// Returns sql.ErrNoRows if the event reference doesn't exist.
|
// Returns sql.ErrNoRows if the event reference doesn't exist.
|
||||||
func (s *previousEventStatements) SelectPreviousEventExists(
|
func (s *previousEventStatements) SelectPreviousEventExists(
|
||||||
ctx context.Context, txn *sql.Tx, eventID string, eventReferenceSHA256 []byte,
|
ctx context.Context, txn *sql.Tx, eventID string,
|
||||||
) error {
|
) error {
|
||||||
var ok int64
|
var ok int64
|
||||||
stmt := sqlutil.TxStmt(txn, s.selectPreviousEventExistsStmt)
|
stmt := sqlutil.TxStmt(txn, s.selectPreviousEventExistsStmt)
|
||||||
return stmt.QueryRowContext(ctx, eventID, eventReferenceSHA256).Scan(&ok)
|
return stmt.QueryRowContext(ctx, eventID).Scan(&ok)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -113,10 +113,10 @@ type RoomAliases interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type PreviousEvents interface {
|
type PreviousEvents interface {
|
||||||
InsertPreviousEvent(ctx context.Context, txn *sql.Tx, previousEventID string, previousEventReferenceSHA256 []byte, eventNID types.EventNID) error
|
InsertPreviousEvent(ctx context.Context, txn *sql.Tx, previousEventID string, eventNID types.EventNID) error
|
||||||
// Check if the event reference exists
|
// Check if the event reference exists
|
||||||
// Returns sql.ErrNoRows if the event reference doesn't exist.
|
// Returns sql.ErrNoRows if the event reference doesn't exist.
|
||||||
SelectPreviousEventExists(ctx context.Context, txn *sql.Tx, eventID string, eventReferenceSHA256 []byte) error
|
SelectPreviousEventExists(ctx context.Context, txn *sql.Tx, eventID string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Invites interface {
|
type Invites interface {
|
||||||
|
|
|
||||||
|
|
@ -45,17 +45,17 @@ func TestPreviousEventsTable(t *testing.T) {
|
||||||
defer close()
|
defer close()
|
||||||
|
|
||||||
for _, x := range room.Events() {
|
for _, x := range room.Events() {
|
||||||
for _, prevEvent := range x.PrevEvents() {
|
for _, eventID := range x.PrevEventIDs() {
|
||||||
err := tab.InsertPreviousEvent(ctx, nil, prevEvent.EventID, prevEvent.EventSHA256, 1)
|
err := tab.InsertPreviousEvent(ctx, nil, eventID, 1)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
err = tab.SelectPreviousEventExists(ctx, nil, prevEvent.EventID, prevEvent.EventSHA256)
|
err = tab.SelectPreviousEventExists(ctx, nil, eventID)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomString with a correct EventSHA256 should fail and return sql.ErrNoRows
|
// RandomString should fail and return sql.ErrNoRows
|
||||||
err := tab.SelectPreviousEventExists(ctx, nil, util.RandomString(16), room.Events()[0].EventReference().EventSHA256)
|
err := tab.SelectPreviousEventExists(ctx, nil, util.RandomString(16))
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue