From 576ad7c3ff8d0d6da691313158ed0f1657bed1d0 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Wed, 19 Oct 2022 22:03:05 +0200 Subject: [PATCH] Renamed some functions, fixed some throws --- src/cache-manager.ts | 12 ++++++------ src/components/ModalInFile.svelte | 4 +++- src/components/ResultItemVault.svelte | 2 +- src/notes-index.ts | 24 +++++++++++------------- src/notes.ts | 4 +++- src/search.ts | 18 +++++++++++------- src/settings.ts | 2 +- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/cache-manager.ts b/src/cache-manager.ts index 2932084..54d4740 100644 --- a/src/cache-manager.ts +++ b/src/cache-manager.ts @@ -94,25 +94,25 @@ class CacheManager { console.log('Omnisearch - Notes cache saved on disk') } - public addNoteToCache(path: string, note: IndexedDocument) { + public addNoteToMemCache(path: string, note: IndexedDocument) { this.notesCache[path] = note this.saveNotesCache() } - public removeNoteFromCache(key: string): void { + public removeNoteFromMemCache(key: string): void { delete this.notesCache[key] } - public getNoteFromCache(key: string): IndexedDocument | undefined { + public getNoteFromMemCache(key: string): IndexedDocument | undefined { return this.notesCache[key] } - public getNonExistingNotesFromCache(): IndexedDocument[] { + public getNonExistingNotesFromMemCache(): IndexedDocument[] { return Object.values(this.notesCache).filter(note => note.doesNotExist) } - public isCacheOutdated(file: TFile): boolean { - const indexedNote = this.getNoteFromCache(file.path) + public isNoteInMemCacheOutdated(file: TFile): boolean { + const indexedNote = this.getNoteFromMemCache(file.path) return !indexedNote || indexedNote.mtime !== file.stat.mtime } } diff --git a/src/components/ModalInFile.svelte b/src/components/ModalInFile.svelte index 09fd530..3012a59 100644 --- a/src/components/ModalInFile.svelte +++ b/src/components/ModalInFile.svelte @@ -122,7 +122,9 @@ // Move cursor to the match const view = app.workspace.getActiveViewOfType(MarkdownView) if (!view) { - throw new Error('OmniSearch - No active MarkdownView') + // Not an editable document, so no cursor to place + return + // throw new Error('OmniSearch - No active MarkdownView') } const offset = groupedOffsets[selectedIndex] ?? 0 diff --git a/src/components/ResultItemVault.svelte b/src/components/ResultItemVault.svelte index a58668a..104fe7e 100644 --- a/src/components/ResultItemVault.svelte +++ b/src/components/ResultItemVault.svelte @@ -12,7 +12,7 @@ $: reg = stringsToRegex(note.foundWords) $: matches = Search.getMatches(note.content, reg) $: cleanedContent = makeExcerpt(note.content, note.matches[0]?.offset ?? -1) - $: glyph = cacheManager.getNoteFromCache(note.path)?.doesNotExist + $: glyph = cacheManager.getNoteFromMemCache(note.path)?.doesNotExist $: title = settings.showShortName ? note.basename : note.path diff --git a/src/notes-index.ts b/src/notes-index.ts index c18a3ce..94eb9fe 100644 --- a/src/notes-index.ts +++ b/src/notes-index.ts @@ -19,7 +19,7 @@ import { cacheManager } from './cache-manager' export const pdfQueue = pLimit(settings.backgroundProcesses) /** - * Adds a file to the index + * Adds a file to the search index * @param file * @returns */ @@ -30,26 +30,24 @@ export async function addToIndexAndCache(file: TAbstractFile): Promise { // Check if the file was already indexed as non-existent, // and if so, remove it from the index (before adding it again) - if (cacheManager.getNoteFromCache(file.path)?.doesNotExist) { + if (cacheManager.getNoteFromMemCache(file.path)?.doesNotExist) { removeFromIndex(file.path) } try { - // console.log(`Omnisearch - adding ${file.path} to index`) - // Look for links that lead to non-existing files, // and index them as well const metadata = app.metadataCache.getFileCache(file) if (metadata) { const nonExisting = getNonExistingNotes(file, metadata) for (const name of nonExisting.filter( - o => !cacheManager.getNoteFromCache(o) + o => !cacheManager.getNoteFromMemCache(o) )) { addNonExistingToIndex(name, file.path) } } - if (cacheManager.getNoteFromCache(file.path)) { + if (cacheManager.getNoteFromMemCache(file.path)) { throw new Error(`${file.basename} is already indexed`) } @@ -82,7 +80,7 @@ export async function addToIndexAndCache(file: TAbstractFile): Promise { } Search.minisearchInstance.add(note) - cacheManager.addNoteToCache(note.path, note) + cacheManager.addNoteToMemCache(note.path, note) } catch (e) { // console.trace('Error while indexing ' + file.basename) console.error(e) @@ -98,7 +96,7 @@ export async function addToIndexAndCache(file: TAbstractFile): Promise { export function addNonExistingToIndex(name: string, parent: string): void { name = removeAnchors(name) const filename = name + (name.endsWith('.md') ? '' : '.md') - if (cacheManager.getNoteFromCache(filename)) return + if (cacheManager.getNoteFromMemCache(filename)) return const note: IndexedDocument = { path: filename, @@ -116,7 +114,7 @@ export function addNonExistingToIndex(name: string, parent: string): void { parent, } Search.minisearchInstance.add(note) - cacheManager.addNoteToCache(filename, note) + cacheManager.addNoteToMemCache(filename, note) } /** @@ -128,12 +126,12 @@ export function removeFromIndex(path: string): void { console.info(`"${path}" is not an indexable file`) return } - const note = cacheManager.getNoteFromCache(path) + const note = cacheManager.getNoteFromMemCache(path) if (note) { Search.minisearchInstance.remove(note) - cacheManager.removeNoteFromCache(path) + cacheManager.removeNoteFromMemCache(path) cacheManager - .getNonExistingNotesFromCache() + .getNonExistingNotesFromMemCache() .filter(n => n.parent === path) .forEach(n => { removeFromIndex(n.path) @@ -171,7 +169,7 @@ export async function indexPDFs() { console.log(`Omnisearch - Indexing ${files.length} PDFs`) const input = [] for (const file of files) { - if (cacheManager.getNoteFromCache(file.path)) { + if (cacheManager.getNoteFromMemCache(file.path)) { removeFromIndex(file.path) } input.push( diff --git a/src/notes.ts b/src/notes.ts index 475b0fe..97323ce 100644 --- a/src/notes.ts +++ b/src/notes.ts @@ -33,7 +33,9 @@ export async function openNote( const view = app.workspace.getActiveViewOfType(MarkdownView) if (!view) { - throw new Error('OmniSearch - No active MarkdownView') + // Not an editable document, so no cursor to place + // throw new Error('OmniSearch - No active MarkdownView') + return } const pos = view.editor.offsetToPos(offset) pos.ch = 0 diff --git a/src/search.ts b/src/search.ts index 7f3d41b..92024c0 100644 --- a/src/search.ts +++ b/src/search.ts @@ -86,7 +86,7 @@ export async function initGlobalSearchIndex(): Promise { let files let notesSuffix if (settings.persistCache) { - files = allFiles.filter(file => cacheManager.isCacheOutdated(file)) + files = allFiles.filter(file => cacheManager.isNoteInMemCacheOutdated(file)) notesSuffix = 'modified notes' } else { files = allFiles @@ -101,7 +101,7 @@ export async function initGlobalSearchIndex(): Promise { const queue = pLimit(10) const input = [] for (const file of files) { - if (cacheManager.getNoteFromCache(file.path)) { + if (cacheManager.getNoteFromMemCache(file.path)) { NotesIndex.removeFromIndex(file.path) } input.push(queue(() => NotesIndex.addToIndexAndCache(file))) @@ -136,6 +136,7 @@ export async function initGlobalSearchIndex(): Promise { async function search(query: Query): Promise { if (!query.segmentsToStr()) return [] + console.time('Omnisearch - searching') let results = minisearchInstance.search(query.segmentsToStr(), { prefix: true, fuzzy: term => (term.length > 4 ? 0.2 : false), @@ -148,7 +149,8 @@ async function search(query: Query): Promise { headings3: settings.weightH3, }, }) - + console.timeEnd('Omnisearch - searching') + // Downrank files that are in Obsidian's excluded list if (settings.respectExcluded) { results.forEach(result => { @@ -166,9 +168,9 @@ async function search(query: Query): Promise { if (exactTerms.length) { results = results.filter(r => { const title = - cacheManager.getNoteFromCache(r.id)?.path.toLowerCase() ?? '' + cacheManager.getNoteFromMemCache(r.id)?.path.toLowerCase() ?? '' const content = stripMarkdownCharacters( - cacheManager.getNoteFromCache(r.id)?.content ?? '' + cacheManager.getNoteFromMemCache(r.id)?.content ?? '' ).toLowerCase() return exactTerms.every(q => content.includes(q) || title.includes(q)) }) @@ -179,7 +181,7 @@ async function search(query: Query): Promise { if (exclusions.length) { results = results.filter(r => { const content = stripMarkdownCharacters( - cacheManager.getNoteFromCache(r.id)?.content ?? '' + cacheManager.getNoteFromMemCache(r.id)?.content ?? '' ).toLowerCase() return exclusions.every(q => !content.includes(q.value)) }) @@ -218,7 +220,9 @@ export async function getSuggestions( options?: Partial<{ singleFilePath: string | null }> ): Promise { // Get the raw results + console.time('search') let results = await search(query) + console.timeEnd('search') if (!results.length) return [] // Extract tags from the query @@ -247,7 +251,7 @@ export async function getSuggestions( // Map the raw results to get usable suggestions return results.map(result => { - const note = cacheManager.getNoteFromCache(result.id) + const note = cacheManager.getNoteFromMemCache(result.id) if (!note) { throw new Error(`Note "${result.id}" not indexed`) } diff --git a/src/settings.ts b/src/settings.ts index e60febb..b3cc485 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -341,7 +341,7 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = { PDFIndexing: false, backgroundProcesses: Platform.isMobileApp ? 1 - : Math.max(1, Math.floor(require('os').cpus().length - 2)), + : Math.max(1, Math.floor(require('os').cpus().length * 0.75)), showIndexingNotices: false, showShortName: false,