Add storeIndexInFile setting

This commit is contained in:
Michael Naumov
2022-06-06 08:12:51 -06:00
parent 7cd4f6c502
commit 9b1123dee8
3 changed files with 34 additions and 11 deletions

View File

@@ -6,6 +6,7 @@ import {
} from 'obsidian' } from 'obsidian'
import type { IndexedNote, ResultNote } from './globals' import type { IndexedNote, ResultNote } from './globals'
import { stringsToRegex } from './utils' import { stringsToRegex } from './utils'
import { settings } from './settings'
/** /**
* This is an in-memory cache of the notes, with all their computed fields * This is an in-memory cache of the notes, with all their computed fields
@@ -21,7 +22,7 @@ export function resetNotesCache(): void {
} }
export async function loadNotesCache(): Promise<void> { export async function loadNotesCache(): Promise<void> {
if (await app.vault.adapter.exists(notesCacheFilePath)) { if (settings.storeIndexInFile && await app.vault.adapter.exists(notesCacheFilePath)) {
try { try {
const json = await app.vault.adapter.read(notesCacheFilePath) const json = await app.vault.adapter.read(notesCacheFilePath)
notesCache = JSON.parse(json) notesCache = JSON.parse(json)

View File

@@ -63,11 +63,12 @@ export async function initGlobalSearchIndex(): Promise<void> {
], ],
} }
if (await app.vault.adapter.exists(searchIndexFilePath)) { if (settings.storeIndexInFile && await app.vault.adapter.exists(searchIndexFilePath)) {
try { try {
const json = await app.vault.adapter.read(searchIndexFilePath) const json = await app.vault.adapter.read(searchIndexFilePath)
minisearchInstance = MiniSearch.loadJSON(json, options) minisearchInstance = MiniSearch.loadJSON(json, options)
console.log("MiniSearch index loaded from the file") console.log("MiniSearch index loaded from the file")
await loadNotesCache()
} }
catch(e) { catch(e) {
console.trace("Could not load MiniSearch index from the file") console.trace("Could not load MiniSearch index from the file")
@@ -80,16 +81,25 @@ export async function initGlobalSearchIndex(): Promise<void> {
resetNotesCache() resetNotesCache()
} }
await loadNotesCache()
// Index files that are already present // Index files that are already present
const start = new Date().getTime() const start = new Date().getTime()
const files = app.vault.getMarkdownFiles().filter(file => isCacheOutdated(file))
const allFiles = app.vault.getMarkdownFiles()
let files
let notesSuffix
if (settings.storeIndexInFile) {
files = allFiles.filter(file => isCacheOutdated(file))
notesSuffix = 'modified notes'
} else {
files = allFiles
notesSuffix = 'notes'
}
console.log(`Omnisearch - indexing ${files.length} ${notesSuffix}`)
// This is basically the same behavior as MiniSearch's `addAllAsync()`. // This is basically the same behavior as MiniSearch's `addAllAsync()`.
// We index files by batches of 10 // We index files by batches of 10
console.log('Omnisearch - indexing ' + files.length + ' modified notes')
for (let i = 0; i < files.length; ++i) { for (let i = 0; i < files.length; ++i) {
if (i % 10 === 0) await wait(0) if (i % 10 === 0) await wait(0)
const file = files[i] const file = files[i]
@@ -102,7 +112,7 @@ export async function initGlobalSearchIndex(): Promise<void> {
} }
if (files.length > 0) { if (files.length > 0) {
const message = `Omnisearch - Indexed ${files.length} modified notes in ${ const message = `Omnisearch - Indexed ${files.length} ${notesSuffix} in ${
new Date().getTime() - start new Date().getTime() - start
}ms` }ms`
@@ -381,7 +391,7 @@ export async function reindexNotes(): Promise<void> {
} }
async function saveIndexToFile(): Promise<void> { async function saveIndexToFile(): Promise<void> {
if (minisearchInstance && isIndexChanged) { if (settings.storeIndexInFile && minisearchInstance && isIndexChanged) {
const json = JSON.stringify(minisearchInstance) const json = JSON.stringify(minisearchInstance)
await app.vault.adapter.write(searchIndexFilePath, json) await app.vault.adapter.write(searchIndexFilePath, json)
console.log("MiniSearch index saved to the file") console.log("MiniSearch index saved to the file")

View File

@@ -15,6 +15,7 @@ export interface OmnisearchSettings extends WeightingSettings {
showShortName: boolean showShortName: boolean
CtrlJK: boolean CtrlJK: boolean
CtrlNP: boolean CtrlNP: boolean
storeIndexInFile: boolean
} }
export class SettingsTab extends PluginSettingTab { export class SettingsTab extends PluginSettingTab {
@@ -68,6 +69,15 @@ export class SettingsTab extends PluginSettingTab {
}), }),
) )
new Setting(containerEl)
.setName('Store index in file')
.addToggle(toggle =>
toggle.setValue(settings.storeIndexInFile).onChange(async v => {
settings.storeIndexInFile = v
await saveSettings(this.plugin)
}),
)
// #endregion Behavior // #endregion Behavior
// #region User Interface // #region User Interface
@@ -174,6 +184,8 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
CtrlJK: false, CtrlJK: false,
CtrlNP: false, CtrlNP: false,
storeIndexInFile: false
} as const } as const
export let settings: OmnisearchSettings = Object.assign({}, DEFAULT_SETTINGS) export let settings: OmnisearchSettings = Object.assign({}, DEFAULT_SETTINGS)