Commit fe36f91890dd6a5ba42057e8bc3d8f4d1c205238
1 parent
887188fe
Alias: Batch support for fetching entities from relations
Showing
1 changed file
with
27 additions
and
3 deletions
... | ... | @@ -1007,16 +1007,40 @@ export class EntityService { |
1007 | 1007 | private entityRelationInfosToEntitiesInfo(entityRelations: Array<EntityRelationInfo>, |
1008 | 1008 | direction: EntitySearchDirection): Observable<Array<EntityInfo>> { |
1009 | 1009 | if (entityRelations.length) { |
1010 | - const tasks: Observable<EntityInfo>[] = []; | |
1010 | + const packs: Observable<EntityInfo>[][] = []; | |
1011 | + let packTasks: Observable<EntityInfo>[] = []; | |
1011 | 1012 | entityRelations.forEach((entityRelation) => { |
1012 | - tasks.push(this.entityRelationInfoToEntityInfo(entityRelation, direction)); | |
1013 | + packTasks.push(this.entityRelationInfoToEntityInfo(entityRelation, direction)); | |
1014 | + if (packTasks.length === 100) { | |
1015 | + packs.push(packTasks); | |
1016 | + packTasks = []; | |
1017 | + } | |
1013 | 1018 | }); |
1014 | - return forkJoin(tasks); | |
1019 | + if (packTasks.length) { | |
1020 | + packs.push(packTasks); | |
1021 | + } | |
1022 | + return this.executePack(packs, 0); | |
1015 | 1023 | } else { |
1016 | 1024 | return of([]); |
1017 | 1025 | } |
1018 | 1026 | } |
1019 | 1027 | |
1028 | + private executePack(packs: Observable<EntityInfo>[][], index: number): Observable<Array<EntityInfo>> { | |
1029 | + return forkJoin(packs[index]).pipe( | |
1030 | + expand(() => { | |
1031 | + index++; | |
1032 | + if (packs[index]) { | |
1033 | + return forkJoin(packs[index]); | |
1034 | + } else { | |
1035 | + return EMPTY; | |
1036 | + } | |
1037 | + } | |
1038 | + ), | |
1039 | + concatMap((data) => data), | |
1040 | + toArray() | |
1041 | + ); | |
1042 | + } | |
1043 | + | |
1020 | 1044 | private entityRelationInfoToEntityInfo(entityRelationInfo: EntityRelationInfo, direction: EntitySearchDirection): Observable<EntityInfo> { |
1021 | 1045 | const entityId = direction === EntitySearchDirection.FROM ? entityRelationInfo.to : entityRelationInfo.from; |
1022 | 1046 | return this.getEntity(entityId.entityType as EntityType, entityId.id, {ignoreLoading: true, ignoreErrors: true}).pipe( | ... | ... |