From e233caf7ac54ae978e9570c341a426dfe2f49c2c Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 12 May 2021 15:36:48 +0200 Subject: [PATCH] change order of type and scope in operation index key to scan per scope --- src/matrix/storage/idb/schema.js | 21 +++++++++++++++++++ .../storage/idb/stores/OperationStore.js | 12 +++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/matrix/storage/idb/schema.js b/src/matrix/storage/idb/schema.js index aeb85a4b..3af31d8d 100644 --- a/src/matrix/storage/idb/schema.js +++ b/src/matrix/storage/idb/schema.js @@ -2,6 +2,7 @@ import {iterateCursor, reqAsPromise} from "./utils.js"; import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js"; import {RoomMemberStore} from "./stores/RoomMemberStore.js"; import {SessionStore} from "./stores/SessionStore.js"; +import {encodeScopeTypeKey} from "./stores/OperationStore.js"; // FUNCTIONS SHOULD ONLY BE APPENDED!! // the index in the array is the database version @@ -14,6 +15,7 @@ export const schema = [ createAccountDataStore, createInviteStore, createArchivedRoomSummaryStore, + migrateOperationScopeIndex, ]; // TODO: how to deal with git merge conflicts of this array? @@ -114,4 +116,23 @@ function createInviteStore(db) { // v8 function createArchivedRoomSummaryStore(db) { db.createObjectStore("archivedRoomSummary", {keyPath: "summary.roomId"}); +} + +// v9 +async function migrateOperationScopeIndex(db, txn) { + try { + const operations = txn.objectStore("operations"); + operations.deleteIndex("byTypeAndScope"); + await iterateCursor(operations.openCursor(), (op, key, cur) => { + const {typeScopeKey} = op; + delete op.typeScopeKey; + const [type, scope] = typeScopeKey.split("|"); + op.scopeTypeKey = encodeScopeTypeKey(scope, type); + cur.update(op); + }); + operations.createIndex("byScopeAndType", "scopeTypeKey", {unique: false}); + } catch (err) { + txn.abort(); + console.error("could not migrate operations", err.stack); + } } \ No newline at end of file diff --git a/src/matrix/storage/idb/stores/OperationStore.js b/src/matrix/storage/idb/stores/OperationStore.js index 47207cc4..4d101430 100644 --- a/src/matrix/storage/idb/stores/OperationStore.js +++ b/src/matrix/storage/idb/stores/OperationStore.js @@ -14,8 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -function encodeTypeScopeKey(type, scope) { - return `${type}|${scope}`; +export function encodeScopeTypeKey(scope, type) { + return `${scope}|${type}`; } export class OperationStore { @@ -28,10 +28,10 @@ export class OperationStore { } async getAllByTypeAndScope(type, scope) { - const key = encodeTypeScopeKey(type, scope); + const key = encodeScopeTypeKey(scope, type); const results = []; - await this._store.index("byTypeAndScope").iterateWhile(key, value => { - if (value.typeScopeKey !== key) { + await this._store.index("byScopeAndType").iterateWhile(key, value => { + if (value.scopeTypeKey !== key) { return false; } results.push(value); @@ -41,7 +41,7 @@ export class OperationStore { } add(operation) { - operation.typeScopeKey = encodeTypeScopeKey(operation.type, operation.scope); + operation.scopeTypeKey = encodeScopeTypeKey(operation.scope, operation.type); this._store.add(operation); }