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')
}
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
}
}

View File

@@ -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

View File

@@ -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
</script>

View File

@@ -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<void> {
// 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<void> {
}
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<void> {
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(

View File

@@ -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

View File

@@ -86,7 +86,7 @@ export async function initGlobalSearchIndex(): Promise<void> {
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<void> {
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<void> {
async function search(query: Query): Promise<SearchResult[]> {
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<SearchResult[]> {
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<SearchResult[]> {
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<SearchResult[]> {
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<ResultNote[]> {
// 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`)
}

View File

@@ -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,