Search in sources :

Example 1 with JobGroup

use of org.eclipse.core.runtime.jobs.JobGroup in project eclipse.platform.text by eclipse.

the class TextSearchVisitor method search.

public IStatus search(IFile[] files, IProgressMonitor monitor) {
    if (files.length == 0) {
        return fStatus;
    }
    fProgressMonitor = monitor == null ? new NullProgressMonitor() : monitor;
    fNumberOfScannedFiles = 0;
    fNumberOfFilesToScan = files.length;
    fCurrentFile = null;
    int maxThreads = fCollector.canRunInParallel() ? NUMBER_OF_LOGICAL_THREADS : 1;
    int jobCount = 1;
    if (maxThreads > 1) {
        jobCount = (files.length + FILES_PER_JOB - 1) / FILES_PER_JOB;
    }
    // $NON-NLS-1$
    final JobGroup jobGroup = new TextSearchJobGroup("Text Search", maxThreads, jobCount);
    long startTime = TRACING ? System.currentTimeMillis() : 0;
    Job monitorUpdateJob = new Job(SearchMessages.TextSearchVisitor_progress_updating_job) {

        private int fLastNumberOfScannedFiles = 0;

        @Override
        public IStatus run(IProgressMonitor inner) {
            while (!inner.isCanceled()) {
                // Propagate user cancellation to the JobGroup.
                if (fProgressMonitor.isCanceled()) {
                    jobGroup.cancel();
                    break;
                }
                IFile file;
                int numberOfScannedFiles;
                synchronized (fLock) {
                    file = fCurrentFile;
                    numberOfScannedFiles = fNumberOfScannedFiles;
                }
                if (file != null) {
                    String fileName = file.getName();
                    Object[] args = { fileName, Integer.valueOf(numberOfScannedFiles), Integer.valueOf(fNumberOfFilesToScan) };
                    fProgressMonitor.subTask(Messages.format(SearchMessages.TextSearchVisitor_scanning, args));
                    int steps = numberOfScannedFiles - fLastNumberOfScannedFiles;
                    fProgressMonitor.worked(steps);
                    fLastNumberOfScannedFiles += steps;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    return Status.OK_STATUS;
                }
            }
            return Status.OK_STATUS;
        }
    };
    try {
        String taskName = fSearchPattern.pattern().length() == 0 ? SearchMessages.TextSearchVisitor_filesearch_task_label : Messages.format(SearchMessages.TextSearchVisitor_textsearch_task_label, fSearchPattern.pattern());
        fProgressMonitor.beginTask(taskName, fNumberOfFilesToScan);
        monitorUpdateJob.setSystem(true);
        monitorUpdateJob.schedule();
        try {
            fCollector.beginReporting();
            Map<IFile, IDocument> documentsInEditors = PlatformUI.isWorkbenchRunning() ? evalNonFileBufferDocuments() : Collections.emptyMap();
            int filesPerJob = (files.length + jobCount - 1) / jobCount;
            IFile[] filesByLocation = new IFile[files.length];
            System.arraycopy(files, 0, filesByLocation, 0, files.length);
            // Sorting files to search by location allows to more easily reuse
            // search results from one file to the other when they have same location
            Arrays.sort(filesByLocation, (o1, o2) -> {
                if (o1 == o2) {
                    return 0;
                }
                if (o1.getLocation() == o2.getLocation()) {
                    return 0;
                }
                if (o1.getLocation() == null) {
                    return +1;
                }
                if (o2.getLocation() == null) {
                    return -1;
                }
                return o1.getLocation().toString().compareTo(o2.getLocation().toString());
            });
            for (int first = 0; first < filesByLocation.length; first += filesPerJob) {
                int end = Math.min(filesByLocation.length, first + filesPerJob);
                Job job = new TextSearchJob(filesByLocation, first, end, documentsInEditors);
                job.setJobGroup(jobGroup);
                job.schedule();
            }
            // The monitorUpdateJob is managing progress and cancellation,
            // so it is ok to pass a null monitor into the job group.
            jobGroup.join(0, null);
            if (fProgressMonitor.isCanceled())
                throw new OperationCanceledException(SearchMessages.TextSearchVisitor_canceled);
            fStatus.addAll(jobGroup.getResult());
            return fStatus;
        } catch (InterruptedException e) {
            throw new OperationCanceledException(SearchMessages.TextSearchVisitor_canceled);
        } finally {
            monitorUpdateJob.cancel();
        }
    } finally {
        fProgressMonitor.done();
        fCollector.endReporting();
        if (TRACING) {
            Object[] args = { Integer.valueOf(fNumberOfScannedFiles), Integer.valueOf(jobCount), Integer.valueOf(NUMBER_OF_LOGICAL_THREADS), Long.valueOf(System.currentTimeMillis() - startTime) };
            System.out.println(Messages.format("[TextSearch] Search duration for {0} files in {1} jobs using {2} threads: {3}ms", // $NON-NLS-1$
            args));
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JobGroup(org.eclipse.core.runtime.jobs.JobGroup) IFile(org.eclipse.core.resources.IFile) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with JobGroup

use of org.eclipse.core.runtime.jobs.JobGroup in project AutoRefactor by JnRouvignac.

the class PrepareApplyRefactoringsJob method run0.

private IStatus run0(IProgressMonitor monitor) throws Exception {
    if (!javaElements.isEmpty()) {
        final Queue<RefactoringUnit> toRefactor = collectRefactoringUnits(javaElements, monitor);
        final int nbCores = Runtime.getRuntime().availableProcessors();
        final int nbWorkers = computeNbWorkers(toRefactor.size(), nbCores);
        final JobGroup jobGroup = new JobGroup("Job name", nbWorkers, nbWorkers);
        for (int i = 0; i < nbWorkers; i++) {
            final Job job = new ApplyRefactoringsJob(toRefactor, clone(refactoringRulesToApply), environment);
            job.setJobGroup(jobGroup);
            job.setUser(true);
            job.schedule();
        }
    }
    return Status.OK_STATUS;
}
Also used : JobGroup(org.eclipse.core.runtime.jobs.JobGroup) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

Job (org.eclipse.core.runtime.jobs.Job)2 JobGroup (org.eclipse.core.runtime.jobs.JobGroup)2 IFile (org.eclipse.core.resources.IFile)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IDocument (org.eclipse.jface.text.IDocument)1