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

View File

@@ -26,7 +26,7 @@ export class NotesIndexer {
public async refreshIndex(): Promise<void> {
for (const file of this.notesToReindex) {
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)

View File

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

View File

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