allow adding fragments to comparer, instead of reloading from db.

This is a suboptimal implementation now,
but is the API we want to end up with. Readers and Writers
in persistence add fragments to the comparer when they become
aware of, create, or link up fragments.
This commit is contained in:
Bruno Windels 2019-06-01 16:42:57 +02:00
parent fa4efe0132
commit 447b0aa03c
4 changed files with 12 additions and 17 deletions

View file

@ -108,6 +108,7 @@ index for fast lookup of how two fragments can be sorted
*/
export default class FragmentIdComparer {
constructor(fragments) {
this._fragmentsById = fragments.reduce((map, f) => {map.set(f.id, f); return map;}, new Map());
this.rebuild(fragments);
}
@ -157,6 +158,10 @@ export default class FragmentIdComparer {
// linkFragments(txn, firstFragmentId, secondFragmentId) {
// }
add(fragment) {
this._fragmentsById[fragment.id] = fragment;
this.rebuild(this._fragmentsById.values());
}
}
//#ifdef TESTS

View file

@ -64,8 +64,9 @@ export default class GapWriter {
txn.timelineFragments.set(neighbourFragmentEntry.fragment);
directionalAppend(entries, neighbourFragmentEntry, direction);
// update fragmentIdComparer here after linking up fragments?
this._fragmentIdComparer.rebuild(await txn.timelineFragments.all());
// update fragmentIdComparer here after linking up fragments
this._fragmentIdComparer.add(fragmentEntry.fragment);
this._fragmentIdComparer.add(neighbourFragmentEntry.fragment);
}
fragmentEntry.token = end;
txn.timelineFragments.set(fragmentEntry.fragment);

View file

@ -40,6 +40,7 @@ export default class SyncWriter {
nextToken: null
};
txn.timelineFragments.add(fragment);
this._fragmentIdComparer.add(fragment);
return fragment;
} else {
return liveFragment;
@ -62,6 +63,7 @@ export default class SyncWriter {
nextToken: null
};
txn.timelineFragments.add(newFragment);
this._fragmentIdComparer.add(newFragment);
return {oldFragment, newFragment};
}
@ -117,11 +119,6 @@ export default class SyncWriter {
}
}
if (timeline.limited) {
const fragments = await txn.timelineFragments.all(this._roomId);
this._fragmentIdComparer.rebuild(fragments);
}
return entries;
}
}

View file

@ -16,7 +16,6 @@ export default class TimelineReader {
this._storage.storeNames.timelineFragments,
]);
let entries = [];
let loadedFragment = false;
const timelineStore = txn.timelineEvents;
const fragmentStore = txn.timelineFragments;
@ -36,28 +35,21 @@ export default class TimelineReader {
const fragment = await fragmentStore.get(this._roomId, eventKey.fragmentId);
// this._fragmentIdComparer.addFragment(fragment);
let fragmentEntry = new FragmentBoundaryEntry(fragment, direction.isBackward, this._fragmentIdComparer);
// append or prepend fragmentEntry, reuse func from GapPersister?
// append or prepend fragmentEntry, reuse func from GapWriter?
directionalAppend(entries, fragmentEntry, direction);
// don't count it in amount perhaps? or do?
if (fragmentEntry.linkedFragmentId) {
const nextFragment = await fragmentStore.get(this._roomId, fragmentEntry.linkedFragmentId);
// this._fragmentIdComparer.addFragment(nextFragment);
this._fragmentIdComparer.add(nextFragment);
const nextFragmentEntry = new FragmentBoundaryEntry(nextFragment, direction.isForward, this._fragmentIdComparer);
directionalAppend(entries, nextFragmentEntry, direction);
eventKey = new EventKey(nextFragmentEntry.fragmentId, nextFragmentEntry.eventIndex);
loadedFragment = true;
} else {
eventKey = null;
}
}
}
// reload fragments
if (loadedFragment) {
const fragments = await fragmentStore.all(this._roomId);
this._fragmentIdComparer.rebuild(fragments);
}
return entries;
}
}