[400105] Simplified resource caching
diff --git a/plugins/org.eclipse.xtend.shared.ui/src/org/eclipse/xtend/shared/ui/core/internal/XtendXpandProject.java b/plugins/org.eclipse.xtend.shared.ui/src/org/eclipse/xtend/shared/ui/core/internal/XtendXpandProject.java
index fba1fe8..09f1241 100644
--- a/plugins/org.eclipse.xtend.shared.ui/src/org/eclipse/xtend/shared/ui/core/internal/XtendXpandProject.java
+++ b/plugins/org.eclipse.xtend.shared.ui/src/org/eclipse/xtend/shared/ui/core/internal/XtendXpandProject.java
@@ -7,13 +7,11 @@
package org.eclipse.xtend.shared.ui.core.internal;
-import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.core.resources.IFile;
@@ -30,7 +28,6 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
-import org.eclipse.internal.xtend.expression.parser.SyntaxConstants;
import org.eclipse.jdt.core.ElementChangedEvent;
import org.eclipse.jdt.core.IElementChangedListener;
import org.eclipse.jdt.core.IJarEntryResource;
@@ -173,16 +170,11 @@
protected void removeResourcesFromJar() {
for (ResourceID id : fromJar) {
- for (Map<ResourceID, IXtendXpandResource> values : resources
- .values()) {
- if (values.remove(id) != null) {
- break;
- }
- }
+ resources.remove(id);
}
}
- private final Map<String, Map<ResourceID, IXtendXpandResource>> resources = new HashMap<String, Map<ResourceID, IXtendXpandResource>>();
+ private final Map<ResourceID, IXtendXpandResource> resources = new HashMap<ResourceID, IXtendXpandResource>();
private final Set<ResourceID> fromJar = new HashSet<ResourceID>();
@@ -190,15 +182,8 @@
* @see IXtendXpandProject#getRegisteredResources()
*/
public IXtendXpandResource[] getRegisteredResources() {
- List<IXtendXpandResource> result = new ArrayList<IXtendXpandResource>();
- int size = 0;
- Collection<Map<ResourceID, IXtendXpandResource>> values = resources
- .values();
- for (Map<ResourceID, IXtendXpandResource> value : values) {
- size = size + value.values().size();
- result.addAll(value.values());
- }
- return result.toArray(new IXtendXpandResource[size]);
+ return resources.values().toArray(
+ new IXtendXpandResource[resources.size()]);
}
/**
@@ -259,13 +244,17 @@
if (res.getUnderlyingStorage() instanceof IFile) {
IFile file = (IFile) res.getUnderlyingStorage();
XtendXpandMarkerManager.deleteMarkers(file);
- for (Map<ResourceID, IXtendXpandResource> values : resources
- .values()) {
- if (values.remove(Activator.findXtendXpandResourceID(
- project, file)) != null) {
+ ResourceID toRemove = null;
+ for (Entry<ResourceID, IXtendXpandResource> entry : resources
+ .entrySet()) {
+ if (entry.getValue() == toRemove) {
+ toRemove = entry.getKey();
break;
}
}
+ if (toRemove != null) {
+ resources.remove(toRemove);
+ }
}
}
}
@@ -279,9 +268,11 @@
assert (extension != null);
if (Activator.getRegisteredResourceContributorFor(extension) == null) {
- return null;
+ throw new IllegalArgumentException(
+ "No resource contributor available for extension "
+ + extension);
}
- // for performance reasons ask the cache first
+ final ResourceID resId = new ResourceID(fqn, extension);
IXtendXpandResource res = findCachedXtendXpandResource(fqn, extension);
if (res == NULL_RESOURCE) {
return null;
@@ -299,23 +290,11 @@
}
// ask to load the resource without looking into jars
res = loadXtendXpandResource(fqn, extension, false);
- if (res != null) {
- return res;
- }
- // look into jars
- res = loadXtendXpandResource(fqn, extension, true);
- // caching NULL_RESOURCE
if (res == null) {
- int index = fqn.lastIndexOf(SyntaxConstants.NS_DELIM);
- String simpleName = index == -1 ? fqn : fqn.substring(index + 2);
- Map<ResourceID, IXtendXpandResource> map = resources
- .get(simpleName);
- if (map == null) {
- map = new HashMap<ResourceID, IXtendXpandResource>();
- resources.put(simpleName, map);
- }
- map.put(new ResourceID(fqn, extension), NULL_RESOURCE);
+ // look into jars
+ res = loadXtendXpandResource(fqn, extension, true);
}
+ resources.put(resId, res != null ? res : NULL_RESOURCE);
return res;
}
@@ -328,33 +307,11 @@
*/
private IXtendXpandResource findCachedXtendXpandResource(final String fqn,
final String extension) {
- int index = fqn.lastIndexOf(SyntaxConstants.NS_DELIM);
- // fqn is a simple name
- if (index == -1) {
- Map<ResourceID, IXtendXpandResource> map = resources.get(fqn);
- if (map != null) {
- ResourceID match = null;
- for (ResourceID id : map.keySet()) {
- if (extension.equals(id.extension)) {
- if (match != null) {
- return null;
- }
- match = id;
- }
- }
- return map.get(match);
- }
- return null;
+ IXtendXpandResource resource = resources.get(new ResourceID(fqn,
+ extension));
+ if (resource != null && resource != NULL_RESOURCE) {
+ return resource;
}
-
- // fqn is a qualified name
- String simpleName = fqn.substring(index + 2);
- Map<ResourceID, IXtendXpandResource> map = resources.get(simpleName);
- if (map != null) {
- ResourceID searchedID = new ResourceID(fqn, extension);
- return map.get(searchedID);
- }
-
return null;
}
@@ -378,12 +335,14 @@
new HashSet<XtendXpandProject>(5));
}
+ @SuppressWarnings("deprecation")
private IXtendXpandResource loadXtendXpandResource(final String fqn,
final String extension, final boolean searchJars,
final Set<XtendXpandProject> projects) {
assert (fqn != null);
assert (extension != null);
+ final ResourceID resourceID = new ResourceID(fqn, extension);
// prevent stackoverflow with recursive project dependencies and
// resources that could not be found
if (!projects.add(this)) {
@@ -401,21 +360,7 @@
// Find out if the storage is already under another key in the cache
ResourceID jdtResourceID = JDTUtil.findXtendXpandResourceID(
getProject(), storage);
- Map<ResourceID, IXtendXpandResource> resourcesWithSameResourceID = null;
- if (jdtResourceID != null) {
- resourcesWithSameResourceID = resources
- .get(getBaseName(jdtResourceID));
- }
IXtendXpandResource result = null;
- if (resourcesWithSameResourceID != null) {
- result = resourcesWithSameResourceID.get(jdtResourceID);
- }
- // if the storage is already in cache with another resourceID return
- // this xtendXpandResource
- if ((result != null) && (result.getUnderlyingStorage() != null)) {
- cacheXtendXpandResource(storage, result);
- return result;
- }
// get the file extension and find the appropriate
// ResourceContributor for this
@@ -436,11 +381,13 @@
if (result != null) {
// cache this resource
- cacheXtendXpandResource(storage, result);
+
+ // cacheXtendXpandResource(storage, result);
+ resources.put(jdtResourceID, result);
// remember, because we need to clean the cache, if
// a jar has been removed
if (!(storage instanceof IFile)) {
- fromJar.add(new ResourceID(fqn, extension));
+ fromJar.add(jdtResourceID);
}
return result;
}
@@ -466,7 +413,7 @@
if (storage == null) {
storage = result.getUnderlyingStorage();
}
- cacheXtendXpandResource(storage, result);
+ resources.put(resourceID, result);
return result;
}
}
@@ -477,41 +424,6 @@
return null;
}
- private void cacheXtendXpandResource(final IStorage storage,
- final IXtendXpandResource result) {
- String baseName = getBaseName(storage);
- Map<ResourceID, IXtendXpandResource> resourcesWithSameBaseName = resources
- .get(baseName);
- if (resourcesWithSameBaseName == null) {
- resourcesWithSameBaseName = new HashMap<ResourceID, IXtendXpandResource>();
- resources.put(baseName, resourcesWithSameBaseName);
- }
- resourcesWithSameBaseName.put(
- Activator.findXtendXpandResourceID(project, storage), result);
- }
-
- private String getBaseName(final IStorage storage) {
- return storage.getFullPath().removeFileExtension().lastSegment();
- }
-
- private String getBaseName(final ResourceID id) {
- if ((id != null) && (id.name != null) && (id.name.length() > 1)) {
- int lastIndexOf = id.name.lastIndexOf(":");
- if (lastIndexOf != -1) {
- if (lastIndexOf < id.name.length()) {
- return id.name.substring(lastIndexOf + 1, id.name.length());
- } else {
- // name ends with a ":"
- return null;
- }
- } else {
- // the name is already simple
- return id.name;
- }
- }
- return null;
- }
-
/**
* @see IXtendXpandProject#findExtXptResource(IPath, boolean)
*/
@@ -535,17 +447,9 @@
*/
public void analyze(final IProgressMonitor monitor,
final ExecutionContext ctx) {
- int size = 0;
- Collection<Map<ResourceID, IXtendXpandResource>> resourceValues = resources
- .values();
- for (Map<ResourceID, IXtendXpandResource> value : resourceValues) {
- size = size + value.values().size();
- }
- List<IXtendXpandResource> resources = Arrays
- .asList(getRegisteredResources());
SubMonitor subMon = SubMonitor.convert(monitor, resources.size());
- for (IXtendXpandResource resource : resources) {
+ for (IXtendXpandResource resource : resources.values()) {
if (monitor.isCanceled()) {
return;
}