From 7e2d2fb74b5c40142ba733dd41538d555e0fb834 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 17 Jan 2024 12:46:50 +0100 Subject: [PATCH] MOBILE-4304 search: Update database usage --- .../search/services/search-history-db.ts | 5 +- .../search/services/search-history.service.ts | 71 ++++++++++++------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/core/features/search/services/search-history-db.ts b/src/core/features/search/services/search-history-db.ts index 19a1e8819..42f034fa3 100644 --- a/src/core/features/search/services/search-history-db.ts +++ b/src/core/features/search/services/search-history-db.ts @@ -18,6 +18,7 @@ import { CoreSiteSchema } from '@services/sites'; * Database variables for CoreSearchHistory service. */ export const SEARCH_HISTORY_TABLE_NAME = 'seach_history'; +export const SEARCH_HISTORY_TABLE_PRIMARY_KEYS = ['searcharea', 'searchedtext'] as const; export const SITE_SCHEMA: CoreSiteSchema = { name: 'CoreSearchHistoryProvider', version: 1, @@ -46,7 +47,7 @@ export const SITE_SCHEMA: CoreSiteSchema = { notNull: true, }, ], - primaryKeys: ['searcharea', 'searchedtext'], + primaryKeys: [...SEARCH_HISTORY_TABLE_PRIMARY_KEYS], }, ], }; @@ -60,3 +61,5 @@ export type CoreSearchHistoryDBRecord = { searchedtext: string; // Text of the performed search. times: number; // Times search has been performed (if previously in history). }; + +export type CoreSearchHistoryDBPrimaryKeys = typeof SEARCH_HISTORY_TABLE_PRIMARY_KEYS[number]; diff --git a/src/core/features/search/services/search-history.service.ts b/src/core/features/search/services/search-history.service.ts index f27c9786e..6c4a48445 100644 --- a/src/core/features/search/services/search-history.service.ts +++ b/src/core/features/search/services/search-history.service.ts @@ -15,9 +15,17 @@ import { Injectable } from '@angular/core'; import { CoreSites } from '@services/sites'; -import { SQLiteDB } from '@classes/sqlitedb'; -import { CoreSearchHistoryDBRecord, SEARCH_HISTORY_TABLE_NAME } from './search-history-db'; +import { + CoreSearchHistoryDBPrimaryKeys, + CoreSearchHistoryDBRecord, + SEARCH_HISTORY_TABLE_NAME, + SEARCH_HISTORY_TABLE_PRIMARY_KEYS, +} from './search-history-db'; import { makeSingleton } from '@singletons'; +import { LazyMap, lazyMap } from '@/core/utils/lazy-map'; +import { AsyncInstance, asyncInstance } from '@/core/utils/async-instance'; +import { CoreDatabaseTable } from '@classes/database/database-table'; +import { CoreDatabaseCachingStrategy } from '@classes/database/database-table-proxy'; /** * Service that enables adding a history to a search box. @@ -27,6 +35,27 @@ export class CoreSearchHistoryProvider { protected static readonly HISTORY_LIMIT = 10; + protected searchHistoryTables: LazyMap< + AsyncInstance> + >; + + constructor() { + this.searchHistoryTables = lazyMap( + siteId => asyncInstance( + () => CoreSites.getSiteTable( + SEARCH_HISTORY_TABLE_NAME, + { + siteId, + primaryKeyColumns: [...SEARCH_HISTORY_TABLE_PRIMARY_KEYS], + rowIdColumn: null, + config: { cachingStrategy: CoreDatabaseCachingStrategy.None }, + onDestroy: () => delete this.searchHistoryTables[siteId], + }, + ), + ), + ); + } + /** * Get a search area history sorted by use. * @@ -35,12 +64,9 @@ export class CoreSearchHistoryProvider { * @returns Promise resolved with the list of items when done. */ async getSearchHistory(searchArea: string, siteId?: string): Promise { - const site = await CoreSites.getSite(siteId); - const conditions = { - searcharea: searchArea, - }; + siteId ??= CoreSites.getCurrentSiteId(); - const history: CoreSearchHistoryDBRecord[] = await site.getDb().getRecords(SEARCH_HISTORY_TABLE_NAME, conditions); + const history = await this.searchHistoryTables[siteId].getMany({ searcharea: searchArea }); // Sorting by last used DESC. return history.sort((a, b) => (b.lastused || 0) - (a.lastused || 0)); @@ -50,10 +76,10 @@ export class CoreSearchHistoryProvider { * Controls search limit and removes the last item if overflows. * * @param searchArea Search area to control - * @param db SQLite DB where to perform the search. + * @param siteId Site id. * @returns Resolved when done. */ - protected async controlSearchLimit(searchArea: string, db: SQLiteDB): Promise { + protected async controlSearchLimit(searchArea: string, siteId: string): Promise { const items = await this.getSearchHistory(searchArea); if (items.length > CoreSearchHistoryProvider.HISTORY_LIMIT) { // Over the limit. Remove the last. @@ -62,12 +88,10 @@ export class CoreSearchHistoryProvider { return; } - const searchItem = { + await this.searchHistoryTables[siteId].delete({ searcharea: lastItem.searcharea, searchedtext: lastItem.searchedtext, - }; - - await db.deleteRecords(SEARCH_HISTORY_TABLE_NAME, searchItem); + }); } } @@ -76,23 +100,23 @@ export class CoreSearchHistoryProvider { * * @param searchArea Area where the search has been performed. * @param text Text of the performed text. - * @param db SQLite DB where to perform the search. + * @param siteId Site id. * @returns True if exists, false otherwise. */ - protected async updateExistingItem(searchArea: string, text: string, db: SQLiteDB): Promise { + protected async updateExistingItem(searchArea: string, text: string, siteId: string): Promise { const searchItem = { searcharea: searchArea, searchedtext: text, }; try { - const existingItem: CoreSearchHistoryDBRecord = await db.getRecord(SEARCH_HISTORY_TABLE_NAME, searchItem); + const existingItem = await this.searchHistoryTables[siteId].getOne(searchItem); // If item exist, update time and number of times searched. existingItem.lastused = Date.now(); existingItem.times++; - await db.updateRecords(SEARCH_HISTORY_TABLE_NAME, existingItem, searchItem); + await this.searchHistoryTables[siteId].update(existingItem, searchItem); return true; } catch { @@ -109,23 +133,20 @@ export class CoreSearchHistoryProvider { * @returns Resolved when done. */ async insertOrUpdateSearchText(searchArea: string, text: string, siteId?: string): Promise { - const site = await CoreSites.getSite(siteId); - const db = site.getDb(); + siteId ??= CoreSites.getCurrentSiteId(); - const exists = await this.updateExistingItem(searchArea, text, db); + const exists = await this.updateExistingItem(searchArea, text, siteId); if (!exists) { // If item is new, control the history does not goes over the limit. - const searchItem: CoreSearchHistoryDBRecord = { + await this.searchHistoryTables[siteId].insert({ searcharea: searchArea, searchedtext: text, lastused: Date.now(), times: 1, - }; + }); - await db.insertRecord(SEARCH_HISTORY_TABLE_NAME, searchItem); - - await this.controlSearchLimit(searchArea, db); + await this.controlSearchLimit(searchArea, siteId); } }