Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesChenX committed Jul 30, 2024
1 parent a6312a6 commit efffb84
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,12 @@ public static <K, V> Map<K, V> deepMerge(

// region intersection/union
public static <T> Set<T> intersection(Set<T> c1, Collection<T> c2) {
Set<T> result = newSetWithExpectedSize(Math.min(c1.size(), c2.size()));
int size1 = c1.size();
int size2 = c2.size();
if (size1 == 0 || size2 == 0) {
return Collections.emptySet();
}
Set<T> result = newSetWithExpectedSize(Math.min(size1, size2));
for (T value : c2) {
if (c1.contains(value)) {
result.add(value);
Expand All @@ -674,8 +679,15 @@ public static <T> Set<T> intersection(Set<T> c1, Collection<T> c2) {
return result;
}

public static <T> List<T> union(List<? extends T> list1, List<? extends T> list2) {
ArrayList<T> result = new ArrayList<>(list1.size() + list2.size());
public static <T> List<T> union(List<T> list1, List<T> list2) {
int size1 = list1.size();
int size2 = list2.size();
if (size1 == 0) {
return list2;
} else if (size2 == 0) {
return list1;
}
ArrayList<T> result = new ArrayList<>(size1 + size2);
result.addAll(list1);
result.addAll(list2);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ public Flux<Long> queryUserJoinedGroupIds(@NotNull Long userId) {
}

public Flux<Long> queryUsersJoinedGroupIds(
@Nullable Set<Long> groupIds,
@NotEmpty Set<Long> userIds,
@Nullable Integer page,
@Nullable Integer size) {
Expand All @@ -892,7 +893,7 @@ public Flux<Long> queryUsersJoinedGroupIds(
} catch (ResponseException e) {
return Flux.error(e);
}
return groupMemberRepository.findUsersJoinedGroupIds(userIds, page, size);
return groupMemberRepository.findUsersJoinedGroupIds(groupIds, userIds, page, size);
}

public Mono<Set<Long>> queryMemberIdsInUsersJoinedGroups(
Expand All @@ -904,7 +905,7 @@ public Mono<Set<Long>> queryMemberIdsInUsersJoinedGroups(
return Mono.error(e);
}
Recyclable<Set<Long>> recyclableSet = SetRecycler.obtain();
return queryUsersJoinedGroupIds(userIds, null, null)
return queryUsersJoinedGroupIds(null, userIds, null, null)
.collect(Collectors.toCollection(recyclableSet::getValue))
.flatMap(groupIds -> groupIds.isEmpty()
? PublisherPool.<Long>emptySet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,25 +389,35 @@ public Flux<Group> queryGroups(
@Nullable Set<Long> memberIds,
@Nullable Integer page,
@Nullable Integer size) {
return queryGroupIdsFromGroupIdsAndMemberIds(ids, memberIds)
.defaultIfEmpty(Collections.emptySet())
.flatMapMany(groupIds -> {
// fix return all groups when query member's groups
if (CollectionUtil.isNotEmpty(memberIds) && CollectionUtil.isEmpty(groupIds)) {
return Flux.empty();
}
return groupRepository.findGroups(groupIds,
typeIds,
creatorIds,
ownerIds,
isActive,
creationDateRange,
deletionDateRange,
lastUpdatedDateRange,
muteEndDateRange,
page,
size);
});
if (CollectionUtil.isEmpty(memberIds)) {
return groupRepository.findGroups(ids,
typeIds,
creatorIds,
ownerIds,
isActive,
creationDateRange,
deletionDateRange,
lastUpdatedDateRange,
muteEndDateRange,
page,
size);
}
return queryGroupIdsFromGroupIdsAndMemberIds(ids, memberIds).flatMapMany(groupIds -> {
if (groupIds.isEmpty()) {
return Flux.empty();
}
return groupRepository.findGroups(groupIds,
typeIds,
creatorIds,
ownerIds,
isActive,
creationDateRange,
deletionDateRange,
lastUpdatedDateRange,
muteEndDateRange,
page,
size);
});
}

/**
Expand Down Expand Up @@ -1139,17 +1149,31 @@ public Mono<Long> countGroups(
@Nullable DateRange lastUpdatedDateRange,
@Nullable DateRange muteEndDateRange,
@Nullable Set<Long> memberIds) {
return queryGroupIdsFromGroupIdsAndMemberIds(ids, memberIds)
.defaultIfEmpty(Collections.emptySet())
.flatMap(groupIds -> groupRepository.countGroups(groupIds,
typeIds,
creatorIds,
ownerIds,
isActive,
creationDateRange,
deletionDateRange,
lastUpdatedDateRange,
muteEndDateRange));
if (CollectionUtil.isEmpty(memberIds)) {
return groupRepository.countGroups(ids,
typeIds,
creatorIds,
ownerIds,
isActive,
creationDateRange,
deletionDateRange,
lastUpdatedDateRange,
muteEndDateRange);
}
return queryGroupIdsFromGroupIdsAndMemberIds(ids, memberIds).flatMap(groupIds -> {
if (groupIds.isEmpty()) {
return PublisherPool.LONG_ZERO;
}
return groupRepository.countGroups(groupIds,
typeIds,
creatorIds,
ownerIds,
isActive,
creationDateRange,
deletionDateRange,
lastUpdatedDateRange,
muteEndDateRange);
});
}

public Mono<Long> countDeletedGroups(@Nullable DateRange dateRange) {
Expand Down Expand Up @@ -1180,20 +1204,12 @@ public Mono<Boolean> isGroupActiveAndNotDeleted(@NotNull Long groupId) {

private Mono<Set<Long>> queryGroupIdsFromGroupIdsAndMemberIds(
@Nullable Set<Long> groupIds,
@Nullable Set<Long> memberIds) {
if (CollectionUtil.isEmpty(memberIds)) {
return CollectionUtil.isEmpty(groupIds)
? PublisherPool.emptySet()
: Mono.just(groupIds);
}
@NotEmpty Set<Long> memberIds) {
Recyclable<List<Long>> recyclableList = ListRecycler.obtain();
Mono<List<Long>> joinedGroupIdListMono =
groupMemberService.queryUsersJoinedGroupIds(memberIds, null, null)
groupMemberService.queryUsersJoinedGroupIds(groupIds, memberIds, null, null)
.collect(Collectors.toCollection(recyclableList::getValue));
return (groupIds == null
? joinedGroupIdListMono.map(CollectionUtil::newSet)
: joinedGroupIdListMono
.map(groupIdList -> CollectionUtil.newSet(groupIdList, groupIds)))
return joinedGroupIdListMono.map(CollectionUtil::newSet)
.doFinally(signalType -> recyclableList.recycle());
}

Expand Down

0 comments on commit efffb84

Please sign in to comment.