2025-03-10 16:19:11 +01:00
|
|
|
import { BlobElementEntity, ElementEntity, ListElementEntity, SomeEntity } from "../../common/EntityTypes.js"
|
|
|
|
|
import { EntityRestClient } from "./EntityRestClient.js"
|
2024-08-28 17:47:42 +02:00
|
|
|
import { firstBiggerThanSecond } from "../../common/utils/EntityUtils.js"
|
2025-03-10 16:19:11 +01:00
|
|
|
import { CacheStorage, expandId, LastUpdateTime } from "./DefaultEntityRestCache.js"
|
|
|
|
|
import { assertNotNull, clone, getFromMap, getTypeId, remove, TypeRef } from "@tutao/tutanota-utils"
|
2022-12-27 15:37:40 +01:00
|
|
|
import { CustomCacheHandlerMap } from "./CustomCacheHandler.js"
|
2025-04-29 10:18:18 +02:00
|
|
|
import { resolveClientTypeReference, resolveServerTypeReference } from "../../common/EntityFunctions.js"
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
import { Type as TypeId } from "../../common/EntityConstants.js"
|
2023-01-12 16:48:28 +01:00
|
|
|
import { ProgrammingError } from "../../common/error/ProgrammingError.js"
|
2024-08-28 17:47:42 +02:00
|
|
|
import { customIdToBase64Url, ensureBase64Ext } from "../offline/OfflineStorage.js"
|
2022-01-12 14:43:01 +01:00
|
|
|
|
2022-10-21 15:53:39 +02:00
|
|
|
/** Cache for a single list. */
|
|
|
|
|
type ListCache = {
|
2022-01-12 14:43:01 +01:00
|
|
|
/** All entities loaded inside the range. */
|
2022-12-27 15:37:40 +01:00
|
|
|
allRange: Id[]
|
|
|
|
|
lowerRangeId: Id
|
|
|
|
|
upperRangeId: Id
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
/** All the entities loaded, inside or outside the range (e.g. load for a single entity). */
|
2022-01-12 14:43:01 +01:00
|
|
|
elements: Map<Id, ListElementEntity>
|
2022-10-21 15:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Map from list id to list cache. */
|
|
|
|
|
type ListTypeCache = Map<Id, ListCache>
|
|
|
|
|
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
type BlobElementCache = {
|
|
|
|
|
/** All the entities loaded, inside or outside the range (e.g. load for a single entity). */
|
|
|
|
|
elements: Map<Id, BlobElementEntity>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Map from list id to list cache. */
|
|
|
|
|
type BlobElementTypeCache = Map<Id, BlobElementCache>
|
|
|
|
|
|
2022-10-21 15:53:39 +02:00
|
|
|
export interface EphemeralStorageInitArgs {
|
2022-12-27 15:37:40 +01:00
|
|
|
userId: Id
|
2022-10-21 15:53:39 +02:00
|
|
|
}
|
2022-01-12 14:43:01 +01:00
|
|
|
|
|
|
|
|
export class EphemeralCacheStorage implements CacheStorage {
|
2022-10-21 15:53:39 +02:00
|
|
|
/** Path to id to entity map. */
|
2022-01-12 14:43:01 +01:00
|
|
|
private readonly entities: Map<string, Map<Id, ElementEntity>> = new Map()
|
|
|
|
|
private readonly lists: Map<string, ListTypeCache> = new Map()
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
private readonly blobEntities: Map<string, BlobElementTypeCache> = new Map()
|
2022-07-04 14:55:17 +02:00
|
|
|
private readonly customCacheHandlerMap: CustomCacheHandlerMap = new CustomCacheHandlerMap()
|
2022-02-28 17:23:22 +01:00
|
|
|
private lastUpdateTime: number | null = null
|
2022-10-21 15:53:39 +02:00
|
|
|
private userId: Id | null = null
|
2022-11-03 11:04:26 +01:00
|
|
|
private lastBatchIdPerGroup = new Map<Id, Id>()
|
2022-10-21 15:53:39 +02:00
|
|
|
|
2022-12-27 15:37:40 +01:00
|
|
|
init({ userId }: EphemeralStorageInitArgs) {
|
2022-10-21 15:53:39 +02:00
|
|
|
this.userId = userId
|
|
|
|
|
}
|
2022-01-12 14:43:01 +01:00
|
|
|
|
2022-07-20 15:28:38 +02:00
|
|
|
deinit() {
|
2022-10-21 15:53:39 +02:00
|
|
|
this.userId = null
|
2022-07-20 15:28:38 +02:00
|
|
|
this.entities.clear()
|
|
|
|
|
this.lists.clear()
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
this.blobEntities.clear()
|
2022-07-20 15:28:38 +02:00
|
|
|
this.lastUpdateTime = null
|
2022-11-03 11:04:26 +01:00
|
|
|
this.lastBatchIdPerGroup.clear()
|
2022-07-20 15:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-12 14:43:01 +01:00
|
|
|
/**
|
|
|
|
|
* Get a given entity from the cache, expects that you have already checked for existence
|
|
|
|
|
*/
|
2024-08-28 17:47:42 +02:00
|
|
|
async get<T extends SomeEntity>(typeRef: TypeRef<T>, listId: Id | null, elementId: Id): Promise<T | null> {
|
2022-01-12 14:43:01 +01:00
|
|
|
// We downcast because we can't prove that map has correct entity on the type level
|
2025-03-10 16:19:11 +01:00
|
|
|
const type = getTypeId(typeRef)
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
elementId = ensureBase64Ext(typeModel, elementId)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
switch (typeModel.type) {
|
2023-01-12 16:48:28 +01:00
|
|
|
case TypeId.Element:
|
2025-03-10 16:19:11 +01:00
|
|
|
return clone((this.entities.get(type)?.get(elementId) as T | undefined) ?? null)
|
2023-01-12 16:48:28 +01:00
|
|
|
case TypeId.ListElement:
|
2025-03-10 16:19:11 +01:00
|
|
|
return clone((this.lists.get(type)?.get(assertNotNull(listId))?.elements.get(elementId) as T | undefined) ?? null)
|
2023-01-12 16:48:28 +01:00
|
|
|
case TypeId.BlobElement:
|
2025-03-10 16:19:11 +01:00
|
|
|
return clone((this.blobEntities.get(type)?.get(assertNotNull(listId))?.elements.get(elementId) as T | undefined) ?? null)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
default:
|
2023-01-12 16:48:28 +01:00
|
|
|
throw new ProgrammingError("must be a persistent type")
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:47:42 +02:00
|
|
|
async deleteIfExists<T>(typeRef: TypeRef<T>, listId: Id | null, elementId: Id): Promise<void> {
|
2025-03-10 16:19:11 +01:00
|
|
|
const type = getTypeId(typeRef)
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
elementId = ensureBase64Ext(typeModel, elementId)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
switch (typeModel.type) {
|
2023-01-12 16:48:28 +01:00
|
|
|
case TypeId.Element:
|
2025-03-10 16:19:11 +01:00
|
|
|
this.entities.get(type)?.delete(elementId)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
break
|
2025-01-03 10:16:07 +01:00
|
|
|
case TypeId.ListElement: {
|
2025-03-10 16:19:11 +01:00
|
|
|
const cache = this.lists.get(type)?.get(assertNotNull(listId))
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
if (cache != null) {
|
2024-08-28 17:47:42 +02:00
|
|
|
cache.elements.delete(elementId)
|
|
|
|
|
remove(cache.allRange, elementId)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
}
|
|
|
|
|
break
|
2025-01-03 10:16:07 +01:00
|
|
|
}
|
2023-01-12 16:48:28 +01:00
|
|
|
case TypeId.BlobElement:
|
2025-03-10 16:19:11 +01:00
|
|
|
this.blobEntities.get(type)?.get(assertNotNull(listId))?.elements.delete(elementId)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
break
|
|
|
|
|
default:
|
2023-01-12 16:48:28 +01:00
|
|
|
throw new ProgrammingError("must be a persistent type")
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private addElementEntity<T extends ElementEntity>(typeRef: TypeRef<T>, id: Id, entity: T) {
|
2025-03-10 16:19:11 +01:00
|
|
|
getFromMap(this.entities, getTypeId(typeRef), () => new Map()).set(id, entity)
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:47:42 +02:00
|
|
|
async isElementIdInCacheRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, elementId: Id): Promise<boolean> {
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
elementId = ensureBase64Ext(typeModel, elementId)
|
|
|
|
|
|
2025-03-10 16:19:11 +01:00
|
|
|
const cache = this.lists.get(getTypeId(typeRef))?.get(listId)
|
2024-08-28 17:47:42 +02:00
|
|
|
return cache != null && !firstBiggerThanSecond(elementId, cache.upperRangeId) && !firstBiggerThanSecond(cache.lowerRangeId, elementId)
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async put(originalEntity: SomeEntity): Promise<void> {
|
|
|
|
|
const entity = clone(originalEntity)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
const typeRef = entity._type
|
2025-03-10 16:19:11 +01:00
|
|
|
const typeModel = await resolveServerTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
let { listId, elementId } = expandId(originalEntity._id)
|
|
|
|
|
elementId = ensureBase64Ext(typeModel, elementId)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
switch (typeModel.type) {
|
2025-01-03 10:16:07 +01:00
|
|
|
case TypeId.Element: {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
const elementEntity = entity as ElementEntity
|
2024-08-28 17:47:42 +02:00
|
|
|
this.addElementEntity(elementEntity._type, elementId, elementEntity)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
break
|
2025-01-03 10:16:07 +01:00
|
|
|
}
|
|
|
|
|
case TypeId.ListElement: {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
const listElementEntity = entity as ListElementEntity
|
|
|
|
|
const listElementTypeRef = typeRef as TypeRef<ListElementEntity>
|
2024-08-28 17:47:42 +02:00
|
|
|
listId = listId as Id
|
|
|
|
|
await this.putListElement(listElementTypeRef, listId, elementId, listElementEntity)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
break
|
2025-01-03 10:16:07 +01:00
|
|
|
}
|
|
|
|
|
case TypeId.BlobElement: {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
const blobElementEntity = entity as BlobElementEntity
|
|
|
|
|
const blobTypeRef = typeRef as TypeRef<BlobElementEntity>
|
2024-08-28 17:47:42 +02:00
|
|
|
listId = listId as Id
|
|
|
|
|
await this.putBlobElement(blobTypeRef, listId, elementId, blobElementEntity)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
break
|
2025-01-03 10:16:07 +01:00
|
|
|
}
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
default:
|
2023-01-12 16:48:28 +01:00
|
|
|
throw new ProgrammingError("must be a persistent type")
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:47:42 +02:00
|
|
|
private async putBlobElement(typeRef: TypeRef<BlobElementEntity>, listId: Id, elementId: Id, entity: BlobElementEntity) {
|
2025-03-10 16:19:11 +01:00
|
|
|
const cache = this.blobEntities.get(getTypeId(typeRef))?.get(listId)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
if (cache == null) {
|
|
|
|
|
// first element in this list
|
|
|
|
|
const newCache = {
|
|
|
|
|
elements: new Map([[elementId, entity]]),
|
|
|
|
|
}
|
2025-03-10 16:19:11 +01:00
|
|
|
getFromMap(this.blobEntities, getTypeId(typeRef), () => new Map()).set(listId, newCache)
|
2022-01-12 14:43:01 +01:00
|
|
|
} else {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
// if the element already exists in the cache, overwrite it
|
|
|
|
|
cache.elements.set(elementId, entity)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:47:42 +02:00
|
|
|
/** prcondition: elementId is converted to base64ext if necessary */
|
|
|
|
|
private async putListElement(typeRef: TypeRef<ListElementEntity>, listId: Id, elementId: Id, entity: ListElementEntity) {
|
2025-03-10 16:19:11 +01:00
|
|
|
const typeId = getTypeId(typeRef)
|
|
|
|
|
const cache = this.lists.get(typeId)?.get(listId)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
if (cache == null) {
|
|
|
|
|
// first element in this list
|
|
|
|
|
const newCache = {
|
|
|
|
|
allRange: [elementId],
|
|
|
|
|
lowerRangeId: elementId,
|
|
|
|
|
upperRangeId: elementId,
|
|
|
|
|
elements: new Map([[elementId, entity]]),
|
|
|
|
|
}
|
2025-03-10 16:19:11 +01:00
|
|
|
getFromMap(this.lists, typeId, () => new Map()).set(listId, newCache)
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
} else {
|
|
|
|
|
// if the element already exists in the cache, overwrite it
|
|
|
|
|
// add new element to existing list if necessary
|
|
|
|
|
cache.elements.set(elementId, entity)
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
if (await this.isElementIdInCacheRange(typeRef, listId, customIdToBase64Url(typeModel, elementId))) {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
this.insertIntoRange(cache.allRange, elementId)
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:47:42 +02:00
|
|
|
/** precondition: elementId is converted to base64ext if necessary */
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
private insertIntoRange(allRange: Array<Id>, elementId: Id) {
|
2022-01-12 14:43:01 +01:00
|
|
|
for (let i = 0; i < allRange.length; i++) {
|
|
|
|
|
const rangeElement = allRange[i]
|
|
|
|
|
if (firstBiggerThanSecond(rangeElement, elementId)) {
|
|
|
|
|
allRange.splice(i, 0, elementId)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (rangeElement === elementId) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
allRange.push(elementId)
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:47:42 +02:00
|
|
|
async provideFromRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, startElementId: Id, count: number, reverse: boolean): Promise<T[]> {
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
startElementId = ensureBase64Ext(typeModel, startElementId)
|
|
|
|
|
|
2025-03-10 16:19:11 +01:00
|
|
|
const listCache = this.lists.get(getTypeId(typeRef))?.get(listId)
|
2022-01-12 14:43:01 +01:00
|
|
|
|
|
|
|
|
if (listCache == null) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let range = listCache.allRange
|
|
|
|
|
let ids: Id[] = []
|
|
|
|
|
if (reverse) {
|
|
|
|
|
let i
|
|
|
|
|
for (i = range.length - 1; i >= 0; i--) {
|
2024-08-28 17:47:42 +02:00
|
|
|
if (firstBiggerThanSecond(startElementId, range[i])) {
|
2022-01-12 14:43:01 +01:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i >= 0) {
|
|
|
|
|
let startIndex = i + 1 - count
|
2022-12-27 15:37:40 +01:00
|
|
|
if (startIndex < 0) {
|
2024-08-28 17:47:42 +02:00
|
|
|
// startElementId index may be negative if more elements have been requested than available when getting elements reverse.
|
2022-01-12 14:43:01 +01:00
|
|
|
startIndex = 0
|
|
|
|
|
}
|
|
|
|
|
ids = range.slice(startIndex, i + 1)
|
|
|
|
|
ids.reverse()
|
|
|
|
|
} else {
|
|
|
|
|
ids = []
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-08-28 17:47:42 +02:00
|
|
|
const i = range.findIndex((id) => firstBiggerThanSecond(id, startElementId))
|
2022-01-12 14:43:01 +01:00
|
|
|
ids = range.slice(i, i + count)
|
|
|
|
|
}
|
|
|
|
|
let result: T[] = []
|
|
|
|
|
for (let a = 0; a < ids.length; a++) {
|
2022-12-27 15:37:40 +01:00
|
|
|
result.push(clone(listCache.elements.get(ids[a]) as T))
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 08:38:58 +02:00
|
|
|
async provideMultiple<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, elementIds: Id[]): Promise<Array<T>> {
|
2025-03-10 16:19:11 +01:00
|
|
|
const listCache = this.lists.get(getTypeId(typeRef))?.get(listId)
|
2024-08-07 08:38:58 +02:00
|
|
|
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
elementIds = elementIds.map((el) => ensureBase64Ext(typeModel, el))
|
|
|
|
|
|
2024-08-07 08:38:58 +02:00
|
|
|
if (listCache == null) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
let result: T[] = []
|
|
|
|
|
for (let a = 0; a < elementIds.length; a++) {
|
|
|
|
|
result.push(clone(listCache.elements.get(elementIds[a]) as T))
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-10 16:19:11 +01:00
|
|
|
async getRangeForList<T extends ListElementEntity>(
|
|
|
|
|
typeRef: TypeRef<T>,
|
|
|
|
|
listId: Id,
|
|
|
|
|
): Promise<{
|
|
|
|
|
lower: Id
|
|
|
|
|
upper: Id
|
|
|
|
|
} | null> {
|
|
|
|
|
const listCache = this.lists.get(getTypeId(typeRef))?.get(listId)
|
2022-01-12 14:43:01 +01:00
|
|
|
|
|
|
|
|
if (listCache == null) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
return {
|
|
|
|
|
lower: customIdToBase64Url(typeModel, listCache.lowerRangeId),
|
|
|
|
|
upper: customIdToBase64Url(typeModel, listCache.upperRangeId),
|
|
|
|
|
}
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:47:42 +02:00
|
|
|
async setUpperRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, upperId: Id): Promise<void> {
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
upperId = ensureBase64Ext(typeModel, upperId)
|
2025-03-10 16:19:11 +01:00
|
|
|
const listCache = this.lists.get(getTypeId(typeRef))?.get(listId)
|
2022-01-12 14:43:01 +01:00
|
|
|
if (listCache == null) {
|
|
|
|
|
throw new Error("list does not exist")
|
|
|
|
|
}
|
2024-08-28 17:47:42 +02:00
|
|
|
listCache.upperRangeId = upperId
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:47:42 +02:00
|
|
|
async setLowerRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, lowerId: Id): Promise<void> {
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
lowerId = ensureBase64Ext(typeModel, lowerId)
|
2025-03-10 16:19:11 +01:00
|
|
|
const listCache = this.lists.get(getTypeId(typeRef))?.get(listId)
|
2022-01-12 14:43:01 +01:00
|
|
|
if (listCache == null) {
|
|
|
|
|
throw new Error("list does not exist")
|
|
|
|
|
}
|
2024-08-28 17:47:42 +02:00
|
|
|
listCache.lowerRangeId = lowerId
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new list cache if there is none. Resets everything but elements.
|
|
|
|
|
* @param typeRef
|
|
|
|
|
* @param listId
|
|
|
|
|
* @param lower
|
|
|
|
|
* @param upper
|
|
|
|
|
*/
|
|
|
|
|
async setNewRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, lower: Id, upper: Id): Promise<void> {
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
lower = ensureBase64Ext(typeModel, lower)
|
|
|
|
|
upper = ensureBase64Ext(typeModel, upper)
|
|
|
|
|
|
2025-03-10 16:19:11 +01:00
|
|
|
const typeId = getTypeId(typeRef)
|
|
|
|
|
const listCache = this.lists.get(typeId)?.get(listId)
|
2022-01-12 14:43:01 +01:00
|
|
|
if (listCache == null) {
|
2025-03-10 16:19:11 +01:00
|
|
|
getFromMap(this.lists, typeId, () => new Map()).set(listId, {
|
2022-01-12 14:43:01 +01:00
|
|
|
allRange: [],
|
|
|
|
|
lowerRangeId: lower,
|
|
|
|
|
upperRangeId: upper,
|
2022-12-27 15:37:40 +01:00
|
|
|
elements: new Map(),
|
2022-01-12 14:43:01 +01:00
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
listCache.lowerRangeId = lower
|
|
|
|
|
listCache.upperRangeId = upper
|
|
|
|
|
listCache.allRange = []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getIdsInRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id): Promise<Array<Id>> {
|
2025-04-29 10:18:18 +02:00
|
|
|
const typeModel = await resolveClientTypeReference(typeRef)
|
2024-08-28 17:47:42 +02:00
|
|
|
return (
|
|
|
|
|
this.lists
|
2025-03-10 16:19:11 +01:00
|
|
|
.get(getTypeId(typeRef))
|
2024-08-28 17:47:42 +02:00
|
|
|
?.get(listId)
|
|
|
|
|
?.allRange.map((elementId) => {
|
|
|
|
|
return customIdToBase64Url(typeModel, elementId)
|
|
|
|
|
}) ?? []
|
|
|
|
|
)
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-03 11:04:26 +01:00
|
|
|
async getLastBatchIdForGroup(groupId: Id): Promise<Id | null> {
|
|
|
|
|
return this.lastBatchIdPerGroup.get(groupId) ?? null
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-03 11:04:26 +01:00
|
|
|
async putLastBatchIdForGroup(groupId: Id, batchId: Id): Promise<void> {
|
|
|
|
|
this.lastBatchIdPerGroup.set(groupId, batchId)
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
purgeStorage(): Promise<void> {
|
2022-12-27 15:37:40 +01:00
|
|
|
return Promise.resolve()
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-07 17:09:25 +02:00
|
|
|
async getLastUpdateTime(): Promise<LastUpdateTime> {
|
2022-12-27 15:37:40 +01:00
|
|
|
return this.lastUpdateTime ? { type: "recorded", time: this.lastUpdateTime } : { type: "never" }
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-28 17:23:22 +01:00
|
|
|
async putLastUpdateTime(value: number): Promise<void> {
|
|
|
|
|
this.lastUpdateTime = value
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|
2022-05-24 18:31:01 +02:00
|
|
|
|
|
|
|
|
async getWholeList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id): Promise<Array<T>> {
|
2025-03-10 16:19:11 +01:00
|
|
|
const listCache = this.lists.get(getTypeId(typeRef))?.get(listId)
|
2022-05-24 18:31:01 +02:00
|
|
|
|
|
|
|
|
if (listCache == null) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-27 15:37:40 +01:00
|
|
|
return listCache.allRange.map((id) => clone(listCache.elements.get(id) as T))
|
2022-05-24 18:31:01 +02:00
|
|
|
}
|
2022-07-04 14:55:17 +02:00
|
|
|
|
|
|
|
|
getCustomCacheHandlerMap(entityRestClient: EntityRestClient): CustomCacheHandlerMap {
|
|
|
|
|
return this.customCacheHandlerMap
|
|
|
|
|
}
|
2022-10-21 15:53:39 +02:00
|
|
|
|
|
|
|
|
getUserId(): Id {
|
|
|
|
|
return assertNotNull(this.userId, "No user id, not initialized?")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deleteAllOwnedBy(owner: Id): Promise<void> {
|
|
|
|
|
for (const typeMap of this.entities.values()) {
|
|
|
|
|
for (const [id, entity] of typeMap.entries()) {
|
|
|
|
|
if (entity._ownerGroup === owner) {
|
|
|
|
|
typeMap.delete(id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const cacheForType of this.lists.values()) {
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
this.deleteAllOwnedByFromCache(cacheForType, owner)
|
|
|
|
|
}
|
|
|
|
|
for (const cacheForType of this.blobEntities.values()) {
|
|
|
|
|
this.deleteAllOwnedByFromCache(cacheForType, owner)
|
|
|
|
|
}
|
|
|
|
|
this.lastBatchIdPerGroup.delete(owner)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 11:57:46 +02:00
|
|
|
async deleteWholeList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id): Promise<void> {
|
2025-03-10 16:19:11 +01:00
|
|
|
this.lists.get(getTypeId(typeRef))?.delete(listId)
|
2024-09-09 11:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
private deleteAllOwnedByFromCache(cacheForType: Map<Id, ListCache | BlobElementCache>, owner: string) {
|
|
|
|
|
// If we find at least one element in the list that is owned by our target owner, we delete the entire list.
|
|
|
|
|
// This is OK in most cases because the vast majority of lists are single owner.
|
|
|
|
|
// For the other cases, we are just clearing the cache a bit sooner than needed.
|
|
|
|
|
const listIdsToDelete: string[] = []
|
|
|
|
|
for (const [listId, listCache] of cacheForType.entries()) {
|
|
|
|
|
for (const [id, element] of listCache.elements.entries()) {
|
|
|
|
|
if (element._ownerGroup === owner) {
|
|
|
|
|
listIdsToDelete.push(listId)
|
|
|
|
|
break
|
2022-10-21 15:53:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
add MailDetails feature, #4719
server issues: 1276, 1271, 1279, 1272, 1270, 1258, 1254, 1253, 1242, 1241
2022-11-03 19:03:54 +01:00
|
|
|
for (const listId of listIdsToDelete) {
|
|
|
|
|
cacheForType.delete(listId)
|
|
|
|
|
}
|
2022-10-21 15:53:39 +02:00
|
|
|
}
|
2022-11-28 17:38:17 +01:00
|
|
|
|
|
|
|
|
clearExcludedData(): Promise<void> {
|
|
|
|
|
return Promise.resolve()
|
|
|
|
|
}
|
2022-11-30 17:15:08 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* We want to lock the access to the "ranges" db when updating / reading the
|
|
|
|
|
* offline available mail list ranges for each mail list (referenced using the listId)
|
|
|
|
|
* @param listId the mail list that we want to lock
|
|
|
|
|
*/
|
|
|
|
|
lockRangesDbAccess(listId: string): Promise<void> {
|
|
|
|
|
return Promise.resolve()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is the counterpart to the function "lockRangesDbAccess(listId)"
|
|
|
|
|
* @param listId the mail list that we want to unlock
|
|
|
|
|
*/
|
|
|
|
|
unlockRangesDbAccess(listId: string): Promise<void> {
|
|
|
|
|
return Promise.resolve()
|
|
|
|
|
}
|
2022-12-27 15:37:40 +01:00
|
|
|
}
|