Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 308402 | Differences between
and this patch

Collapse All | Expand All

(-)search/org/eclipse/jdt/core/search/SearchParticipant.java (-1 / +7 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 49-54 Link Here
49
 */
49
 */
50
public abstract class SearchParticipant {
50
public abstract class SearchParticipant {
51
51
52
	private IPath lastIndexLocation;
53
52
	/**
54
	/**
53
	 * Creates a new search participant.
55
	 * Creates a new search participant.
54
	 */
56
	 */
Lines 205-210 Link Here
205
		// TODO (frederic) should not have to create index manually, should expose API that recreates index instead
207
		// TODO (frederic) should not have to create index manually, should expose API that recreates index instead
206
		manager.ensureIndexExists(indexLocation, containerPath);
208
		manager.ensureIndexExists(indexLocation, containerPath);
207
		manager.scheduleDocumentIndexing(document, containerPath, indexLocation, this);
209
		manager.scheduleDocumentIndexing(document, containerPath, indexLocation, this);
210
		if (indexLocation != this.lastIndexLocation) {
211
			manager.updateParticipant(indexLocation, containerPath);
212
			this.lastIndexLocation = indexLocation;
213
		}
208
	}
214
	}
209
215
210
	/**
216
	/**
(-)search/org/eclipse/jdt/internal/core/search/indexing/IndexManager.java (+99 lines)
Lines 58-63 Link Here
58
	public static final Integer UPDATING_STATE = new Integer(1);
58
	public static final Integer UPDATING_STATE = new Integer(1);
59
	public static final Integer UNKNOWN_STATE = new Integer(2);
59
	public static final Integer UNKNOWN_STATE = new Integer(2);
60
	public static final Integer REBUILDING_STATE = new Integer(3);
60
	public static final Integer REBUILDING_STATE = new Integer(3);
61
	
62
	// search participants who register indexes with the index manager
63
	private SimpleLookupTable participantsContainers = null;
64
	private boolean participantUpdated = false;
65
	private static final String PARTICIPANTS_INDEX_NAMES = "participantsIndexNames.txt"; //$NON-NLS-1$
61
66
62
	// Debug
67
	// Debug
63
	public static boolean DEBUG = false;
68
	public static boolean DEBUG = false;
Lines 324-329 Link Here
324
					rebuildIndex(indexLocation, containerPath);
329
					rebuildIndex(indexLocation, containerPath);
325
					index = null;
330
					index = null;
326
				}
331
				}
332
			} else {
333
				if (!getJavaPluginWorkingLocation().isPrefixOf(indexLocation)) { // the index belongs to non-jdt search participant
334
					if (indexLocation.toFile().exists()) { 
335
						try {
336
							IPath container = getParticipantsContainer(indexLocation);
337
							if (container != null) {
338
								index = new Index(indexLocation.toOSString(), container.toOSString(), true /*reuse index file*/);
339
								this.indexes.put(indexLocation, index);
340
							}
341
						} catch (IOException e) {
342
							// ignore
343
						}
344
					} 
345
				}
327
			}
346
			}
328
		}
347
		}
329
		if (index != null)
348
		if (index != null)
Lines 370-375 Link Here
370
	}
389
	}
371
	return this.indexStates;
390
	return this.indexStates;
372
}
391
}
392
private IPath getParticipantsContainer(IPath indexLocation) {
393
	if (this.participantsContainers == null) {
394
		readParticipantsIndexNamesFile();
395
		if (this.participantsContainers == null) return null;
396
	}
397
	return (IPath)this.participantsContainers.get(indexLocation);
398
}
373
private IPath getJavaPluginWorkingLocation() {
399
private IPath getJavaPluginWorkingLocation() {
374
	if (this.javaPluginLocation != null) return this.javaPluginLocation;
400
	if (this.javaPluginLocation != null) return this.javaPluginLocation;
375
401
Lines 662-667 Link Here
662
			this.indexes.removeKey(locations[i]);
688
			this.indexes.removeKey(locations[i]);
663
		removeIndexesState(locations);
689
		removeIndexesState(locations);
664
	}
690
	}
