mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-26 19:06:24 +07:00
stub hierarchy: serialize hashes instead of names for faster reading, smaller index data and a very unlikely collision probability
This commit is contained in:
+58
-17
@@ -16,6 +16,7 @@
|
||||
package com.intellij.psi.impl.java.stubs.index;
|
||||
|
||||
import com.intellij.psi.impl.java.stubs.hierarchy.IndexTree;
|
||||
import com.intellij.util.BitUtil;
|
||||
import com.intellij.util.io.DataExternalizer;
|
||||
import com.intellij.util.io.DataInputOutputUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -24,19 +25,34 @@ import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.intellij.psi.impl.java.stubs.index.JavaUnitDescriptor.ImportFlags.*;
|
||||
|
||||
public class JavaUnitDescriptor implements DataExternalizer<IndexTree.Unit> {
|
||||
public static final JavaUnitDescriptor INSTANCE = new JavaUnitDescriptor();
|
||||
|
||||
private static void writeIntArray(DataOutput out, int[] array) throws IOException {
|
||||
DataInputOutputUtil.writeINT(out, array.length);
|
||||
for (int i : array) {
|
||||
out.writeInt(i);
|
||||
}
|
||||
}
|
||||
private static int[] readIntArray(DataInput in) throws IOException {
|
||||
int length = DataInputOutputUtil.readINT(in);
|
||||
int[] result = new int[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
result[i] = in.readInt();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(@NotNull DataOutput out, IndexTree.Unit value) throws IOException {
|
||||
out.writeUTF(value.myPackageId);
|
||||
writeIntArray(out, value.myPackageName);
|
||||
out.writeByte(value.myUnitType);
|
||||
if (value.myUnitType != IndexTree.BYTECODE) {
|
||||
DataInputOutputUtil.writeINT(out, value.imports.length);
|
||||
for (IndexTree.Import anImport : value.imports) {
|
||||
out.writeUTF(anImport.myFullname);
|
||||
out.writeBoolean(anImport.myStaticImport);
|
||||
out.writeBoolean(anImport.myOnDemand);
|
||||
writeImport(out, anImport);
|
||||
}
|
||||
}
|
||||
// class Declaration
|
||||
@@ -47,12 +63,12 @@ public class JavaUnitDescriptor implements DataExternalizer<IndexTree.Unit> {
|
||||
}
|
||||
|
||||
private void saveClassDecl(@NotNull DataOutput out, IndexTree.ClassDecl value) throws IOException {
|
||||
out.writeInt(value.myStubId);
|
||||
DataInputOutputUtil.writeINT(out, value.myStubId);
|
||||
DataInputOutputUtil.writeINT(out, value.myMods);
|
||||
out.writeUTF(value.myName == null ? "" : value.myName);
|
||||
out.writeInt(value.myName);
|
||||
DataInputOutputUtil.writeINT(out, value.mySupers.length);
|
||||
for (String aSuper : value.mySupers) {
|
||||
out.writeUTF(aSuper);
|
||||
for (int[] aSuper : value.mySupers) {
|
||||
writeIntArray(out, aSuper);
|
||||
}
|
||||
DataInputOutputUtil.writeINT(out, value.myDecls.length);
|
||||
for (IndexTree.Decl def : value.myDecls) {
|
||||
@@ -76,13 +92,13 @@ public class JavaUnitDescriptor implements DataExternalizer<IndexTree.Unit> {
|
||||
|
||||
@Override
|
||||
public IndexTree.Unit read(@NotNull DataInput in) throws IOException {
|
||||
String pid = in.readUTF();
|
||||
int[] pid = readIntArray(in);
|
||||
byte type = in.readByte();
|
||||
IndexTree.Import[] imports = IndexTree.Import.EMPTY_ARRAY;
|
||||
if (type != IndexTree.BYTECODE) {
|
||||
imports = new IndexTree.Import[DataInputOutputUtil.readINT(in)];
|
||||
for (int i = 0; i < imports.length; i++) {
|
||||
imports[i] = new IndexTree.Import(in.readUTF(), in.readBoolean(), in.readBoolean(), null);
|
||||
imports[i] = readImport(in);
|
||||
}
|
||||
}
|
||||
IndexTree.ClassDecl[] classes = new IndexTree.ClassDecl[DataInputOutputUtil.readINT(in)];
|
||||
@@ -93,15 +109,12 @@ public class JavaUnitDescriptor implements DataExternalizer<IndexTree.Unit> {
|
||||
}
|
||||
|
||||
private IndexTree.ClassDecl readClassDecl(DataInput in) throws IOException {
|
||||
int stubId = in.readInt();
|
||||
int stubId = DataInputOutputUtil.readINT(in);
|
||||
int mods = DataInputOutputUtil.readINT(in);
|
||||
String name = in.readUTF();
|
||||
if (name.isEmpty()) {
|
||||
name = null;
|
||||
}
|
||||
String[] supers = new String[DataInputOutputUtil.readINT(in)];
|
||||
int name = in.readInt();
|
||||
int[][] supers = new int[DataInputOutputUtil.readINT(in)][];
|
||||
for (int i = 0; i < supers.length; i++) {
|
||||
supers[i] = in.readUTF();
|
||||
supers[i] = readIntArray(in);
|
||||
}
|
||||
IndexTree.Decl[] decls = new IndexTree.Decl[DataInputOutputUtil.readINT(in)];
|
||||
for (int i = 0; i < decls.length; i++) {
|
||||
@@ -124,4 +137,32 @@ public class JavaUnitDescriptor implements DataExternalizer<IndexTree.Unit> {
|
||||
}
|
||||
}
|
||||
|
||||
interface ImportFlags {
|
||||
int IS_STATIC = 1;
|
||||
int IS_ON_DEMAND = 2;
|
||||
int HAS_ALIAS = 4;
|
||||
}
|
||||
|
||||
private static void writeImport(@NotNull DataOutput out, IndexTree.Import anImport) throws IOException {
|
||||
writeIntArray(out, anImport.myFullname);
|
||||
boolean hasAlias = anImport.myAlias != 0;
|
||||
int flags = 0;
|
||||
flags = BitUtil.set(flags, IS_STATIC, anImport.myStaticImport);
|
||||
flags = BitUtil.set(flags, IS_ON_DEMAND, anImport.myOnDemand);
|
||||
flags = BitUtil.set(flags, HAS_ALIAS, hasAlias);
|
||||
out.writeByte(flags);
|
||||
if (hasAlias) {
|
||||
out.writeInt(anImport.myAlias);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static IndexTree.Import readImport(@NotNull DataInput in) throws IOException {
|
||||
int[] fullname = readIntArray(in);
|
||||
int flags = in.readByte();
|
||||
return new IndexTree.Import(fullname,
|
||||
BitUtil.isSet(flags, IS_STATIC), BitUtil.isSet(flags, IS_ON_DEMAND),
|
||||
BitUtil.isSet(flags, HAS_ALIAS) ? in.readInt() : 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user