change timelineEventStore.insert to tryInsert

This commit is contained in:
Bruno Windels 2021-09-21 21:04:10 +02:00
parent 12add19c31
commit 6cded5319a
6 changed files with 30 additions and 21 deletions

View file

@ -247,8 +247,8 @@ export function tests() {
storage.storeNames.timelineFragments storage.storeNames.timelineFragments
]); ]);
txn.timelineFragments.add({id: 1, roomId}); txn.timelineFragments.add({id: 1, roomId});
txn.timelineEvents.insert({fragmentId: 1, eventIndex: 2, event: messageEvent, roomId}); txn.timelineEvents.tryInsert({fragmentId: 1, eventIndex: 2, event: messageEvent, roomId});
txn.timelineEvents.insert({fragmentId: 1, eventIndex: 3, event: myReactionEvent, roomId}); txn.timelineEvents.tryInsert({fragmentId: 1, eventIndex: 3, event: myReactionEvent, roomId});
await relationWriter.writeRelation(myReactionEntry, txn, new NullLogItem()); await relationWriter.writeRelation(myReactionEntry, txn, new NullLogItem());
await txn.complete(); await txn.complete();
// 2. setup queue & timeline // 2. setup queue & timeline
@ -309,7 +309,7 @@ export function tests() {
storage.storeNames.timelineFragments storage.storeNames.timelineFragments
]); ]);
txn.timelineFragments.add({id: 1, roomId}); txn.timelineFragments.add({id: 1, roomId});
txn.timelineEvents.insert({fragmentId: 1, eventIndex: 2, event: messageEvent, roomId}); txn.timelineEvents.tryInsert({fragmentId: 1, eventIndex: 2, event: messageEvent, roomId});
await txn.complete(); await txn.complete();
// 2. setup queue & timeline // 2. setup queue & timeline
const queue = new SendQueue({roomId, storage, hsApi: new MockHomeServer().api}); const queue = new SendQueue({roomId, storage, hsApi: new MockHomeServer().api});

View file

@ -447,7 +447,7 @@ export function tests() {
// 1. put event and reaction into storage // 1. put event and reaction into storage
const storage = await createMockStorage(); const storage = await createMockStorage();
const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]); const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]);
txn.timelineEvents.insert({ txn.timelineEvents.tryInsert({
event: withContent(createAnnotation(messageId, "👋"), createEvent("m.reaction", reactionId, bob)), event: withContent(createAnnotation(messageId, "👋"), createEvent("m.reaction", reactionId, bob)),
fragmentId: 1, eventIndex: 1, roomId fragmentId: 1, eventIndex: 1, roomId
}); });
@ -543,7 +543,7 @@ export function tests() {
// 1. put reaction in storage // 1. put reaction in storage
const storage = await createMockStorage(); const storage = await createMockStorage();
const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]); const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]);
txn.timelineEvents.insert({ txn.timelineEvents.tryInsert({
event: withContent(createAnnotation(messageId, "👋"), createEvent("m.reaction", reactionId, bob)), event: withContent(createAnnotation(messageId, "👋"), createEvent("m.reaction", reactionId, bob)),
fragmentId: 1, eventIndex: 3, roomId fragmentId: 1, eventIndex: 3, roomId
}); });

View file

@ -124,9 +124,12 @@ export class GapWriter {
if (updatedRelationTargetEntries) { if (updatedRelationTargetEntries) {
updatedEntries.push(...updatedRelationTargetEntries); updatedEntries.push(...updatedRelationTargetEntries);
} }
txn.timelineEvents.insert(eventStorageEntry); if (await txn.timelineEvents.tryInsert(eventStorageEntry)) {
const eventEntry = new EventEntry(eventStorageEntry, this._fragmentIdComparer); const eventEntry = new EventEntry(eventStorageEntry, this._fragmentIdComparer);
directionalAppend(entries, eventEntry, direction); directionalAppend(entries, eventEntry, direction);
} else {
log.log({l: `could not write event`, id: event.event_id}, log.level.Warn);
}
} }
return {entries, updatedEntries}; return {entries, updatedEntries};
} }

View file