691
	if (this.participantsContainers != null && this.participantsContainers.get(path.toOSString()) != null) {
692
		this.participantsContainers.removeKey(path.toOSString());	
693
	}
665
}
694
}
666
/**
695
/**
667
 * Removes all indexes whose paths start with (or are equal to) the given path.
696
 * Removes all indexes whose paths start with (or are equal to) the given path.
Lines 802-807 Link Here
802
			monitor.exitRead();
831
			monitor.exitRead();
803
		}
832
		}
804
	}
833
	}
834
	if (this.participantsContainers != null && this.participantUpdated) {
835
		writeParticipantsIndexNamesFile();
836
		this.participantUpdated = false;
837
	}
805
	this.needToSave = !allSaved;
838
	this.needToSave = !allSaved;
806
}
839
}
807
public void scheduleDocumentIndexing(final SearchDocument searchDocument, IPath container, final IPath indexLocation, final SearchParticipant searchParticipant) {
840
public void scheduleDocumentIndexing(final SearchDocument searchDocument, IPath container, final IPath indexLocation, final SearchParticipant searchParticipant) {
Lines 861-866 Link Here
861
	}
894
	}
862
	return null;
895
	return null;
863
}
896
}
897
private void readParticipantsIndexNamesFile() {
898
	try {
899
		File participantsIndexNamesFile = new File(getSavedIndexesDirectory(), PARTICIPANTS_INDEX_NAMES);
900
		char[] savedIndexNames = org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(participantsIndexNamesFile, null);
901
		if (savedIndexNames.length > 0) {
902
			char[][] names = CharOperation.splitOn('\n', savedIndexNames);
903
			if (names.length >= 3) {
904
				// First line is DiskIndex signature  (see writeParticipantsIndexNamesFile())
905
				String savedSignature = DiskIndex.SIGNATURE;
906
				if (savedSignature.equals(new String(names[0]))) {
907
					SimpleLookupTable containers = new SimpleLookupTable(3);
908
					for (int i = 1, l = names.length-1 ; i < l ; i+=2) {
909
						containers.put(new Path(new String(names[i])), new Path(new String(names[i+1])));
910
					}
911
					this.participantsContainers = containers;
912
				}				
913
			}
914
		}	
915
	} catch (IOException ignored) {
916
		if (VERBOSE)
917
			Util.verbose("Failed to read saved index file names"); //$NON-NLS-1$
918
	}
919
	return;
920
}
864
private synchronized void removeIndexesState(IPath[] locations) {
921
private synchronized void removeIndexesState(IPath[] locations) {
865
	getIndexStates(); // ensure the states are initialized
922
	getIndexStates(); // ensure the states are initialized
866
	int length = locations.length;
923
	int length = locations.length;
Lines 907-912 Link Here
907
	}
964
	}
908
965
909
}
966
}
967
public void updateParticipant(IPath indexLocation, IPath containerPath) {
968
	if (this.participantsContainers == null) {
969
		readParticipantsIndexNamesFile();
970
		if (this.participantsContainers == null)
971
			this.participantsContainers = new SimpleLookupTable(3);
972
	} 
973
	if (this.participantsContainers.get(indexLocation) == null) {
974
		this.participantsContainers.put(indexLocation, containerPath);
975
		this.participantUpdated  = true;
976
	}
977
}
910
private void writeJavaLikeNamesFile() {
978
private void writeJavaLikeNamesFile() {
911
	BufferedWriter writer = null;
979
	BufferedWriter writer = null;
912
	String pathName = getJavaPluginWorkingLocation().toOSString();
980
	String pathName = getJavaPluginWorkingLocation().toOSString();
Lines 941-946 Link Here
941
		}
1009
		}
942
	}
1010
	}
943
}
1011
}
1012
private void writeParticipantsIndexNamesFile() {
1013
	BufferedWriter writer = null;
1014
	try {
1015
		File participantsIndexNamesFile = new File(getSavedIndexesDirectory(), PARTICIPANTS_INDEX_NAMES);
1016
		writer = new BufferedWriter(new FileWriter(participantsIndexNamesFile));
1017
		writer.write(DiskIndex.SIGNATURE);
1018
		writer.write('\n');
1019
		Object[] indexFiles = this.participantsContainers.keyTable;
1020
		Object[] containers = this.participantsContainers.valueTable;
1021
		for (int i = 0, l = indexFiles.length; i < l; i++) {
1022
			IPath indexFile = (IPath)indexFiles[i];
1023
			if (indexFile != null) {
1024
				writer.write(indexFile.toOSString());
1025
				writer.write('\n');
1026
				writer.write(((IPath)containers[i]).toOSString());
1027
				writer.write('\n');
1028
			}
1029
		}
1030
	} catch (IOException ignored) {
1031
		if (VERBOSE)
1032
			Util.verbose("Failed to write participant index file names", System.err); //$NON-NLS-1$
1033
	} finally {
1034
		if (writer != null) {
1035
			try {
1036
				writer.close();
1037
			} catch (IOException e) {
1038
				// ignore
1039
			}
1040
		}
1041
	}
1042
}
944
private void writeSavedIndexNamesFile() {
1043
private void writeSavedIndexNamesFile() {
945
	BufferedWriter writer = null;
1044
	BufferedWriter writer = null;
946
	try {
1045
	try {
(-)src/org/eclipse/jdt/core/tests/model/SearchParticipantTests.java (-2 / +38 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 127-133 Link Here
127
	// Use this static initializer to specify subset for tests
127
	// Use this static initializer to specify subset for tests
128
	// All specified tests which do not belong to the class are skipped...
128
	// All specified tests which do not belong to the class are skipped...
129
	static {
129
	static {
130
//		TESTS_NAMES = new String[] { "testSearch"};
130
	//	TESTS_NAMES = new String[] { "testSearchAfterRestart"};
131
	//	TESTS_NUMBERS = new int[] { 23, 28, 38 };
131
	//	TESTS_NUMBERS = new int[] { 23, 28, 38 };
132
	//	TESTS_RANGE = new int[] { 21, 38 };
132
	//	TESTS_RANGE = new int[] { 21, 38 };
133
	}
133
	}
Lines 314-317 Link Here
314
			"X.test X [X]",
314
			"X.test X [X]",
315
			requestor);
315
			requestor);
316
	}
316
	}
317
	
318
	/*
319
	 * Ensures that a simple search that forwards queries to the default participant works as expected even after restart
320
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=308402
321
	 */
322
	public void testSearchAfterRestart() throws CoreException {
323
		createFile(
324
			"/P/X.test",
325
			"public class X {\n" +
326
			"}"
327
		);
328
329
		// index file
330
		TestSearchParticipant participant = new TestSearchParticipant();
331
		TestSearchDocument document = new TestSearchDocument("/P/X.test", participant);
332
		participant.scheduleDocumentIndexing(document, getIndexLocation());
333
		waitUntilIndexesReady();
334
		try {
335
			Thread.sleep(5000); // wait for the indexes to go into the disk
336
		} catch (InterruptedException e) {
337
			// ignore
338
		}
339
		simulateExit();
340
		simulateRestart();
341
		waitUntilIndexesReady();
342
	
343
		// search for declaration of X
344
		SearchPattern pattern = SearchPattern.createPattern("X", IJavaSearchConstants.DECLARATIONS, IJavaSearchConstants.TYPE, SearchPattern.R_EXACT_MATCH);
345
		IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
346
		SearchRequestor requestor =  new TestResultCollector();
347
		new SearchEngine().search(pattern, new SearchParticipant[] {participant}, scope, requestor, null);
348
		assertSearchResults(
349
			"X.test X [X]",
350
			requestor);
351
		
352
	}
317
}
353
}

Return to bug 308402