Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1cc0a221f75d81243cf22426a7cec364fde4b5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.core.search.indexing;

import java.io.IOException;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.internal.core.index.IIndex;
import org.eclipse.cdt.internal.core.search.processing.IJob;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;

public abstract class IndexRequest implements IJob {
	protected boolean isCancelled = false;
	protected IPath indexPath;
	protected IndexManager manager;

	public IndexRequest(IPath indexPath, IndexManager manager) {
		this.indexPath = indexPath;
		this.manager = manager;
	}
	
	public boolean belongsTo(String projectName) {
		return projectName.equals(this.indexPath.segment(0));
	}
	
	public void cancel() {
		this.manager.jobFinishedNotification( this );
		this.manager.jobWasCancelled(this.indexPath);
		this.isCancelled = true;
	}
	
	public boolean isReadyToRun() {
		IProject project = CCorePlugin.getWorkspace().getRoot().getProject(indexPath.segment(0));
		if ( !project.isAccessible() || !this.manager.isIndexEnabled( project ) )
			return false;
		
		// tag the index as inconsistent
		this.manager.aboutToUpdateIndex(indexPath, updatedIndexState());
		return true;
	}
	/*
	 * This code is assumed to be invoked while monitor has read lock
	 */
	protected void saveIfNecessary(IIndex index, ReadWriteMonitor monitor) throws IOException {
		/* if index has changed, commit these before querying */
		if (index.hasChanged()) {
			try {
				monitor.exitRead(); // free read lock
				monitor.enterWrite(); // ask permission to write
				this.manager.saveIndex(index);
			} finally {
				monitor.exitWriteEnterRead(); // finished writing and reacquire read permission
			}
		}
	}
	
	protected Integer updatedIndexState() {
		return IndexManager.UPDATING_STATE;
	}
}

Back to the top