Repository navigation
Unify and speed opening of DirectoryGroup up
#9821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
75dd243
63ad78e
b19a9ff
8eb9c48
9cc806e
1191e97
58c4e23
7b8c414
8834684
6ddf648
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.netbeans.modules.project.ui; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.Map; | ||
| import java.util.Queue; | ||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import org.netbeans.api.progress.ProgressHandle; | ||
| import org.netbeans.api.project.Project; | ||
| import org.netbeans.api.project.ProjectUtils; | ||
| import static org.netbeans.modules.project.ui.Bundle.OpenProjectList_deleted_project; | ||
| import static org.netbeans.modules.project.ui.Bundle.OpenProjectList_finding_subprojects; | ||
| import org.netbeans.spi.project.ProjectContainerProvider; | ||
| import org.netbeans.spi.project.SubprojectProvider; | ||
| import org.openide.awt.StatusDisplayer; | ||
| import org.openide.filesystems.FileUtil; | ||
|
|
||
| /** | ||
| * Helper class to handle computation of subprojects for projects. Instantiate | ||
| * once and then query via {@link #getSubprojects} as many times as necessary. | ||
| * The computed results are cached. | ||
| */ | ||
| public final class SubprojectsResolver { | ||
| private final Map<Project, Set<? extends Project>> subprojectsCache = new HashMap<>(); // #59098 | ||
|
|
||
| final Set<? extends Project> getSubprojects(Project p, ProgressHandle handle, boolean[] recurseOut) { | ||
| boolean recurse = true; | ||
| Set<? extends Project> subprojects = subprojectsCache.get(p); | ||
| if (subprojects == null) { | ||
| ProjectContainerProvider pcp = p.getLookup().lookup(ProjectContainerProvider.class); | ||
| if (pcp != null) { | ||
| if (handle != null) { | ||
| handle.progress(OpenProjectList_finding_subprojects(ProjectUtils.getInformation(p).getDisplayName())); | ||
| } | ||
| ProjectContainerProvider.Result res = pcp.getContainedProjects(); | ||
| subprojects = res.getProjects(); | ||
| if (res.isRecursive()) { | ||
| recurse = false; | ||
| } | ||
| } else { | ||
| SubprojectProvider spp = p.getLookup().lookup(SubprojectProvider.class); | ||
| if (spp != null) { | ||
| if (handle != null) { | ||
| handle.progress(OpenProjectList_finding_subprojects(ProjectUtils.getInformation(p).getDisplayName())); | ||
| } | ||
| subprojects = spp.getSubprojects(); | ||
| } else { | ||
| subprojects = Collections.emptySet(); | ||
| } | ||
| } | ||
| subprojectsCache.put(p, subprojects); | ||
| } | ||
| recurseOut[0] = recurse; | ||
| return subprojects; | ||
| } | ||
|
|
||
| /** Processes given set of projects. When {@code includeSubprojects} is | ||
| * {@code true}, then it computes also transitive closure of all projects. | ||
| * | ||
| * @param core the core set of projects to process | ||
| * @param canceled either {@code null} or an instance of boolean holder to cancel | ||
| * the computation | ||
| * @param includeSubprojects should subprojects be counted | ||
| * @param handle {@code null} or handle to report progress to | ||
| * @param maxWork maximum work to report - only needed when {@code handle} is specified | ||
| * @param currentWork the current work in the {@code handle} | ||
| * @return the projects to process | ||
| */ | ||
| public final Set<Project> getSubprojectsClosure( | ||
| Collection<Project> core, | ||
| AtomicBoolean canceled, | ||
| boolean includeSubprojects, | ||
| ProgressHandle handle, | ||
| double maxWork, | ||
| double currentWork | ||
| ) { | ||
| Queue<Project> toHandle = new ArrayDeque<>(core); | ||
| Set<Project> projectsToOpen = new LinkedHashSet<>(); | ||
| while (!toHandle.isEmpty()) { | ||
| if (canceled != null && canceled.get()) { | ||
| break; | ||
| } | ||
|
|
||
| Project p = toHandle.remove(); | ||
| assert p != null; | ||
| if (!p.getProjectDirectory().isValid()) { | ||
| StatusDisplayer.getDefault().setStatusText(OpenProjectList_deleted_project(FileUtil.getFileDisplayName(p.getProjectDirectory()))); | ||
| continue; | ||
| } | ||
| boolean[] recurse = new boolean[1]; | ||
| Set<? extends Project> subprojects = includeSubprojects ? | ||
| getSubprojects(p, handle, recurse) | ||
| : | ||
| Collections.<Project>emptySet(); | ||
| projectsToOpen.add(p); | ||
|
|
||
| for (Project sub : subprojects) { | ||
| assert sub != null; | ||
| if (sub != null && /** #224592 we need to test for null sub as some subprojectProvider implementations could be faulty and return null and with final releases assert won't fire */ | ||
| !projectsToOpen.contains(sub) && !toHandle.contains(sub)) { | ||
| if (recurse[0]) { | ||
| toHandle.add(sub); | ||
| } else { | ||
| projectsToOpen.add(sub); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| double workPerOneProject = (maxWork - currentWork) / (toHandle.size() + 1); | ||
| int lastState = (int) currentWork; | ||
|
|
||
| currentWork += workPerOneProject; | ||
|
|
||
| if (handle != null && lastState < (int) currentWork) { | ||
| handle.progress((int) currentWork); | ||
| } | ||
| } | ||
| return projectsToOpen; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,7 +67,7 @@ public static DirectoryGroup create(String name, FileObject dir) { | |
|
|
||
| @Override | ||
| protected void findProjects(Set<Project> projects, ProgressHandle h, int start, int end) { | ||
| assert !SwingUtilities.isEventDispatchThread(); | ||
| assert !SwingUtilities.isEventDispatchThread(); | ||
| String dir = prefs().get(KEY_PATH, null); | ||
| FileObject fo = null; | ||
| try { | ||
|
|
@@ -111,6 +111,11 @@ public GroupEditPanel createPropertiesPanel() { | |
| return new DirectoryGroupEditPanel(this); | ||
| } | ||
|
|
||
| @Override | ||
| boolean isSubprojects() { | ||
| return true; | ||
| } | ||
|
|
||
| private void processFolderChildren(FileObject fo, Set<Project> projects, ProgressHandle h, Counter counter, boolean checkShare) throws IllegalArgumentException { | ||
| Enumeration<? extends FileObject> e = fo.getFolders(false); | ||
| while (e.hasMoreElements()) { | ||
|
|
@@ -129,10 +134,11 @@ private void processFolderChildren(FileObject fo, Set<Project> projects, Progres | |
| if (h != null) { | ||
| h.progress(progressMessage(p), Math.min(++counter.start, counter.end)); | ||
| } | ||
| continue; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As soon as a project is found, skip its further directory processing... |
||
| } | ||
| checkShare = share != SharabilityQuery.Sharability.SHARABLE; | ||
| // don't need to check the sharability if the current folder is marked as recursively sharable | ||
|
|
||
| processFolderChildren(em, projects, h, counter, checkShare); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } catch (IOException x) { | ||
| Exceptions.printStackTrace(x); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,7 @@ | |
| import org.netbeans.modules.project.ui.OpenProjectListSettings; | ||
| import org.netbeans.modules.project.ui.ProjectTab; | ||
| import org.netbeans.modules.project.ui.ProjectUtilities; | ||
| import org.netbeans.modules.project.ui.SubprojectsResolver; | ||
| import static org.netbeans.modules.project.ui.groups.Bundle.*; | ||
| import org.openide.cookies.CloseCookie; | ||
| import org.openide.filesystems.FileObject; | ||
|
|
@@ -471,10 +472,17 @@ protected static Project projectForPath(String path) { | |
| } | ||
|
|
||
| /** | ||
| * The projects (currently) contained in the group. | ||
| * The projects (currently) contained in the group. This includes all specified | ||
| * projects as well as <em>required projects</em> when {@link #isSubprojects()} | ||
| * of the group is on. | ||
| */ | ||
| public Set<Project> getProjects() { | ||
| return getProjects(null, 0, 0); | ||
| public final Set<Project> getProjects() { | ||
| Set<Project> core = getProjects(null, 0, 0); | ||
| if (isSubprojects()) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eppleton, this addresses your concern with respect to Main Project action, e.g. the following shall be fine:
|
||
| var resolver = new SubprojectsResolver(); | ||
| core = resolver.getSubprojectsClosure(core, null, true, null, 0, 0); | ||
| } | ||
| return core; | ||
| } | ||
|
|
||
| private Set<Project> getProjects(ProgressHandle h, int start, int end) { | ||
|
|
@@ -578,7 +586,10 @@ static void open(final Group g, String oldGroupName, boolean isNewGroup, Prefere | |
| oldOpen.add(real != null ? real : open); | ||
| } | ||
|
|
||
| long then = System.currentTimeMillis(); | ||
| Set<Project> newOpen = g != null ? g.getProjects(h, 10, 100) : Collections.emptySet(); | ||
| long now = System.currentTimeMillis(); | ||
| LOG.log(Level.FINE, "Found {0} projects of group {1} in {2} ms", new Object[] { newOpen.size(), g, now - then }); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| final Set<Project> toClose = new HashSet<Project>(oldOpen); | ||
| toClose.removeAll(newOpen); | ||
| final Set<Project> toOpen = new HashSet<Project>(newOpen); | ||
|
|
@@ -591,12 +602,14 @@ static void open(final Group g, String oldGroupName, boolean isNewGroup, Prefere | |
| IndexingBridge.Lock lock = IndexingBridge.getDefault().protectedMode(); | ||
| try { | ||
| h.progress(Group_progress_closing(toClose.size()), 110); | ||
| Project[] arrClose = toClose.toArray(new Project[0]); | ||
| //close and remember the last opened files in the old group | ||
| opl.close(toClose.toArray(new Project[0]), false, oldGroupName); | ||
| opl.close(arrClose, false, oldGroupName); | ||
| h.switchToIndeterminate(); | ||
| h.progress(Group_progress_opening(toOpen.size())); | ||
| //open the projects with current group | ||
| opl.open(toOpen.toArray(new Project[0]), false, false, h, null); | ||
| Project[] arrOpen = toOpen.toArray(new Project[0]); | ||
| opl.open(arrOpen, false, g != null && g.isSubprojects(), h, null); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, this one is tough!
I have to think about it. |
||
|
|
||
| if(!isNewGroup) { | ||
| //for old and new group project intersection, save the old files list, | ||
|
|
@@ -753,4 +766,6 @@ private void notifyListeners(Object object, String property, String oldValue, St | |
| listener.propertyChange(new PropertyChangeEvent(object, property, oldValue, newValue)); | ||
| } | ||
| } | ||
|
|
||
| abstract boolean isSubprojects(); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re. @neilcsmith-net's comment: