Fixed potential indexing issues
This commit is contained in:
@@ -60,12 +60,10 @@ async function getAndMapIndexedDocument(
|
||||
}
|
||||
|
||||
// ** Image or PDF **
|
||||
if (extractor) {
|
||||
if (extractor.canFileBeExtracted(path)) {
|
||||
else if (extractor?.canFileBeExtracted(path)) {
|
||||
content = await extractor.extractText(file)
|
||||
} else {
|
||||
throw new Error('Invalid file format: ' + file.path)
|
||||
}
|
||||
throw new Error(`Unsupported file type: "${path}"`)
|
||||
}
|
||||
|
||||
if (content === null || content === undefined) {
|
||||
@@ -125,6 +123,10 @@ class CacheManager {
|
||||
*/
|
||||
private documents: Map<string, IndexedDocument> = new Map()
|
||||
|
||||
/**
|
||||
* Set or update the live cache with the content of the given file.
|
||||
* @param path
|
||||
*/
|
||||
public async addToLiveCache(path: string): Promise<void> {
|
||||
try {
|
||||
const doc = await getAndMapIndexedDocument(path)
|
||||
|
||||
22
src/main.ts
22
src/main.ts
@@ -64,11 +64,12 @@ export default class OmnisearchPlugin extends Plugin {
|
||||
},
|
||||
})
|
||||
|
||||
app.workspace.onLayoutReady(async () => {
|
||||
// Listeners to keep the search index up-to-date
|
||||
this.registerEvent(
|
||||
this.app.vault.on('create', async file => {
|
||||
this.app.vault.on('create', file => {
|
||||
if (isFileIndexable(file.path)) {
|
||||
await cacheManager.addToLiveCache(file.path)
|
||||
// await cacheManager.addToLiveCache(file.path)
|
||||
searchEngine.addFromPaths([file.path])
|
||||
}
|
||||
})
|
||||
@@ -98,7 +99,6 @@ export default class OmnisearchPlugin extends Plugin {
|
||||
})
|
||||
)
|
||||
|
||||
app.workspace.onLayoutReady(async () => {
|
||||
this.executeFirstLaunchTasks()
|
||||
await this.populateIndex()
|
||||
})
|
||||
@@ -145,31 +145,39 @@ export default class OmnisearchPlugin extends Plugin {
|
||||
indexingStep.set(IndexingStepType.ReadingFiles)
|
||||
const files = app.vault.getFiles().filter(f => isFileIndexable(f.path))
|
||||
console.log(`Omnisearch - ${files.length} files total`)
|
||||
|
||||
console.log(
|
||||
`Omnisearch - Cache is ${isCacheEnabled() ? 'enabled' : 'disabled'}`
|
||||
)
|
||||
// Map documents in the background
|
||||
// Promise.all(files.map(f => cacheManager.addToLiveCache(f.path)))
|
||||
|
||||
if (isCacheEnabled()) {
|
||||
console.time('Omnisearch - Loading index from cache')
|
||||
indexingStep.set(IndexingStepType.LoadingCache)
|
||||
await searchEngine.loadCache()
|
||||
const hasCache = await searchEngine.loadCache()
|
||||
if (hasCache) {
|
||||
console.timeEnd('Omnisearch - Loading index from cache')
|
||||
}
|
||||
}
|
||||
|
||||
const diff = searchEngine.getDiff(
|
||||
files.map(f => ({ path: f.path, mtime: f.stat.mtime }))
|
||||
)
|
||||
|
||||
if (isCacheEnabled()) {
|
||||
if (diff.toAdd.length) {
|
||||
console.log(
|
||||
'Omnisearch - Total number of files to add/update: ' + diff.toAdd.length
|
||||
'Omnisearch - Total number of files to add/update: ' +
|
||||
diff.toAdd.length
|
||||
)
|
||||
}
|
||||
if (diff.toRemove.length) {
|
||||
console.log(
|
||||
'Omnisearch - Total number of files to remove: ' + diff.toRemove.length
|
||||
'Omnisearch - Total number of files to remove: ' +
|
||||
diff.toRemove.length
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (diff.toAdd.length >= 1000 && isCacheEnabled()) {
|
||||
new Notice(
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
import { Notice, Platform } from 'obsidian'
|
||||
import type { Query } from './query'
|
||||
import { cacheManager } from '../cache-manager'
|
||||
import { sortBy } from 'lodash-es'
|
||||
|
||||
const tokenize = (text: string): string[] => {
|
||||
const tokens = text.split(SPACE_OR_PUNCTUATION)
|
||||
@@ -59,12 +60,19 @@ export class Omnisearch {
|
||||
this.minisearch = new MiniSearch(Omnisearch.options)
|
||||
}
|
||||
|
||||
async loadCache(): Promise<void> {
|
||||
/**
|
||||
* Return true if the cache is valid
|
||||
*/
|
||||
async loadCache(): Promise<boolean> {
|
||||
const cache = await cacheManager.getMinisearchCache()
|
||||
if (cache) {
|
||||
// console.log('Omnisearch - Cache', cache)
|
||||
this.minisearch = MiniSearch.loadJS(cache.data, Omnisearch.options)
|
||||
this.indexedDocuments = new Map(cache.paths.map(o => [o.path, o.mtime]))
|
||||
return true
|
||||
}
|
||||
console.log('Omnisearch - No cache found')
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,11 +85,13 @@ export class Omnisearch {
|
||||
} {
|
||||
const docsMap = new Map(docs.map(d => [d.path, d.mtime]))
|
||||
|
||||
// console.log(this.indexedDocuments)
|
||||
const toAdd = docs.filter(
|
||||
d =>
|
||||
!this.indexedDocuments.has(d.path) ||
|
||||
this.indexedDocuments.get(d.path) !== d.mtime
|
||||
)
|
||||
// console.log(toAdd)
|
||||
const toRemove = [...this.indexedDocuments]
|
||||
.filter(
|
||||
([path, mtime]) => !docsMap.has(path) || docsMap.get(path) !== mtime
|
||||
@@ -100,6 +110,8 @@ export class Omnisearch {
|
||||
paths.map(async path => await cacheManager.getDocument(path))
|
||||
)
|
||||
).filter(d => !!d?.path)
|
||||
// Index markdown files first
|
||||
documents = sortBy(documents, d => (d.path.endsWith('.md') ? 0 : 1))
|
||||
|
||||
// If a document is already added, discard it
|
||||
this.removeFromPaths(
|
||||
|
||||
@@ -237,8 +237,9 @@ export function getCtrlKeyLabel(): 'ctrl' | '⌘' {
|
||||
}
|
||||
|
||||
export function isFileIndexable(path: string): boolean {
|
||||
const canIndexPDF = !!getTextExtractor() && settings.PDFIndexing
|
||||
const canIndexImages = !!getTextExtractor() && settings.imagesIndexing
|
||||
const hasTextExtractor = !!getTextExtractor()
|
||||
const canIndexPDF = hasTextExtractor && settings.PDFIndexing
|
||||
const canIndexImages = hasTextExtractor && settings.imagesIndexing
|
||||
return (
|
||||
isFilePlaintext(path) ||
|
||||
isFileCanvas(path) ||
|
||||
|
||||
Reference in New Issue
Block a user