Skip to content

Commit

Permalink
Fix reindexing resources of previously deleted categories
Browse files Browse the repository at this point in the history
  • Loading branch information
tkleinke committed Jun 15, 2022
1 parent d029a7d commit ae68891
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
20 changes: 10 additions & 10 deletions core/src/index/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { Document } from '../model/document';
export module Indexer {

export async function reindex(indexFacade: IndexFacade, db: PouchDB.Database, documentCache: DocumentCache,
converter: CategoryConverter, setIndexedDocuments?: (count: number) => Promise<void>,
converter: CategoryConverter, keepCachedInstances: boolean,
setIndexedDocuments?: (count: number) => Promise<void>,
setIndexing?: () => Promise<void>, setError?: (error: string) => Promise<void>) {

indexFacade.clear();
Expand All @@ -29,8 +30,16 @@ import { Document } from '../model/document';
setIndexing && await setIndexing();

try {
if (keepCachedInstances) {
documents = documents.filter(document => !documentCache.get(document.resource.id));
}
documents = convertDocuments(documents, converter);
documents.forEach(doc => documentCache.set(doc));

if (keepCachedInstances) {
documents = documents.concat(documentCache.getAll());
}

await indexFacade.putMultiple(documents, setIndexedDocuments);
} catch (err) {
console.error(err);
Expand All @@ -40,15 +49,6 @@ import { Document } from '../model/document';
}


export async function reindexFromCache(indexFacade: IndexFacade, documentCache: DocumentCache) {

indexFacade.clear();

const documents: Array<Document> = documentCache.getAll();
await indexFacade.putMultiple(documents);
}


async function fetchAll(db: PouchDB.Database) {

return (await db
Expand Down
1 change: 1 addition & 0 deletions desktop/src/app/components/app-initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ const loadDocuments = async (

await Indexer.reindex(serviceLocator.indexFacade, db, documentCache,
new CategoryConverter(serviceLocator.projectConfiguration),
false,
(count) => progress.setIndexedDocuments(count),
() => progress.setPhase('indexingDocuments'),
(error) => progress.setError(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,12 @@ export class ConfigurationComponent implements OnInit, OnDestroy {
});
});

await Indexer.reindexFromCache(this.indexFacade, this.documentCache);
await Indexer.reindex(
this.indexFacade,
this.pouchdbDatastore.getDb(),
this.documentCache,
this.categoryConverter,
true
);
}
}
3 changes: 2 additions & 1 deletion desktop/src/app/services/app-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export class AppController {
this.indexFacade,
db,
this.documentCache,
new CategoryConverter(this.projectConfiguration)
new CategoryConverter(this.projectConfiguration),
false
);

const configurationDocument: ConfigurationDocument = await ConfigurationDocument.getConfigurationDocument(
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/repositories/document-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const buildDatastore = async (
const documentCache = new DocumentCache();
const converter = new CategoryConverter(projectConfiguration);

await Indexer.reindex(indexFacade, db, documentCache, converter);
await Indexer.reindex(indexFacade, db, documentCache, converter, false);

return [
new Datastore(pouchdbDatastore, indexFacade, documentCache, converter, () => username),
Expand Down

0 comments on commit ae68891

Please sign in to comment.