#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 MiniSearch from 'minisearch'
import { minisearchOptions } from './search/search-engine'
import { fileToIndexedDocument } from './file-loader'
class CacheManager {
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>
constructor() {
super(app.appId + '_omnisearch')
this.version(4).stores({
pdf: 'path, hash, size, text',
searchHistory: '++id, query',
minisearch: 'date, data'
super('omnisearch/cache/' + app.appId)
this.version(5).stores({
pdf: 'path, hash, size',
searchHistory: '++id',
minisearch: 'date',
})
}
}

View File

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

View File

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