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.equals(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 (+94 lines)
Lines 53-63 Link Here
53
	// key = indexLocation path, value = index state integer
53
	// key = indexLocation path, value = index state integer
54
	private SimpleLookupTable indexStates = null;
54
	private SimpleLookupTable indexStates = null;
55
	private File savedIndexNamesFile = new File(getSavedIndexesDirectory(), "savedIndexNames.txt"); //$NON-NLS-1$
55
	private File savedIndexNamesFile = new File(getSavedIndexesDirectory(), "savedIndexNames.txt"); //$NON-NLS-1$
56
	private File participantIndexNamesFile = new File(getSavedIndexesDirectory(), "participantsIndexNames.txt"); //$NON-NLS-1$
56
	private boolean javaLikeNamesChanged = true;
57
	private boolean javaLikeNamesChanged = true;
57
	public static final Integer SAVED_STATE = new Integer(0);
58
	public static final Integer SAVED_STATE = new Integer(0);
58
	public static final Integer UPDATING_STATE = new Integer(1);
59
	public static final Integer UPDATING_STATE = new Integer(1);
59
	public static final Integer UNKNOWN_STATE = new Integer(2);
60
	public static final Integer UNKNOWN_STATE = new Integer(2);
60
	public static final Integer REBUILDING_STATE = new Integer(3);
61
	public static final Integer REBUILDING_STATE = new Integer(3);
62
	
63
	// search participants who register indexes with the index manager
64
	private SimpleLookupTable participantsContainers = null;
65
	private boolean participantUpdated = false;
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
	}
396
	return (IPath)this.participantsContainers.get(indexLocation);
397
}
373
private IPath getJavaPluginWorkingLocation() {
398
private IPath getJavaPluginWorkingLocation() {
374
	if (this.javaPluginLocation != null) return this.javaPluginLocation;
399
	if (this.javaPluginLocation != null) return this.javaPluginLocation;
375
400
Lines 661-666 Link Here
661
		for (int i = 0; i < count; i++)
686
		for (int i = 0; i < count; i++)
662
			this.indexes.removeKey(locations[i]);
687
			this.indexes.removeKey(locations[i]);
663
		removeIndexesState(locations);
688
		removeIndexesState(locations);
689
		if (this.participantsContainers != null && this.participantsContainers.get(path.toOSString()) != null) {
690
			this.participantsContainers.removeKey(path.toOSString());	
691
			writeParticipantsIndexNamesFile();
692
		}
664
	}
693
	}
