Eclipse JDT的Index机制

1. Index是什么?

JDT的Index目的是为了快速的查找变量,方法,构造函数,引用等位置的机制。

如下图,当我们在JDT中使用我框起来的功能时,都是Index机制在为我们服务。

2. Index保存在哪里?

Index的保存路径一般在: .metadata/.plugins/org.eclipse.jdt.core

以下是我的机器上的输出:

/home/test/runtime-EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core
[test@ht191w org.eclipse.jdt.core]$ ls -alh
total 11M
drwxrwxr-x 2 test users 4.0K Nov 15 17:16 .
drwxrwxr-x 20 test users 4.0K Nov 15 12:48 ..
-rw-rw-r– 1 test users 169K Nov 15 17:16 1189544457.index
-rw-rw-r– 1 test users 270K Nov 15 17:16 1266648557.index
-rw-rw-r– 1 test users 9.4M Nov 15 17:16 1346760340.index
-rw-rw-r– 1 test users 91K Nov 15 17:16 1481681510.index
-rw-rw-r– 1 test users 25 Nov 15 17:01 1501317170.index
-rw-rw-r– 1 test users 174K Nov 15 17:16 1580850431.index
-rw-rw-r– 1 test users 387 Nov 15 17:16 2266944301.index
-rw-rw-r– 1 test users 38K Nov 15 17:16 2268464613.index
-rw-rw-r– 1 test users 90K Nov 15 17:16 3421611052.index
-rw-rw-r– 1 test users 53K Nov 15 17:16 4283481814.index
-rw-rw-r– 1 test users 4.2K Nov 15 17:16 680003802.index
-rw-rw-r– 1 test users 22K Nov 15 17:16 690271857.index
-rw-rw-r– 1 test users 870 Nov 15 18:19 externalLibsTimeStamps
-rw-rw-r– 1 test users 4 Nov 15 18:19 invalidArchivesCache
-rw-rw-r– 1 test users 4 Nov 15 10:36 javaLikeNames.txt
-rw-rw-r– 1 test users 782 Nov 15 18:19 nonChainingJarsCache
-rw-rw-r– 1 test users 304 Nov 15 17:16 savedIndexNames.txt
-rw-rw-r– 1 test users 21K Nov 15 18:19 variablesAndContainers.dat

3. Index机制的基本描述

3.1 Index机制的参与类

org.eclipse.jdt.internal.core.search.indexing.IndexManager
用来管理Index,创建,删除,重建,查找等操作

org.eclipse.jdt.internal.core.index.Index 用来操作Index,它包含了

org.eclipse.jdt.internal.core.index.MemoryIndex
和org.eclipse.jdt.internal.core.index.DiskIndex

org.eclipse.jdt.internal.core.search.indexing.IndexRequest
用来给IndexManager发送请求来建立Index

下面是它的子类:

org.eclipse.jdt.internal.core.search.processing.IJob是IndexManager处理请求的所有基类:

org.eclipse.jdt.internal.core.search.PatternSearchJob用来处理所有查找的对象,它的一个注意成员就是SearchPattern,以下是它们继承等级:

3.2 IndexManager的创建

当jdt.core plugins启动时,
org.eclipse.jdt.core.JavaCore将创建IndexManager类,来处理对Index的各种请求

如下图高亮的就是Java Indexing线程:

3.3 Index的建立和使用

通过org.eclipse.jdt.internal.core.search.indexing.IndexManager的

