giladw commented on a change in pull request #237:
URL: https://github.com/apache/unomi/pull/237#discussion_r575806041



##########
File path: 
services/src/main/java/org/apache/unomi/services/impl/segments/SegmentServiceImpl.java
##########
@@ -919,74 +943,93 @@ private void updateExistingProfilesForSegment(Segment 
segment) {
             profilesToRemoveSubConditions.add(notNewSegmentCondition);
             profilesToRemoveCondition.setParameter("subConditions", 
profilesToRemoveSubConditions);
 
-            PartialList<Profile> profilesToRemove = 
persistenceService.query(profilesToRemoveCondition, null, Profile.class, 0, 
segmentUpdateBatchSize, "10m");
-            PartialList<Profile> profilesToAdd = 
persistenceService.query(profilesToAddCondition, null, Profile.class, 0, 
segmentUpdateBatchSize, "10m");
-
-            while (profilesToAdd.getList().size() > 0) {
-                long profilesToAddStartTime = System.currentTimeMillis();
-                for (Profile profileToAdd : profilesToAdd.getList()) {
-                    profileToAdd.getSegments().add(segment.getItemId());
-                    Map<String,Object> sourceMap = new HashMap<>();
-                    sourceMap.put("segments", profileToAdd.getSegments());
-                    profileToAdd.setSystemProperty("lastUpdated", new Date());
-                    sourceMap.put("systemProperties", 
profileToAdd.getSystemProperties());
-                    persistenceService.update(profileToAdd, null, 
Profile.class, sourceMap);
-                    Event profileUpdated = new Event("profileUpdated", null, 
profileToAdd, null, null, profileToAdd, new Date());
-                    profileUpdated.setPersistent(false);
-                    eventService.send(profileUpdated);
-                    updatedProfileCount++;
-                }
-                logger.info("{} profiles added to segment in {}ms", 
profilesToAdd.size(), System.currentTimeMillis() - profilesToAddStartTime);
-                profilesToAdd = 
persistenceService.continueScrollQuery(Profile.class, 
profilesToAdd.getScrollIdentifier(), profilesToAdd.getScrollTimeValidity());
-                if (profilesToAdd == null || profilesToAdd.getList().size() == 
0) {
-                    break;
-                }
+            updatedProfileCount += 
updateProfilesSegment(profilesToAddCondition, segmentId, true);
+            updatedProfileCount += 
updateProfilesSegment(profilesToRemoveCondition, segmentId, false);
+        } else {
+            updatedProfileCount += updateProfilesSegment(segmentCondition, 
segmentId, false);
+        }
+        logger.info("{} profiles updated in {}ms", updatedProfileCount, 
System.currentTimeMillis() - updateProfilesForSegmentStartTime);
+    }
+
+    private long updateProfilesSegment(Condition profilesToUpdateCondition, 
String segmentId, boolean isAdd){
+        long updatedProfileCount= 0;
+        PartialList<Profile> profiles = 
persistenceService.query(profilesToUpdateCondition, null, Profile.class, 0, 
segmentUpdateBatchSize, "10m");
+
+        while (profiles != null && profiles.getList().size() > 0) {
+            long startTime = System.currentTimeMillis();
+            if (batchSegmentProfileUpdate) {
+                batchUpdateProfilesSegment(segmentId, profiles.getList(), 
isAdd);
             }
-            while (profilesToRemove.getList().size() > 0) {
-                long profilesToRemoveStartTime = System.currentTimeMillis();
-                for (Profile profileToRemove : profilesToRemove.getList()) {
-                    profileToRemove.getSegments().remove(segment.getItemId());
-                    Map<String,Object> sourceMap = new HashMap<>();
-                    sourceMap.put("segments", profileToRemove.getSegments());
-                    profileToRemove.setSystemProperty("lastUpdated", new 
Date());
-                    sourceMap.put("systemProperties", 
profileToRemove.getSystemProperties());
-                    persistenceService.update(profileToRemove, null, 
Profile.class, sourceMap);
-                    Event profileUpdated = new Event("profileUpdated", null, 
profileToRemove, null, null, profileToRemove, new Date());
-                    profileUpdated.setPersistent(false);
-                    eventService.send(profileUpdated);
-                    updatedProfileCount++;
-                }
-                logger.info("{} profiles removed from segment in {}ms", 
profilesToRemove.size(), System.currentTimeMillis() - profilesToRemoveStartTime 
);
-                profilesToRemove = 
persistenceService.continueScrollQuery(Profile.class, 
profilesToRemove.getScrollIdentifier(), 
profilesToRemove.getScrollTimeValidity());
-                if (profilesToRemove == null || 
profilesToRemove.getList().size() == 0) {
-                    break;
+            else { //send update profile one by one
+                for (Profile profileToUpdate : profiles.getList()) {
+                    Map<String, Object> sourceMap = 
buildPropertiesMapForUpdateSegment(profileToUpdate, segmentId, isAdd);
+                    persistenceService.update(profileToUpdate, null, 
Profile.class, sourceMap);
                 }
             }
+           if (sendProfileUpdateEventForSegmentUpdate)
+                sendProfileUpdatedEvent(profiles.getList());

Review comment:
       we dont have this check for regular update also (not batch).
   maybe it should be done separately?
   (currently there is also no handling of errors)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to