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 {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);
}
}

View file

@ -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);
}