From e1a20e1a7dd14dfa50b0c35dc0b369aab2f0b405 Mon Sep 17 00:00:00 2001 From: John Camelon Date: Mon, 11 Aug 2003 13:42:55 +0000 Subject: [PATCH] Patch for Bogdan Gheorghe Put in Indexer shut down which cleans up the .metadata directory of any suspicious looking index files Put in CSearchScope changes (in both UI and core) to enable working set searches --- .../core/indexer/tests/IndexManagerTests.java | 44 ++++++++++++- core/org.eclipse.cdt.core/index/ChangeLog | 3 + .../cdt/internal/core/search/Util.java | 6 -- .../core/search/indexing/IndexManager.java | 32 +++++----- .../internal/core/model/CModelManager.java | 4 ++ core/org.eclipse.cdt.core/search/ChangeLog | 7 +++ .../eclipse/cdt/core/search/SearchEngine.java | 28 ++++++++- .../internal/core/search/CSearchScope.java | 63 ++++++++++++++----- core/org.eclipse.cdt.ui/ChangeLog | 4 ++ .../ui/search/CSearchScopeFactory.java | 62 ++++++++++++++++-- 10 files changed, 209 insertions(+), 44 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/indexer/org/eclipse/cdt/core/indexer/tests/IndexManagerTests.java b/core/org.eclipse.cdt.core.tests/indexer/org/eclipse/cdt/core/indexer/tests/IndexManagerTests.java index bf20749ecc4..2f268bc4dd7 100644 --- a/core/org.eclipse.cdt.core.tests/indexer/org/eclipse/cdt/core/indexer/tests/IndexManagerTests.java +++ b/core/org.eclipse.cdt.core.tests/indexer/org/eclipse/cdt/core/indexer/tests/IndexManagerTests.java @@ -10,7 +10,10 @@ */ package org.eclipse.cdt.core.indexer.tests; +import java.io.File; import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; @@ -85,7 +88,7 @@ public class IndexManagerTests extends TestCase { public static Test suite() { TestSuite suite = new TestSuite(); - suite.addTest(new IndexManagerTests("testRefs")); + suite.addTest(new IndexManagerTests("testIndexShutdown")); return suite; //return new TestSuite(IndexManagerTests.class); } @@ -420,6 +423,45 @@ public class IndexManagerTests extends TestCase { } } + public void testIndexShutdown() throws Exception{ + //Add a new file to the project, give it some time to index + importFile("reftest.cpp","resources/indexer/reftest.cpp"); + //Enable indexing on the created project + //By doing this, we force the Index Manager to indexAll() + indexManager = CCorePlugin.getDefault().getCoreModel().getIndexManager(); + indexManager.setEnabled(testProject,true); + Thread.sleep(TIMEOUT); + //Make sure project got added to index + IPath testProjectPath = testProject.getFullPath(); + IIndex ind = indexManager.getIndex(testProjectPath,true,true); + assertTrue("Index exists for project",ind != null); + + //Create an empty index file + String badIndexFile = CCorePlugin.getDefault().getStateLocation().append("badIndex.index").toOSString(); + FileWriter writer = null; + try { + writer = new FileWriter(badIndexFile); + writer.flush(); + writer.close(); + } + catch (IOException e){} + + File indexesDirectory = new File(CCorePlugin.getDefault().getStateLocation().toOSString()); + + //This should get rid of the empty index file from the metadata and + //remove the index from the indexes (since its .index file is missing) + indexManager.shutdown(); + + File[] indexesFiles = indexesDirectory.listFiles(); + if (indexesFiles != null) { + for (int i = 0, indexesFilesLength = indexesFiles.length; i < indexesFilesLength; i++) { + if(indexesFiles[i].getName().equals("badIndex.index")){ + fail("Shutdown did not delete .index file"); + } + } + } + } + public void testDependencyTree() throws Exception{ //Add a file to the project IFile depTest = importFile("DepTest.cpp","resources/dependency/DepTest.cpp"); diff --git a/core/org.eclipse.cdt.core/index/ChangeLog b/core/org.eclipse.cdt.core/index/ChangeLog index 37e2f511d5a..247f8ea7652 100644 --- a/core/org.eclipse.cdt.core/index/ChangeLog +++ b/core/org.eclipse.cdt.core/index/ChangeLog @@ -1,3 +1,6 @@ +2003-08-07 Bogdan Gheorghe + - Added shutdown cleanup routine in IndexManager + 2003-07-28 Andrew Niefer - added support for '?' wildcards in AbstractIndexer.bestPrefix diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/Util.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/Util.java index 95867561c2a..2dde51b261b 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/Util.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/Util.java @@ -318,12 +318,6 @@ public class Util { } else if (existingExternalFiles.contains(externalFile)) { return externalFile; } else { - //TODO: BOG do we need to add something here? ANSWER YES! - /* - if (JavaModelManager.ZIP_ACCESS_VERBOSE) { - System.out.println("(" + Thread.currentThread() + ") [JavaModel.getTarget(...)] Checking existence of " + path.toString()); //$NON-NLS-1$ //$NON-NLS-2$ - } - */ if (externalFile.exists()) { // cache external file existingExternalFiles.add(externalFile); diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/indexing/IndexManager.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/indexing/IndexManager.java index 9d333d4ea3e..02dbf50883b 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/indexing/IndexManager.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/indexing/IndexManager.java @@ -30,6 +30,8 @@ import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.QualifiedName; import org.eclipse.cdt.internal.core.search.processing.JobManager; import org.eclipse.cdt.internal.core.search.processing.IJob; +import org.eclipse.cdt.internal.core.search.CWorkspaceScope; +import org.eclipse.cdt.internal.core.search.IndexSelector; import org.eclipse.cdt.internal.core.search.SimpleLookupTable; import org.eclipse.cdt.internal.core.search.CharOperation; import org.eclipse.cdt.internal.core.index.IIndex; @@ -51,12 +53,12 @@ public class IndexManager extends JobManager implements IIndexConstants { /* need to save ? */ private boolean needToSave = false; private static final CRC32 checksumCalculator = new CRC32(); - private IPath javaPluginLocation = null; + private IPath cCorePluginLocation = null; /* can only replace a current state if its less than the new one */ private SimpleLookupTable indexStates = null; private File savedIndexNamesFile = - new File(getJavaPluginWorkingLocation().append("savedIndexNames.txt").toOSString()); //$NON-NLS-1$ + new File(getCCorePluginWorkingLocation().append("savedIndexNames.txt").toOSString()); //$NON-NLS-1$ public static Integer SAVED_STATE = new Integer(0); public static Integer UPDATING_STATE = new Integer(1); public static Integer UNKNOWN_STATE = new Integer(2); @@ -126,7 +128,7 @@ public class IndexManager extends JobManager implements IIndexConstants { String fileName = Long.toString(checksumCalculator.getValue()) + ".index"; //$NON-NLS-1$ if (VERBOSE) JobManager.verbose("-> index name for " + pathString + " is " + fileName); //$NON-NLS-1$ //$NON-NLS-2$ - name = getJavaPluginWorkingLocation().append(fileName).toOSString(); + name = getCCorePluginWorkingLocation().append(fileName).toOSString(); indexNames.put(path, name); } return name; @@ -216,10 +218,10 @@ public class IndexManager extends JobManager implements IIndexConstants { return this.indexStates; } - private IPath getJavaPluginWorkingLocation() { - if (this.javaPluginLocation != null) return this.javaPluginLocation; + private IPath getCCorePluginWorkingLocation() { + if (this.cCorePluginLocation != null) return this.cCorePluginLocation; - return this.javaPluginLocation = CCorePlugin.getDefault().getStateLocation(); + return this.cCorePluginLocation = CCorePlugin.getDefault().getStateLocation(); } /** * Index access is controlled through a read-write monitor so as @@ -430,7 +432,7 @@ public class IndexManager extends JobManager implements IIndexConstants { this.indexStates = null; } this.indexNames = new SimpleLookupTable(); - this.javaPluginLocation = null; + this.cCorePluginLocation = null; } public void saveIndex(IIndex index) throws IOException { @@ -492,26 +494,28 @@ public class IndexManager extends JobManager implements IIndexConstants { public void shutdown() { if (VERBOSE) JobManager.verbose("Shutdown"); //$NON-NLS-1$ -//TODO: BOG Put in Shutdown -/* - IndexSelector indexSelector = new IndexSelector(new JavaWorkspaceScope(), null, false, this); + //Get index entries for all projects in the workspace, store their absolute paths + IndexSelector indexSelector = new IndexSelector(new CWorkspaceScope(), null, false, this); IIndex[] selectedIndexes = indexSelector.getIndexes(); SimpleLookupTable knownPaths = new SimpleLookupTable(); for (int i = 0, max = selectedIndexes.length; i < max; i++) { String path = selectedIndexes[i].getIndexFile().getAbsolutePath(); knownPaths.put(path, path); } - + //Any index entries that are in the index state must have a corresponding + //path entry - if not they are removed from the saved indexes file if (indexStates != null) { Object[] indexNames = indexStates.keyTable; for (int i = 0, l = indexNames.length; i < l; i++) { String key = (String) indexNames[i]; - if (key != null && !knownPaths.containsKey(key)) + if (key != null && !knownPaths.containsKey(key)) //here is an index that is in t updateIndexState(key, null); } } - File indexesDirectory = new File(getJavaPluginWorkingLocation().toOSString()); + //Clean up the .metadata folder - if there are any files in the directory that + //are not associated to an index we delete them + File indexesDirectory = new File(getCCorePluginWorkingLocation().toOSString()); if (indexesDirectory.isDirectory()) { File[] indexesFiles = indexesDirectory.listFiles(); if (indexesFiles != null) { @@ -525,7 +529,7 @@ public class IndexManager extends JobManager implements IIndexConstants { } } } -*/ + super.shutdown(); } diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelManager.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelManager.java index 285b3054c4d..82cd701a33d 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelManager.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelManager.java @@ -758,6 +758,10 @@ public class CModelManager implements IResourceChangeListener { * */ public void shutdown() { + if (this.fDeltaProcessor.indexManager != null){ // no more indexing + this.fDeltaProcessor.indexManager.shutdown(); + } + // Do any shutdown of services. ResourcesPlugin.getWorkspace().removeResourceChangeListener(factory); diff --git a/core/org.eclipse.cdt.core/search/ChangeLog b/core/org.eclipse.cdt.core/search/ChangeLog index b163170197d..7bbacffe6e1 100644 --- a/core/org.eclipse.cdt.core/search/ChangeLog +++ b/core/org.eclipse.cdt.core/search/ChangeLog @@ -1,3 +1,10 @@ +2003-08-08 Bogdan Gheorghe + - Added CreateSearchScope to create a search scope out of + CElements + - Filled out CSearchScope to enable: + - adding a project to scope, include referenced projects + - adding individual CElements to scope + 2003-08-06 Andrew Niefer - Create OrPattern which matches for search if any of its constituent patterns matches - modified MatchLocator to support the OrPattern diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/SearchEngine.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/SearchEngine.java index 21e99914e7c..d5f6e221694 100644 --- a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/SearchEngine.java +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/SearchEngine.java @@ -13,9 +13,13 @@ */ package org.eclipse.cdt.core.search; +import java.util.HashSet; + import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.internal.core.model.CModelManager; import org.eclipse.cdt.internal.core.model.IWorkingCopy; +import org.eclipse.cdt.internal.core.search.CSearchScope; import org.eclipse.cdt.internal.core.search.CWorkspaceScope; import org.eclipse.cdt.internal.core.search.PathCollector; import org.eclipse.cdt.internal.core.search.PatternSearchJob; @@ -64,13 +68,31 @@ public class SearchEngine implements ICSearchConstants{ return new CWorkspaceScope(); } + public static ICSearchScope createCSearchScope(ICElement[] elements) { + return createCSearchScope(elements, true); + } /** * @param objects * @return */ - public static ICSearchScope createCSearchScope(Object[] objects) { - // TODO Auto-generated method stub - return null; + public static ICSearchScope createCSearchScope(ICElement[] elements, boolean includeReferencedProjects) { + CSearchScope scope = new CSearchScope(); + HashSet visitedProjects = new HashSet(2); + for (int i = 0, length = elements.length; i < length; i++) { + ICElement element = elements[i]; + if (element != null) { + try { + if (element instanceof ICProject) { + scope.add((ICProject)element, includeReferencedProjects, visitedProjects); + } else { + scope.add(element); + } + } catch (Exception e) { + // ignore + } + } + } + return scope; } public static ICSearchPattern createSearchPattern( String stringPattern, SearchFor searchFor, LimitTo limitTo, boolean isCaseSensitive){ diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CSearchScope.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CSearchScope.java index 436725c3742..925932f01f9 100644 --- a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CSearchScope.java +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CSearchScope.java @@ -16,15 +16,16 @@ import java.util.HashSet; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.IMember; import org.eclipse.cdt.core.search.ICSearchScope; import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; public class CSearchScope implements ICSearchScope { private ArrayList elements; - /* The paths of the resources in this search scope*/ private IPath[] paths; private boolean[] pathWithSubFolders; @@ -63,6 +64,24 @@ public class CSearchScope implements ICSearchScope { if (!project.isAccessible() || !visitedProjects.add(project)) return; this.addEnclosingProject(project.getFullPath()); + ICElement[] projChildren = cProject.getChildren(); + for (int i=0; i< projChildren.length; i++){ + this.add(projChildren[i]); + } + + if (includesPrereqProjects){ + IProject[] refProjects=null; + try { + refProjects = project.getReferencedProjects(); + } catch (CoreException e) { + } + for (int i=0; i