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



##########
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());
 
-        } else {
-            PartialList<Profile> profilesToRemove = 
persistenceService.query(segmentCondition, null, Profile.class, 0, 200, "10m");
-            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;
-                }
-            }
+            updatedProfileCount += profiles.size();

Review comment:
       since the retry is synchron we could also get the failed ids from the 
retry, and send the events for only successful updated events, and udpate the 
count accordingly.
   In term of implementation, event it was the case before it's sounds not 
correct to considered a failed update profile as counted in the number of 
profiled updated, and also send the updated event for a profile that was not 
updated.
   But again, since can be done in a separate unomi PR and ticket.

##########
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 have the List<String> failedItemsIds as the return from 
persistenceService.update.
   So we could filter and only send the event for the not failed update and 
update the count accordingly.
   But as it was already the case before, it can also be part of a separate 
improvement.

##########
File path: 
persistence-spi/src/main/java/org/apache/unomi/persistence/spi/PersistenceService.java
##########
@@ -175,6 +176,17 @@
      */
     boolean update(Item item, Date dateHint, Class<?> clazz, Map<?, ?> source, 
final boolean alwaysOverwrite);
 
+    /**
+     * Updates Map of items of the specified class and identified by the 
specified identifier with a new property value for the specified property name. 
Same as
+     * {@code update(itemId, dateHint, clazz, 
Collections.singletonMap(propertyName, propertyValue))}
+     *
+     * @param items         A map the consist of item (key) and properties to 
update (value)
+     * @param dateHint      a Date helping in identifying where the item is 
located
+     * @param clazz         the Item subclass of the item to update
+     * @return List of failed Items Ids, if all succesful then returns an 
empty list. if the whole operation failed then will return null
+     */
+    List<String> update(Map<Item, Map> items, Date dateHint, Class clazz);

Review comment:
       Yes you are right even the regular single update is doing that, but I 
think it would be great to do the same check.
   
   before processing the item we could check for:
   `item.getClass().isAssignableFrom(clazz.class)`
   if not
   `throw new IllegalArgumentException();`
   
   But it could be done and check in a separate PR/unomi ticket, as it is not 
the current PR topic.




----------------------------------------------------------------
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