2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-08-10 02:26:20 +05:30
|
|
|
import { encodeUint32, decodeUint32 } from "../utils";
|
2021-08-10 02:14:07 +05:30
|
|
|
import {KeyLimits} from "../../common";
|
2019-07-01 13:30:29 +05:30
|
|
|
|
|
|
|
function encodeKey(roomId, queueIndex) {
|
|
|
|
return `${roomId}|${encodeUint32(queueIndex)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeKey(key) {
|
|
|
|
const [roomId, encodedQueueIndex] = key.split("|");
|
|
|
|
const queueIndex = decodeUint32(encodedQueueIndex);
|
|
|
|
return {roomId, queueIndex};
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
export class PendingEventStore {
|
2019-07-01 13:30:29 +05:30
|
|
|
constructor(eventStore) {
|
|
|
|
this._eventStore = eventStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getMaxQueueIndex(roomId) {
|
2021-06-02 16:01:13 +05:30
|
|
|
const range = this._eventStore.IDBKeyRange.bound(
|
2020-10-26 15:04:35 +05:30
|
|
|
encodeKey(roomId, KeyLimits.minStorageKey),
|
|
|
|
encodeKey(roomId, KeyLimits.maxStorageKey),
|
2019-07-01 13:30:29 +05:30
|
|
|
false,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
const maxKey = await this._eventStore.findMaxKey(range);
|
|
|
|
if (maxKey) {
|
|
|
|
return decodeKey(maxKey).queueIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 01:33:57 +05:30
|
|
|
remove(roomId, queueIndex) {
|
2021-06-02 16:01:13 +05:30
|
|
|
const keyRange = this._eventStore.IDBKeyRange.only(encodeKey(roomId, queueIndex));
|
2019-07-27 01:33:57 +05:30
|
|
|
this._eventStore.delete(keyRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
async exists(roomId, queueIndex) {
|
2021-06-02 16:01:13 +05:30
|
|
|
const keyRange = this._eventStore.IDBKeyRange.only(encodeKey(roomId, queueIndex));
|
2020-09-09 16:12:26 +05:30
|
|
|
const key = await this._eventStore.getKey(keyRange);
|
2019-07-27 01:33:57 +05:30
|
|
|
return !!key;
|
|
|
|
}
|
|
|
|
|
2019-07-01 13:30:29 +05:30
|
|
|
add(pendingEvent) {
|
|
|
|
pendingEvent.key = encodeKey(pendingEvent.roomId, pendingEvent.queueIndex);
|
2020-09-29 15:21:14 +05:30
|
|
|
this._eventStore.add(pendingEvent);
|
2019-07-01 13:30:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
update(pendingEvent) {
|
2020-09-29 15:21:14 +05:30
|
|
|
this._eventStore.put(pendingEvent);
|
2019-07-01 13:30:29 +05:30
|
|
|
}
|
|
|
|
|
2019-07-27 02:03:33 +05:30
|
|
|
getAll() {
|
2019-07-01 13:30:29 +05:30
|
|
|
return this._eventStore.selectAll();
|
|
|
|
}
|
2021-05-12 19:08:11 +05:30
|
|
|
|
|
|
|
removeAllForRoom(roomId) {
|
|
|
|
const minKey = encodeKey(roomId, KeyLimits.minStorageKey);
|
|
|
|
const maxKey = encodeKey(roomId, KeyLimits.maxStorageKey);
|
2021-06-02 16:01:13 +05:30
|
|
|
const range = this._eventStore.IDBKeyRange.bound(minKey, maxKey);
|
2021-05-12 19:08:11 +05:30
|
|
|
this._eventStore.delete(range);
|
|
|
|
}
|
2019-07-01 13:30:29 +05:30
|
|
|
}
|