@ -275,7 +275,7 @@ export function tests() {
const storage = await createMockStorage(); const storage = await createMockStorage();
const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]); const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]);
txn.timelineEvents.insert({fragmentId: 1, eventIndex: 2, event, roomId}); txn.timelineEvents.tryInsert({fragmentId: 1, eventIndex: 2, event, roomId});
const updatedEntries = await relationWriter.writeRelation(redactionEntry, txn, new NullLogItem()); const updatedEntries = await relationWriter.writeRelation(redactionEntry, txn, new NullLogItem());
await txn.complete(); await txn.complete();
@ -300,7 +300,7 @@ export function tests() {
const storage = await createMockStorage(); const storage = await createMockStorage();
const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]); const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]);
txn.timelineEvents.insert({fragmentId: 1, eventIndex: 2, event, roomId}); txn.timelineEvents.tryInsert({fragmentId: 1, eventIndex: 2, event, roomId});
const updatedEntries = await relationWriter.writeRelation(reactionEntry, txn, new NullLogItem()); const updatedEntries = await relationWriter.writeRelation(reactionEntry, txn, new NullLogItem());
await txn.complete(); await txn.complete();
@ -329,7 +329,7 @@ export function tests() {
const storage = await createMockStorage(); const storage = await createMockStorage();
const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]); const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]);
txn.timelineEvents.insert({fragmentId: 1, eventIndex: 2, event, roomId}); txn.timelineEvents.tryInsert({fragmentId: 1, eventIndex: 2, event, roomId});
await relationWriter.writeRelation(reaction1Entry, txn, new NullLogItem()); await relationWriter.writeRelation(reaction1Entry, txn, new NullLogItem());
const updatedEntries = await relationWriter.writeRelation(reaction2Entry, txn, new NullLogItem()); const updatedEntries = await relationWriter.writeRelation(reaction2Entry, txn, new NullLogItem());
await txn.complete(); await txn.complete();
@ -358,10 +358,10 @@ export function tests() {
const storage = await createMockStorage(); const storage = await createMockStorage();
const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]); const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]);
txn.timelineEvents.insert({fragmentId: 1, eventIndex: 2, event, roomId}); txn.timelineEvents.tryInsert({fragmentId: 1, eventIndex: 2, event, roomId});
txn.timelineEvents.insert({fragmentId: 1, eventIndex: 3, event: myReaction, roomId}); txn.timelineEvents.tryInsert({fragmentId: 1, eventIndex: 3, event: myReaction, roomId});
await relationWriter.writeRelation(myReactionEntry, txn, new NullLogItem()); await relationWriter.writeRelation(myReactionEntry, txn, new NullLogItem());
txn.timelineEvents.insert({fragmentId: 1, eventIndex: 4, event: bobReaction, roomId}); txn.timelineEvents.tryInsert({fragmentId: 1, eventIndex: 4, event: bobReaction, roomId});
await relationWriter.writeRelation(bobReactionEntry, txn, new NullLogItem()); await relationWriter.writeRelation(bobReactionEntry, txn, new NullLogItem());
const updatedEntries = await relationWriter.writeRelation(myReactionRedactionEntry, txn, new NullLogItem()); const updatedEntries = await relationWriter.writeRelation(myReactionRedactionEntry, txn, new NullLogItem());
await txn.complete(); await txn.complete();

View file

@ -162,7 +162,11 @@ export class SyncWriter {
storageEntry.displayName = member.displayName; storageEntry.displayName = member.displayName;
storageEntry.avatarUrl = member.avatarUrl; storageEntry.avatarUrl = member.avatarUrl;
} }
txn.timelineEvents.insert(storageEntry, log); const couldInsert = await txn.timelineEvents.tryInsert(storageEntry, log);
if (!couldInsert) {
log.log({l: `could not write event, likely #504`, id: event.event_id}, log.level.Warn);
continue;
}
const entry = new EventEntry(storageEntry, this._fragmentIdComparer); const entry = new EventEntry(storageEntry, this._fragmentIdComparer);
entries.push(entry); entries.push(entry);
const updatedRelationTargetEntries = await this._relationWriter.writeRelation(entry, txn, log); const updatedRelationTargetEntries = await this._relationWriter.writeRelation(entry, txn, log);

View file

@ -261,15 +261,17 @@ export class TimelineEventStore {
return firstFoundKey && decodeEventIdKey(firstFoundKey).eventId; return firstFoundKey && decodeEventIdKey(firstFoundKey).eventId;
} }
/** Inserts a new entry into the store. The combination of roomId and eventKey should not exist yet, or an error is thrown. /** Inserts a new entry into the store.
* @param entry the entry to insert *
* @return nothing. To wait for the operation to finish, await the transaction it's part of. * If the event already exists in the store (either the eventKey or the event id
* @throws {StorageError} ... * are already known for the given roomId), this operation has no effect.
*
* Returns if the event was not yet known and the entry was written.
*/ */
insert(entry: TimelineEventEntry, log: LogItem): void { tryInsert(entry: TimelineEventEntry, log: LogItem): Promise<boolean> {
(entry as TimelineEventStorageEntry).key = encodeKey(entry.roomId, entry.fragmentId, entry.eventIndex); (entry as TimelineEventStorageEntry).key = encodeKey(entry.roomId, entry.fragmentId, entry.eventIndex);
(entry as TimelineEventStorageEntry).eventIdKey = encodeEventIdKey(entry.roomId, entry.event.event_id); (entry as TimelineEventStorageEntry).eventIdKey = encodeEventIdKey(entry.roomId, entry.event.event_id);
this._timelineStore.add(entry as TimelineEventStorageEntry, log); return this._timelineStore.tryAdd(entry as TimelineEventStorageEntry, log);
} }
/** Updates the entry into the store with the given [roomId, eventKey] combination. /** Updates the entry into the store with the given [roomId, eventKey] combination.