Search in sources :

Example 1 with ResourceDescriptionImpl

use of org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl in project xtext-eclipse by eclipse.

the class ClusteringBuilderState method doUpdate.

/**
 * Actually do the build.
 *
 * @param buildData
 *            the data that should be considered for the update
 * @param newData
 *            the new resource descriptions as they are to be persisted (the new index after the build).
 *            Initially contains the old resource descriptions.
 * @param monitor
 *            The progress monitor
 * @return A list of deltas describing all changes made by the build.
 */
@Override
protected Collection<Delta> doUpdate(BuildData buildData, ResourceDescriptionsData newData, IProgressMonitor monitor) {
    final SubMonitor progress = SubMonitor.convert(monitor, 100);
    // Step 1: Clean the set of deleted URIs. If any of them are also added, they're not deleted.
    final Set<URI> toBeDeleted = buildData.getAndRemoveToBeDeleted();
    // Step 2: Create a new state (old state minus the deleted resources). This, by virtue of the flag
    // NAMED_BUILDER_SCOPE in the resource set's load options
    // and a Guice binding, is the index that is used during the build; i.e., linking during the build will
    // use this. Once the build is completed, the persistable index is reset to the contents of newState by
    // virtue of the newMap, which is maintained in synch with this.
    ResourceSet resourceSet = buildData.getResourceSet();
    final CurrentDescriptions newState = new CurrentDescriptions(resourceSet, newData, buildData);
    buildData.getSourceLevelURICache().cacheAsSourceURIs(toBeDeleted);
    installSourceLevelURIs(buildData);
    // Step 3: Create a queue; write new temporary resource descriptions for the added or updated resources so that we can link
    // subsequently; put all the added or updated resources into the queue.
    writeNewResourceDescriptions(buildData, this, newState, progress.newChild(20));
    if (progress.isCanceled()) {
        throw new OperationCanceledException();
    }
    // in this set as potential candidates.
    for (final URI uri : toBeDeleted) {
        newData.removeDescription(uri);
    }
    final Set<URI> allRemainingURIs = Sets.newLinkedHashSet(newData.getAllURIs());
    allRemainingURIs.removeAll(buildData.getToBeUpdated());
    for (URI remainingURI : buildData.getAllRemainingURIs()) {
        allRemainingURIs.remove(remainingURI);
    }
    // TODO: consider to remove any entry from upstream projects and independent projects
    // from the set of remaining uris (template method or service?)
    // this should reduce the number of to-be-checked descriptions significantly
    // for common setups (large number of reasonable sized projects)
    // Our return value. It contains all the deltas resulting from this build.
    final Set<Delta> allDeltas = Sets.newHashSet();
    // Step 5: Put all resources depending on a deleted resource into the queue. Also register the deltas in allDeltas.
    if (!toBeDeleted.isEmpty()) {
        for (final URI uri : toBeDeleted) {
            final IResourceDescription oldDescription = this.getResourceDescription(uri);
            if (oldDescription != null) {
                allDeltas.add(new DefaultResourceDescriptionDelta(oldDescription, null));
            }
        }
    }
    // Add all pending deltas to all deltas (may be scheduled java deltas)
    Collection<Delta> pendingDeltas = buildData.getAndRemovePendingDeltas();
    allDeltas.addAll(pendingDeltas);
    queueAffectedResources(allRemainingURIs, this, newState, allDeltas, allDeltas, buildData, progress.newChild(1));
    installSourceLevelURIs(buildData);
    IProject currentProject = getBuiltProject(buildData);
    LoadOperation loadOperation = null;
    try {
        Queue<URI> queue = buildData.getURIQueue();
        loadOperation = crossLinkingResourceLoader.create(resourceSet, currentProject);
        loadOperation.load(queue);
        // Step 6: Iteratively got through the queue. For each resource, create a new resource description and queue all depending
        // resources that are not yet in the delta. Validate resources. Do this in chunks.
        final SubMonitor subProgress = progress.newChild(80);
        CancelIndicator cancelMonitor = new MonitorBasedCancelIndicator(progress);
        int index = 0;
        while (!queue.isEmpty()) {
            // heuristic: only 2/3 of ticks will be consumed; rest kept for affected resources
            if (index % MONITOR_DO_UPDATE_CHUNK == 0) {
                subProgress.setWorkRemaining(((queue.size() / MONITOR_DO_UPDATE_CHUNK) + 1) * 3);
            }
            int clusterIndex = 0;
            final List<Delta> changedDeltas = Lists.newArrayList();
            while (!queue.isEmpty()) {
                if (subProgress.isCanceled()) {
                    loadOperation.cancel();
                    throw new OperationCanceledException();
                }
                if (!clusteringPolicy.continueProcessing(resourceSet, null, clusterIndex)) {
                    break;
                }
                URI changedURI = null;
                URI actualResourceURI = null;
                Resource resource = null;
                Delta newDelta = null;
                try {
                    // Load the resource and create a new resource description
                    LoadResult loadResult = loadOperation.next();
                    changedURI = loadResult.getUri();
                    actualResourceURI = loadResult.getResource().getURI();
                    resource = addResource(loadResult.getResource(), resourceSet);
                    if (index % MONITOR_DO_UPDATE_CHUNK == 0) {
                        subProgress.subTask("Updating resource descriptions chunk " + (index / MONITOR_DO_UPDATE_CHUNK + 1) + " of " + ((index + queue.size()) / MONITOR_DO_UPDATE_CHUNK + 1));
                    }
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Update resource description " + actualResourceURI);
                    }
                    queue.remove(changedURI);
                    if (toBeDeleted.contains(changedURI)) {
                        break;
                    }
                    buildLogger.log("indexing " + changedURI);
                    final IResourceDescription.Manager manager = getResourceDescriptionManager(actualResourceURI);
                    if (manager != null) {
                        // Resolve links here!
                        try {
                            EcoreUtil2.resolveLazyCrossReferences(resource, cancelMonitor);
                            final IResourceDescription description = manager.getResourceDescription(resource);
                            final IResourceDescription copiedDescription = BuilderStateUtil.create(description);
                            newDelta = manager.createDelta(this.getResourceDescription(actualResourceURI), copiedDescription);
                        } catch (OperationCanceledException e) {
                            loadOperation.cancel();
                            throw e;
                        } catch (WrappedException e) {
                            throw e;
                        } catch (RuntimeException e) {
                            LOGGER.error("Error resolving cross references on resource '" + actualResourceURI + "'", e);
                            throw new LoadOperationException(actualResourceURI, e);
                        }
                    }
                } catch (final WrappedException ex) {
                    if (ex instanceof LoadOperationException) {
                        changedURI = ((LoadOperationException) ex).getUri();
                    }
                    Throwable cause = ex.getCause();
                    boolean wasResourceNotFound = false;
                    if (cause instanceof CoreException) {
                        if (IResourceStatus.RESOURCE_NOT_FOUND == ((CoreException) cause).getStatus().getCode()) {
                            wasResourceNotFound = true;
                        }
                    }
                    if (changedURI == null) {
                        // $NON-NLS-1$
                        LOGGER.error("Error loading resource", ex);
                    } else {
                        queue.remove(changedURI);
                        if (toBeDeleted.contains(changedURI))
                            break;
                        if (!wasResourceNotFound)
                            // $NON-NLS-1$
                            LOGGER.error("Error loading resource from: " + changedURI.toString(), ex);
                        if (resource != null) {
                            resourceSet.getResources().remove(resource);
                        }
                        final IResourceDescription oldDescription = this.getResourceDescription(changedURI);
                        final IResourceDescription newDesc = newState.getResourceDescription(changedURI);
                        ResourceDescriptionImpl indexReadyDescription = newDesc != null ? BuilderStateUtil.create(newDesc) : null;
                        if ((oldDescription != null || indexReadyDescription != null) && oldDescription != indexReadyDescription) {
                            newDelta = new DefaultResourceDescriptionDelta(oldDescription, indexReadyDescription);
                        }
                    }
                }
                if (newDelta != null) {
                    allDeltas.add(newDelta);
                    clusterIndex++;
                    if (newDelta.haveEObjectDescriptionsChanged())
                        changedDeltas.add(newDelta);
                    // Make the new resource description known and update the map.
                    newState.register(newDelta);
                    // Validate now.
                    if (!buildData.isIndexingOnly()) {
                        try {
                            updateMarkers(newDelta, resourceSet, subProgress);
                        } catch (OperationCanceledException e) {
                            loadOperation.cancel();
                            throw e;
                        } catch (Exception e) {
                            LOGGER.error("Error validating " + newDelta.getUri(), e);
                        }
                    }
                }
                index++;
                if (index % MONITOR_DO_UPDATE_CHUNK == 0) {
                    subProgress.worked(1);
                }
            }
            loadOperation.cancel();
            queueAffectedResources(allRemainingURIs, this, newState, changedDeltas, allDeltas, buildData, subProgress.newChild(1));
            installSourceLevelURIs(buildData);
            if (queue.size() > 0) {
                loadOperation = crossLinkingResourceLoader.create(resourceSet, currentProject);
                loadOperation.load(queue);
            }
            // Release memory
            if (!queue.isEmpty() && !clusteringPolicy.continueProcessing(resourceSet, null, clusterIndex))
                clearResourceSet(resourceSet);
        }
    } finally {
        if (loadOperation != null)
            loadOperation.cancel();
        if (!progress.isCanceled())
            progress.done();
    }
    return allDeltas;
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) ResourceDescriptionImpl(org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) URI(org.eclipse.emf.common.util.URI) MonitorBasedCancelIndicator(org.eclipse.xtext.builder.MonitorBasedCancelIndicator) LoadOperation(org.eclipse.xtext.builder.resourceloader.IResourceLoader.LoadOperation) DefaultResourceDescriptionDelta(org.eclipse.xtext.resource.impl.DefaultResourceDescriptionDelta) WrappedException(org.eclipse.emf.common.util.WrappedException) SubMonitor(org.eclipse.core.runtime.SubMonitor) Resource(org.eclipse.emf.ecore.resource.Resource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IProject(org.eclipse.core.resources.IProject) LoadResult(org.eclipse.xtext.builder.resourceloader.IResourceLoader.LoadResult) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) LoadOperationException(org.eclipse.xtext.builder.resourceloader.IResourceLoader.LoadOperationException) WrappedException(org.eclipse.emf.common.util.WrappedException) LoadOperationException(org.eclipse.xtext.builder.resourceloader.IResourceLoader.LoadOperationException) CoreException(org.eclipse.core.runtime.CoreException) Delta(org.eclipse.xtext.resource.IResourceDescription.Delta) DefaultResourceDescriptionDelta(org.eclipse.xtext.resource.impl.DefaultResourceDescriptionDelta) MonitorBasedCancelIndicator(org.eclipse.xtext.builder.MonitorBasedCancelIndicator) CancelIndicator(org.eclipse.xtext.util.CancelIndicator)

