Skip to content

Commit a7b5091

Browse files
authored
Merge pull request #3514 from NoelDeMartin/MOBILE-4081
MOBILE-4081 core: Use inmemory database singletons
2 parents 9cb4819 + d786b7d commit a7b5091

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

src/core/classes/database/eager-database-table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
import { CoreError } from '@classes/errors/error';
1616
import { SQLiteDBRecordValues } from '@classes/sqlitedb';
17+
import { CoreInMemoryDatabaseTable } from './inmemory-database-table';
1718
import {
18-
CoreDatabaseTable,
1919
CoreDatabaseConditions,
2020
GetDBRecordPrimaryKey,
2121
CoreDatabaseReducer,
@@ -32,7 +32,7 @@ export class CoreEagerDatabaseTable<
3232
DBRecord extends SQLiteDBRecordValues = SQLiteDBRecordValues,
3333
PrimaryKeyColumn extends keyof DBRecord = 'id',
3434
PrimaryKey extends GetDBRecordPrimaryKey<DBRecord, PrimaryKeyColumn> = GetDBRecordPrimaryKey<DBRecord, PrimaryKeyColumn>
35-
> extends CoreDatabaseTable<DBRecord, PrimaryKeyColumn, PrimaryKey> {
35+
> extends CoreInMemoryDatabaseTable<DBRecord, PrimaryKeyColumn, PrimaryKey> {
3636

3737
protected records: Record<string, DBRecord> = {};
3838

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

src/core/classes/database/lazy-database-table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
import { CoreError } from '@classes/errors/error';
1616
import { SQLiteDBRecordValues } from '@classes/sqlitedb';
17+
import { CoreInMemoryDatabaseTable } from './inmemory-database-table';
1718
import {
1819
CoreDatabaseConfiguration,
19-
CoreDatabaseTable,
2020
CoreDatabaseConditions,
2121
GetDBRecordPrimaryKey,
2222
CoreDatabaseQueryOptions,
@@ -32,7 +32,7 @@ export class CoreLazyDatabaseTable<
3232
DBRecord extends SQLiteDBRecordValues = SQLiteDBRecordValues,
3333
PrimaryKeyColumn extends keyof DBRecord = 'id',
3434
PrimaryKey extends GetDBRecordPrimaryKey<DBRecord, PrimaryKeyColumn> = GetDBRecordPrimaryKey<DBRecord, PrimaryKeyColumn>
35-
> extends CoreDatabaseTable<DBRecord, PrimaryKeyColumn, PrimaryKey> {
35+
> extends CoreInMemoryDatabaseTable<DBRecord, PrimaryKeyColumn, PrimaryKey> {
3636

3737
protected readonly DEFAULT_CACHE_LIFETIME = 60000;
3838

0 commit comments

Comments
 (0)