Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
feat: further improves performance of translating ids to codenames by…
Browse files Browse the repository at this point in the history
… storing previously found codenames
  • Loading branch information
Enngage committed Jan 22, 2020
1 parent b84d393 commit 1c1d49c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
39 changes: 32 additions & 7 deletions src/core/codename-translate-helper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { IIdCodenameTranslationResult } from './core.models';

export class CodenameTranslateHelper {
public replaceIdReferencesWithCodenames(data: any, allData: any): void {
public replaceIdReferencesWithCodenames(
data: any,
allData: any,
storedCodenames: IIdCodenameTranslationResult
): void {
if (data) {
if (Array.isArray(data)) {
for (const arrayItem of data) {
this.replaceIdReferencesWithCodenames(arrayItem, allData);
this.replaceIdReferencesWithCodenames(arrayItem, allData, storedCodenames);
}
} else {
for (const key of Object.keys(data)) {
Expand All @@ -14,7 +20,7 @@ export class CodenameTranslateHelper {

if (!codename) {
// replace id with codename
const foundCodename = this.tryFindCodenameForId(id, allData);
const foundCodename = this.tryFindCodenameForId(id, allData, storedCodenames);

if (foundCodename) {
// remove id prop
Expand All @@ -27,7 +33,7 @@ export class CodenameTranslateHelper {
}

if (typeof val === 'object' && val !== null) {
this.replaceIdReferencesWithCodenames(val, allData);
this.replaceIdReferencesWithCodenames(val, allData, storedCodenames);
}
}
}
Expand Down Expand Up @@ -61,11 +67,24 @@ export class CodenameTranslateHelper {
}
}

public tryFindCodenameForId(findId: string, data: any, foundCodename?: string): string | undefined {
public tryFindCodenameForId(
findId: string,
data: any,
storedCodenames: IIdCodenameTranslationResult,
foundCodename?: string
): string | undefined {

// try looking up codename in stored references
const storedCodename = storedCodenames[findId];

if (storedCodename) {
return storedCodename;
}

if (data) {
if (Array.isArray(data)) {
for (const arrayItem of data) {
foundCodename = this.tryFindCodenameForId(findId, arrayItem, foundCodename);
foundCodename = this.tryFindCodenameForId(findId, arrayItem, storedCodenames, foundCodename);
}
} else {
for (const key of Object.keys(data)) {
Expand All @@ -74,12 +93,18 @@ export class CodenameTranslateHelper {
const id = (data as any).id;
const codename = (data as any).codename;

if (id && codename) {
// store id -> codename mapping so that we don't have to always
// search for it as its expensive operation
storedCodenames[id] = codename;
}

if (codename && id === findId) {
return codename;
}
}
if (typeof val === 'object' && val !== null) {
foundCodename = this.tryFindCodenameForId(findId, val, foundCodename);
foundCodename = this.tryFindCodenameForId(findId, val, storedCodenames, foundCodename);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/core.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ export interface IImportItemResult<TRaw, TModel> {
originalId?: string;
importId?: string;
}

export interface IIdCodenameTranslationResult {
[key: string]: string;
}
6 changes: 4 additions & 2 deletions src/import/import.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export class ImportHelper {
// translate internal ids to codenames
codenameTranslateHelper.replaceIdReferencesWithCodenames(
sourceData.importData.contentTypes,
sourceData.importData
sourceData.importData,
{}
);
codenameTranslateHelper.replaceIdReferencesWithCodenames(
sourceData.importData.contentTypeSnippets,
sourceData.importData
sourceData.importData,
{}
);

// flatten data
Expand Down
17 changes: 11 additions & 6 deletions src/import/import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class ImportService {
importedItems.push(...importedLanguageVariants);

if (this.config.enableLog) {
console.log(`Finished importig data`);
console.log(`Finished importing data`);
}

return importedItems;
Expand All @@ -121,23 +121,28 @@ export class ImportService {
private translateIds(source: IImportSource): void {
codenameTranslateHelper.replaceIdReferencesWithCodenames(
source.importData.contentTypes,
source.importData
source.importData,
{}
);
codenameTranslateHelper.replaceIdReferencesWithCodenames(
source.importData.contentTypeSnippets,
source.importData
source.importData,
{}
);
codenameTranslateHelper.replaceIdReferencesWithCodenames(
source.importData.assets,
source.importData
source.importData,
{}
);
codenameTranslateHelper.replaceIdReferencesWithCodenames(
source.importData.contentItems,
source.importData
source.importData,
{}
);
codenameTranslateHelper.replaceIdReferencesWithCodenames(
source.importData.languageVariants,
source.importData
source.importData,
{}
);
}

Expand Down

0 comments on commit 1c1d49c

Please sign in to comment.