optimized find queries & refactorings

This commit is contained in:
undergroundwires
2020-01-06 17:45:53 +01:00
parent c646c10273
commit d38f6cd6a8

View File

@@ -4,45 +4,50 @@ import { IScript } from './IScript';
import { IApplication } from './IApplication';
export class Application implements IApplication {
private static mustHaveCategories(categories: ReadonlyArray<ICategory>) {
if (!categories || categories.length === 0) {
throw new Error('an application must consist of at least one category');
public get totalScripts(): number { return this.flattened.allScripts.length; }
public get totalCategories(): number { return this.flattened.allCategories.length; }
private readonly flattened: IFlattenedApplication;
constructor(
public readonly name: string,
public readonly version: number,
public readonly categories: ReadonlyArray<ICategory>) {
if (!name) {
throw Error('Application has no name');
}
if (!version) {
throw Error('Version cannot be zero');
}
this.flattened = flatten(categories);
if (this.flattened.allCategories.length === 0) {
throw new Error('An application must consist of at least one category');
}
ensureNoDuplicates(this.flattened.allCategories);
ensureNoDuplicates(this.flattened.allScripts);
}
public findCategory(categoryId: number): ICategory | undefined {
return this.flattened.allCategories.find((category) => category.id === categoryId);
}
public findScript(scriptId: string): IScript | undefined {
return this.flattened.allScripts.find((script) => script.id === scriptId);
}
public getAllScripts(): IScript[] {
return this.flattened.allScripts;
}
}
/**
* Checks all categories against duplicates, throws exception if it find any duplicates
* @return {number} Total unique categories
*/
/** Checks all categories against duplicates, throws exception if it find any duplicates returns total categories */
private static mustNotHaveDuplicatedCategories(categories: ReadonlyArray<ICategory>): number {
return Application.ensureNoDuplicateEntities(categories, Application.visitAllCategoriesOnce);
}
/**
* Checks all scripts against duplicates, throws exception if it find any scripts duplicates total scripts.
* @return {number} Total unique scripts
*/
private static mustNotHaveDuplicatedScripts(categories: ReadonlyArray<ICategory>): number {
return Application.ensureNoDuplicateEntities(categories, Application.visitAllScriptsOnce);
}
/**
* Checks entities against duplicates using a visit function, throws exception if it find any duplicates.
* @return {number} Result from the visit function
*/
private static ensureNoDuplicateEntities<TKey>(
categories: ReadonlyArray<ICategory>,
visitFunction: (categories: ReadonlyArray<ICategory>,
handler: (entity: IEntity<TKey>) => any) => number): number {
function ensureNoDuplicates<TKey>(entities: ReadonlyArray<IEntity<TKey>>) {
const totalOccurencesById = new Map<TKey, number>();
const totalVisited = visitFunction(categories,
(entity) =>
totalOccurencesById.set(entity.id,
(totalOccurencesById.get(entity.id) || 0) + 1));
for (const entity of entities) {
totalOccurencesById.set(entity.id, (totalOccurencesById.get(entity.id) || 0) + 1);
}
const duplicatedIds = new Array<TKey>();
totalOccurencesById.forEach((count, id) => {
if (count > 1) {
totalOccurencesById.forEach((index, id) => {
if (index > 1) {
duplicatedIds.push(id);
}
});
@@ -51,83 +56,37 @@ export class Application implements IApplication {
throw new Error(
`Duplicate entities are detected with following id(s): ${duplicatedIdsText}`);
}
return totalVisited;
}
// Runs handler on each category and returns sum of total visited categories
private static visitAllCategoriesOnce(
interface IFlattenedApplication {
allCategories: ICategory[];
allScripts: IScript[];
}
function flattenRecursive(
categories: ReadonlyArray<ICategory>,
handler: (category: ICategory) => any): number {
let total = 0;
flattened: IFlattenedApplication) {
for (const category of categories) {
handler(category);
total++;
if (category.subCategories && category.subCategories.length > 0) {
total += Application.visitAllCategoriesOnce(
category.subCategories as ReadonlyArray<ICategory>,
handler);
}
}
return total;
}
// Runs handler on each script and returns sum of total visited scripts
private static visitAllScriptsOnce(
categories: ReadonlyArray<ICategory>,
handler: (script: IScript) => any): number {
let total = 0;
Application.visitAllCategoriesOnce(categories,
(category) => {
flattened.allCategories.push(category);
if (category.scripts) {
for (const script of category.scripts) {
handler(script);
total++;
flattened.allScripts.push(script);
}
}
if (category.subCategories && category.subCategories.length > 0) {
flattenRecursive(
category.subCategories as ReadonlyArray<ICategory>,
flattened);
}
}
});
return total;
}
public readonly totalScripts: number;
public readonly totalCategories: number;
constructor(
public readonly name: string,
public readonly version: number,
public readonly categories: ReadonlyArray<ICategory>) {
Application.mustHaveCategories(categories);
this.totalCategories = Application.mustNotHaveDuplicatedCategories(categories);
this.totalScripts = Application.mustNotHaveDuplicatedScripts(categories);
}
public findCategory(categoryId: number): ICategory | undefined {
let result: ICategory | undefined;
Application.visitAllCategoriesOnce(this.categories,
(category) => {
if (category.id === categoryId) {
result = category;
}
});
return result;
}
public findScript(scriptId: string): IScript | undefined {
let result: IScript | undefined;
Application.visitAllScriptsOnce(this.categories,
(script) => {
if (script.id === scriptId) {
result = script;
}
});
return result;
}
public getAllScripts(): IScript[] {
const result = new Array<IScript>();
Application.visitAllScriptsOnce(this.categories,
(script) => {
result.push(script);
});
return result;
}
function flatten(
categories: ReadonlyArray<ICategory>): IFlattenedApplication {
const flattened: IFlattenedApplication = {
allCategories: new Array<ICategory>(),
allScripts: new Array<IScript>(),
};
flattenRecursive(categories, flattened);
return flattened;
}