|
| 1 | +// (C) Copyright 2015 Moodle Pty Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { CoreConstants } from '@/core/constants'; |
| 16 | +import { SQLiteDB, SQLiteDBRecordValues } from '@classes/sqlitedb'; |
| 17 | +import { CoreLogger } from '@singletons/logger'; |
| 18 | +import { CoreDatabaseTable, GetDBRecordPrimaryKey } from './database-table'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Database wrapper that caches database records in memory to speed up read operations. |
| 22 | + * |
| 23 | + * Extensions of this class should only be used as singletons, or the data integrity of the inmemory cache |
| 24 | + * could be compromised. |
| 25 | + */ |
| 26 | +export abstract class CoreInMemoryDatabaseTable< |
| 27 | + DBRecord extends SQLiteDBRecordValues = SQLiteDBRecordValues, |
| 28 | + PrimaryKeyColumn extends keyof DBRecord = 'id', |
| 29 | + PrimaryKey extends GetDBRecordPrimaryKey<DBRecord, PrimaryKeyColumn> = GetDBRecordPrimaryKey<DBRecord, PrimaryKeyColumn> |
| 30 | +> extends CoreDatabaseTable<DBRecord, PrimaryKeyColumn, PrimaryKey> { |
| 31 | + |
| 32 | + private static readonly ACTIVE_TABLES: WeakMap<SQLiteDB, Set<string>> = new WeakMap(); |
| 33 | + private static readonly LOGGER: CoreLogger = CoreLogger.getInstance('CoreInMemoryDatabaseTable'); |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritdoc |
| 37 | + */ |
| 38 | + async initialize(): Promise<void> { |
| 39 | + await super.initialize(); |
| 40 | + |
| 41 | + const activeTables = CoreInMemoryDatabaseTable.ACTIVE_TABLES.get(this.database) ?? new Set(); |
| 42 | + |
| 43 | + if (activeTables.has(this.tableName)) { |
| 44 | + const message = `Trying to create multiple instances of an in-memory table for '${this.tableName}', ` + |
| 45 | + 'use singletons instead.'; |
| 46 | + |
| 47 | + if (!CoreConstants.BUILD.isProduction) { |
| 48 | + throw new Error(message); |
| 49 | + } |
| 50 | + |
| 51 | + CoreInMemoryDatabaseTable.LOGGER.warn(message); |
| 52 | + } |
| 53 | + |
| 54 | + activeTables.add(this.tableName); |
| 55 | + CoreInMemoryDatabaseTable.ACTIVE_TABLES.set(this.database, activeTables); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @inheritdoc |
| 60 | + */ |
| 61 | + async destroy(): Promise<void> { |
| 62 | + await super.destroy(); |
| 63 | + |
| 64 | + const activeTables = CoreInMemoryDatabaseTable.ACTIVE_TABLES.get(this.database); |
| 65 | + |
| 66 | + activeTables?.delete(this.tableName); |
| 67 | + |
| 68 | + if (activeTables?.size === 0) { |
| 69 | + CoreInMemoryDatabaseTable.ACTIVE_TABLES.delete(this.database); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments