Renamed some functions, fixed some throws

This commit is contained in:
Simon Cambier
2022-10-19 22:03:05 +02:00
parent ec71beca5a
commit 576ad7c3ff
7 changed files with 36 additions and 30 deletions

View File

@@ -94,25 +94,25 @@ class CacheManager {
console.log('Omnisearch - Notes cache saved on disk') 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.notesCache[path] = note
this.saveNotesCache() this.saveNotesCache()
} }
public removeNoteFromCache(key: string): void { public removeNoteFromMemCache(key: string): void {
delete this.notesCache[key] delete this.notesCache[key]
} }
public getNoteFromCache(key: string): IndexedDocument | undefined { public getNoteFromMemCache(key: string): IndexedDocument | undefined {
return this.notesCache[key] return this.notesCache[key]
} }
public getNonExistingNotesFromCache(): IndexedDocument[] { public getNonExistingNotesFromMemCache(): IndexedDocument[] {
return Object.values(this.notesCache).filter(note => note.doesNotExist) return Object.values(this.notesCache).filter(note => note.doesNotExist)
} }
public isCacheOutdated(file: TFile): boolean { public isNoteInMemCacheOutdated(file: TFile): boolean {
const indexedNote = this.getNoteFromCache(file.path) const indexedNote = this.getNoteFromMemCache(file.path)
return !indexedNote || indexedNote.mtime !== file.stat.mtime return !indexedNote || indexedNote.mtime !== file.stat.mtime
} }
} }

View File

