From 2ee67f47615a73273fe86a45bb4d9a38dd313cc0 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Mon, 10 Jun 2019 16:54:45 +0300 Subject: [PATCH] extract generalized StubForwardIndexExternalizer GitOrigin-RevId: ff7ad119c9c571119d6b58cbdb8bd66439425c32 --- .../stubs/StubForwardIndexExternalizer.java | 75 +++++++++++++++++++ .../StubUpdatingForwardIndexAccessor.java | 62 ++++----------- 2 files changed, 90 insertions(+), 47 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/psi/stubs/StubForwardIndexExternalizer.java diff --git a/platform/lang-impl/src/com/intellij/psi/stubs/StubForwardIndexExternalizer.java b/platform/lang-impl/src/com/intellij/psi/stubs/StubForwardIndexExternalizer.java new file mode 100644 index 000000000000..1455fe1048c1 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/psi/stubs/StubForwardIndexExternalizer.java @@ -0,0 +1,75 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.psi.stubs; + +import com.intellij.openapi.progress.ProgressManager; +import com.intellij.util.indexing.ID; +import com.intellij.util.io.DataExternalizer; +import com.intellij.util.io.DataInputOutputUtil; +import gnu.trove.THashMap; +import org.jetbrains.annotations.NotNull; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.Collections; +import java.util.Map; +import java.util.Set; + +abstract class StubForwardIndexExternalizer implements DataExternalizer { + private volatile boolean myEnsuredStubElementTypesLoaded; + + protected abstract StubKeySerializationState createStubIndexKeySerializationState(@NotNull DataOutput out, @NotNull Set set) throws IOException; + + protected abstract void writeStubIndexKey(@NotNull DataOutput out, @NotNull StubIndexKey key, StubKeySerializationState state) throws IOException; + + protected abstract StubKeySerializationState createStubIndexKeySerializationState(@NotNull DataInput input, int stubIndexKeyCount) throws IOException; + + protected abstract ID readStubIndexKey(@NotNull DataInput input, StubKeySerializationState stubKeySerializationState) throws IOException; + + @Override + public void save(@NotNull DataOutput out, IndexedStubs indexedStubs) throws IOException { + byte[] hash = indexedStubs.getStubTreeHash(); + DataInputOutputUtil.writeINT(out, hash.length); + out.write(hash); + Map> stubIndicesValueMap = indexedStubs.getStubIndicesValueMap(); + DataInputOutputUtil.writeINT(out, stubIndicesValueMap.size()); + if (!stubIndicesValueMap.isEmpty()) { + StubKeySerializationState stubKeySerializationState = createStubIndexKeySerializationState(out, stubIndicesValueMap.keySet()); + + StubIndexImpl stubIndex = (StubIndexImpl)StubIndex.getInstance(); + for (StubIndexKey stubIndexKey : stubIndicesValueMap.keySet()) { + writeStubIndexKey(out, stubIndexKey, stubKeySerializationState); + Map map = stubIndicesValueMap.get(stubIndexKey); + stubIndex.serializeIndexValue(out, stubIndexKey, map); + } + } + } + + @Override + public IndexedStubs read(@NotNull DataInput in) throws IOException { + byte[] hash = new byte[DataInputOutputUtil.readINT(in)]; + in.readFully(hash); + if (!myEnsuredStubElementTypesLoaded) { + ProgressManager.getInstance().executeNonCancelableSection(() -> { + SerializationManager.getInstance().initSerializers(); + StubIndexImpl.initExtensions(); + }); + myEnsuredStubElementTypesLoaded = true; + } + int stubIndicesValueMapSize = DataInputOutputUtil.readINT(in); + if (stubIndicesValueMapSize > 0) { + THashMap> stubIndicesValueMap = new THashMap<>(stubIndicesValueMapSize); + StubIndexImpl stubIndex = (StubIndexImpl)StubIndex.getInstance(); + StubKeySerializationState stubKeySerializationState = createStubIndexKeySerializationState(in, stubIndicesValueMapSize); + for (int i = 0; i < stubIndicesValueMapSize; ++i) { + ID indexKey = (ID)readStubIndexKey(in, stubKeySerializationState); + if (indexKey instanceof StubIndexKey) { // indexKey can be ID in case of removed index + StubIndexKey stubIndexKey = (StubIndexKey)indexKey; + stubIndicesValueMap.put(stubIndexKey, stubIndex.deserializeIndexValue(in, stubIndexKey)); + } + } + return new IndexedStubs(hash, stubIndicesValueMap); + } + return new IndexedStubs(hash, Collections.emptyMap()); + } +} diff --git a/platform/lang-impl/src/com/intellij/psi/stubs/StubUpdatingForwardIndexAccessor.java b/platform/lang-impl/src/com/intellij/psi/stubs/StubUpdatingForwardIndexAccessor.java index 704b288ab53a..50a58c309abf 100644 --- a/platform/lang-impl/src/com/intellij/psi/stubs/StubUpdatingForwardIndexAccessor.java +++ b/platform/lang-impl/src/com/intellij/psi/stubs/StubUpdatingForwardIndexAccessor.java @@ -1,73 +1,41 @@ // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.psi.stubs; -import com.intellij.openapi.progress.ProgressManager; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.indexing.ID; import com.intellij.util.indexing.impl.InputData; import com.intellij.util.indexing.impl.InputDataDiffBuilder; import com.intellij.util.indexing.impl.forward.AbstractForwardIndexAccessor; -import com.intellij.util.io.DataExternalizer; import com.intellij.util.io.DataInputOutputUtil; -import gnu.trove.THashMap; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; -import java.util.Collections; import java.util.Map; +import java.util.Set; class StubUpdatingForwardIndexAccessor extends AbstractForwardIndexAccessor { - StubUpdatingForwardIndexAccessor() {super(new DataExternalizer() { - private volatile boolean myEnsuredStubElementTypesLoaded; - + StubUpdatingForwardIndexAccessor() {super(new StubForwardIndexExternalizer() { @Override - public void save(@NotNull DataOutput out, IndexedStubs indexedStubs) throws IOException { - byte[] hash = indexedStubs.getStubTreeHash(); - DataInputOutputUtil.writeINT(out, hash.length); - out.write(hash); - Map> stubIndicesValueMap = indexedStubs.getStubIndicesValueMap(); - DataInputOutputUtil.writeINT(out, stubIndicesValueMap.size()); - if (!stubIndicesValueMap.isEmpty()) { - StubIndexImpl stubIndex = (StubIndexImpl)StubIndex.getInstance(); - - for (StubIndexKey stubIndexKey : stubIndicesValueMap.keySet()) { - DataInputOutputUtil.writeINT(out, stubIndexKey.getUniqueId()); - Map map = stubIndicesValueMap.get(stubIndexKey); - stubIndex.serializeIndexValue(out, stubIndexKey, map); - } - } + protected void writeStubIndexKey(@NotNull DataOutput out, @NotNull StubIndexKey key, Void aVoid) throws IOException { + DataInputOutputUtil.writeINT(out, key.getUniqueId()); } @Override - public IndexedStubs read(@NotNull DataInput in) throws IOException { - byte[] hash = new byte[DataInputOutputUtil.readINT(in)]; - in.readFully(hash); - if (!myEnsuredStubElementTypesLoaded) { - ProgressManager.getInstance().executeNonCancelableSection(() -> { - SerializationManager.getInstance().initSerializers(); - StubIndexImpl.initExtensions(); - }); - myEnsuredStubElementTypesLoaded = true; - } - int stubIndicesValueMapSize = DataInputOutputUtil.readINT(in); - if (stubIndicesValueMapSize > 0) { - THashMap> stubIndicesValueMap = new THashMap<>(stubIndicesValueMapSize); - StubIndexImpl stubIndex = (StubIndexImpl)StubIndex.getInstance(); + protected Void createStubIndexKeySerializationState(@NotNull DataOutput out, @NotNull Set set) { + return null; + } - for (int i = 0; i < stubIndicesValueMapSize; ++i) { - int stubIndexId = DataInputOutputUtil.readINT(in); - ID indexKey = (ID)ID.findById(stubIndexId); - if (indexKey instanceof StubIndexKey) { // indexKey can be ID in case of removed index - StubIndexKey stubIndexKey = (StubIndexKey)indexKey; - stubIndicesValueMap.put(stubIndexKey, stubIndex.deserializeIndexValue(in, stubIndexKey)); - } - } - return new IndexedStubs(hash, stubIndicesValueMap); - } - return new IndexedStubs(hash, Collections.emptyMap()); + @Override + protected ID readStubIndexKey(@NotNull DataInput input, Void aVoid) throws IOException { + return ID.findById(DataInputOutputUtil.readINT(input)); + } + + @Override + protected Void createStubIndexKeySerializationState(@NotNull DataInput input, int stubIndexKeyCount) { + return null; } });}