change order of type and scope in operation index key to scan per scope

This commit is contained in:
Bruno Windels 2021-05-12 15:36:48 +02:00
parent 2d2ec25f86
commit e233caf7ac
2 changed files with 27 additions and 6 deletions

View file

@ -2,6 +2,7 @@ import {iterateCursor, reqAsPromise} from "./utils.js";
import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js"; import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js";
import {RoomMemberStore} from "./stores/RoomMemberStore.js"; import {RoomMemberStore} from "./stores/RoomMemberStore.js";
import {SessionStore} from "./stores/SessionStore.js"; import {SessionStore} from "./stores/SessionStore.js";
import {encodeScopeTypeKey} from "./stores/OperationStore.js";
// FUNCTIONS SHOULD ONLY BE APPENDED!! // FUNCTIONS SHOULD ONLY BE APPENDED!!
// the index in the array is the database version // the index in the array is the database version
@ -14,6 +15,7 @@ export const schema = [
createAccountDataStore, createAccountDataStore,
createInviteStore, createInviteStore,
createArchivedRoomSummaryStore, createArchivedRoomSummaryStore,
migrateOperationScopeIndex,
]; ];
// TODO: how to deal with git merge conflicts of this array? // TODO: how to deal with git merge conflicts of this array?
@ -115,3 +117,22 @@ function createInviteStore(db) {
function createArchivedRoomSummaryStore(db) { function createArchivedRoomSummaryStore(db) {
db.createObjectStore("archivedRoomSummary", {keyPath: "summary.roomId"}); 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);
}
}

View file

@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
function encodeTypeScopeKey(type, scope) { export function encodeScopeTypeKey(scope, type) {
return `${type}|${scope}`; return `${scope}|${type}`;
} }
export class OperationStore { export class OperationStore {
@ -28,10 +28,10 @@ export class OperationStore {
} }
async getAllByTypeAndScope(type, scope) { async getAllByTypeAndScope(type, scope) {
const key = encodeTypeScopeKey(type, scope); const key = encodeScopeTypeKey(scope, type);
const results = []; const results = [];
await this._store.index("byTypeAndScope").iterateWhile(key, value => { await this._store.index("byScopeAndType").iterateWhile(key, value => {
if (value.typeScopeKey !== key) { if (value.scopeTypeKey !== key) {
return false; return false;
} }
results.push(value); results.push(value);
@ -41,7 +41,7 @@ export class OperationStore {
} }
add(operation) { add(operation) {
operation.typeScopeKey = encodeTypeScopeKey(operation.type, operation.scope); operation.scopeTypeKey = encodeScopeTypeKey(operation.scope, operation.type);
this._store.add(operation); this._store.add(operation);
} }