[526225] Fix a regression related to statistics indexing

The fix for bug 518356
(http://git.eclipse.org/c/viatra/org.eclipse.viatra.git/commit/?id=aee1e492e61a238edd804dba7becd53af5a6f664
) erased the statistics indexes before executing a full indexing.
However, when moving model elements outside of the model scope, this
might have resulted in clearing the statistics when it is not
recalculated.

This change (backported from master) fixes the issue by only clearing
the stats store if the old indexing level had statistics indexing but
did not have instance indexing.

Conflicts:
	query/plugins/org.eclipse.viatra.query.runtime.base/src/org/eclipse/viatra/query/runtime/base/core/NavigationHelperImpl.java

Change-Id: I2d7fe18ccbc4c0f43199a42f753b50edf96ff114
Signed-off-by: Zoltan Ujhelyi <ujhelyiz@incquerylabs.com>
diff --git a/query/plugins/org.eclipse.viatra.query.runtime.base/src/org/eclipse/viatra/query/runtime/base/core/NavigationHelperImpl.java b/query/plugins/org.eclipse.viatra.query.runtime.base/src/org/eclipse/viatra/query/runtime/base/core/NavigationHelperImpl.java
index 38dd002..f34dc52 100644
--- a/query/plugins/org.eclipse.viatra.query.runtime.base/src/org/eclipse/viatra/query/runtime/base/core/NavigationHelperImpl.java
+++ b/query/plugins/org.eclipse.viatra.query.runtime.base/src/org/eclipse/viatra/query/runtime/base/core/NavigationHelperImpl.java
@@ -1263,6 +1263,8 @@
                     delayTraversals = false;
                     callable = null;
 
+                    // This set collects all types whose indexing level increases from statistics to full
+                    Set<Object> moveFromStatsToFullRequests = new HashSet<>();
                     for(Entry<Object, IndexingLevel> entry: observedFeatures.entrySet()){
                         IndexingLevel requested = delayedFeatures.get(entry.getKey());
                         if (requested != null){
@@ -1270,9 +1272,12 @@
                             IndexingLevel merged = requested.merge(old);
                             if (merged == old){
                                 delayedFeatures.remove(entry.getKey());
-                            }else{
+                            } else {
                                 delayedFeatures.put(entry.getKey(), merged);
                             }
+                            if (merged.hasInstances() && old.hasStatistics() && !old.hasInstances()) {
+                                moveFromStatsToFullRequests.add(entry.getKey());
+                            }
                         }
                     }
                     for(Entry<Object, IndexingLevel> entry: directlyObservedClasses.entrySet()){
@@ -1285,6 +1290,9 @@
                             } else{
                                 delayedClasses.put(entry.getKey(), merged);
                             }
+                            if (merged.hasInstances() && old.hasStatistics() && !old.hasInstances()) {
+                                moveFromStatsToFullRequests.add(entry.getKey());
+                            }
                         }
                     }
                     for(Entry<Object, IndexingLevel> entry: observedDataTypes.entrySet()){
@@ -1297,9 +1305,14 @@
                             } else {
                                 delayedDataTypes.put(entry.getKey(), merged);
                             }
+                            if (merged.hasInstances() && old.hasStatistics() && !old.hasInstances()) {
+                                moveFromStatsToFullRequests.add(entry.getKey());
+                            }
                         }
                     }
 
+                    
+                    
                     boolean classesWarrantTraversal = !Maps
                             .difference(delayedClasses, getAllObservedClassesInternal()).areEqual();
 
@@ -1321,19 +1334,16 @@
                                 delayedDataTypes);
                         
                         // Instance indexing would add extra entries to the statistics store, so we have to clean the
-                        // appropriate entries. If no re-traversal is required, it is detected earlier; at this point we
-                        // only have to consider the target indexing level. See bug
+                        // appropriate entries. At this point we simply empty the stats store for elements where
+                        // indexing level is increased from statistics to full. See bug
                         // https://bugs.eclipse.org/bugs/show_bug.cgi?id=518356 for more details.
-                        
+
                         // Technically, the statsStore cleanup seems only necessary for EDataTypes; otherwise everything
                         // works as expected, but it seems a better idea to do the cleanup for all types in the same way
-                        for (Entry<Object, IndexingLevel> entry : Iterables.concat(toGatherClasses.entrySet(),
-                                toGatherFeatures.entrySet(), toGatherDataTypes.entrySet())) {
-                            if (entry.getValue().hasInstances()) {
-                                statsStore.removeType(entry.getKey());
-                            }
+                        for (Object type : moveFromStatsToFullRequests) {
+                            statsStore.removeType(type);
                         }
-
+                        
                         if (classesWarrantTraversal || !toGatherFeatures.isEmpty() || !toGatherDataTypes.isEmpty()) {
                             // repeat the cycle with this visit
                             final NavigationHelperVisitor visitor = new NavigationHelperVisitor.TraversingVisitor(this,