Example 2 with ResourceDescriptionImpl

use of org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl in project xtext-eclipse by eclipse.

the class AbstractEMFBasedPersisterTest method createResourceDescription.

public IResourceDescription createResourceDescription(int idx) {
    ResourceDescriptionImpl result = (ResourceDescriptionImpl) BuilderStateFactory.eINSTANCE.createResourceDescription();
    result.setURI(URI.createPlatformResourceURI(idx + ".uri", true));
    result.getExportedObjects().add(createObjectDescription(1));
    result.getExportedObjects().add(createObjectDescription(2));
    result.getExportedObjects().add(createObjectDescription(3));
    result.getReferenceDescriptions().add(createReferenceDescription(1));
    result.getReferenceDescriptions().add(createReferenceDescription(2));
    result.getReferenceDescriptions().add(createReferenceDescription(3));
    result.getImportedNames().add(createQualifiedName("name1"));
    result.getImportedNames().add(createQualifiedName("name2"));
    result.getImportedNames().add(createQualifiedName("name3"));
    return result;
}
Also used : ResourceDescriptionImpl(org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl)

Example 3 with ResourceDescriptionImpl

use of org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl in project xtext-eclipse by eclipse.

the class AbstractEMFBasedPersisterTest method testBug321536.

/**
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=321536
 */
