#118 - Improved caching

That will teach me to read documentation (it won't).
This commit is contained in:
Simon Cambier
2022-10-28 13:03:43 +02:00
parent 422f84c1cf
commit 9af71e3efc
4 changed files with 21 additions and 14 deletions

View File

@@ -3,7 +3,6 @@ import type { IndexedDocument } from './globals'
import { database } from './database' import { database } from './database'
import MiniSearch from 'minisearch' import MiniSearch from 'minisearch'
import { minisearchOptions } from './search/search-engine' import { minisearchOptions } from './search/search-engine'
import { fileToIndexedDocument } from './file-loader'
class CacheManager { class CacheManager {
private documentsCache: Map<string, IndexedDocument> = new Map() private documentsCache: Map<string, IndexedDocument> = new Map()

View File

@@ -9,11 +9,11 @@ class OmnisearchCache extends Dexie {
minisearch!: Dexie.Table<{ date: string; data: string }, string> minisearch!: Dexie.Table<{ date: string; data: string }, string>
constructor() { constructor() {
super(app.appId + '_omnisearch') super('omnisearch/cache/' + app.appId)
this.version(4).stores({ this.version(5).stores({
pdf: 'path, hash, size, text', pdf: 'path, hash, size',
searchHistory: '++id, query', searchHistory: '++id',
minisearch: 'date, data' minisearch: 'date',
}) })
} }
} }

View File

@@ -116,6 +116,9 @@ async function populateIndex(): Promise<void> {
console.log(`Omnisearch - Indexed ${files.length} notes`) console.log(`Omnisearch - Indexed ${files.length} notes`)
console.timeEnd('Omnisearch - Timing') console.timeEnd('Omnisearch - Timing')
// Load normal notes into the main search engine
SearchEngine.loadTmpDataIntoMain()
// Load PDFs // Load PDFs
if (settings.PDFIndexing) { if (settings.PDFIndexing) {
console.time('Omnisearch - Timing') console.time('Omnisearch - Timing')
@@ -126,10 +129,12 @@ async function populateIndex(): Promise<void> {
console.timeEnd('Omnisearch - Timing') console.timeEnd('Omnisearch - Timing')
} }
// Load PDFs into the main search engine, and write cache
SearchEngine.loadTmpDataIntoMain()
await tmpEngine.writeToCache() await tmpEngine.writeToCache()
SearchEngine.swapEngines()
// Save minisearch // Clear memory
SearchEngine.clearTmp()
} }
async function cleanOldCacheFiles() { async function cleanOldCacheFiles() {

View File

@@ -89,12 +89,15 @@ export class SearchEngine {
} }
/** /**
* Must be called when the background indexing is done, * Loads the freshest indexed data into the main instance.
* to load the freshest data into the main instance
*/ */
public static swapEngines(): void { public static loadTmpDataIntoMain(): void {
;[this.engine, this.tmpEngine] = [this.tmpEngine, this.engine] const tmpData = this.tmpEngine.minisearch.toJSON()
this.isIndexing.set(false) this.engine.minisearch = MiniSearch.loadJS(tmpData, minisearchOptions);
}
public static clearTmp():void {
this.tmpEngine.minisearch = new MiniSearch(minisearchOptions)
} }
private minisearch: MiniSearch private minisearch: MiniSearch