Skip to content

Commit

Permalink
Fix ConcurrentModificationException
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikJannsen authored and alejandrogarcia83 committed Feb 5, 2025
1 parent d1463ff commit 45a0411
Showing 1 changed file with 3 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static long getAgeInDays(long date) {
@Getter
protected final Observable<Pair<String, Long>> userProfileIdScorePair = new Observable<>();
private final Map<ByteArray, Set<T>> pendingDataSetByHash = new ConcurrentHashMap<>();
private final Map<ByteArray, UserProfile> userProfileByUserProfileKey = new ConcurrentHashMap<>();
private Pin userProfileByIdPin;


Expand Down Expand Up @@ -186,6 +187,7 @@ protected static double getAgeBoostFactor(long eventTime) {

private void handleAddedUserProfile(UserProfile userProfile) {
ByteArray userProfileKey = getUserProfileKey(userProfile);
userProfileByUserProfileKey.putIfAbsent(userProfileKey, userProfile);
Set<T> dataSet = pendingDataSetByHash.get(userProfileKey);
if (dataSet != null) {
// Clone to avoid ConcurrentModificationException
Expand All @@ -206,11 +208,7 @@ private void handleAddedUserProfile(UserProfile userProfile) {

private void handleAddedAuthorizedDistributedData(T data) {
ByteArray providedHash = getDataKey(data);
// Clone to avoid ConcurrentModificationException
Set<UserProfile> clone = new HashSet<>(userProfileService.getUserProfileById().values());
clone.stream()
.filter(userProfile -> getUserProfileKey(userProfile).equals(providedHash))
.findAny() // We can only have one user profile for a given AuthorizedDistributedData
Optional.ofNullable(userProfileByUserProfileKey.get(providedHash))
.ifPresentOrElse(userProfile -> {
ByteArray hash = getUserProfileKey(userProfile);
if (!dataSetByHash.containsKey(hash)) {
Expand Down

0 comments on commit 45a0411

Please sign in to comment.