@@ -122,7 +122,9 @@
// Move cursor to the match // Move cursor to the match
const view = app.workspace.getActiveViewOfType(MarkdownView) const view = app.workspace.getActiveViewOfType(MarkdownView)
if (!view) { 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 const offset = groupedOffsets[selectedIndex] ?? 0

View File

@@ -12,7 +12,7 @@
$: reg = stringsToRegex(note.foundWords) $: reg = stringsToRegex(note.foundWords)
$: matches = Search.getMatches(note.content, reg) $: matches = Search.getMatches(note.content, reg)
$: cleanedContent = makeExcerpt(note.content, note.matches[0]?.offset ?? -1) $: 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 $: title = settings.showShortName ? note.basename : note.path
</script> </script>

View File

@@ -19,7 +19,7 @@ import { cacheManager } from './cache-manager'
export const pdfQueue = pLimit(settings.backgroundProcesses) export const pdfQueue = pLimit(settings.backgroundProcesses)
/** /**
* Adds a file to the index * Adds a file to the search index
* @param file * @param file
* @returns * @returns
*/ */
@@ -30,26 +30,24 @@ export async function addToIndexAndCache(file: TAbstractFile): Promise<void> {
// Check if the file was already indexed as non-existent, // Check if the file was already indexed as non-existent,
// and if so, remove it from the index (before adding it again) // 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) removeFromIndex(file.path)
} }
try { try {
// console.log(`Omnisearch - adding ${file.path} to index`)
// Look for links that lead to non-existing files, // Look for links that lead to non-existing files,
// and index them as well // and index them as well
const metadata = app.metadataCache.getFileCache(file) const metadata = app.metadataCache.getFileCache(file)
if (metadata) { if (metadata) {
const nonExisting = getNonExistingNotes(file, metadata) const nonExisting = getNonExistingNotes(file, metadata)
for (const name of nonExisting.filter( for (const name of nonExisting.filter(
o => !cacheManager.getNoteFromCache(o) o => !cacheManager.getNoteFromMemCache(o)
)) { )) {
addNonExistingToIndex(name, file.path) addNonExistingToIndex(name, file.path)
} }
} }
if (cacheManager.getNoteFromCache(file.path)) { if (cacheManager.getNoteFromMemCache(file.path)) {
throw new Error(`${file.basename} is already indexed`) throw new Error(`${file.basename} is already indexed`)
} }
@@ -82,7 +80,7 @@ export async function addToIndexAndCache(file: TAbstractFile): Promise<void> {
} }
Search.minisearchInstance.add(note) Search.minisearchInstance.add(note)
cacheManager.addNoteToCache(note.path, note) cacheManager.addNoteToMemCache(note.path, note)
} catch (e) { } catch (e) {
// console.trace('Error while indexing ' + file.basename) // console.trace('Error while indexing ' + file.basename)
console.error(e) console.error(e)
@@ -98,7 +96,7 @@ export async function addToIndexAndCache(file: TAbstractFile): Promise<void> {
export function addNonExistingToIndex(name: string, parent: string): void { export function addNonExistingToIndex(name: string, parent: string): void {
name = removeAnchors(name) name = removeAnchors(name)
const filename = name + (name.endsWith('.md') ? '' : '.md') const filename = name + (name.endsWith('.md') ? '' : '.md')
if (cacheManager.getNoteFromCache(filename)) return if (cacheManager.getNoteFromMemCache(filename)) return
const note: IndexedDocument = { const note: IndexedDocument = {
path: filename, path: filename,
@@ -116,7 +114,7 @@ export function addNonExistingToIndex(name: string, parent: string): void {
parent, parent,
} }
Search.minisearchInstance.add(note) 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`) console.info(`"${path}" is not an indexable file`)
return return
} }
const note = cacheManager.getNoteFromCache(path) const note = cacheManager.getNoteFromMemCache(path)
if (note) { if (note) {
Search.minisearchInstance.remove(note) Search.minisearchInstance.remove(note)
cacheManager.removeNoteFromCache(path) cacheManager.removeNoteFromMemCache(path)
cacheManager cacheManager
.getNonExistingNotesFromCache() .getNonExistingNotesFromMemCache()
.filter(n => n.parent === path) .filter(n => n.parent === path)
.forEach(n => { .forEach(n => {
removeFromIndex(n.path) removeFromIndex(n.path)
@@ -171,7 +169,7 @@ export async function indexPDFs() {
console.log(`Omnisearch - Indexing ${files.length} PDFs`) console.log(`Omnisearch - Indexing ${files.length} PDFs`)
const input = [] const input = []
for (const file of files) { for (const file of files) {
if (cacheManager.getNoteFromCache(file.path)) { if (cacheManager.getNoteFromMemCache(file.path)) {
removeFromIndex(file.path) removeFromIndex(file.path)
} }
input.push( input.push(

View File

@@ -33,7 +33,9 @@ export async function openNote(
const view = app.workspace.getActiveViewOfType(MarkdownView) const view = app.workspace.getActiveViewOfType(MarkdownView)
if (!view) { 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) const pos = view.editor.offsetToPos(offset)
pos.ch = 0 pos.ch = 0

View File

@@ -86,7 +86,7 @@ export async function initGlobalSearchIndex(): Promise<void> {
let files let files
let notesSuffix let notesSuffix
if (settings.persistCache) { if (settings.persistCache) {
files = allFiles.filter(file => cacheManager.isCacheOutdated(file)) files = allFiles.filter(file => cacheManager.isNoteInMemCacheOutdated(file))
notesSuffix = 'modified notes' notesSuffix = 'modified notes'
} else { } else {
files = allFiles files = allFiles
@@ -101,7 +101,7 @@ export async function initGlobalSearchIndex(): Promise<void> {
const queue = pLimit(10) const queue = pLimit(10)
const input = [] const input = []
for (const file of files) { for (const file of files) {
if (cacheManager.getNoteFromCache(file.path)) { if (cacheManager.getNoteFromMemCache(file.path)) {
NotesIndex.removeFromIndex(file.path) NotesIndex.removeFromIndex(file.path)
} }
input.push(queue(() => NotesIndex.addToIndexAndCache(file))) input.push(queue(() => NotesIndex.addToIndexAndCache(file)))
@@ -136,6 +136,7 @@ export async function initGlobalSearchIndex(): Promise<void> {
async function search(query: Query): Promise<SearchResult[]> { async function search(query: Query): Promise<SearchResult[]> {
if (!query.segmentsToStr()) return [] if (!query.segmentsToStr()) return []
console.time('Omnisearch - searching')
let results = minisearchInstance.search(query.segmentsToStr(), { let results = minisearchInstance.search(query.segmentsToStr(), {
prefix: true, prefix: true,
fuzzy: term => (term.length > 4 ? 0.2 : false), fuzzy: term => (term.length > 4 ? 0.2 : false),
@@ -148,7 +149,8 @@ async function search(query: Query): Promise<SearchResult[]> {
headings3: settings.weightH3, headings3: settings.weightH3,
}, },
}) })
console.timeEnd('Omnisearch - searching')
// Downrank files that are in Obsidian's excluded list // Downrank files that are in Obsidian's excluded list
if (settings.respectExcluded) { if (settings.respectExcluded) {
results.forEach(result => { results.forEach(result => {
@@ -166,9 +168,9 @@ async function search(query: Query): Promise<SearchResult[]> {
if (exactTerms.length) { if (exactTerms.length) {
results = results.filter(r => { results = results.filter(r => {
const title = const title =
cacheManager.getNoteFromCache(r.id)?.path.toLowerCase() ?? '' cacheManager.getNoteFromMemCache(r.id)?.path.toLowerCase() ?? ''
const content = stripMarkdownCharacters( const content = stripMarkdownCharacters(
cacheManager.getNoteFromCache(r.id)?.content ?? '' cacheManager.getNoteFromMemCache(r.id)?.content ?? ''
).toLowerCase() ).toLowerCase()
return exactTerms.every(q => content.includes(q) || title.includes(q)) return exactTerms.every(q => content.includes(q) || title.includes(q))
}) })
@@ -179,7 +181,7 @@ async function search(query: Query): Promise<SearchResult[]> {
if (exclusions.length) { if (exclusions.length) {
results = results.filter(r => { results = results.filter(r => {
const content = stripMarkdownCharacters( const content = stripMarkdownCharacters(
cacheManager.getNoteFromCache(r.id)?.content ?? '' cacheManager.getNoteFromMemCache(r.id)?.content ?? ''
).toLowerCase() ).toLowerCase()
return exclusions.every(q => !content.includes(q.value)) return exclusions.every(q => !content.includes(q.value))
}) })
@@ -218,7 +220,9 @@ export async function getSuggestions(
options?: Partial<{ singleFilePath: string | null }> options?: Partial<{ singleFilePath: string | null }>
): Promise<ResultNote[]> { ): Promise<ResultNote[]> {
// Get the raw results // Get the raw results
console.time('search')
let results = await search(query) let results = await search(query)
console.timeEnd('search')
if (!results.length) return [] if (!results.length) return []
// Extract tags from the query // Extract tags from the query
@@ -247,7 +251,7 @@ export async function getSuggestions(
// Map the raw results to get usable suggestions // Map the raw results to get usable suggestions
return results.map(result => { return results.map(result => {
const note = cacheManager.getNoteFromCache(result.id) const note = cacheManager.getNoteFromMemCache(result.id)
if (!note) { if (!note) {
throw new Error(`Note "${result.id}" not indexed`) throw new Error(`Note "${result.id}" not indexed`)
} }

View File

@@ -341,7 +341,7 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
PDFIndexing: false, PDFIndexing: false,
backgroundProcesses: Platform.isMobileApp backgroundProcesses: Platform.isMobileApp
? 1 ? 1
: Math.max(1, Math.floor(require('os').cpus().length - 2)), : Math.max(1, Math.floor(require('os').cpus().length * 0.75)),
showIndexingNotices: false, showIndexingNotices: false,
showShortName: false, showShortName: false,