1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Cleaning up the Full and Fast indexers. They are pretty close now.

This commit is contained in:
Doug Schaefer 2006-04-24 18:31:33 +00:00
parent 7f420507cd
commit c7708b4e2b
7 changed files with 183 additions and 202 deletions

View file

@ -11,18 +11,20 @@
package org.eclipse.cdt.internal.core.pdom.indexer.fast;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICElementDelta;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMFile;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
@ -32,9 +34,9 @@ class PDOMFastHandleDelta extends PDOMFastIndexerJob {
private final ICElementDelta delta;
private List addedTUs;
private List changedTUs;
private List removedTUs;
private List added = new ArrayList();
private List changed = new ArrayList();
private List removed = new ArrayList();
public PDOMFastHandleDelta(PDOM pdom, ICElementDelta delta) {
super(pdom);
@ -44,56 +46,44 @@ class PDOMFastHandleDelta extends PDOMFastIndexerJob {
protected IStatus run(IProgressMonitor monitor) {
try {
long start = System.currentTimeMillis();
processDelta(delta);
int count = (addedTUs != null ? addedTUs.size() : 0)
+ (changedTUs != null ? changedTUs.size() : 0)
+ (removedTUs != null ? removedTUs.size() : 0);
if (count == 0) {
return Status.OK_STATUS;
}
if (addedTUs != null)
for (Iterator i = addedTUs.iterator(); i.hasNext();) {
if (monitor.isCanceled())
return Status.CANCEL_STATUS;
ITranslationUnit tu = (ITranslationUnit) i.next();
monitor.subTask(String.valueOf(count--) + " files remaining - "
+ tu.getPath().toString());
addTU(tu);
}
if (changedTUs != null)
for (Iterator i = changedTUs.iterator(); i.hasNext();) {
if (monitor.isCanceled())
return Status.CANCEL_STATUS;
ITranslationUnit tu = (ITranslationUnit) i.next();
monitor.subTask(String.valueOf(count--) + " files remaining - "
+ tu.getPath().toString());
int count = changed.size() + added.size() + removed.size();
if (count > 0) {
monitor.beginTask("Indexing", count);
Iterator i = changed.iterator();
while (i.hasNext()) {
ITranslationUnit tu = (ITranslationUnit)i.next();
monitor.subTask(tu.getElementName());
changeTU(tu);
monitor.worked(1);
}
if (removedTUs != null)
for (Iterator i = removedTUs.iterator(); i.hasNext();) {
if (monitor.isCanceled())
return Status.CANCEL_STATUS;
ITranslationUnit tu = (ITranslationUnit) i.next();
monitor.subTask(String.valueOf(count--) + " files remaining - "
+ tu.getPath().toString());
i = added.iterator();
while (i.hasNext()) {
ITranslationUnit tu = (ITranslationUnit)i.next();
monitor.subTask(tu.getElementName());
addTU(tu);
monitor.worked(1);
}
i = removed.iterator();
while (i.hasNext()) {
ITranslationUnit tu = (ITranslationUnit)i.next();
monitor.subTask(tu.getElementName());
removeTU(tu);
monitor.worked(1);
}
String showTimings = Platform.getDebugOption(CCorePlugin.PLUGIN_ID
+ "/debug/pdomtimings"); //$NON-NLS-1$
if (showTimings != null)
if (showTimings.equalsIgnoreCase("true")) //$NON-NLS-1$
System.out
.println("PDOM Update Time: " + (System.currentTimeMillis() - start)); //$NON-NLS-1$
String showTimings = Platform.getDebugOption(CCorePlugin.PLUGIN_ID
+ "/debug/pdomtimings"); //$NON-NLS-1$
if (showTimings != null && showTimings.equalsIgnoreCase("true")) //$NON-NLS-1$
System.out.println("PDOM Full Delta Time: " + (System.currentTimeMillis() - start)); //$NON-NLS-1$
}
return Status.OK_STATUS;
} catch (CoreException e) {
return e.getStatus();
@ -102,79 +92,68 @@ class PDOMFastHandleDelta extends PDOMFastIndexerJob {
}
}
private void processDelta(ICElementDelta delta) {
ICElement element = delta.getElement();
// If this is a project add skip over to the reindex job
if (element.getElementType() == ICElement.C_PROJECT
&& delta.getKind() == ICElementDelta.ADDED) {
new PDOMFastReindex(pdom).schedule();
return;
}
// process the children first
ICElementDelta[] children = delta.getAffectedChildren();
for (int i = 0; i < children.length; ++i)
processDelta(children[i]);
// what have we got
if (element.getElementType() == ICElement.C_UNIT) {
ITranslationUnit tu = (ITranslationUnit) element;
if (tu.isWorkingCopy())
// Don't care about working copies either
return;
switch (delta.getKind()) {
case ICElementDelta.ADDED:
if (addedTUs == null)
addedTUs = new LinkedList();
addedTUs.add(element);
break;
case ICElementDelta.CHANGED:
if (changedTUs == null)
changedTUs = new LinkedList();
changedTUs.add(element);
break;
case ICElementDelta.REMOVED:
if (removedTUs == null)
removedTUs = new LinkedList();
removedTUs.add(element);
break;
protected void processDelta(ICElementDelta delta) throws CoreException {
int flags = delta.getFlags();
if ((flags & ICElementDelta.F_CHILDREN) != 0) {
ICElementDelta[] children = delta.getAffectedChildren();
for (int i = 0; i < children.length; ++i) {
processDelta(children[i]);
}
}
}
protected void changeTU(ITranslationUnit tu) throws InterruptedException, CoreException {
ILanguage language = tu.getLanguage();
if (language == null)
return;
// get the AST in a "Fast" way
IASTTranslationUnit ast = language.getASTTranslationUnit(tu,
ILanguage.AST_USE_INDEX |
ILanguage.AST_SKIP_INDEXED_HEADERS |
ILanguage.AST_SKIP_IF_NO_BUILD_INFO);
if (ast == null)
return;
pdom.acquireWriteLock();
try {
pdom.removeSymbols(tu);
pdom.addSymbols(language, ast);
} finally {
pdom.releaseWriteLock();
ICElement element = delta.getElement();
switch (element.getElementType()) {
case ICElement.C_UNIT:
ITranslationUnit tu = (ITranslationUnit)element;
switch (delta.getKind()) {
case ICElementDelta.CHANGED:
if ((flags & ICElementDelta.F_CONTENT) != 0)
changed.add(tu);
break;
case ICElementDelta.ADDED:
if (!tu.isWorkingCopy())
added.add(tu);
break;
case ICElementDelta.REMOVED:
if (!tu.isWorkingCopy())
removed.add(tu);
break;
}
break;
}
}
protected void removeTU(ITranslationUnit tu) throws InterruptedException, CoreException {
protected void changeTU(ITranslationUnit tu) throws CoreException, InterruptedException {
IASTTranslationUnit ast = parse(tu);
if (ast == null)
return;
pdom.acquireWriteLock();
try {
pdom.removeSymbols(tu);
// TODO delete the file itself from the database
// the removeSymbols only removes the names in the file
// Remove the old symbols in the tu
IPath path = ((IFile)tu.getResource()).getLocation();
PDOMFile file = pdom.getFile(path);
if (file != null)
file.clear();
// Add the new symbols
pdom.addSymbols(tu.getLanguage(), ast);
} finally {
pdom.releaseWriteLock();
}
}
protected void removeTU(ITranslationUnit tu) throws CoreException, InterruptedException {
pdom.acquireWriteLock();
try {
IPath path = ((IFile)tu.getResource()).getLocation();
PDOMFile file = pdom.getFile(path);
if (file != null)
file.clear();
} finally {
pdom.releaseWriteLock();
}
}
}

View file

@ -24,19 +24,18 @@ import org.eclipse.core.runtime.CoreException;
*/
public class PDOMFastIndexer implements IPDOMIndexer {
private PDOM pdom;
private IPDOM pdom;
public void setPDOM(IPDOM pdom) {
if (pdom instanceof PDOM)
this.pdom = (PDOM)pdom;
this.pdom = pdom;
}
public void handleDelta(ICElementDelta delta) {
new PDOMFastHandleDelta(pdom, delta).schedule();
new PDOMFastHandleDelta((PDOM)pdom, delta).schedule();
}
public void reindex() throws CoreException {
new PDOMFastReindex(pdom).schedule();
new PDOMFastReindex((PDOM)pdom).schedule();
}
}

View file

@ -33,22 +33,26 @@ public abstract class PDOMFastIndexerJob extends Job {
setRule(CCorePlugin.getPDOMManager().getIndexerSchedulingRule());
}
protected void addTU(ITranslationUnit tu) throws InterruptedException, CoreException {
protected IASTTranslationUnit parse(ITranslationUnit tu) throws CoreException {
ILanguage language = tu.getLanguage();
if (language == null)
return;
return null;
// get the AST in a "Fast" way
IASTTranslationUnit ast = language.getASTTranslationUnit(tu,
return language.getASTTranslationUnit(tu,
ILanguage.AST_USE_INDEX
| ILanguage.AST_SKIP_INDEXED_HEADERS
| ILanguage.AST_SKIP_IF_NO_BUILD_INFO);
}
protected void addTU(ITranslationUnit tu) throws InterruptedException, CoreException {
IASTTranslationUnit ast = parse(tu);
if (ast == null)
return;
pdom.acquireWriteLock();
try {
pdom.addSymbols(language, ast);
pdom.addSymbols(tu.getLanguage(), ast);
} finally {
pdom.releaseWriteLock();
}

View file

@ -11,24 +11,17 @@
package org.eclipse.cdt.internal.core.pdom.indexer.fast;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICElementVisitor;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentType;
/**
* @author Doug Schaefer
@ -40,67 +33,82 @@ public class PDOMFastReindex extends PDOMFastIndexerJob {
super(pdom);
}
protected IStatus run(IProgressMonitor monitor) {
protected IStatus run(final IProgressMonitor monitor) {
try {
long start = System.currentTimeMillis();
// First clear out the DB
pdom.clear();
// Get a count of all the elements that we'll be visiting for the monitor
final int[] count = { 0 };
pdom.getProject().accept(new ICElementVisitor() {
public boolean visit(ICElement element) throws CoreException {
switch (element.getElementType()) {
case ICElement.C_UNIT:
++count[0];
return false;
case ICElement.C_CCONTAINER:
case ICElement.C_PROJECT:
return true;
}
return false;
}
});
monitor.beginTask("Indexing", count[0]);
// First index all the source files (i.e. not headers)
final List addedSources = new ArrayList();
pdom.getProject().getProject().accept(new IResourceProxyVisitor() {
public boolean visit(IResourceProxy proxy) throws CoreException {
if (proxy.getType() == IResource.FILE) {
String fileName = proxy.getName();
IContentType contentType = Platform.getContentTypeManager().findContentTypeFor(fileName);
if (contentType == null)
return true;
String contentTypeId = contentType.getId();
if (CCorePlugin.CONTENT_TYPE_CXXSOURCE.equals(contentTypeId)
|| CCorePlugin.CONTENT_TYPE_CSOURCE.equals(contentTypeId)) {
addedSources.add((ITranslationUnit)CoreModel.getDefault().create((IFile)proxy.requestResource()));
pdom.getProject().accept(new ICElementVisitor() {
public boolean visit(ICElement element) throws CoreException {
switch (element.getElementType()) {
case ICElement.C_UNIT:
ITranslationUnit tu = (ITranslationUnit)element;
if (tu.isSourceUnit()) {
monitor.subTask(tu.getElementName());
try {
addTU(tu);
} catch (InterruptedException e) {
throw new CoreException(Status.CANCEL_STATUS);
}
monitor.worked(1);
}
return false;
} else {
case ICElement.C_CCONTAINER:
case ICElement.C_PROJECT:
return true;
}
return false;
}
}, 0);
});
for (Iterator i = addedSources.iterator(); i.hasNext();)
addTU((ITranslationUnit)i.next());
// Now add in the header files but only if they aren't already indexed
final List addedHeaders = new ArrayList();
pdom.getProject().getProject().accept(new IResourceProxyVisitor() {
public boolean visit(IResourceProxy proxy) throws CoreException {
if (proxy.getType() == IResource.FILE) {
String fileName = proxy.getName();
IContentType contentType = Platform.getContentTypeManager().findContentTypeFor(fileName);
if (contentType == null)
return true;
String contentTypeId = contentType.getId();
if (CCorePlugin.CONTENT_TYPE_CXXHEADER.equals(contentTypeId)
|| CCorePlugin.CONTENT_TYPE_CHEADER.equals(contentTypeId)) {
IFile rfile = (IFile)proxy.requestResource();
pdom.getProject().accept(new ICElementVisitor() {
public boolean visit(ICElement element) throws CoreException {
switch (element.getElementType()) {
case ICElement.C_UNIT:
ITranslationUnit tu = (ITranslationUnit)element;
if (tu.isHeaderUnit()) {
IFile rfile = (IFile)tu.getUnderlyingResource();
String filename = rfile.getLocation().toOSString();
if (pdom.getFile(filename) == null)
addedHeaders.add((ITranslationUnit)CoreModel.getDefault().create(rfile));
if (pdom.getFile(filename) == null) {
monitor.subTask(tu.getElementName());
try {
addTU(tu);
} catch (InterruptedException e) {
throw new CoreException(Status.CANCEL_STATUS);
}
}
monitor.worked(1);
}
return false;
} else {
case ICElement.C_CCONTAINER:
case ICElement.C_PROJECT:
return true;
}
return false;
}
}, 0);
for (Iterator i = addedHeaders.iterator(); i.hasNext();)
addTU((ITranslationUnit)i.next());
});
String showTimings = Platform.getDebugOption(CCorePlugin.PLUGIN_ID
+ "/debug/pdomtimings"); //$NON-NLS-1$
@ -110,8 +118,6 @@ public class PDOMFastReindex extends PDOMFastIndexerJob {
return Status.OK_STATUS;
} catch (CoreException e) {
return e.getStatus();
} catch (InterruptedException e) {
return Status.CANCEL_STATUS;
}
}

View file

@ -172,11 +172,13 @@ public class PDOMFullHandleDelta extends PDOMFullIndexerJob {
if (ast == null)
return;
// Remove the old symbols in the tu and all the headers
pdom.acquireWriteLock();
try {
// Remove the old symbols in the tu and all the headers
removeTU(tu);
IPath path = ((IFile)tu.getResource()).getLocation();
PDOMFile file = pdom.getFile(path);
if (file != null)
file.clear();
IASTPreprocessorIncludeStatement[] includes = ast.getIncludeDirectives();
for (int i = 0; i < includes.length; ++i) {
@ -193,24 +195,16 @@ public class PDOMFullHandleDelta extends PDOMFullIndexerJob {
}
}
protected void addTU(ITranslationUnit tu) throws CoreException, InterruptedException {
IASTTranslationUnit ast = parse(tu);
if (ast == null)
return;
protected void removeTU(ITranslationUnit tu) throws CoreException, InterruptedException {
pdom.acquireWriteLock();
try {
pdom.addSymbols(tu.getLanguage(), ast);
IPath path = ((IFile)tu.getResource()).getLocation();
PDOMFile file = pdom.getFile(path);
if (file != null)
file.clear();
} finally {
pdom.releaseWriteLock();
}
}
protected void removeTU(ITranslationUnit tu) throws CoreException, InterruptedException {
IPath path = ((IFile)tu.getResource()).getLocation();
PDOMFile file = pdom.getFile(path);
if (file != null)
file.clear();
}
}

View file

@ -41,5 +41,18 @@ public abstract class PDOMFullIndexerJob extends Job {
// get the AST in the "Full" way, i.e. don't skip anything.
return language.getASTTranslationUnit(tu, ILanguage.AST_SKIP_IF_NO_BUILD_INFO);
}
protected void addTU(ITranslationUnit tu) throws InterruptedException, CoreException {
IASTTranslationUnit ast = parse(tu);
if (ast == null)
return;
pdom.acquireWriteLock();
try {
pdom.addSymbols(tu.getLanguage(), ast);
} finally {
pdom.releaseWriteLock();
}
}
}

View file

@ -12,7 +12,6 @@
package org.eclipse.cdt.internal.core.pdom.indexer.full;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICElementVisitor;
import org.eclipse.cdt.core.model.ITranslationUnit;
@ -122,17 +121,4 @@ public class PDOMFullReindex extends PDOMFullIndexerJob {
}
}
protected void addTU(ITranslationUnit tu) throws InterruptedException, CoreException {
IASTTranslationUnit ast = parse(tu);
if (ast == null)
return;
pdom.acquireWriteLock();
try {
pdom.addSymbols(tu.getLanguage(), ast);
} finally {
pdom.releaseWriteLock();
}
}
}