Skip to content

Commit

Permalink
[JBIDE-21704] preload all image stream tags (was: loading in UI thread)
Browse files Browse the repository at this point in the history
  • Loading branch information
adietish committed Feb 18, 2016
1 parent cdb47a2 commit c3030e4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -78,20 +79,32 @@ public synchronized void refresh() {
}


@Override
public Map<IService, Collection<IResource>> getAllImageStreamTags() {
return deployments.stream()
.collect(Collectors.toMap(
deployment -> deployment.getService(),
deployment -> getImageStreamTagsFor(deployment)));
}

@Override
public Collection<IResource> getImageStreamTagsFor(IService service) {
Optional<Deployment> deployment = deployments.stream().filter(d->service.equals(d.getService())).findFirst();
if(deployment.isPresent()) {
Set<String> imageRefs = deployment.get()
return getImageStreamTagsFor(deployment.get());
}
return Collections.emptySet();
}

private Collection<IResource> getImageStreamTagsFor(Deployment deployment) {
Set<String> imageRefs = deployment
.getBuildConfigs()
.stream()
.map(bc->ResourceUtils.imageRef((IBuildConfig) bc.getResource())).collect(Collectors.toSet());
final String projectName = service.getNamespace();
final String projectName = deployment.getService().getNamespace();
return imageRefs.stream()
.map(ref->conn.<IResource>getResource(ResourceKind.IMAGE_STREAM_TAG, projectName, ref))
.collect(Collectors.toSet());
}
return Collections.emptySet();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.jboss.tools.openshift.internal.ui.models;

import java.util.Collection;
import java.util.Map;

import org.jboss.tools.common.databinding.IObservablePojo;
import org.jboss.tools.openshift.common.core.IRefreshable;
Expand All @@ -31,4 +32,6 @@ public interface IDeploymentResourceMapper extends IObservablePojo, IRefreshable
* @return The collection of imagestreamtags or an empty collection
*/
Collection<IResource> getImageStreamTagsFor(IService service);

Map<IService, Collection<IResource>> getAllImageStreamTags();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import org.eclipse.wst.server.core.IServerWorkingCopy;
import org.jboss.tools.openshift.core.connection.Connection;
import org.jboss.tools.openshift.internal.ui.models.IDeploymentResourceMapper;
import org.jboss.tools.openshift.internal.ui.treeitem.ObservableTreeItem;

