Skip to content

Commit 99b5892

Browse files
authored
Change sequential requests to parallel. (microsoft#909)
Parallel get label.json files. Atomic update tagCount.
1 parent e445dcc commit 99b5892

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/electron/providers/storage/localFileSystem.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export default class LocalFileSystem implements IStorageProvider {
171171
folderPath = [folderPath, folderName].join("/");
172172
const result: IAsset[] = [];
173173
const files = await this.listFiles(path.normalize(folderPath));
174-
for (const file of files) {
174+
await Promise.all(files.map(async (file) => {
175175
const fileParts = file.split(/[\\\/]/);
176176
const fileName = fileParts[fileParts.length - 1];
177177
const asset = await AssetService.createAssetFromFilePath(file, folderName + "/" + fileName, true);
@@ -191,11 +191,9 @@ export default class LocalFileSystem implements IStorageProvider {
191191
} else {
192192
asset.state = AssetState.NotVisited;
193193
}
194-
195194
result.push(asset);
196195
}
197-
}
198-
196+
}));
199197
return result;
200198
}
201199

src/providers/storage/azureBlobStorage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export class AzureBlobStorage implements IStorageProvider {
205205
public async getAssets(folderPath?: string, folderName?: string): Promise<IAsset[]> {
206206
const files: string[] = await this.listFiles(folderPath);
207207
const result: IAsset[] = [];
208-
for (const file of files) {
208+
await Promise.all(files.map(async (file) => {
209209
const url = this.getUrl(file);
210210
const asset = await AssetService.createAssetFromFilePath(url, this.getFileName(url));
211211
if (this.isSupportedAssetType(asset.type)) {
@@ -228,7 +228,7 @@ export class AzureBlobStorage implements IStorageProvider {
228228
}
229229
result.push(asset);
230230
}
231-
}
231+
}));
232232
return result;
233233
}
234234

src/services/projectService.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,15 @@ export default class ProjectService implements IProjectService {
259259
try {
260260
const blobs = new Set<string>(await storageProvider.listFiles(project.folderPath));
261261
const assetLabel = asset ? asset + constants.labelFileExtension : undefined;
262-
for (const blob of blobs) {
262+
await Promise.all(Array.from(blobs).map(async (blob) => {
263263
const blobFolderPath = blob.substr(0, blob.lastIndexOf("/"));
264264
if (blobFolderPath === project.folderPath
265265
&& blob.endsWith(constants.labelFileExtension)
266266
&& blobs.has(blob.substr(0, blob.length - constants.labelFileExtension.length))) {
267267
try {
268268
if (!assetLabel || assetLabel === blob) {
269269
const content = JSON.parse(await storageProvider.readText(blob)) as ILabelData;
270+
const localTagDocumentCount = {};
270271
content.labels.forEach((label) => {
271272
if (constants.supportedLabelsSchemas.has(content?.$schema) && label.label.split("/").length > 1) {
272273
return;
@@ -277,22 +278,32 @@ export default class ProjectService implements IProjectService {
277278
} else {
278279
labelName = label.label
279280
}
280-
tagNameSet.add(labelName);
281-
if (tagDocumentCount[labelName]) {
282-
tagDocumentCount[labelName] += 1;
281+
if (localTagDocumentCount[labelName]) {
282+
localTagDocumentCount[labelName] += 1;
283283
} else {
284-
tagDocumentCount[labelName] = 1;
284+
localTagDocumentCount[labelName] = 1;
285285
}
286286
});
287-
}
288-
if (assetLabel && assetLabel === blob) {
289-
break;
287+
return localTagDocumentCount;
290288
}
291289
} catch (err) {
292290
// ignore err
293291
}
294292
}
295-
}
293+
})).then(localTagDocumentCounts => {
294+
for (const localTagDocumentCount of localTagDocumentCounts) {
295+
if (_.isPlainObject(localTagDocumentCount)) {
296+
for (const [labelName, labelCount] of Object.entries(localTagDocumentCount)) {
297+
tagNameSet.add(labelName);
298+
if (tagDocumentCount[labelName]) {
299+
tagDocumentCount[labelName] += labelCount;
300+
} else {
301+
tagDocumentCount[labelName] = labelCount;
302+
}
303+
}
304+
}
305+
}
306+
});
296307
const tagNameArray = Array.from(tagNameSet);
297308
if (tagNameArray.containsDuplicates((name) => name)) {
298309
const reason = interpolate(

0 commit comments

Comments
 (0)