665
}
694
}
666
/**
695
/**
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
	SimpleLookupTable containers = new SimpleLookupTable(3);
899
	try {
900
		char[] participantIndexNames = org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(this.participantIndexNamesFile, null);
901
		if (participantIndexNames.length > 0) {
902
			char[][] names = CharOperation.splitOn('\n', participantIndexNames);
903
			if (names.length >= 3) {
904
				// First line is DiskIndex signature  (see writeParticipantsIndexNamesFile())
905
				if (DiskIndex.SIGNATURE.equals(new String(names[0]))) {					
906
					for (int i = 1, l = names.length-1 ; i < l ; i+=2) {
907
						containers.put(new Path(new String(names[i])), new Path(new String(names[i+1])));
908
					}
909
				}				
910
			}
911
		}	
912
	} catch (IOException ignored) {
913
		if (VERBOSE)
914
			Util.verbose("Failed to read participant index file names"); //$NON-NLS-1$
915
	}
916
	this.participantsContainers = containers;
917
	return;
918
}
864
private synchronized void removeIndexesState(IPath[] locations) {
919
private synchronized void removeIndexesState(IPath[] locations) {
865
	getIndexStates(); // ensure the states are initialized
920
	getIndexStates(); // ensure the states are initialized
866
	int length = locations.length;
921
	int length = locations.length;
Lines 907-912 Link Here
907
	}
962
	}
908
963
909
}
964
}
965
public void updateParticipant(IPath indexLocation, IPath containerPath) {
966
	if (this.participantsContainers == null) {
967
		readParticipantsIndexNamesFile();
968
	} 
969
	if (this.participantsContainers.get(indexLocation) == null) {
970
		this.participantsContainers.put(indexLocation, containerPath);
971
		this.participantUpdated  = true;
972
	}
973
}
910
private void writeJavaLikeNamesFile() {
974
private void writeJavaLikeNamesFile() {
911
	BufferedWriter writer = null;
975
	BufferedWriter writer = null;
912
	String pathName = getJavaPluginWorkingLocation().toOSString();
976
	String pathName = getJavaPluginWorkingLocation().toOSString();
Lines 941-946 Link Here
941
		}
1005
		}
942
	}
1006
	}
943
}
1007
}
1008
private void writeParticipantsIndexNamesFile() {
1009
	BufferedWriter writer = null;
1010
	try {
1011
		writer = new BufferedWriter(new FileWriter(this.participantIndexNamesFile));
1012
		writer.write(DiskIndex.SIGNATURE);
1013
		writer.write('\n');
1014
		Object[] indexFiles = this.participantsContainers.keyTable;
1015
		Object[] containers = this.participantsContainers.valueTable;
1016
		for (int i = 0, l = indexFiles.length; i < l; i++) {
1017
			IPath indexFile = (IPath)indexFiles[i];
1018
			if (indexFile != null) {
1019
				writer.write(indexFile.toOSString());
1020
				writer.write('\n');
1021
				writer.write(((IPath)containers[i]).toOSString());
1022
				writer.write('\n');
1023
			}
1024
		}
1025
	} catch (IOException ignored) {
1026
		if (VERBOSE)
1027
			Util.verbose("Failed to write participant index file names", System.err); //$NON-NLS-1$
1028
	} finally {
1029
		if (writer != null) {
1030
			try {
1031
				writer.close();
1032
			} catch (IOException e) {
1033
				// ignore
1034
			}
1035
		}
1036
	}
1037
}
944
private void writeSavedIndexNamesFile() {
1038
private void writeSavedIndexNamesFile() {
945
	BufferedWriter writer = null;
1039
	BufferedWriter writer = null;
946
	try {
1040
	try {
(-)src/org/eclipse/jdt/core/tests/model/SearchParticipantTests.java (-1 / +82 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 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
	}
353
	
354
	/*
355
	 * Ensures that a simple search that forwards queries to the default participant works as expected even after restart
356
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=308402
357
	 */
358
	public void testSearchAfterRemoveAndRestart() throws CoreException {
359
		createFile(
360
			"/P/X.test",
361
			"public class X {\n" +
362
			"}"
363
		);
364
365
		// index file
366
		TestSearchParticipant participant = new TestSearchParticipant();
367
		TestSearchDocument document = new TestSearchDocument("/P/X.test", participant);
368
		participant.scheduleDocumentIndexing(document, getIndexLocation());
369
		waitUntilIndexesReady();
370
		
371
		// search for declaration of X
372
		SearchPattern pattern = SearchPattern.createPattern("X", IJavaSearchConstants.DECLARATIONS, IJavaSearchConstants.TYPE, SearchPattern.R_EXACT_MATCH);
373
		IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
374
		SearchRequestor requestor =  new TestResultCollector();
375
		new SearchEngine().search(pattern, new SearchParticipant[] {participant}, scope, requestor, null);
376
		
377
		// remove the index
378
		participant.removeIndex(getIndexLocation());
379
		assertSearchResults(
380
			"X.test X [X]",
381
			requestor);
382
		try {
383
			Thread.sleep(5000); // wait for the indexes to go into the disk
384
		} catch (InterruptedException e) {
385
			// ignore
386
		}
387
		requestor =  new TestResultCollector();
388
		new SearchEngine().search(pattern, new SearchParticipant[] {participant}, scope, requestor, null);
389
		assertSearchResults("", requestor);
390
		
391
		simulateExit();
392
		simulateRestart();
393
		waitUntilIndexesReady();
394
		requestor =  new TestResultCollector();
395
		new SearchEngine().search(pattern, new SearchParticipant[] {participant}, scope, requestor, null);
396
		assertSearchResults("", requestor);	
397
	}
317
}
398
}

Return to bug 308402