#120 - Cleaning of old cache databases
This commit is contained in:
@@ -1,6 +1,35 @@
|
|||||||
import Dexie from 'dexie'
|
import Dexie from 'dexie'
|
||||||
|
|
||||||
class OmnisearchCache extends Dexie {
|
export class OmnisearchCache extends Dexie {
|
||||||
|
public static readonly dbVersion = 6
|
||||||
|
public static readonly dbPrefix = 'omnisearch/cache/'
|
||||||
|
public static readonly dbName = OmnisearchCache.dbPrefix + app.appId
|
||||||
|
|
||||||
|
private static instance: OmnisearchCache
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes Omnisearch databases that have an older version than the current one
|
||||||
|
*/
|
||||||
|
public static async clearOldDatabases(): Promise<void> {
|
||||||
|
const toDelete = (await indexedDB.databases()).filter(
|
||||||
|
db =>
|
||||||
|
db.name?.startsWith(OmnisearchCache.dbPrefix) &&
|
||||||
|
// version multiplied by 10 https://github.com/dexie/Dexie.js/issues/59
|
||||||
|
db.version !== OmnisearchCache.dbVersion * 10
|
||||||
|
)
|
||||||
|
if (toDelete.length) {
|
||||||
|
console.log('Omnisearch - Those IndexedDb databases will be deleted:')
|
||||||
|
for (const db of toDelete) {
|
||||||
|
if (db.name) {
|
||||||
|
console.log(db.name + ' ' + db.version)
|
||||||
|
indexedDB.deleteDatabase(db.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//#region Table declarations
|
||||||
|
|
||||||
pdf!: Dexie.Table<
|
pdf!: Dexie.Table<
|
||||||
{ path: string; hash: string; size: number; text: string },
|
{ path: string; hash: string; size: number; text: string },
|
||||||
string
|
string
|
||||||
@@ -8,9 +37,19 @@ class OmnisearchCache extends Dexie {
|
|||||||
searchHistory!: Dexie.Table<{ id?: number; query: string }, number>
|
searchHistory!: Dexie.Table<{ id?: number; query: string }, number>
|
||||||
minisearch!: Dexie.Table<{ date: string; data: string }, string>
|
minisearch!: Dexie.Table<{ date: string; data: string }, string>
|
||||||
|
|
||||||
constructor() {
|
//#endregion Table declarations
|
||||||
super('omnisearch/cache/' + app.appId)
|
|
||||||
this.version(5).stores({
|
public static getInstance() {
|
||||||
|
if (!OmnisearchCache.instance) {
|
||||||
|
OmnisearchCache.instance = new OmnisearchCache()
|
||||||
|
}
|
||||||
|
return OmnisearchCache.instance
|
||||||
|
}
|
||||||
|
|
||||||
|
private constructor() {
|
||||||
|
super(OmnisearchCache.dbName)
|
||||||
|
// Database structure
|
||||||
|
this.version(OmnisearchCache.dbVersion).stores({
|
||||||
pdf: 'path, hash, size',
|
pdf: 'path, hash, size',
|
||||||
searchHistory: '++id',
|
searchHistory: '++id',
|
||||||
minisearch: 'date',
|
minisearch: 'date',
|
||||||
@@ -18,4 +57,4 @@ class OmnisearchCache extends Dexie {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const database = new OmnisearchCache()
|
export const database = OmnisearchCache.getInstance()
|
||||||
|
|||||||
14
src/main.ts
14
src/main.ts
@@ -11,12 +11,14 @@ import api from './tools/api'
|
|||||||
import { isFilePlaintext, wait } from './tools/utils'
|
import { isFilePlaintext, wait } from './tools/utils'
|
||||||
import * as NotesIndex from './notes-index'
|
import * as NotesIndex from './notes-index'
|
||||||
import * as FileLoader from './file-loader'
|
import * as FileLoader from './file-loader'
|
||||||
|
import { OmnisearchCache } from './database'
|
||||||
|
|
||||||
export default class OmnisearchPlugin extends Plugin {
|
export default class OmnisearchPlugin extends Plugin {
|
||||||
private ribbonButton?: HTMLElement
|
private ribbonButton?: HTMLElement
|
||||||
|
|
||||||
async onload(): Promise<void> {
|
async onload(): Promise<void> {
|
||||||
await cleanOldCacheFiles()
|
await cleanOldCacheFiles()
|
||||||
|
await OmnisearchCache.clearOldDatabases()
|
||||||
await loadSettings(this)
|
await loadSettings(this)
|
||||||
|
|
||||||
// Initialize minisearch
|
// Initialize minisearch
|
||||||
@@ -105,6 +107,14 @@ export default class OmnisearchPlugin extends Plugin {
|
|||||||
* Read the files and feed them to Minisearch
|
* Read the files and feed them to Minisearch
|
||||||
*/
|
*/
|
||||||
async function populateIndex(): Promise<void> {
|
async function populateIndex(): Promise<void> {
|
||||||
|
// We use a tmp minisearch instance to leave the main instance mostly untouched.
|
||||||
|
// Otherwise, we'd have to clear the main instance, and (asynchronously) load the notes.
|
||||||
|
// That would cause a "downtime" in Omnisearch while the index is being gradually rebuilt.
|
||||||
|
//
|
||||||
|
// With the tmp method, we still have access to the cache data while all the
|
||||||
|
// fresh indexing is done in the background.
|
||||||
|
// Once all notes are loaded in tmp, we (synchronously) export tmp and import it into main.
|
||||||
|
// That can cause a small freeze, but no downtime.
|
||||||
const tmpEngine = SearchEngine.getTmpEngine()
|
const tmpEngine = SearchEngine.getTmpEngine()
|
||||||
|
|
||||||
// Load plain text files
|
// Load plain text files
|
||||||
@@ -123,7 +133,7 @@ async function populateIndex(): Promise<void> {
|
|||||||
console.time('Omnisearch - Timing')
|
console.time('Omnisearch - Timing')
|
||||||
const pdfs = await FileLoader.getPDFFiles()
|
const pdfs = await FileLoader.getPDFFiles()
|
||||||
// Index them
|
// Index them
|
||||||
await tmpEngine.addAllToMinisearch(pdfs)
|
await SearchEngine.getEngine().addAllToMinisearch(pdfs)
|
||||||
console.log(`Omnisearch - Indexed ${pdfs.length} PDFs`)
|
console.log(`Omnisearch - Indexed ${pdfs.length} PDFs`)
|
||||||
console.timeEnd('Omnisearch - Timing')
|
console.timeEnd('Omnisearch - Timing')
|
||||||
}
|
}
|
||||||
@@ -131,7 +141,7 @@ async function populateIndex(): Promise<void> {
|
|||||||
// Load PDFs into the main search engine, and write cache
|
// Load PDFs into the main search engine, and write cache
|
||||||
SearchEngine.loadTmpDataIntoMain()
|
SearchEngine.loadTmpDataIntoMain()
|
||||||
SearchEngine.isIndexing.set(false)
|
SearchEngine.isIndexing.set(false)
|
||||||
await tmpEngine.writeToCache()
|
await SearchEngine.getEngine().writeToCache()
|
||||||
|
|
||||||
// Clear memory
|
// Clear memory
|
||||||
SearchEngine.clearTmp()
|
SearchEngine.clearTmp()
|
||||||
|
|||||||
@@ -317,8 +317,6 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
|
|||||||
weightH2: 1.3,
|
weightH2: 1.3,
|
||||||
weightH3: 1.1,
|
weightH3: 1.1,
|
||||||
|
|
||||||
// persistCache: false,
|
|
||||||
|
|
||||||
welcomeMessage: '',
|
welcomeMessage: '',
|
||||||
} as const
|
} as const
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user