2022-01-12 14:43:01 +01:00
|
|
|
import {ElementEntity, ListElementEntity, SomeEntity} from "../../common/EntityTypes.js"
|
2022-07-04 14:55:17 +02:00
|
|
|
import {EntityRestClient, typeRefToPath} from "./EntityRestClient.js"
|
2022-01-12 14:43:01 +01:00
|
|
|
import {firstBiggerThanSecond, getElementId, getListId, isElementEntity} from "../../common/utils/EntityUtils.js"
|
2022-09-07 17:09:25 +02:00
|
|
|
import {CacheStorage, LastUpdateTime} from "./DefaultEntityRestCache.js"
|
2022-10-21 15:53:39 +02:00
|
|
|
import {assertNotNull, clone, getFromMap, remove, TypeRef} from "@tutao/tutanota-utils"
|
2022-07-04 14:55:17 +02:00
|
|
|
import {CustomCacheHandlerMap} from "./CustomCacheHandler.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. */
|
|
|
|
|
allRange: Id[],
|
|
|
|
|
lowerRangeId: Id,
|
|
|
|
|
upperRangeId: Id,
|
|
|
|
|
/** All the entities loaded, inside or outside of the range (e.g. load for a single entity). */
|
|
|
|
|
elements: Map<Id, ListElementEntity>
|
2022-10-21 15:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Map from list id to list cache. */
|
|
|
|
|
type ListTypeCache = Map<Id, ListCache>
|
|
|
|
|
|
|
|
|
|
export interface EphemeralStorageInitArgs {
|
|
|
|
|
userId: Id,
|
|
|
|
|
}
|
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()
|
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
|
|
|
|
|
|
|
|
init({userId}: EphemeralStorageInitArgs) {
|
|
|
|
|
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()
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
async get<T extends SomeEntity>(typeRef: TypeRef<T>, listId: Id | null, id: Id): Promise<T | null> {
|
|
|
|
|
// We downcast because we can't prove that map has correct entity on the type level
|
|
|
|
|
const path = typeRefToPath(typeRef)
|
|
|
|
|
if (listId) {
|
|
|
|
|
return clone((this.lists.get(path)?.get(listId)?.elements.get(id) as T | undefined) ?? null)
|
|
|
|
|
} else {
|
|
|
|
|
return clone((this.entities.get(path)?.get(id) as T | undefined) ?? null)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deleteIfExists<T>(typeRef: TypeRef<T>, listId: Id | null, id: Id): Promise<void> {
|
|
|
|
|
const path = typeRefToPath(typeRef)
|
|
|
|
|
if (listId) {
|
|
|
|
|
const cache = this.lists.get(path)?.get(listId)
|
|
|
|
|
if (cache != null) {
|
|
|
|
|
cache.elements.delete(id)
|
|
|
|
|
remove(cache.allRange, id)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.entities.get(path)?.delete(id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private addElementEntity<T extends ElementEntity>(typeRef: TypeRef<T>, id: Id, entity: T) {
|
|
|
|
|
getFromMap(this.entities, typeRefToPath(typeRef), () => new Map()).set(id, entity)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async isElementIdInCacheRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, id: Id): Promise<boolean> {
|
|
|
|
|
const cache = this.lists.get(typeRefToPath(typeRef))?.get(listId)
|
|
|
|
|
return cache != null
|
|
|
|
|
&& !firstBiggerThanSecond(id, cache.upperRangeId)
|
|
|
|
|
&& !firstBiggerThanSecond(cache.lowerRangeId, id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async put(originalEntity: SomeEntity): Promise<void> {
|
|
|
|
|
const entity = clone(originalEntity)
|
|
|
|
|
if (isElementEntity(entity)) {
|
|
|
|
|
this.addElementEntity(entity._type, entity._id, entity)
|
|
|
|
|
} else {
|
|
|
|
|
const listId = getListId(entity)
|
|
|
|
|
const elementId = getElementId(entity)
|
|
|
|
|
const typeRef = entity._type
|
|
|
|
|
const cache = this.lists.get(typeRefToPath(typeRef))?.get(listId)
|
|
|
|
|
if (cache == null) {
|
|
|
|
|
// first element in this list
|
|
|
|
|
const newCache = {
|
|
|
|
|
allRange: [elementId],
|
|
|
|
|
lowerRangeId: elementId,
|
|
|
|
|
upperRangeId: elementId,
|
|
|
|
|
elements: new Map([[elementId, entity]])
|
|
|
|
|
}
|
|
|
|
|
getFromMap(this.lists, typeRefToPath(typeRef), () => new Map())
|
|
|
|
|
.set(listId, newCache)
|
|
|
|
|
} else {
|
|
|
|
|
// if the element already exists in the cache, overwrite it
|
|
|
|
|
// add new element to existing list if necessary
|
|
|
|
|
cache.elements.set(elementId, entity)
|
|
|
|
|
if (await this.isElementIdInCacheRange(typeRef, listId, elementId)) {
|
|
|
|
|
this._insertIntoRange(cache.allRange, elementId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_insertIntoRange(allRange: Array<Id>, elementId: Id) {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async provideFromRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, start: Id, count: number, reverse: boolean): Promise<T[]> {
|
|
|
|
|
const listCache = this.lists.get(typeRefToPath(typeRef))?.get(listId)
|
|
|
|
|
|
|
|
|
|
if (listCache == null) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let range = listCache.allRange
|
|
|
|
|
let ids: Id[] = []
|
|
|
|
|
if (reverse) {
|
|
|
|
|
let i
|
|
|
|
|
for (i = range.length - 1; i >= 0; i--) {
|
|
|
|
|
if (firstBiggerThanSecond(start, range[i])) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i >= 0) {
|
|
|
|
|
let startIndex = i + 1 - count
|
|
|
|
|
if (startIndex < 0) { // start index may be negative if more elements have been requested than available when getting elements reverse.
|
|
|
|
|
startIndex = 0
|
|
|
|
|
}
|
|
|
|
|
ids = range.slice(startIndex, i + 1)
|
|
|
|
|
ids.reverse()
|
|
|
|
|
} else {
|
|
|
|
|
ids = []
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const i = range.findIndex(id => firstBiggerThanSecond(id, start))
|
|
|
|
|
ids = range.slice(i, i + count)
|
|
|
|
|
}
|
|
|
|
|
let result: T[] = []
|
|
|
|
|
for (let a = 0; a < ids.length; a++) {
|
|
|
|
|
result.push(clone((listCache.elements.get(ids[a]) as T)))
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id): Promise<{lower: Id, upper: Id} | null> {
|
|
|
|
|
const listCache = this.lists.get(typeRefToPath(typeRef))?.get(listId)
|
|
|
|
|
|
|
|
|
|
if (listCache == null) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {lower: listCache.lowerRangeId, upper: listCache.upperRangeId}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setUpperRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, id: Id): Promise<void> {
|
|
|
|
|
const listCache = this.lists.get(typeRefToPath(typeRef))?.get(listId)
|
|
|
|
|
if (listCache == null) {
|
|
|
|
|
throw new Error("list does not exist")
|
|
|
|
|
}
|
|
|
|
|
listCache.upperRangeId = id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setLowerRangeForList<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id, id: Id): Promise<void> {
|
|
|
|
|
const listCache = this.lists.get(typeRefToPath(typeRef))?.get(listId)
|
|
|
|
|
if (listCache == null) {
|
|
|
|
|
throw new Error("list does not exist")
|
|
|
|
|
}
|
|
|
|
|
listCache.lowerRangeId = id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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> {
|
|
|
|
|
const listCache = this.lists.get(typeRefToPath(typeRef))?.get(listId)
|
|
|
|
|
if (listCache == null) {
|
|
|
|
|
getFromMap(this.lists, typeRefToPath(typeRef), () => new Map()).set(listId, {
|
|
|
|
|
allRange: [],
|
|
|
|
|
lowerRangeId: lower,
|
|
|
|
|
upperRangeId: upper,
|
|
|
|
|
elements: new Map()
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
listCache.lowerRangeId = lower
|
|
|
|
|
listCache.upperRangeId = upper
|
|
|
|
|
listCache.allRange = []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getIdsInRange<T extends ListElementEntity>(typeRef: TypeRef<T>, listId: Id): Promise<Array<Id>> {
|
|
|
|
|
return this.lists.get(typeRefToPath(typeRef))?.get(listId)?.allRange ?? []
|
|
|
|
|
}
|
|
|
|
|
|
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> {
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-09-07 17:09:25 +02:00
|
|
|
async getLastUpdateTime(): Promise<LastUpdateTime> {
|
|
|
|
|
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>> {
|
|
|
|
|
const listCache = this.lists.get(typeRefToPath(typeRef))?.get(listId)
|
|
|
|
|
|
|
|
|
|
if (listCache == null) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return listCache.allRange.map(id => clone((listCache.elements.get(id) as T)))
|
|
|
|
|
}
|
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()) {
|
2022-11-02 11:37:37 +01:00
|
|
|
const listIdsToDelete: string[] = []
|
|
|
|
|
for (const [listId, listCache] of cacheForType.entries()) {
|
2022-10-21 15:53:39 +02:00
|
|
|
for (const [id, element] of listCache.elements.entries()) {
|
|
|
|
|
if (element._ownerGroup === owner) {
|
2022-11-02 11:37:37 +01:00
|
|
|
listIdsToDelete.push(listId)
|
|
|
|
|
break
|
2022-10-21 15:53:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-02 11:37:37 +01:00
|
|
|
for (const listId of listIdsToDelete) {
|
|
|
|
|
cacheForType.delete(listId)
|
|
|
|
|
}
|
2022-10-21 15:53:39 +02:00
|
|
|
}
|
2022-11-03 11:04:26 +01:00
|
|
|
|
|
|
|
|
this.lastBatchIdPerGroup.delete(owner)
|
2022-10-21 15:53:39 +02:00
|
|
|
}
|
2022-01-12 14:43:01 +01:00
|
|
|
}
|