mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-25 15:53:09 -06:00
Refactor forward extremities, again
This commit is contained in:
parent
f09e6112e9
commit
e066b8152f
|
|
@ -17,7 +17,6 @@
|
||||||
package input
|
package input
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
|
@ -28,7 +27,6 @@ import (
|
||||||
"github.com/matrix-org/dendrite/roomserver/types"
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// updateLatestEvents updates the list of latest events for this room in the database and writes the
|
// updateLatestEvents updates the list of latest events for this room in the database and writes the
|
||||||
|
|
@ -142,7 +140,7 @@ func (u *latestEventsUpdater) doUpdateLatestEvents() error {
|
||||||
// Work out what the latest events are. This will include the new
|
// Work out what the latest events are. This will include the new
|
||||||
// event if it is not already referenced.
|
// event if it is not already referenced.
|
||||||
if err := u.calculateLatest(
|
if err := u.calculateLatest(
|
||||||
oldLatest,
|
oldLatest, &u.event,
|
||||||
types.StateAtEventAndReference{
|
types.StateAtEventAndReference{
|
||||||
EventReference: u.event.EventReference(),
|
EventReference: u.event.EventReference(),
|
||||||
StateAtEvent: u.stateAtEvent,
|
StateAtEvent: u.stateAtEvent,
|
||||||
|
|
@ -250,46 +248,56 @@ func (u *latestEventsUpdater) latestState() error {
|
||||||
// true if the new event is included in those extremites, false otherwise.
|
// true if the new event is included in those extremites, false otherwise.
|
||||||
func (u *latestEventsUpdater) calculateLatest(
|
func (u *latestEventsUpdater) calculateLatest(
|
||||||
oldLatest []types.StateAtEventAndReference,
|
oldLatest []types.StateAtEventAndReference,
|
||||||
newEvent types.StateAtEventAndReference,
|
newEvent *gomatrixserverlib.Event,
|
||||||
|
newStateAndRef types.StateAtEventAndReference,
|
||||||
) error {
|
) error {
|
||||||
var newLatest []types.StateAtEventAndReference
|
// First of all, get a list of all of the events that our current
|
||||||
|
// forward extremities reference.
|
||||||
|
existingIDs := make(map[string]*types.StateAtEventAndReference)
|
||||||
|
existingRefMap := make(map[string]struct{})
|
||||||
|
existingNIDs := []types.EventNID{}
|
||||||
|
for i, old := range oldLatest {
|
||||||
|
existingIDs[old.EventID] = &oldLatest[i]
|
||||||
|
existingNIDs = append(existingNIDs, old.EventNID)
|
||||||
|
}
|
||||||
|
|
||||||
// First of all, let's see if any of the existing forward extremities
|
// Look up the old extremity events. This allows us to find their
|
||||||
// now have entries in the previous events table. If they do then we
|
// prev events.
|
||||||
// will no longer include them as forward extremities.
|
events, err := u.api.DB.Events(u.ctx, existingNIDs)
|
||||||
for _, l := range oldLatest {
|
if err != nil {
|
||||||
referenced, err := u.updater.IsReferenced(l.EventReference)
|
return fmt.Errorf("u.api.DB.Events: %w", err)
|
||||||
if err != nil {
|
}
|
||||||
logrus.WithError(err).Errorf("Failed to retrieve event reference for %q", l.EventID)
|
for _, old := range events {
|
||||||
return fmt.Errorf("u.updater.IsReferenced (old): %w", err)
|
for _, prevEventID := range old.PrevEventIDs() {
|
||||||
} else if !referenced {
|
existingRefMap[prevEventID] = struct{}{}
|
||||||
newLatest = append(newLatest, l)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then check and see if our new event is already included in that set.
|
// If the "new" event is already referenced by a forward extremity
|
||||||
// This ordinarily won't happen but it covers the edge-case that we've
|
// then do nothing - it's not a candidate to be a new extremity.
|
||||||
// already seen this event before and it's a forward extremity, so rather
|
if _, ok := existingRefMap[newEvent.EventID()]; ok {
|
||||||
// than adding a duplicate, we'll just return the set as complete.
|
return nil
|
||||||
for _, l := range newLatest {
|
}
|
||||||
if l.EventReference.EventID == newEvent.EventReference.EventID && bytes.Equal(l.EventReference.EventSHA256, newEvent.EventReference.EventSHA256) {
|
|
||||||
// We've already referenced this new event so we can just return
|
// If the "new" event is already a forward extremity then do nothing
|
||||||
// the newly completed extremities at this point.
|
// either - we won't add it twice.
|
||||||
u.latest = newLatest
|
for _, event := range events {
|
||||||
|
if event.EventID() == newEvent.EventID() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point we've processed the old extremities, and we've checked
|
// Include our new event in the extremities.
|
||||||
// that our new event isn't already in that set. Therefore now we can
|
newLatest := []types.StateAtEventAndReference{newStateAndRef}
|
||||||
// check if our *new* event is a forward extremity, and if it is, add
|
|
||||||
// it in.
|
// Then run through and see if the other extremities are still valid.
|
||||||
referenced, err := u.updater.IsReferenced(newEvent.EventReference)
|
for _, prevEventID := range newEvent.PrevEventIDs() {
|
||||||
if err != nil {
|
delete(existingIDs, prevEventID)
|
||||||
logrus.WithError(err).Errorf("Failed to retrieve event reference for %q", newEvent.EventReference.EventID)
|
}
|
||||||
return fmt.Errorf("u.updater.IsReferenced (new): %w", err)
|
|
||||||
} else if !referenced || len(newLatest) == 0 {
|
// Then re-add any old extremities that are still valid.
|
||||||
newLatest = append(newLatest, newEvent)
|
for _, old := range existingIDs {
|
||||||
|
newLatest = append(newLatest, *old)
|
||||||
}
|
}
|
||||||
|
|
||||||
u.latest = newLatest
|
u.latest = newLatest
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue