Renamed CacheManager to DocumentsRepository

This commit is contained in:
Simon Cambier
2024-10-13 18:16:25 +02:00
parent 4ed3b4d612
commit fc19f96dfd
4 changed files with 21 additions and 23 deletions

View File

@@ -24,7 +24,7 @@ import {
import { notifyOnIndexed, registerAPI } from './tools/api' import { notifyOnIndexed, registerAPI } from './tools/api'
import { Database } from './database' import { Database } from './database'
import { SearchEngine } from './search/search-engine' import { SearchEngine } from './search/search-engine'
import { CacheManager } from './cache-manager' import { DocumentsRepository } from './repositories/documents-repository'
import { logDebug } from './tools/utils' import { logDebug } from './tools/utils'
import { NotesIndexer } from './notes-indexer' import { NotesIndexer } from './notes-indexer'
import { TextProcessor } from './tools/text-processing' import { TextProcessor } from './tools/text-processing'
@@ -36,8 +36,8 @@ export default class OmnisearchPlugin extends Plugin {
public apiHttpServer: null | any = null public apiHttpServer: null | any = null
public settings: OmnisearchSettings = getDefaultSettings(this.app) public settings: OmnisearchSettings = getDefaultSettings(this.app)
// FIXME: merge cache and cacheManager, or find other names public readonly documentsRepository: DocumentsRepository
public readonly cacheManager: CacheManager public readonly embedsRepository = new EmbedsRepository(this)
public readonly database = new Database(this) public readonly database = new Database(this)
public readonly notesIndexer = new NotesIndexer(this) public readonly notesIndexer = new NotesIndexer(this)
@@ -45,14 +45,12 @@ export default class OmnisearchPlugin extends Plugin {
public readonly searchEngine = new SearchEngine(this) public readonly searchEngine = new SearchEngine(this)
public readonly searchHistory = new SearchHistory(this) public readonly searchHistory = new SearchHistory(this)
public readonly embedsRepository = new EmbedsRepository(this)
private ribbonButton?: HTMLElement private ribbonButton?: HTMLElement
private refreshIndexCallback?: () => void private refreshIndexCallback?: () => void
constructor(app: App, manifest: PluginManifest) { constructor(app: App, manifest: PluginManifest) {
super(app, manifest) super(app, manifest)
this.cacheManager = new CacheManager(this) this.documentsRepository = new DocumentsRepository(this)
} }
async onload(): Promise<void> { async onload(): Promise<void> {
@@ -121,7 +119,7 @@ export default class OmnisearchPlugin extends Plugin {
this.registerEvent( this.registerEvent(
this.app.vault.on('delete', file => { this.app.vault.on('delete', file => {
logDebug('Removing file', file.path) logDebug('Removing file', file.path)
this.cacheManager.removeFromLiveCache(file.path) this.documentsRepository.removeDocument(file.path)
searchEngine.removeFromPaths([file.path]) searchEngine.removeFromPaths([file.path])
this.embedsRepository.removeFile(file.path) this.embedsRepository.removeFile(file.path)
}) })
@@ -138,8 +136,8 @@ export default class OmnisearchPlugin extends Plugin {
this.app.vault.on('rename', async (file, oldPath) => { this.app.vault.on('rename', async (file, oldPath) => {
if (this.notesIndexer.isFileIndexable(file.path)) { if (this.notesIndexer.isFileIndexable(file.path)) {
logDebug('Renaming file', file.path) logDebug('Renaming file', file.path)
this.cacheManager.removeFromLiveCache(oldPath) this.documentsRepository.removeDocument(oldPath)
await this.cacheManager.addToLiveCache(file.path) await this.documentsRepository.addDocument(file.path)
searchEngine.removeFromPaths([oldPath]) searchEngine.removeFromPaths([oldPath])
await searchEngine.addFromPaths([file.path]) await searchEngine.addFromPaths([file.path])

View File

@@ -26,7 +26,7 @@ export class NotesIndexer {
public async refreshIndex(): Promise<void> { public async refreshIndex(): Promise<void> {
for (const file of this.notesToReindex) { for (const file of this.notesToReindex) {
logDebug('Updating file', file.path) logDebug('Updating file', file.path)
await this.plugin.cacheManager.addToLiveCache(file.path) await this.plugin.documentsRepository.addDocument(file.path)
} }
const paths = [...this.notesToReindex].map(n => n.path) const paths = [...this.notesToReindex].map(n => n.path)

View File

@@ -1,5 +1,5 @@
import { normalizePath, TFile } from 'obsidian' import { normalizePath, TFile } from 'obsidian'
import type { IndexedDocument } from './globals' import type { IndexedDocument } from '../globals'
import { import {
extractHeadingsFromCache, extractHeadingsFromCache,
getAliasesFromMetadata, getAliasesFromMetadata,
@@ -12,12 +12,12 @@ import {
logDebug, logDebug,
removeDiacritics, removeDiacritics,
stripMarkdownCharacters, stripMarkdownCharacters,
} from './tools/utils' } from '../tools/utils'
import type { CanvasData } from 'obsidian/canvas' import type { CanvasData } from 'obsidian/canvas'
import type OmnisearchPlugin from './main' import type OmnisearchPlugin from '../main'
import { getNonExistingNotes } from './tools/notes' import { getNonExistingNotes } from '../tools/notes'
export class CacheManager { export class DocumentsRepository {
/** /**
* The "live cache", containing all indexed vault files * The "live cache", containing all indexed vault files
@@ -31,7 +31,7 @@ export class CacheManager {
* Set or update the live cache with the content of the given file. * Set or update the live cache with the content of the given file.
* @param path * @param path
*/ */
public async addToLiveCache(path: string): Promise<void> { public async addDocument(path: string): Promise<void> {
try { try {
const doc = await this.getAndMapIndexedDocument(path) const doc = await this.getAndMapIndexedDocument(path)
if (!doc.path) { if (!doc.path) {
@@ -45,12 +45,12 @@ export class CacheManager {
} catch (e) { } catch (e) {
console.warn(`Omnisearch: Error while adding "${path}" to live cache`, e) console.warn(`Omnisearch: Error while adding "${path}" to live cache`, e)
// Shouldn't be needed, but... // Shouldn't be needed, but...
this.removeFromLiveCache(path) this.removeDocument(path)
// TODO: increment errors counter // TODO: increment errors counter
} }
} }
public removeFromLiveCache(path: string): void { public removeDocument(path: string): void {
this.documents.delete(path) this.documents.delete(path)
} }
@@ -59,7 +59,7 @@ export class CacheManager {
return this.documents.get(path)! return this.documents.get(path)!
} }
logDebug('Generating IndexedDocument from', path) logDebug('Generating IndexedDocument from', path)
await this.addToLiveCache(path) await this.addDocument(path)
return this.documents.get(path)! return this.documents.get(path)!
} }

View File

@@ -79,7 +79,7 @@ export class SearchEngine {
let documents = ( let documents = (
await Promise.all( await Promise.all(
paths.map( paths.map(
async path => await this.plugin.cacheManager.getDocument(path) async path => await this.plugin.documentsRepository.getDocument(path)
) )
) )
).filter(d => !!d?.path) ).filter(d => !!d?.path)
@@ -301,7 +301,7 @@ export class SearchEngine {
const documents = await Promise.all( const documents = await Promise.all(
results.map( results.map(
async result => await this.plugin.cacheManager.getDocument(result.id) async result => await this.plugin.documentsRepository.getDocument(result.id)
) )
) )
@@ -377,7 +377,7 @@ export class SearchEngine {
const documents = await Promise.all( const documents = await Promise.all(
results.map( results.map(
async result => await this.plugin.cacheManager.getDocument(result.id) async result => await this.plugin.documentsRepository.getDocument(result.id)
) )
) )
@@ -394,7 +394,7 @@ export class SearchEngine {
// Inject embeds in the results // Inject embeds in the results
for (const embed of embeds) { for (const embed of embeds) {
total++ total++
const newDoc = await this.plugin.cacheManager.getDocument(embed) const newDoc = await this.plugin.documentsRepository.getDocument(embed)
documents.splice(i + 1, 0, newDoc) documents.splice(i + 1, 0, newDoc)
results.splice(i + 1, 0, { results.splice(i + 1, 0, {
id: newDoc.path, id: newDoc.path,