Skip to content

Commit dc9c877

Browse files
jywadhwaniJohn Plaisted
authored and
John Plaisted
committed
[scsi] preserve the order of urns
1 parent 5e70f36 commit dc9c877

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

metadata-dao-impl/ebean-dao/src/main/java/com/linkedin/metadata/dao/EbeanLocalDAO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ void addEntityTypeFilter(@Nonnull IndexFilter indexFilter) {
742742
/**
743743
* Returns list of urns from strongly consistent secondary index that satisfy the given filter conditions.
744744
*
745-
* <p>Results are sorted in increasing alphabetical order of urn.
745+
* <p>Results are ordered lexicographically by the string representation of the URN.
746746
*
747747
* <p>NOTE: Currently this works for upto 10 filter conditions.
748748
*

metadata-dao/src/main/java/com/linkedin/metadata/dao/BaseLocalDAO.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.ArrayList;
3030
import java.util.HashMap;
3131
import java.util.HashSet;
32+
import java.util.LinkedHashMap;
3233
import java.util.LinkedList;
3334
import java.util.List;
3435
import java.util.Map;
@@ -384,6 +385,8 @@ protected abstract <ASPECT extends RecordTemplate> void updateLocalIndex(@Nonnul
384385
/**
385386
* Returns list of urns from local secondary index that satisfy the given filter conditions.
386387
*
388+
* <p>Results are ordered lexicographically by the string representation of the URN.
389+
*
387390
* @param indexFilter {@link IndexFilter} containing filter conditions to be applied
388391
* @param lastUrn last urn of the previous fetched page. For the first page, this should be set as NULL
389392
* @param pageSize maximum number of distinct urns to return
@@ -404,13 +407,15 @@ public ListResult<URN> listUrns(@Nonnull Class<URN> urnClazz, @Nullable URN last
404407

405408
/**
406409
* Retrieves {@link ListResult} of {@link UrnAspectEntry} containing latest version of aspects along with the urn for the list of urns
407-
* returned from local secondary index that satisfy given filter conditions.
410+
* returned from local secondary index that satisfy given filter conditions. The returned list is ordered lexicographically by the string
411+
* representation of the URN.
408412
*
409413
* @param aspectClasses aspect classes whose latest versions need to be retrieved
410414
* @param indexFilter {@link IndexFilter} containing filter conditions to be applied
411415
* @param lastUrn last urn of the previous fetched page. For the first page, this should be set as NULL
412416
* @param pageSize maximum number of distinct urns whose aspects need to be retrieved
413-
* @return {@link ListResult} containing latest versions of aspects along with urns returned from local secondary index satisfying given filter conditions
417+
* @return {@link ListResult} containing ordered list of latest versions of aspects along with urns returned from local secondary index
418+
* satisfying given filter conditions
414419
*/
415420
@Nonnull
416421
public ListResult<UrnAspectEntry<URN>> getAspects(@Nonnull Set<Class<? extends RecordTemplate>> aspectClasses,
@@ -420,17 +425,17 @@ public ListResult<UrnAspectEntry<URN>> getAspects(@Nonnull Set<Class<? extends R
420425
final Map<URN, Map<Class<? extends RecordTemplate>, Optional<? extends RecordTemplate>>> urnAspectMap =
421426
get(aspectClasses, new HashSet<>(urns.getValues()));
422427

423-
final Map<URN, List<RecordTemplate>> urnListAspectMap = new HashMap<>();
424-
for (Map.Entry<URN, Map<Class<? extends RecordTemplate>, Optional<? extends RecordTemplate>>> entry : urnAspectMap.entrySet()) {
425-
final Map<Class<? extends RecordTemplate>, Optional<? extends RecordTemplate>> aspectMap = entry.getValue();
426-
urnListAspectMap.compute(entry.getKey(), (k, v) -> {
428+
final Map<URN, List<RecordTemplate>> urnListAspectMap = new LinkedHashMap<>();
429+
for (URN urn : urns.getValues()) {
430+
final Map<Class<? extends RecordTemplate>, Optional<? extends RecordTemplate>> aspectMap = urnAspectMap.get(urn);
431+
urnListAspectMap.compute(urn, (k, v) -> {
427432
if (v == null) {
428433
v = new ArrayList<>();
429434
}
430435
return v;
431436
});
432437
for (Optional<? extends RecordTemplate> aspect : aspectMap.values()) {
433-
aspect.ifPresent(record -> urnListAspectMap.get(entry.getKey()).add(record));
438+
aspect.ifPresent(record -> urnListAspectMap.get(urn).add(record));
434439
}
435440
}
436441

metadata-restli-resource/src/main/java/com/linkedin/metadata/restli/BaseEntityResource.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import java.util.Collection;
3737
import java.util.Collections;
3838
import java.util.Comparator;
39-
import java.util.HashMap;
39+
import java.util.LinkedHashMap;
4040
import java.util.List;
4141
import java.util.Map;
4242
import java.util.Set;
@@ -341,14 +341,15 @@ public Task<String[]> listUrnsFromIndex(@ActionParam(PARAM_FILTER) @Optional @Nu
341341
}
342342

343343
/**
344-
* Returns {@link CollectionResult} containing the values of multiple entities obtained after filtering urns from local secondary index.
345-
* {@link ListResultMetadata} contains relevant list of urns.
344+
* Returns {@link CollectionResult} containing ordered list of values of multiple entities obtained after filtering urns
345+
* from local secondary index. The returned list is ordered lexicographically by the string representation of the URN.
346+
* The list of values is in the same order as the list of urns contained in {@link ListResultMetadata}.
346347
*
347348
* @param aspectClasses set of aspect classes that needs to be populated in the values
348349
* @param filter {@link IndexFilter} that defines the filter conditions
349350
* @param lastUrn last urn of the previous fetched page. For the first page, this should be set as NULL
350351
* @param pagingContext {@link PagingContext} defining the paging parameters of the request
351-
* @return {@link CollectionResult} containing the values of multiple entities
352+
* @return {@link CollectionResult} containing ordered list of values of multiple entities
352353
*/
353354
@Nonnull
354355
private CollectionResult<VALUE, ListResultMetadata> filterAspects(
@@ -358,7 +359,7 @@ private CollectionResult<VALUE, ListResultMetadata> filterAspects(
358359
final ListResult<UrnAspectEntry<URN>> urnAspectEntries =
359360
getLocalDAO().getAspects(aspectClasses, filter, parseUrnParam(lastUrn), pagingContext.getCount());
360361

361-
final Map<URN, List<UnionTemplate>> urnAspectsMap = new HashMap<>();
362+
final Map<URN, List<UnionTemplate>> urnAspectsMap = new LinkedHashMap<>();
362363
for (UrnAspectEntry<URN> entry : urnAspectEntries.getValues()) {
363364
urnAspectsMap.compute(entry.getUrn(), (k, v) -> {
364365
if (v == null) {
@@ -383,27 +384,27 @@ private CollectionResult<VALUE, ListResultMetadata> filterAspects(
383384
}
384385

385386
/**
386-
* Returns {@link CollectionResult} containing the values of multiple entities obtained after filtering urns from local secondary index.
387-
* Here the value does not contain any metadata aspect, only parts of the urn (if applicable). {@link ListResultMetadata} contains relevant list of urns.
387+
* Returns {@link CollectionResult} containing ordered list of values of multiple entities obtained after filtering urns
388+
* from local secondary index. The returned list is ordered lexicographically by the string representation of the URN.
389+
* The values returned do not contain any metadata aspect, only parts of the urn (if applicable).
390+
* The list of values is in the same order as the list of urns contained in {@link ListResultMetadata}.
388391
*
389392
* @param filter {@link IndexFilter} that defines the filter conditions
390393
* @param lastUrn last urn of the previous fetched page
391394
* @param pagingContext {@link PagingContext} defining the paging parameters of the request
392-
* @return {@link CollectionResult} containing the values of multiple entities
395+
* @return {@link CollectionResult} containing ordered list of values of multiple entities
393396
*/
394397
@Nonnull
395398
private CollectionResult<VALUE, ListResultMetadata> filterUrns(@Nonnull IndexFilter filter, @Nullable String lastUrn,
396399
@Nonnull PagingContext pagingContext) {
397400

398401
final ListResult<URN> urns = getLocalDAO().listUrns(filter, parseUrnParam(lastUrn), pagingContext.getCount());
399-
final Map<URN, VALUE> urnValueMap = urns.getValues()
400-
.stream()
401-
.distinct()
402-
.collect(Collectors.toMap(Function.identity(), urn -> toValue(newSnapshot(urn))));
403-
ListResultMetadata resultMetadata = new ListResultMetadata().setExtraInfos(new ExtraInfoArray(
404-
urnValueMap.keySet().stream().map(urn -> new ExtraInfo().setUrn(urn)).collect(Collectors.toList())));
402+
final List<VALUE> values =
403+
urns.getValues().stream().map(urn -> toValue(newSnapshot(urn))).collect(Collectors.toList());
404+
final ListResultMetadata resultMetadata = new ListResultMetadata().setExtraInfos(new ExtraInfoArray(
405+
urns.getValues().stream().map(urn -> new ExtraInfo().setUrn(urn)).collect(Collectors.toList())));
405406

406-
return new CollectionResult<>(new ArrayList<>(urnValueMap.values()), urns.getTotalCount(), resultMetadata);
407+
return new CollectionResult<>(new ArrayList<>(values), urns.getTotalCount(), resultMetadata);
407408
}
408409

409410
/**

0 commit comments

Comments
 (0)