Re-added the live cache, lazily populated

This commit is contained in:
Simon Cambier
2022-11-26 22:20:48 +01:00
parent d4519b57c2
commit 911ef193ae
6 changed files with 45 additions and 39 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "scambier.obsidian-search",
"version": "1.9.0-beta.1",
"version": "1.9.0-beta.2",
"description": "A search engine for Obsidian",
"main": "dist/main.js",
"scripts": {

View File

@@ -1,4 +1,4 @@
import { Notice, TFile } from 'obsidian'
import { Notice } from 'obsidian'
import type { DocumentRef, IndexedDocument } from './globals'
import { database } from './database'
import type { AsPlainObject } from 'minisearch'
@@ -11,13 +11,11 @@ import {
isFilePDF,
isFilePlaintext,
makeMD5,
removeDiacritics
removeDiacritics,
} from './tools/utils'
import { getImageText, getPdfText } from "obsidian-text-extract";
import { getImageText, getPdfText } from 'obsidian-text-extract'
async function getIndexedDocument(
path: string
): Promise<IndexedDocument> {
async function getIndexedDocument(path: string): Promise<IndexedDocument> {
const file = app.vault.getFiles().find(f => f.path === path)
if (!file) throw new Error(`Invalid file path: "${path}"`)
let content: string
@@ -76,12 +74,20 @@ class CacheManager {
*/
private nextQueryIsEmpty = false
/**
* The "live cache", containing all indexed vault files
* in the form of IndexedDocuments
*/
private documents: Map<string, IndexedDocument> = new Map()
public async addToLiveCache(path: string): Promise<void> {
const doc = await getIndexedDocument(path)
this.documents.set(path, doc)
console.log(path)
// console.log(path)
}
public removeFromLiveCache(path: string): void {
this.documents.delete(path)
}
public async getDocument(path: string): Promise<IndexedDocument> {

View File

@@ -32,7 +32,7 @@
// the next time we open the modal, the search field will be empty
cacheManager.addToSearchHistory('')
dispatch('input', value)
}, 200)
}, 500)
</script>
<div class="omnisearch-input-container">

View File

@@ -3,13 +3,10 @@
import { onDestroy, onMount, tick } from 'svelte'
import InputSearch from './InputSearch.svelte'
import ModalContainer from './ModalContainer.svelte'
import { eventBus, indexingStep, IndexingStepType, type ResultNote } from 'src/globals'
import { eventBus, indexingStep, IndexingStepType, type ResultNote, } from 'src/globals'
import { createNote, openNote } from 'src/tools/notes'
import { getCtrlKeyLabel, getExtension, isFilePDF, loopIndex } from 'src/tools/utils'
import {
OmnisearchInFileModal,
type OmnisearchVaultModal,
} from 'src/components/modals'
import { getCtrlKeyLabel, getExtension, isFilePDF, loopIndex, } from 'src/tools/utils'
import { OmnisearchInFileModal, type OmnisearchVaultModal, } from 'src/components/modals'
import ResultItemVault from './ResultItemVault.svelte'
import { Query } from 'src/search/query'
import { settings } from '../settings'
@@ -25,12 +22,18 @@
let resultNotes: ResultNote[] = []
let query: Query
let indexingStepDesc = ''
let searching = true
$: selectedNote = resultNotes[selectedIndex]
$: searchQuery = searchQuery ?? previousQuery
$: if (searchQuery) {
updateResults()
resultNotes = []
searching = true
updateResults().then(() => {
searching = false
})
} else {
searching = false
resultNotes = []
}
$: {
@@ -246,11 +249,13 @@
on:mousemove="{_ => (selectedIndex = i)}"
on:click="{onClick}" />
{/each}
{#if !resultNotes.length && searchQuery}
<div style="text-align: center;">
<div style="text-align: center;">
{#if !resultNotes.length && searchQuery && !searching}
We found 0 result for your search here.
</div>
{/if}
{:else if searching}
Searching...
{/if}
</div>
</ModalContainer>
<div class="prompt-instructions">

View File

@@ -6,13 +6,7 @@ import {
import { loadSettings, settings, SettingsTab, showExcerpt } from './settings'
import { eventBus, EventNames, indexingStep, IndexingStepType } from './globals'
import api from './tools/api'
import {
isFileImage,
isFileIndexable,
isFilePDF,
isFilePlaintext,
wait,
} from './tools/utils'
import { isFileIndexable, isFilePlaintext } from './tools/utils'
import { OmnisearchCache } from './database'
import * as NotesIndex from './notes-index'
import { searchEngine } from './search/omnisearch'
@@ -59,23 +53,28 @@ 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', file => {
this.app.vault.on('create', async file => {
cacheManager.addToLiveCache(file.path)
searchEngine.addFromPaths([file.path], false)
})
)
this.registerEvent(
this.app.vault.on('delete', file => {
cacheManager.removeFromLiveCache(file.path)
searchEngine.removeFromPaths([file.path])
})
)
this.registerEvent(
this.app.vault.on('modify', async file => {
cacheManager.addToLiveCache(file.path)
NotesIndex.markNoteForReindex(file)
})
)
this.registerEvent(
this.app.vault.on('rename', async (file, oldPath) => {
if (file instanceof TFile && isFilePlaintext(file.path)) {
cacheManager.removeFromLiveCache(oldPath)
cacheManager.addToLiveCache(file.path)
searchEngine.removeFromPaths([oldPath])
await searchEngine.addFromPaths([file.path], false)
}
@@ -115,7 +114,7 @@ async function populateIndex(): Promise<void> {
const files = app.vault.getFiles().filter(f => isFileIndexable(f.path))
// Map documents in the background
files.forEach(f => cacheManager.addToLiveCache(f.path))
// Promise.all(files.map(f => cacheManager.addToLiveCache(f.path)))
if (!Platform.isIosApp) {
console.time('Omnisearch - Loading index from cache')

View File

@@ -1,8 +1,4 @@
import MiniSearch, {
type AsPlainObject,
type Options,
type SearchResult,
} from 'minisearch'
import MiniSearch, { type Options, type SearchResult } from 'minisearch'
import type {
DocumentRef,
IndexedDocument,
@@ -183,6 +179,8 @@ export class Omnisearch {
})
}
results = results.slice(0, 50)
const documents = await Promise.all(
results.map(async result => await cacheManager.getDocument(result.id))
)
@@ -212,11 +210,9 @@ export class Omnisearch {
}
// FIXME:
// Dedupe results - clutch for https://github.com/scambier/obsidian-omnisearch/issues/129
results = results
.filter(
(result, index, arr) => arr.findIndex(t => t.id === result.id) === index
)
.slice(0, 50)
results = results.filter(
(result, index, arr) => arr.findIndex(t => t.id === result.id) === index
)
this.previousResults = results