Simplify calculateLatest
(#2430)
* Simplify `calculateLatest` * Comments
This commit is contained in:
parent
85c00208c5
commit
6bc6184d70
|
@ -316,40 +316,30 @@ func (u *latestEventsUpdater) calculateLatest(
|
||||||
// Then let's see if any of the existing forward extremities now
|
// Then let's see if any of the existing forward extremities now
|
||||||
// have entries in the previous events table. If they do then we
|
// have entries in the previous events table. If they do then we
|
||||||
// will no longer include them as forward extremities.
|
// will no longer include them as forward extremities.
|
||||||
existingPrevs := make(map[string]struct{})
|
for k, l := range existingRefs {
|
||||||
for _, l := range existingRefs {
|
|
||||||
referenced, err := u.updater.IsReferenced(l.EventReference)
|
referenced, err := u.updater.IsReferenced(l.EventReference)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("u.updater.IsReferenced: %w", err)
|
return false, fmt.Errorf("u.updater.IsReferenced: %w", err)
|
||||||
} else if referenced {
|
} else if referenced {
|
||||||
existingPrevs[l.EventID] = struct{}{}
|
delete(existingRefs, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Include our new event in the extremities.
|
// Start off with our new unreferenced event. We're reusing the backing
|
||||||
newLatest := []types.StateAtEventAndReference{newStateAndRef}
|
// array here rather than allocating a new one.
|
||||||
|
u.latest = append(u.latest[:0], newStateAndRef)
|
||||||
|
|
||||||
// Then run through and see if the other extremities are still valid.
|
// If our new event references any of the existing forward extremities
|
||||||
// If our new event references them then they are no longer good
|
// then they are no longer forward extremities, so remove them.
|
||||||
// candidates.
|
|
||||||
for _, prevEventID := range newEvent.PrevEventIDs() {
|
for _, prevEventID := range newEvent.PrevEventIDs() {
|
||||||
delete(existingRefs, prevEventID)
|
delete(existingRefs, prevEventID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that we don't add any candidate forward extremities from
|
|
||||||
// the old set that are, themselves, referenced by the old set of
|
|
||||||
// forward extremities. This shouldn't happen but guards against
|
|
||||||
// the possibility anyway.
|
|
||||||
for prevEventID := range existingPrevs {
|
|
||||||
delete(existingRefs, prevEventID)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then re-add any old extremities that are still valid after all.
|
// Then re-add any old extremities that are still valid after all.
|
||||||
for _, old := range existingRefs {
|
for _, old := range existingRefs {
|
||||||
newLatest = append(newLatest, *old)
|
u.latest = append(u.latest, *old)
|
||||||
}
|
}
|
||||||
|
|
||||||
u.latest = newLatest
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue