work on gap filling + tests (doesn't work yet)

This commit is contained in:
Bruno Windels 2019-03-29 23:01:27 +01:00
parent aaff9eea6c
commit 3f2f656db7

View File

@ -70,7 +70,9 @@ export default class RoomPersister {
throw new Error("Gap is not present in the timeline"); throw new Error("Gap is not present in the timeline");
} }
// find the previous event before the gap we could merge with // find the previous event before the gap we could merge with
const neighbourEventEntry = await roomTimeline.findSubsequentEvent(this._roomId, gapKey, backwards); const neighbourEventEntry = await (backwards ?
roomTimeline.previousEvent(this._roomId, gapKey) :
roomTimeline.nextEvent(this._roomId, gapKey));
const neighbourEventId = neighbourEventEntry ? neighbourEventEntry.event.event_id : undefined; const neighbourEventId = neighbourEventEntry ? neighbourEventEntry.event.event_id : undefined;
const {newEntries, eventFound} = this._createNewGapEntries(chunk, end, gapKey, neighbourEventId, backwards); const {newEntries, eventFound} = this._createNewGapEntries(chunk, end, gapKey, neighbourEventId, backwards);
const neighbourEventKey = eventFound ? neighbourEventEntry.sortKey : undefined; const neighbourEventKey = eventFound ? neighbourEventEntry.sortKey : undefined;
@ -123,7 +125,6 @@ export default class RoomPersister {
nextKey = nextKey.nextKeyWithGap(); nextKey = nextKey.nextKeyWithGap();
entries.push(this._createBackwardGapEntry(nextKey, timeline.prev_batch)); entries.push(this._createBackwardGapEntry(nextKey, timeline.prev_batch));
} }
// const startOfChunkSortKey = nextKey;
if (timeline.events) { if (timeline.events) {
for(const event of timeline.events) { for(const event of timeline.events) {
nextKey = nextKey.nextKey(); nextKey = nextKey.nextKey();
@ -132,7 +133,7 @@ export default class RoomPersister {
} }
// write to store // write to store
for(const entry of entries) { for(const entry of entries) {
txn.roomTimeline.add(entry); txn.roomTimeline.insert(entry);
} }
// right thing to do? if the txn fails, not sure we'll continue anyways ... // right thing to do? if the txn fails, not sure we'll continue anyways ...
// only advance the key once the transaction has // only advance the key once the transaction has
@ -189,11 +190,28 @@ export default class RoomPersister {
} }
//#ifdef TESTS //#ifdef TESTS
import {StorageMock, RoomTimelineMock} from "../../src/mocks/storage.js"; import MemoryStorage from "../storage/memory/MemoryStorage.js";
export async function tests() { export function tests() {
const roomId = "!abc:hs.tld"; const roomId = "!abc:hs.tld";
// sets sortKey and roomId on an array of entries
function createTimeline(roomId, entries) {
let key = new SortKey();
for (let entry of entries) {
if (entry.gap && entry.gap.prev_batch) {
key = key.nextKeyWithGap();
}
entry.sortKey = key;
if (entry.gap && entry.gap.next_batch) {
key = key.nextKeyWithGap();
} else if (!entry.gap) {
key = key.nextKey();
}
entry.roomId = roomId;
}
}
function areSorted(entries) { function areSorted(entries) {
for (var i = 1; i < entries.length; i++) { for (var i = 1; i < entries.length; i++) {
const isSorted = entries[i - 1].sortKey.compare(entries[i].sortKey) < 0; const isSorted = entries[i - 1].sortKey.compare(entries[i].sortKey) < 0;
@ -208,13 +226,12 @@ export async function tests() {
"test backwards gap fill with overlapping neighbouring event": async function(assert) { "test backwards gap fill with overlapping neighbouring event": async function(assert) {
const currentPaginationToken = "abc"; const currentPaginationToken = "abc";
const gap = {gap: {prev_batch: currentPaginationToken}}; const gap = {gap: {prev_batch: currentPaginationToken}};
// assigns roomId and sortKey const storage = new MemoryStorage({roomTimeline: createTimeline(roomId, [
const roomTimeline = RoomTimelineMock.forRoom(roomId, [
{event: {event_id: "b"}}, {event: {event_id: "b"}},
{gap: {next_batch: "ghi"}}, {gap: {next_batch: "ghi"}},
gap, gap,
]); ])});
const persister = new RoomPersister(roomId, new StorageMock({roomTimeline})); const persister = new RoomPersister({roomId, storage});
const response = { const response = {
start: currentPaginationToken, start: currentPaginationToken,
end: "def", end: "def",
@ -240,13 +257,12 @@ export async function tests() {
const currentPaginationToken = "abc"; const currentPaginationToken = "abc";
const newPaginationToken = "def"; const newPaginationToken = "def";
const gap = {gap: {prev_batch: currentPaginationToken}}; const gap = {gap: {prev_batch: currentPaginationToken}};
// assigns roomId and sortKey const storage = new MemoryStorage({roomTimeline: createTimeline(roomId, [
const roomTimeline = RoomTimelineMock.forRoom(roomId, [
{event: {event_id: "a"}}, {event: {event_id: "a"}},
{gap: {next_batch: "ghi"}}, {gap: {next_batch: "ghi"}},
gap, gap,
]); ])});
const persister = new RoomPersister(roomId, new StorageMock({roomTimeline})); const persister = new RoomPersister({roomId, storage});
const response = { const response = {
start: currentPaginationToken, start: currentPaginationToken,
end: newPaginationToken, end: newPaginationToken,