import com.openshift.restclient.model.IService;
Expand All @@ -37,7 +36,7 @@ public OpenShiftServerEditorModel(IServerWorkingCopy server, Connection connecti

private void update(boolean overrideProject, Connection connection, List<Connection> connections,
org.eclipse.core.resources.IProject deployProject, List<org.eclipse.core.resources.IProject> projects,
String sourcePath, String podPath, Map<String, IDeploymentResourceMapper> deploymentMapperByProjectName,
String sourcePath, String podPath, Map<String, ProjectImageStreamTags> deploymentMapperByProjectName,
IService service, List<ObservableTreeItem> serviceItems) {
update(connection, connections, deployProject, projects, sourcePath, podPath, deploymentMapperByProjectName, service, serviceItems);
firePropertyChange(PROPERTY_OVERRIDE_PROJECT, this.overrideProject, this.overrideProject = overrideProject);
Expand All @@ -49,7 +48,7 @@ public boolean isOverrideProject() {

public void setOverrideProject(boolean overrideProject) {
update(overrideProject, getConnection(), getConnections(), getDeployProject(), getProjects(),
getSourcePath(), getPodPath(), getImageStreamTags(), getService(), getServiceItems());
getSourcePath(), getPodPath(), getImageStreamTagsMap(), getService(), getServiceItems());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.eclipse.wst.server.core.IServerWorkingCopy;
import org.jboss.tools.openshift.common.core.connection.ConnectionURL;
Expand Down Expand Up @@ -72,7 +73,7 @@ public class ServerSettingsViewModel extends ServiceViewModel {
protected String podPath;
protected String inferredPodPath = null;
private IServerWorkingCopy server;
private Map<String, IDeploymentResourceMapper> deploymentMapperByProjectName;
private Map<String, ProjectImageStreamTags> imageStreamTagsMap;

public ServerSettingsViewModel(IServerWorkingCopy server, Connection connection) {
super(connection);
Expand All @@ -81,9 +82,9 @@ public ServerSettingsViewModel(IServerWorkingCopy server, Connection connection)

protected void update(Connection connection, List<Connection> connections,
org.eclipse.core.resources.IProject deployProject, List<org.eclipse.core.resources.IProject> projects,
String sourcePath, String podPath, Map<String, IDeploymentResourceMapper> deploymentMapperByProjectName,
String sourcePath, String podPath, Map<String, ProjectImageStreamTags> imageStreamsMap,
IService service, List<ObservableTreeItem> serviceItems) {
boolean serviceOrDeploymentChanged = this.deploymentMapperByProjectName != deploymentMapperByProjectName;
boolean serviceOrDeploymentChanged = this.imageStreamTagsMap != imageStreamsMap;
IService oldService = getService();
update(connection, connections, service, serviceItems);
serviceOrDeploymentChanged |= (oldService != getService());
Expand All @@ -95,7 +96,7 @@ protected void update(Connection connection, List<Connection> connections,
deployProject = updateDeployProject(deployProject, projects);
updateSourcePath(sourcePath, deployProject);
if(serviceOrDeploymentChanged) {
updatePodPath(podPath, deploymentMapperByProjectName, service);
updatePodPath(podPath, imageStreamsMap, service);
} else {
updatePodPath(podPath);
}
Expand Down Expand Up @@ -131,9 +132,9 @@ protected void updateSourcePath(String sourcePath, org.eclipse.core.resources.IP
firePropertyChange(PROPERTY_SOURCE_PATH, this.sourcePath, this.sourcePath = sourcePath);
}

protected void updatePodPath(String newPodPath, Map<String, IDeploymentResourceMapper> deploymentResourceMapperByProjectName, IService service) {
this.deploymentMapperByProjectName = deploymentResourceMapperByProjectName;
String inferredPodPath = getInferredDeploymentDirectory(service, deploymentResourceMapperByProjectName, newPodPath);
protected void updatePodPath(String newPodPath, Map<String, ProjectImageStreamTags> imageStreamsMap, IService service) {
this.imageStreamTagsMap = imageStreamsMap;
String inferredPodPath = getInferredDeploymentDirectory(service, imageStreamsMap, newPodPath);
firePropertyChange(PROPERTY_POD_PATH_EDITABLE, this.inferredPodPath == null, (this.inferredPodPath = inferredPodPath) == null);
if(inferredPodPath != null) {
newPodPath = inferredPodPath;
Expand All @@ -149,13 +150,13 @@ private void updatePodPath(String newPodPath) {
firePropertyChange(PROPERTY_POD_PATH, this.podPath, this.podPath = newPodPath);
}

private String getInferredDeploymentDirectory(IService service, Map<String, IDeploymentResourceMapper> deploymentResourceMapperByProjectName, String selectedDeploymentDirectory) {
private String getInferredDeploymentDirectory(IService service, Map<String, ProjectImageStreamTags> imageStreamsMap, String selectedDeploymentDirectory) {
String deploymentDirectory = null;
if (service != null
&& deploymentResourceMapperByProjectName != null) {
IDeploymentResourceMapper deploymentMapper = deploymentResourceMapperByProjectName.get(service.getNamespace());
if (deploymentMapper != null) {
for (Iterator<IResource> istagsIterator = deploymentMapper.getImageStreamTagsFor(service).iterator();
&& imageStreamsMap != null) {
ProjectImageStreamTags imageStreams = imageStreamsMap.get(service.getNamespace());
if (imageStreams != null) {
for (Iterator<IResource> istagsIterator = imageStreams.getImageStreamTags(service).iterator();
istagsIterator.hasNext();) {
String imageStreamTag = istagsIterator.next().toJson(true);
String dir = null;
Expand Down Expand Up @@ -196,7 +197,7 @@ private String matchFirstGroup(String imageStreamTag, Pattern pattern) {

public void setDeployProject(org.eclipse.core.resources.IProject project) {
update(getConnection(), getConnections(), project, this.projects, this.sourcePath,
this.podPath, this.deploymentMapperByProjectName, getService(), getServiceItems());
this.podPath, this.imageStreamTagsMap, getService(), getServiceItems());
}

public org.eclipse.core.resources.IProject getDeployProject() {
Expand All @@ -205,7 +206,7 @@ public org.eclipse.core.resources.IProject getDeployProject() {

protected void setProjects(List<org.eclipse.core.resources.IProject> projects) {
update(getConnection(), getConnections(), this.deployProject, projects, this.sourcePath,
this.podPath, this.deploymentMapperByProjectName, getService(), getServiceItems());
this.podPath, this.imageStreamTagsMap, getService(), getServiceItems());
}

public List<org.eclipse.core.resources.IProject> getProjects() {
Expand All @@ -214,7 +215,7 @@ public List<org.eclipse.core.resources.IProject> getProjects() {

public void setSourcePath(String sourcePath) {
update(getConnection(), getConnections(), this.deployProject, this.projects, sourcePath,
this.podPath, this.deploymentMapperByProjectName, getService(), getServiceItems());
this.podPath, this.imageStreamTagsMap, getService(), getServiceItems());
}

public String getSourcePath() {
Expand All @@ -223,7 +224,7 @@ public String getSourcePath() {

public void setPodPath(String podPath) {
update(getConnection(), getConnections(), this.deployProject, this.projects, this.sourcePath,
podPath, this.deploymentMapperByProjectName, getService(), getServiceItems());
podPath, this.imageStreamTagsMap, getService(), getServiceItems());
}

public String getPodPath() {
Expand All @@ -238,7 +239,7 @@ public boolean isPodPathEditable() {
public void setService(IService service) {
update(getConnection(), getConnections(),
this.deployProject, this.projects,
this.sourcePath, podPath, deploymentMapperByProjectName,
this.sourcePath, podPath, imageStreamTagsMap,
service, getServiceItems());
}

Expand All @@ -257,31 +258,30 @@ public void loadResources() {
public void loadResources(Connection newConnection) {
super.loadResources(newConnection);
setProjects(loadProjects());
setDeploymentMappers(createDeploymentMappers(newConnection));
setImageStreamTagsMap(createImageStreamTagsMap(newConnection));
}

private void setDeploymentMappers(Map<String, IDeploymentResourceMapper> deploymentMapperByProjectName) {
update(getConnection(), getConnections(), this.deployProject, this.projects, this.sourcePath, podPath, deploymentMapperByProjectName, getService(), getServiceItems());
private void setImageStreamTagsMap(Map<String, ProjectImageStreamTags> imageStreamsMap) {
update(getConnection(), getConnections(), this.deployProject, this.projects, this.sourcePath, podPath, imageStreamsMap, getService(), getServiceItems());
}

protected Map<String, IDeploymentResourceMapper> getImageStreamTags(){
return deploymentMapperByProjectName;
protected Map<String, ProjectImageStreamTags> getImageStreamTagsMap(){
return imageStreamTagsMap;
}

private Map<String, IDeploymentResourceMapper> createDeploymentMappers(Connection connection) {
Map<String, IDeploymentResourceMapper> deploymentMapperByProjectName = new HashMap<>();
private Map<String, ProjectImageStreamTags> createImageStreamTagsMap(Connection connection) {
if (connection != null) {
List<IProject> projects = connection.getResources(ResourceKind.PROJECT);
if (projects != null) {
projects.forEach(project -> {
IDeploymentResourceMapper deploymentMapper =
new DeploymentResourceMapper(connection, new ProjectAdapterFake((IProject) project));
deploymentMapper.refresh();
deploymentMapperByProjectName.put(project.getName(), deploymentMapper);
});
return projects.stream()
.collect(Collectors.toMap(
project -> project.getName(),
project -> new ProjectImageStreamTags(project, connection))
);
}
}
return deploymentMapperByProjectName;
return Collections.emptyMap();

}

protected List<org.eclipse.core.resources.IProject> loadProjects() {
Expand Down Expand Up @@ -318,22 +318,38 @@ protected IServerWorkingCopy getServer() {
return server;
}

public static class ProjectImageStreamTags {

private Map<IService, Collection<IResource>> imageStreamTagsByService;

public ProjectImageStreamTags(IProject project, Connection connection) {
this.imageStreamTagsByService = createImageStreamsMap(project, connection);
}

private Map<IService, Collection<IResource>> createImageStreamsMap(IProject project, Connection connection) {
IDeploymentResourceMapper deploymentMapper =
new DeploymentResourceMapper(connection, new ProjectAdapterFake((IProject) project));
deploymentMapper.refresh();
return deploymentMapper.getAllImageStreamTags();
}

public Collection<IResource> getImageStreamTags(IService service) {
return imageStreamTagsByService.get(service);
}
}

private static class ProjectAdapterFake implements IProjectAdapter {

private IProject project;

private ProjectAdapterFake(IProject project) {
this.project = project;
}


@Override
public void dispose() {
// TODO Auto-generated method stub

}


@Override
public Collection<IResourceUIModel> getBuilds() {
return null;
Expand Down Expand Up @@ -480,16 +496,12 @@ public void setDeployments(Collection<Deployment> deployment) {

@Override
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
// TODO Auto-generated method stub

}

@Override
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
// TODO Auto-generated method stub

}

}

}

0 comments on commit c3030e4

Please sign in to comment.