Skip to content

Commit 21eecdc

Browse files
committed
Avoid unnecessary persistence of activity enablements #1084
The ActivityPersistenceHelper persists all activity enablements upon every notification about a changed activity enablement it receives. Persistence is, however, only done and thus necessary for activities that are not expression-controlled. So the current implementation performs unnecessary persistence operations. With this change, the ActivityPersistenceHelper only persists the enablements when the enablement of a non-expression-controlled activity has changed. To this end, the received ActivityManagerEvent is extended by the information about whether such an activity was affected. This information is either directly passed to the event construction in case it has already been calculated in the calling context or is calculated lazily when accessing the value. Contributes to #1084.
1 parent f100822 commit 21eecdc

File tree

4 files changed

+134
-12
lines changed

4 files changed

+134
-12
lines changed

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/activities/ActivityManagerEvent.java

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
package org.eclipse.ui.activities;
1616

17+
import static java.util.Collections.emptySet;
18+
1719
import java.util.Set;
20+
import java.util.stream.Stream;
1821
import org.eclipse.ui.internal.util.Util;
1922

2023
/**
@@ -39,23 +42,31 @@ public final class ActivityManagerEvent {
3942

4043
private boolean enabledActivityIdsChanged;
4144

45+
/**
46+
* Indicates whether enabled IDs of non-expression-controlled activities have
47+
* changed. The value is calculated from changed enabled activity IDs given to
48+
* the constructor if known by the caller or is set to <code>null</null> and
49+
* will be calculated on demand upon first access to the value.
50+
*/
51+
private Boolean enabledNonExpressionControlledActivityIdsChanged = null;
52+
4253
/**
4354
* The set of activity identifiers (strings) that were defined before the change
44-
* occurred. If the defined activities did not changed, then this value is
55+
* occurred. If the defined activities did not change, then this value is
4556
* <code>null</code>.
4657
*/
4758
private final Set<String> previouslyDefinedActivityIds;
4859

4960
/**
5061
* The set of category identifiers (strings) that were defined before the change
51-
* occurred. If the defined category did not changed, then this value is
62+
* occurred. If the defined category did not change, then this value is
5263
* <code>null</code>.
5364
*/
5465
private final Set<String> previouslyDefinedCategoryIds;
5566

5667
/**
5768
* The set of activity identifiers (strings) that were enabled before the change
58-
* occurred. If the enabled activities did not changed, then this value is
69+
* occurred. If the enabled activities did not change, then this value is
5970
* <code>null</code>.
6071
*/
6172
private final Set<String> previouslyEnabledActivityIds;
@@ -145,6 +156,91 @@ public ActivityManagerEvent(IActivityManager activityManager, boolean definedAct
145156
this.enabledActivityIdsChanged = enabledActivityIdsChanged;
146157
}
147158