@Test
public void testBug321536() throws Exception {
    ResourceDescriptionImpl result = (ResourceDescriptionImpl) BuilderStateFactory.eINSTANCE.createResourceDescription();
    URI uri = URI.createURI("copy of model.uri", true);
    result.setURI(uri);
    persister.save(Collections.singleton((IResourceDescription) result));
    Iterable<IResourceDescription> load = persister.load();
    URI uriAfterSave = load.iterator().next().getURI();
    assertEquals(uri, uriAfterSave);
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) ResourceDescriptionImpl(org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl) URI(org.eclipse.emf.common.util.URI) Test(org.junit.Test)

Example 4 with ResourceDescriptionImpl

use of org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl in project xtext-xtend by eclipse.

the class BuilderStateUtilBenchmark method setUp.

@Override
protected void setUp() throws Exception {
    ResourceDescriptionImpl resourceDescription = (ResourceDescriptionImpl) BuilderStateFactory.eINSTANCE.createResourceDescription();
    resourceDescription.setURI(URI.createURI("myURI"));
    createExportedObjects(resourceDescription);
    createImportedNames(resourceDescription);
    // createReferenceDescriptions(resourceDescription);
    copyMe = resourceDescription;
}
Also used : ResourceDescriptionImpl(org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl)

Example 5 with ResourceDescriptionImpl

