From 64581daef9f90cd57696f723d46d1da92f309f11 Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Mon, 29 May 2017 17:48:35 +0200 Subject: [PATCH] ability to specify MapValueType in MapBasedForwardIndex --- .../com/intellij/java/index/StringIndex.java | 6 +-- .../CompilerBackwardReferenceIndex.java | 10 +++-- .../indexing/SharedMapBasedForwardIndex.java | 10 ++--- .../util/indexing/VfsAwareMapReduceIndex.java | 4 +- .../impl/KeyCollectionBasedForwardIndex.java | 38 +++++++++++++++++++ .../indexing/impl/MapBasedForwardIndex.java | 25 ++++++------ 6 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 platform/util/src/com/intellij/util/indexing/impl/KeyCollectionBasedForwardIndex.java diff --git a/java/java-tests/testSrc/com/intellij/java/index/StringIndex.java b/java/java-tests/testSrc/com/intellij/java/index/StringIndex.java index aaca2b6baa16..74f7907115ab 100644 --- a/java/java-tests/testSrc/com/intellij/java/index/StringIndex.java +++ b/java/java-tests/testSrc/com/intellij/java/index/StringIndex.java @@ -21,9 +21,7 @@ import com.intellij.util.indexing.DataIndexer; import com.intellij.util.indexing.IndexExtension; import com.intellij.util.indexing.IndexId; import com.intellij.util.indexing.StorageException; -import com.intellij.util.indexing.impl.IndexStorage; -import com.intellij.util.indexing.impl.MapBasedForwardIndex; -import com.intellij.util.indexing.impl.MapReduceIndex; +import com.intellij.util.indexing.impl.*; import com.intellij.util.io.DataExternalizer; import com.intellij.util.io.EnumeratorStringDescriptor; import com.intellij.util.io.KeyDescriptor; @@ -81,7 +79,7 @@ public class StringIndex { return 0; } }; - myIndex = new MapReduceIndex(extension, storage, new MapBasedForwardIndex(extension) { + myIndex = new MapReduceIndex(extension, storage, new KeyCollectionBasedForwardIndex(extension) { @NotNull @Override public PersistentHashMap> createMap() throws IOException { diff --git a/jps/jps-builders/src/org/jetbrains/jps/backwardRefs/CompilerBackwardReferenceIndex.java b/jps/jps-builders/src/org/jetbrains/jps/backwardRefs/CompilerBackwardReferenceIndex.java index f9e9f0277416..ce3e72498846 100644 --- a/jps/jps-builders/src/org/jetbrains/jps/backwardRefs/CompilerBackwardReferenceIndex.java +++ b/jps/jps-builders/src/org/jetbrains/jps/backwardRefs/CompilerBackwardReferenceIndex.java @@ -33,8 +33,10 @@ import org.jetbrains.jps.builders.storage.BuildDataCorruptedException; import java.io.*; import java.io.DataOutputStream; -import java.util.*; -import java.util.stream.Collectors; +import java.util.Collection; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; public class CompilerBackwardReferenceIndex { private final static Logger LOG = Logger.getInstance(CompilerBackwardReferenceIndex.class); @@ -232,11 +234,11 @@ public class CompilerBackwardReferenceIndex { throws IOException { super(extension, createIndexStorage(extension.getKeyDescriptor(), extension.getValueExternalizer(), extension.getName(), indexDir, readOnly), - readOnly ? null : new MapBasedForwardIndex(extension) { + readOnly ? null : new KeyCollectionBasedForwardIndex(extension) { @NotNull @Override public PersistentHashMap> createMap() throws IOException { - IndexId id = extension.getName(); + IndexId id = getIndexExtension().getName(); return new PersistentHashMap<>(new File(indexDir, id.getName() + ".inputs"), EnumeratorIntegerDescriptor.INSTANCE, new InputIndexDataExternalizer<>(extension.getKeyDescriptor(), diff --git a/platform/lang-impl/src/com/intellij/util/indexing/SharedMapBasedForwardIndex.java b/platform/lang-impl/src/com/intellij/util/indexing/SharedMapBasedForwardIndex.java index fd07f7f87bac..9102411b61c6 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/SharedMapBasedForwardIndex.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/SharedMapBasedForwardIndex.java @@ -27,9 +27,9 @@ import java.util.Map; class SharedMapBasedForwardIndex extends AbstractForwardIndex { private final DataExternalizer> mySnapshotIndexExternalizer; - private final MapBasedForwardIndex myUnderlying; + private final KeyCollectionBasedForwardIndex myUnderlying; - SharedMapBasedForwardIndex(IndexExtension extension, @Nullable MapBasedForwardIndex underlying) { + SharedMapBasedForwardIndex(IndexExtension extension, @Nullable KeyCollectionBasedForwardIndex underlying) { super(extension); myUnderlying = underlying; mySnapshotIndexExternalizer = VfsAwareMapReduceIndex.createInputsIndexExternalizer(extension); @@ -42,7 +42,7 @@ class SharedMapBasedForwardIndex extends AbstractForwardIndex keys = SharedIndicesData.recallFileData(inputId, (ID)myIndexId, mySnapshotIndexExternalizer); if (myUnderlying != null) { - Collection keysFromInputsIndex = myUnderlying.getInputsIndex().get(inputId); + Collection keysFromInputsIndex = myUnderlying.getInput(inputId); if (keys == null && keysFromInputsIndex != null || !DebugAssertions.equals(keysFromInputsIndex, keys, myKeyDescriptor) @@ -58,14 +58,14 @@ class SharedMapBasedForwardIndex extends AbstractForwardIndex(inputId, keys); } - return new CollectionInputDataDiffBuilder<>(inputId, myUnderlying.getInputsIndex().get(inputId)); + return new CollectionInputDataDiffBuilder<>(inputId, myUnderlying.getInput(inputId)); } @Override public void putInputData(int inputId, @NotNull Map data) throws IOException { Collection keySeq = data.keySet(); - if (myUnderlying != null) myUnderlying.putData(inputId, keySeq); + if (myUnderlying != null) myUnderlying.putInputData(inputId, data); if (SharedIndicesData.ourFileSharedIndicesEnabled) { if (keySeq.isEmpty()) keySeq = null; SharedIndicesData.associateFileData(inputId, (ID)myIndexId, keySeq, mySnapshotIndexExternalizer); diff --git a/platform/lang-impl/src/com/intellij/util/indexing/VfsAwareMapReduceIndex.java b/platform/lang-impl/src/com/intellij/util/indexing/VfsAwareMapReduceIndex.java index e61d74273b27..5e6a185c0240 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/VfsAwareMapReduceIndex.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/VfsAwareMapReduceIndex.java @@ -219,12 +219,12 @@ public class VfsAwareMapReduceIndex extends MapReduceIndex backgroundIndex = + KeyCollectionBasedForwardIndex backgroundIndex = !SharedIndicesData.ourFileSharedIndicesEnabled || SharedIndicesData.DO_CHECKS ? new MyForwardIndex<>(indexExtension) : null; return new SharedMapBasedForwardIndex<>(indexExtension, backgroundIndex); } - private static class MyForwardIndex extends MapBasedForwardIndex { + private static class MyForwardIndex extends KeyCollectionBasedForwardIndex { protected MyForwardIndex(IndexExtension indexExtension) throws IOException { super(indexExtension); } diff --git a/platform/util/src/com/intellij/util/indexing/impl/KeyCollectionBasedForwardIndex.java b/platform/util/src/com/intellij/util/indexing/impl/KeyCollectionBasedForwardIndex.java new file mode 100644 index 000000000000..884ee0a6c6dd --- /dev/null +++ b/platform/util/src/com/intellij/util/indexing/impl/KeyCollectionBasedForwardIndex.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.indexing.impl; + +import com.intellij.util.indexing.IndexExtension; + +import java.io.IOException; +import java.util.Collection; +import java.util.Map; + +public abstract class KeyCollectionBasedForwardIndex extends MapBasedForwardIndex> { + protected KeyCollectionBasedForwardIndex(IndexExtension indexExtension) throws IOException { + super(indexExtension); + } + + @Override + protected InputDataDiffBuilder getDiffBuilder(int inputId, Collection keys) throws IOException { + return new CollectionInputDataDiffBuilder(inputId, keys); + } + + @Override + protected Collection convertToMapValueType(int inputId, Map map) throws IOException { + return map.keySet(); + } +} diff --git a/platform/util/src/com/intellij/util/indexing/impl/MapBasedForwardIndex.java b/platform/util/src/com/intellij/util/indexing/impl/MapBasedForwardIndex.java index 0a1b5362042c..e47262b75376 100644 --- a/platform/util/src/com/intellij/util/indexing/impl/MapBasedForwardIndex.java +++ b/platform/util/src/com/intellij/util/indexing/impl/MapBasedForwardIndex.java @@ -22,12 +22,11 @@ import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; -import java.util.Collection; import java.util.Map; -public abstract class MapBasedForwardIndex extends AbstractForwardIndex { +public abstract class MapBasedForwardIndex extends AbstractForwardIndex { @NotNull - private volatile PersistentHashMap> myInputsIndex; + private volatile PersistentHashMap myInputsIndex; protected MapBasedForwardIndex(IndexExtension indexExtension) throws IOException { super(indexExtension); @@ -35,27 +34,25 @@ public abstract class MapBasedForwardIndex extends AbstractForwardIn } @NotNull - public abstract PersistentHashMap> createMap() throws IOException; + public abstract PersistentHashMap createMap() throws IOException; @NotNull @Override public InputDataDiffBuilder getDiffBuilder(final int inputId) throws IOException { - return new CollectionInputDataDiffBuilder(inputId, myInputsIndex.get(inputId)); + return getDiffBuilder(inputId, getInput(inputId)); } - @NotNull - public PersistentHashMap> getInputsIndex() { - return myInputsIndex; + protected abstract InputDataDiffBuilder getDiffBuilder(int inputId, MapValueType mapValueType) throws IOException; + protected abstract MapValueType convertToMapValueType(int inputId, Map map) throws IOException; + + public MapValueType getInput(int inputId) throws IOException { + return myInputsIndex.get(inputId); } @Override public void putInputData(int inputId, @NotNull Map data) throws IOException { - putData(inputId, data.keySet()); - } - - public void putData(int inputId, Collection keyCollection) throws IOException { - if (keyCollection.size() > 0) { - myInputsIndex.put(inputId, keyCollection); + if (!data.isEmpty()) { + myInputsIndex.put(inputId, convertToMapValueType(inputId, data)); } else { myInputsIndex.remove(inputId);