More comments

This commit is contained in:
Mark Haines 2017-02-21 11:57:46 +00:00
parent e7555944aa
commit 0f4787efeb
2 changed files with 21 additions and 7 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
// updateLatestEvents updates the list of latest events for this room.
func updateLatestEvents( func updateLatestEvents(
db RoomEventDatabase, roomNID types.RoomNID, stateAtEvent types.StateAtEvent, event gomatrixserverlib.Event, db RoomEventDatabase, roomNID types.RoomNID, stateAtEvent types.StateAtEvent, event gomatrixserverlib.Event,
) error { ) error {
@ -14,8 +15,15 @@ func updateLatestEvents(
return err return err
} }
defer func() { defer func() {
// Commit if there wasn't an error. if err == nil {
updater.Close(err == nil) // Commit if there wasn't an error.
// Set the returned err value if we encounter an error committing.
err = updater.Close(true)
} else {
// Ignore any error we get rolling back since we don't want to
// clobber the current error
updater.Close(false)
}
}() }()
var prevEvents []gomatrixserverlib.EventReference var prevEvents []gomatrixserverlib.EventReference
@ -54,11 +62,11 @@ func updateLatestEvents(
}) })
} }
err = updater.SetLatestEvents(roomNID, newLatest) if err = updater.SetLatestEvents(roomNID, newLatest); err != nil {
if err != nil {
return err return err
} }
// The deferred fires and the transaction closes. // The err should be nil at this point.
return nil // But when we call Close in the defer above it might set an error here.
return err
} }

View file

@ -21,7 +21,11 @@ CREATE TABLE IF NOT EXISTS previous_events (
); );
` `
// Insert an entry of the prev event or if the prev_event doesn't exist. // Insert an entry into the previous_events table.
// The there is already an entry indicating that an event references that previous event then
// add the event NID to the list to indicate that this event references that previous event as well.
// This should only be modified while holding a "FOR UPDATE" lock on the row in the rooms table for this room.
// The lock is necessary to avoid data races when checking whether an event is already referenced by another event.
const insertPreviousEventSQL = "" + const insertPreviousEventSQL = "" +
"INSERT INTO previous_events VALUES" + "INSERT INTO previous_events VALUES" +
" (previous_event_id, previous_reference_sha256, event_nids)" + " (previous_event_id, previous_reference_sha256, event_nids)" +
@ -30,6 +34,8 @@ const insertPreviousEventSQL = "" +
" DO UPDATE SET event_nids = array_append(previous_events.event_nids, $3)" + " DO UPDATE SET event_nids = array_append(previous_events.event_nids, $3)" +
" WHERE $3 != ANY(previous_events.event_nids)" " WHERE $3 != ANY(previous_events.event_nids)"
// 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.
const selectPreviousEventExistsSQL = "" + const selectPreviousEventExistsSQL = "" +
"SELECT 1 FROM previous_events" + "SELECT 1 FROM previous_events" +
" WHERE previous_event_id = $1 AND previous_reference_sha256 = $2" " WHERE previous_event_id = $1 AND previous_reference_sha256 = $2"