use of org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl in project xtext-eclipse by eclipse.

the class BuilderStateUtil method create.

public static ResourceDescriptionImpl create(IResourceDescription desc) {
    if (desc instanceof ResourceDescriptionImpl)
        return (ResourceDescriptionImpl) desc;
    ResourceDescriptionImpl result = (ResourceDescriptionImpl) BuilderStateFactory.eINSTANCE.createResourceDescription();
    result.setURI(desc.getURI());
    copyExportedObject(desc, result);
    copyReferenceDescriptions(desc, result);
    copyImportedNames(desc, result);
    return result;
}
Also used : ResourceDescriptionImpl(org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl)

Aggregations

ResourceDescriptionImpl (org.eclipse.xtext.builder.builderState.impl.ResourceDescriptionImpl)7 Test (org.junit.Test)3 URI (org.eclipse.emf.common.util.URI)2 EObjectDescriptionImpl (org.eclipse.xtext.builder.builderState.impl.EObjectDescriptionImpl)2 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)2 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 WrappedException (org.eclipse.emf.common.util.WrappedException)1 EClass (org.eclipse.emf.ecore.EClass)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 MonitorBasedCancelIndicator (org.eclipse.xtext.builder.MonitorBasedCancelIndicator)1 LoadOperation (org.eclipse.xtext.builder.resourceloader.IResourceLoader.LoadOperation)1 LoadOperationException (org.eclipse.xtext.builder.resourceloader.IResourceLoader.LoadOperationException)1 LoadResult (org.eclipse.xtext.builder.resourceloader.IResourceLoader.LoadResult)1 Delta (org.eclipse.xtext.resource.IResourceDescription.Delta)1 DefaultResourceDescriptionDelta (org.eclipse.xtext.resource.impl.DefaultResourceDescriptionDelta)1