159+
/**
160+
* Creates a new instance of this class.
161+
*
162+
* @param activityManager the instance of the interface that
163+
* changed.
164+
* @param definedActivityIdsChanged <code>true</code>, iff the
165+
* definedActivityIds property changed.
166+
* @param definedCategoryIdsChanged <code>true</code>, iff the
167+
* definedCategoryIds property changed.
168+
* @param enabledActivityIdsChanged <code>true</code>, iff the
169+
* enabledActivityIds property changed.
170+
* @param previouslyDefinedActivityIds the set of identifiers to previously
171+
* defined activities. This set may be
172+
* empty. If this set is not empty, it must
173+
* only contain instances of
174+
* <code>String</code> . This set must be
175+
* <code>null</code> if
176+
* definedActivityIdsChanged is
177+
* <code>false</code> and must not be null
178+
* if definedActivityIdsChanged is
179+
* <code>true</code>.
180+
* @param previouslyDefinedCategoryIds the set of identifiers to previously
181+
* defined category. This set may be empty.
182+
* If this set is not empty, it must only
183+
* contain instances of <code>String</code>.
184+
* This set must be <code>null</code> if
185+
* definedCategoryIdsChanged is
186+
* <code>false</code> and must not be null
187+
* if definedCategoryIdsChanged is
188+
* <code>true</code>.
189+
* @param previouslyEnabledActivityIds the set of identifiers to previously
190+
* enabled activities. This set may be
191+
* empty. If this set is not empty, it must
192+
* only contain instances of
193+
* <code>String</code>. This set must be
194+
* <code>null</code> if
195+
* enabledActivityIdsChanged is
196+
* <code>false</code> and must not be null
197+
* if enabledActivityIdsChanged is
198+
* <code>true</code>.
199+
* @param changedEnabledActivityIds the set of identifiers to activities
200+
* whose enabled state has changed. This set
201+
* may be empty. If this set is not empty,
202+
* it must only contain instances of
203+
* <code>String</code>. This set must be
204+
* <code>null</code> if
205+
* enabledActivityIdsChanged is
206+
* <code>false</code> and must not be null
207+
* if enabledActivityIdsChanged is
208+
* <code>true</code>.
209+
* @since 3.131
210+
*/
211+
public ActivityManagerEvent(IActivityManager activityManager, boolean definedActivityIdsChanged,
212+
boolean definedCategoryIdsChanged, boolean enabledActivityIdsChanged,
213+
final Set<String> previouslyDefinedActivityIds, final Set<String> previouslyDefinedCategoryIds,
214+
final Set<String> previouslyEnabledActivityIds, Set<String> changedEnabledActivityIds) {
215+
this(activityManager, definedActivityIdsChanged, definedCategoryIdsChanged, enabledActivityIdsChanged,
216+
previouslyDefinedActivityIds, previouslyDefinedCategoryIds, previouslyEnabledActivityIds);
217+
if (enabledActivityIdsChanged) {
218+
this.enabledNonExpressionControlledActivityIdsChanged = changedEnabledActivityIds.stream()
219+
.anyMatch(this::isIdOfNonExpressionControlledActivity);
220+
} else {
221+
this.enabledNonExpressionControlledActivityIdsChanged = false;
222+
}
223+
224+
}
225+
226+
/**
227+
* Creates a copy of this {@link ActivityManagerEvent} containing the given
228+
* {@code newActivityManager}.
229+
*
230+
* @param newActivityManager the new activity manager to be referenced by the
231+
* copied event
232+
*
233+
* @return a copy of this event referencing the given activity manager
234+
* @since 3.131
235+
*/
236+
public ActivityManagerEvent copyFor(IActivityManager newActivityManager) {
237+
ActivityManagerEvent copy = new ActivityManagerEvent(newActivityManager, definedActivityIdsChanged,
238+
definedCategoryIdsChanged, enabledActivityIdsChanged, previouslyDefinedActivityIds,
239+
previouslyDefinedCategoryIds, previouslyEnabledActivityIds);
240+
copy.enabledNonExpressionControlledActivityIdsChanged = enabledNonExpressionControlledActivityIdsChanged;
241+
return copy;
242+
}
243+
148244
/**
149245
* Returns the instance of the interface that changed.
150246
*
@@ -214,4 +310,35 @@ public boolean haveDefinedCategoryIdsChanged() {
214310
public boolean haveEnabledActivityIdsChanged() {
215311
return enabledActivityIdsChanged;
216312
}
313+
314+
/**
315+
* Returns whether or not enabledActivityIds property changed and any of the
316+
* changed IDs belongs to a non-expression-controlled activity.
317+
*
318+
* @return <code>true>/code> iff the enabledActivityIds property changed and any
319+
* of the changed IDs belongs to a non-expression-controlled activity.
320+
* @since 3.131
321+
*/
322+
public boolean haveEnabledNonExpressionControlledActivityIdsChanged() {
323+
if (enabledNonExpressionControlledActivityIdsChanged == null) {
324+
enabledNonExpressionControlledActivityIdsChanged = calculateHaveEnabledNonExpressionControlledActivityIdsChanged();
325+
}
326+
return enabledNonExpressionControlledActivityIdsChanged;
327+
}
328+
329+
private boolean calculateHaveEnabledNonExpressionControlledActivityIdsChanged() {
330+
Set<String> previousIds = previouslyEnabledActivityIds == null ? emptySet() : previouslyEnabledActivityIds;
331+
Set<String> currentIds = activityManager.getEnabledActivityIds();
332+
Stream<String> addedIds = currentIds.stream().filter(id -> !previousIds.contains(id));
333+
if (addedIds.anyMatch(this::isIdOfNonExpressionControlledActivity)) {
334+
return true;
335+
}
336+
Stream<String> removedIds = previousIds.stream().filter(id -> !currentIds.contains(id));
337+
return removedIds.anyMatch(this::isIdOfNonExpressionControlledActivity);
338+
}
339+
340+
private boolean isIdOfNonExpressionControlledActivity(String id) {
341+
return activityManager.getActivity(id).getExpression() == null;
342+
}
343+
217344
}

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/ActivityPersistanceHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ final class ActivityPersistanceHelper {
5757
// state
5858
loadEnabledStates(activityManagerEvent.getActivityManager().getEnabledActivityIds(), delta);
5959
}
60-
if (activityManagerEvent.haveEnabledActivityIdsChanged()) {
60+
if (activityManagerEvent.haveEnabledNonExpressionControlledActivityIdsChanged()) {
6161
saveEnabledStates();
6262
}
6363
};

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/activities/MutableActivityManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ private void updateListeners(boolean activityManagerChanged, Map<String, Activit
527527

528528
if (activityManagerChanged) {
529529
fireActivityManagerChanged(
530-
new ActivityManagerEvent(this, false, false, true, null, null, previouslyEnabledActivityIds));
530+
new ActivityManagerEvent(this, false, false, true, null, null,
531+
previouslyEnabledActivityIds, deltaActivityIds));
531532
}
532533
}
533534

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/activities/ProxyActivityManager.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ public ProxyActivityManager(IActivityManager activityManager) {
3232
this.activityManager = activityManager;
3333

3434
this.activityManager.addActivityManagerListener(activityManagerEvent -> {
35-
ActivityManagerEvent proxyActivityManagerEvent = new ActivityManagerEvent(ProxyActivityManager.this,
36-
activityManagerEvent.haveDefinedActivityIdsChanged(),
37-
activityManagerEvent.haveDefinedCategoryIdsChanged(),
38-
activityManagerEvent.haveEnabledActivityIdsChanged(),
39-
activityManagerEvent.getPreviouslyDefinedActivityIds(),
40-
activityManagerEvent.getPreviouslyDefinedCategoryIds(),
41-
activityManagerEvent.getPreviouslyEnabledActivityIds());
35+
ActivityManagerEvent proxyActivityManagerEvent = activityManagerEvent.copyFor(ProxyActivityManager.this);
4236
fireActivityManagerChanged(proxyActivityManagerEvent);
4337
});
4438
}

0 commit comments

Comments
 (0)