Refactor finding overlapping to no longer need entries

This commit is contained in:
Danila Fedorin 2021-09-09 12:24:14 -07:00
parent 57c4070505
commit fdfea95d22

View File

@ -28,10 +28,10 @@ export class GapWriter {
this._relationWriter = relationWriter; this._relationWriter = relationWriter;
} }
// events is in reverse-chronological order (last event comes at index 0) if backwards // events is in reverse-chronological order (last event comes at index 0) if backwards
async _findOverlappingEvents(fragmentEntry, events, txn, log) { async _findOverlappingEventsFor(currentFragmentId, linkedFragmentId, direction, events, txn, log) {
let expectedOverlappingEventId; let expectedOverlappingEventId;
if (fragmentEntry.hasLinkedFragment) { if (linkedFragmentId !== null) {
expectedOverlappingEventId = await this._findExpectedOverlappingEventId(fragmentEntry, txn); expectedOverlappingEventId = await this._findExpectedOverlappingEventId(linkedFragmentId, direction, txn);
} }
let remainingEvents = events; let remainingEvents = events;
let nonOverlappingEvents = []; let nonOverlappingEvents = [];
@ -55,7 +55,7 @@ export class GapWriter {
// get neighbour fragment to link it up later on // get neighbour fragment to link it up later on
const neighbourEvent = await txn.timelineEvents.getByEventId(this._roomId, duplicateEventId); const neighbourEvent = await txn.timelineEvents.getByEventId(this._roomId, duplicateEventId);
const neighbourFragment = await txn.timelineFragments.get(this._roomId, neighbourEvent.fragmentId); const neighbourFragment = await txn.timelineFragments.get(this._roomId, neighbourEvent.fragmentId);
neighbourFragmentEntry = fragmentEntry.createNeighbourEntry(neighbourFragment); neighbourFragmentEntry = new FragmentBoundaryEntry(neighbourFragment, direction.isForward, this._fragmentIdComparer);
} }
} }
// If more events remain, or if this wasn't the expected overlapping event, // If more events remain, or if this wasn't the expected overlapping event,
@ -68,18 +68,23 @@ export class GapWriter {
remainingEvents = null; remainingEvents = null;
} }
} }
if (neighbourFragmentEntry?.fragmentId === fragmentEntry.fragmentId) { if (neighbourFragmentEntry?.fragmentId === currentFragmentId) {
log.log("hit #160, prevent fragment linking to itself", log.level.Warn); log.log("hit #160, prevent fragment linking to itself", log.level.Warn);
neighbourFragmentEntry = null; neighbourFragmentEntry = null;
} }
return {nonOverlappingEvents, neighbourFragmentEntry}; return {nonOverlappingEvents, neighbourFragmentEntry};
} }
async _findExpectedOverlappingEventId(fragmentEntry, txn) { async _findOverlappingEvents(fragmentEntry, events, txn, log) {
const linkedFragmentId = fragmentEntry.hasLinkedFragment ? fragmentEntry.linkedFragmentId : null;
return this._findOverlappingEventsFor(fragmentEntry.fragmentId, linkedFragmentId, fragmentEntry.direction, events, txn, log);
}
async _findExpectedOverlappingEventId(linkedFragmentId, direction, txn) {
const eventEntry = await this._findFragmentEdgeEvent( const eventEntry = await this._findFragmentEdgeEvent(
fragmentEntry.linkedFragmentId, linkedFragmentId,
// reverse because it's the oppose edge of the linked fragment // reverse because it's the oppose edge of the linked fragment
fragmentEntry.direction.reverse(), direction.reverse(),
txn); txn);
if (eventEntry) { if (eventEntry) {
return eventEntry.event.event_id; return eventEntry.event.event_id;
@ -318,7 +323,6 @@ export class GapWriter {
import {FragmentIdComparer} from "../FragmentIdComparer.js"; import {FragmentIdComparer} from "../FragmentIdComparer.js";
import {RelationWriter} from "./RelationWriter.js"; import {RelationWriter} from "./RelationWriter.js";
import {createMockStorage} from "../../../../mocks/Storage.js"; import {createMockStorage} from "../../../../mocks/Storage.js";
import {FragmentBoundaryEntry} from "../entries/FragmentBoundaryEntry.js";
import {NullLogItem} from "../../../../logging/NullLogger.js"; import {NullLogItem} from "../../../../logging/NullLogger.js";
import {TimelineMock, eventIds, eventId} from "../../../../mocks/TimelineMock.ts"; import {TimelineMock, eventIds, eventId} from "../../../../mocks/TimelineMock.ts";
import {SyncWriter} from "./SyncWriter.js"; import {SyncWriter} from "./SyncWriter.js";