From 5d139dff438eaea33ea3df3d21be9840efcfc85f Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 12 May 2021 15:38:11 +0200 Subject: [PATCH] Support deleting data in several stores for room id --- src/matrix/storage/idb/QueryTarget.js | 12 ++++++++++-- .../idb/stores/GroupSessionDecryptionStore.js | 10 ++++++++++ .../storage/idb/stores/InboundGroupSessionStore.js | 10 ++++++++++ src/matrix/storage/idb/stores/OperationStore.js | 13 +++++++++++++ src/matrix/storage/idb/stores/PendingEventStore.js | 7 +++++++ src/matrix/storage/idb/stores/TimelineEventStore.js | 7 +++++++ .../storage/idb/stores/TimelineFragmentStore.js | 4 ++++ 7 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/matrix/storage/idb/QueryTarget.js b/src/matrix/storage/idb/QueryTarget.js index c0c8ed2c..348e67f9 100644 --- a/src/matrix/storage/idb/QueryTarget.js +++ b/src/matrix/storage/idb/QueryTarget.js @@ -113,10 +113,18 @@ export class QueryTarget { return maxKey; } + + async iterateValues(range, callback) { + const cursor = this._target.openCursor(range, "next"); + await iterateCursor(cursor, (value, key, cur) => { + return {done: callback(value, key, cur)}; + }); + } + async iterateKeys(range, callback) { const cursor = this._target.openKeyCursor(range, "next"); - await iterateCursor(cursor, (_, key) => { - return {done: callback(key)}; + await iterateCursor(cursor, (_, key, cur) => { + return {done: callback(key, cur)}; }); } diff --git a/src/matrix/storage/idb/stores/GroupSessionDecryptionStore.js b/src/matrix/storage/idb/stores/GroupSessionDecryptionStore.js index 8f8df3e7..df3c6fbe 100644 --- a/src/matrix/storage/idb/stores/GroupSessionDecryptionStore.js +++ b/src/matrix/storage/idb/stores/GroupSessionDecryptionStore.js @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +import {MIN_UNICODE, MAX_UNICODE} from "./common.js"; + function encodeKey(roomId, sessionId, messageIndex) { return `${roomId}|${sessionId}|${messageIndex}`; } @@ -31,4 +33,12 @@ export class GroupSessionDecryptionStore { decryption.key = encodeKey(roomId, sessionId, messageIndex); this._store.put(decryption); } + + removeAllForRoom(roomId) { + const range = IDBKeyRange.bound( + encodeKey(roomId, MIN_UNICODE, MIN_UNICODE), + encodeKey(roomId, MAX_UNICODE, MAX_UNICODE) + ); + this._store.delete(range); + } } diff --git a/src/matrix/storage/idb/stores/InboundGroupSessionStore.js b/src/matrix/storage/idb/stores/InboundGroupSessionStore.js index d05c67ff..928f7572 100644 --- a/src/matrix/storage/idb/stores/InboundGroupSessionStore.js +++ b/src/matrix/storage/idb/stores/InboundGroupSessionStore.js @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +import {MIN_UNICODE, MAX_UNICODE} from "./common.js"; + function encodeKey(roomId, senderKey, sessionId) { return `${roomId}|${senderKey}|${sessionId}`; } @@ -37,4 +39,12 @@ export class InboundGroupSessionStore { session.key = encodeKey(session.roomId, session.senderKey, session.sessionId); this._store.put(session); } + + removeAllForRoom(roomId) { + const range = IDBKeyRange.bound( + encodeKey(roomId, MIN_UNICODE, MIN_UNICODE), + encodeKey(roomId, MAX_UNICODE, MAX_UNICODE) + ); + this._store.delete(range); + } } diff --git a/src/matrix/storage/idb/stores/OperationStore.js b/src/matrix/storage/idb/stores/OperationStore.js index 4d101430..09e4552f 100644 --- a/src/matrix/storage/idb/stores/OperationStore.js +++ b/src/matrix/storage/idb/stores/OperationStore.js @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ +import {MIN_UNICODE, MAX_UNICODE} from "./common.js"; export function encodeScopeTypeKey(scope, type) { return `${scope}|${type}`; @@ -52,4 +53,16 @@ export class OperationStore { remove(id) { this._store.delete(id); } + + async removeAllForScope(scope) { + const range = IDBKeyRange.bound( + encodeScopeTypeKey(scope, MIN_UNICODE), + encodeScopeTypeKey(scope, MAX_UNICODE) + ); + const index = this._store.index("byScopeAndType"); + await index.iterateValues(range, (_, __, cur) => { + cur.delete(); + return true; + }); + } } diff --git a/src/matrix/storage/idb/stores/PendingEventStore.js b/src/matrix/storage/idb/stores/PendingEventStore.js index 3659299f..a6727ec5 100644 --- a/src/matrix/storage/idb/stores/PendingEventStore.js +++ b/src/matrix/storage/idb/stores/PendingEventStore.js @@ -68,4 +68,11 @@ export class PendingEventStore { getAll() { return this._eventStore.selectAll(); } + + removeAllForRoom(roomId) { + const minKey = encodeKey(roomId, KeyLimits.minStorageKey); + const maxKey = encodeKey(roomId, KeyLimits.maxStorageKey); + const range = IDBKeyRange.bound(minKey, maxKey); + this._eventStore.delete(range); + } } diff --git a/src/matrix/storage/idb/stores/TimelineEventStore.js b/src/matrix/storage/idb/stores/TimelineEventStore.js index 32468b21..9c40aad2 100644 --- a/src/matrix/storage/idb/stores/TimelineEventStore.js +++ b/src/matrix/storage/idb/stores/TimelineEventStore.js @@ -257,4 +257,11 @@ export class TimelineEventStore { getByEventId(roomId, eventId) { return this._timelineStore.index("byEventId").get(encodeEventIdKey(roomId, eventId)); } + + removeAllForRoom(roomId) { + const minKey = encodeKey(roomId, KeyLimits.minStorageKey, KeyLimits.minStorageKey); + const maxKey = encodeKey(roomId, KeyLimits.maxStorageKey, KeyLimits.maxStorageKey); + const range = IDBKeyRange.bound(minKey, maxKey); + this._timelineStore.delete(range); + } } diff --git a/src/matrix/storage/idb/stores/TimelineFragmentStore.js b/src/matrix/storage/idb/stores/TimelineFragmentStore.js index 8f11cb3e..10eaede1 100644 --- a/src/matrix/storage/idb/stores/TimelineFragmentStore.js +++ b/src/matrix/storage/idb/stores/TimelineFragmentStore.js @@ -72,4 +72,8 @@ export class TimelineFragmentStore { get(roomId, fragmentId) { return this._store.get(encodeKey(roomId, fragmentId)); } + + removeAllForRoom(roomId) { + this._store.delete(this._allRange(roomId)); + } }