Add method for calculating the room state after a list of events

This commit is contained in:
Mark Haines 2017-03-06 11:49:30 +00:00
parent 9a8a8aedcb
commit c6493c9144
2 changed files with 12 additions and 6 deletions

View file

@ -91,7 +91,7 @@ func processRoomEvent(db RoomEventDatabase, ow OutputRoomEventWriter, input api.
} }
} else { } else {
// We haven't been told what the state at the event is so we need to calculate it from the prev_events // We haven't been told what the state at the event is so we need to calculate it from the prev_events
if stateAtEvent.BeforeStateSnapshotNID, err = calculateAndStoreState(db, event, roomNID); err != nil { if stateAtEvent.BeforeStateSnapshotNID, err = calculateAndStoreStateBeforeEvent(db, event, roomNID); err != nil {
return err return err
} }
} }

View file

@ -10,7 +10,7 @@ import (
// calculateAndStoreState calculates a snapshot of the state of a room before an event. // calculateAndStoreState calculates a snapshot of the state of a room before an event.
// Stores the snapshot of the state in the database. // Stores the snapshot of the state in the database.
// Returns a numeric ID for that snapshot. // Returns a numeric ID for that snapshot.
func calculateAndStoreState( func calculateAndStoreStateBeforeEvent(
db RoomEventDatabase, event gomatrixserverlib.Event, roomNID types.RoomNID, db RoomEventDatabase, event gomatrixserverlib.Event, roomNID types.RoomNID,
) (types.StateSnapshotNID, error) { ) (types.StateSnapshotNID, error) {
// Load the state at the prev events. // Load the state at the prev events.
@ -25,6 +25,12 @@ func calculateAndStoreState(
return 0, err return 0, err
} }
return calculateAndStoreStateAfterEvents(db, roomNID, prevStates)
}
// calculateAndStoreStateAfterEvents finds the room state after the given events.
// Stores the resulting state in the database and returns a numeric ID for that snapshot.
func calculateAndStoreStateAfterEvents(db RoomEventDatabase, roomNID types.RoomNID, prevStates []types.StateAtEvent) (types.StateSnapshotNID, error) {
if len(prevStates) == 0 { if len(prevStates) == 0 {
// 2) There weren't any prev_events for this event so the state is // 2) There weren't any prev_events for this event so the state is
// empty. // empty.
@ -57,7 +63,7 @@ func calculateAndStoreState(
// If there are too many deltas then we need to calculate the full state // If there are too many deltas then we need to calculate the full state
// So fall through to calculateAndStoreStateMany // So fall through to calculateAndStoreStateMany
} }
return calculateAndStoreStateMany(db, roomNID, prevStates) return calculateAndStoreStateAfterManyEvents(db, roomNID, prevStates)
} }
// maxStateBlockNIDs is the maximum number of state data blocks to use to encode a snapshot of room state. // maxStateBlockNIDs is the maximum number of state data blocks to use to encode a snapshot of room state.
@ -67,10 +73,10 @@ func calculateAndStoreState(
// TODO: Tune this to get the right balance between size and lookup performance. // TODO: Tune this to get the right balance between size and lookup performance.
const maxStateBlockNIDs = 64 const maxStateBlockNIDs = 64
// calculateAndStoreStateMany calculates the state of the room before an event // calculateAndStoreStateAfterManyEvents finds the room state after the given events.
// using the states at each of the event's prev events. // This handles the slow path of calculateAndStoreStateAfterEvents for when there is more than one event.
// Stores the resulting state and returns a numeric ID for the snapshot. // Stores the resulting state and returns a numeric ID for the snapshot.
func calculateAndStoreStateMany(db RoomEventDatabase, roomNID types.RoomNID, prevStates []types.StateAtEvent) (types.StateSnapshotNID, error) { func calculateAndStoreStateAfterManyEvents(db RoomEventDatabase, roomNID types.RoomNID, prevStates []types.StateAtEvent) (types.StateSnapshotNID, error) {
// Conflict resolution. // Conflict resolution.
// First stage: load the state after each of the prev events. // First stage: load the state after each of the prev events.
combined, err := loadCombinedStateAfterEvents(db, prevStates) combined, err := loadCombinedStateAfterEvents(db, prevStates)