public synchronized IndexLocation computeIndexLocation(IPath containerPath) {
    IndexLocation indexLocation = (IndexLocation) this.indexLocations.get(containerPath);
    if (indexLocation == null) {
        String pathString = containerPath.toOSString();
        CRC32 checksumCalculator = new CRC32();
        checksumCalculator.update(pathString.getBytes());
        String fileName = Long.toString(checksumCalculator.getValue()) + ".index"; //$NON-NLS-1$
        if (VERBOSE)
            Util.verbose("-> index name for " + pathString + " is " + fileName); //$NON-NLS-1$ //$NON-NLS-2$
        // to share the indexLocation between the indexLocations and indexStates tables, get the key from the indexStates table
        indexLocation = (IndexLocation) getIndexStates().getKey(new FileIndexLocation(new File(getSavedIndexesDirectory(), fileName)));
        this.indexLocations.put(containerPath, indexLocation);
    }
    return indexLocation;
}

来计算index文件的名字
然后检查对应的index文件是否存在,如果存在,则直接使用,如果不存在的话,通过

private void rebuildIndex(IndexLocation indexLocation, IPath containerPath) {
    Object target = JavaModel.getTarget(containerPath, true);
    if (target == null) return;

    if (VERBOSE)
        Util.verbose("-> request to rebuild index: "+indexLocation+" path: "+containerPath); //$NON-NLS-1$ //$NON-NLS-2$

    updateIndexState(indexLocation, REBUILDING_STATE);
    IndexRequest request = null;
    if (target instanceof IProject) {
        IProject p = (IProject) target;
        if (JavaProject.hasJavaNature(p))
            request = new IndexAllProject(p, this);
    } else if (target instanceof IFolder) {
        request = new IndexBinaryFolder((IFolder) target, this);
    } else if (target instanceof IFile) {
        request = new AddJarFileToIndex((IFile) target, null, this);
    } else if (target instanceof File) {
        request = new AddJarFileToIndex(containerPath, null, this);
    }
    if (request != null)
        request(request);
}

发送IndexRequest来重建index。 不管是Java工程还是Jar文件,最终都分为两类,一类是source文件,一类是class文件,如下图:

IndexManager来处理收到的IndexRequest来生成Index。

比如下面的堆栈,是处理jar文件并生成Index的调用:

以下是其中一次调用所传递的参数:

_category [t, y, p, e, D, e, c, l]
key [a, c, c, e, s, s, i, b, i, l, i, t, y, /, c, o, m, ., s, u, n, ., a, c,
c, e, s, s, i, b, i, l, i, t, y, ., i, n, t, e, r, n, a, l, ., r, e, s, o, u,
r, c, e, s, /, /, 1,
documentName com/sun/accessibility/internal/resources/accessibility.class _

生成的Index最后会保存道磁盘上,便于以后使用,不需要再次创建,Index文件的header当前是INDEX VERSION 1.126。

以下是DiskIndex中重要的两个变量的Variable截图:

如果我们收到org.eclipse.jdt.core.search.SearchRequestor,我们只需要根据对应的catogory读取文件中对应的位置(根据categoryOffsets和categoryEnds),

然后就可以快速的查找到对应请求的关系,比如变量a的调用的地方,方法b的声明的地方,方法c的父类声明在哪里等。

下面是IndexManager对一个Java工程进行完Index后,所有的Index:

file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/690271857.index - >
Index for
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/localedata.jar
file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/680003802.index ->
Index for /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/dnsns.jar
file:/home/test/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/3421611052.index ->
Index for
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/sunpkcs11.jar
file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/1580850431.index ->
Index for /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/charsets.jar
file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/1346760340.index ->
Index for /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/rt.jar
file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/2268464613.index ->
Index for /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/jce.jar
file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/4283481814.index ->
Index for
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/sunjce_provider.jar
file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/1481681510.index ->
Index for /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/jsse.jar
file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/1266648557.index ->
Index for /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/rhino.jar
file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/1189544457.index ->
Index for /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/gnome-
java-bridge.jar
file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/1501317170.index ->
Index for /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/resources.jar
file:/home/jialiang/runtime-
EclipseApplication/.metadata/.plugins/org.eclipse.jdt.core/2266944301.index ->
Index for /dd

Index的使用都是通过调用IndexManager的performConcurrentJob来完成的,以下是它的调用堆栈: