make inputId re-mappable

GitOrigin-RevId: 100394759bf4f26b431c2b3d88ca72c8df9ac2b3
This commit is contained in:
Dmitry Batkovich
2019-07-29 14:01:31 +03:00
committed by intellij-monorepo-bot
parent 91736d5d2a
commit 658dd3b912
5 changed files with 34 additions and 16 deletions
@@ -273,7 +273,8 @@ public class CompilerReferenceIndex<Input> {
16 * 1024,
false,
true,
readOnly) {
readOnly,
null) {
@Override
public void checkCanceled() {
//TODO
@@ -58,7 +58,7 @@ public final class VfsAwareMapIndexStorage<Key, Value> extends MapIndexStorage<K
final int cacheSize,
final boolean readOnly
) throws IOException {
super(storageFile, keyDescriptor, valueExternalizer, cacheSize, false, true, readOnly);
super(storageFile, keyDescriptor, valueExternalizer, cacheSize, false, true, readOnly, null);
myBuildKeyHashToVirtualFileMapping = false;
}
@@ -68,7 +68,7 @@ public final class VfsAwareMapIndexStorage<Key, Value> extends MapIndexStorage<K
final int cacheSize,
boolean keyIsUniqueForIndexedFile,
boolean buildKeyHashToVirtualFileMapping) throws IOException {
super(storageFile, keyDescriptor, valueExternalizer, cacheSize, keyIsUniqueForIndexedFile, false, false);
super(storageFile, keyDescriptor, valueExternalizer, cacheSize, keyIsUniqueForIndexedFile, false, false, null);
myBuildKeyHashToVirtualFileMapping = buildKeyHashToVirtualFileMapping;
initMapAndCache();
}
@@ -17,6 +17,7 @@ package com.intellij.util.indexing.impl;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.IntIntFunction;
import com.intellij.util.Processor;
import com.intellij.util.containers.SLRUCache;
import com.intellij.util.indexing.StorageException;
@@ -44,13 +45,14 @@ public abstract class MapIndexStorage<Key, Value> implements IndexStorage<Key, V
private final DataExternalizer<Value> myDataExternalizer;
private final boolean myKeyIsUniqueForIndexedFile;
private final boolean myReadOnly;
@Nullable private final IntIntFunction myInputRemapping;
protected MapIndexStorage(@NotNull File storageFile,
@NotNull KeyDescriptor<Key> keyDescriptor,
@NotNull DataExternalizer<Value> valueExternalizer,
final int cacheSize,
boolean keyIsUniqueForIndexedFile) throws IOException {
this(storageFile, keyDescriptor, valueExternalizer, cacheSize, keyIsUniqueForIndexedFile, true, false);
this(storageFile, keyDescriptor, valueExternalizer, cacheSize, keyIsUniqueForIndexedFile, true, false, null);
}
protected MapIndexStorage(@NotNull File storageFile,
@@ -59,13 +61,20 @@ public abstract class MapIndexStorage<Key, Value> implements IndexStorage<Key, V
final int cacheSize,
boolean keyIsUniqueForIndexedFile,
boolean initialize,
boolean readOnly) throws IOException {
boolean readOnly,
@Nullable IntIntFunction inputRemapping) throws IOException {
myBaseStorageFile = storageFile;
myKeyDescriptor = keyDescriptor;
myCacheSize = cacheSize;
myDataExternalizer = valueExternalizer;
myKeyIsUniqueForIndexedFile = keyIsUniqueForIndexedFile;
myReadOnly = readOnly;
if (inputRemapping != null) {
LOG.assertTrue(myReadOnly, "input remapping allowed only for read-only storage");
} else {
inputRemapping = IntIntFunction.IDENTITY;
}
myInputRemapping = inputRemapping;
if (initialize) initMapAndCache();
}
@@ -83,7 +92,7 @@ public abstract class MapIndexStorage<Key, Value> implements IndexStorage<Key, V
PersistentHashMapValueStorage.CreationTimeOptions.HAS_NO_CHUNKS.set(Boolean.TRUE);
}
try {
map = new ValueContainerMap<Key, Value>(getStorageFile(), myKeyDescriptor, myDataExternalizer, myKeyIsUniqueForIndexedFile) {
map = new ValueContainerMap<Key, Value>(getStorageFile(), myKeyDescriptor, myDataExternalizer, myKeyIsUniqueForIndexedFile, myInputRemapping) {
@Override
protected boolean isReadOnly() {
return myReadOnly;
@@ -17,6 +17,7 @@
package com.intellij.util.indexing.impl;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.IntIntFunction;
import com.intellij.util.SmartList;
import com.intellij.util.containers.EmptyIterator;
import com.intellij.util.indexing.ValueContainer;
@@ -454,14 +455,16 @@ class ValueContainerImpl<Value> extends UpdatableValueContainer<Value> implement
static final int NUMBER_OF_VALUES_THRESHOLD = 20;
public void readFrom(DataInputStream stream, DataExternalizer<? extends Value> externalizer) throws IOException {
public void readFrom(@NotNull DataInputStream stream,
@NotNull DataExternalizer<? extends Value> externalizer,
@NotNull IntIntFunction inputRemapping) throws IOException {
FileId2ValueMapping<Value> mapping = null;
while (stream.available() > 0) {
final int valueCount = DataInputOutputUtil.readINT(stream);
if (valueCount < 0) {
// ChangeTrackingValueContainer marked inputId as invalidated, see ChangeTrackingValueContainer.saveTo
final int inputId = -valueCount;
final int inputId = inputRemapping.fun(-valueCount);
if (mapping == null && size() > NUMBER_OF_VALUES_THRESHOLD) { // avoid O(NumberOfValues)
mapping = new FileId2ValueMapping<>(this);
@@ -484,7 +487,7 @@ class ValueContainerImpl<Value> extends UpdatableValueContainer<Value> implement
if (idCountOrSingleValue > 0) {
addValue(idCountOrSingleValue, value);
if (mapping != null) mapping.associateFileIdToValue(idCountOrSingleValue, value);
if (mapping != null) mapping.associateFileIdToValue(inputRemapping.fun(idCountOrSingleValue), value);
} else {
idCountOrSingleValue = -idCountOrSingleValue;
ChangeBufferingList changeBufferingList = ensureFileSetCapacityForValue(value, idCountOrSingleValue);
@@ -492,9 +495,10 @@ class ValueContainerImpl<Value> extends UpdatableValueContainer<Value> implement
for (int i = 0; i < idCountOrSingleValue; i++) {
final int id = DataInputOutputUtil.readINT(stream);
if (changeBufferingList != null) changeBufferingList.add(prev + id);
else addValue(prev + id, value);
if (mapping != null) mapping.associateFileIdToValue(prev + id, value);
int remappedInputId = inputRemapping.fun(prev + id);
if (changeBufferingList != null) changeBufferingList.add(remappedInputId);
else addValue(remappedInputId, value);
if (mapping != null) mapping.associateFileIdToValue(remappedInputId, value);
prev += id;
}
}
@@ -15,6 +15,7 @@
*/
package com.intellij.util.indexing.impl;
import com.intellij.util.IntIntFunction;
import com.intellij.util.io.DataExternalizer;
import com.intellij.util.io.KeyDescriptor;
import com.intellij.util.io.PersistentHashMap;
@@ -32,8 +33,9 @@ class ValueContainerMap<Key, Value> extends PersistentHashMap<Key, UpdatableValu
ValueContainerMap(@NotNull final File file,
@NotNull KeyDescriptor<Key> keyKeyDescriptor,
@NotNull DataExternalizer<Value> valueExternalizer,
boolean keyIsUniqueForIndexedFile) throws IOException {
super(file, keyKeyDescriptor, new ValueContainerExternalizer<>(valueExternalizer));
boolean keyIsUniqueForIndexedFile,
@NotNull IntIntFunction inputRemapping) throws IOException {
super(file, keyKeyDescriptor, new ValueContainerExternalizer<>(valueExternalizer, inputRemapping));
myValueExternalizer = valueExternalizer;
myKeyIsUniqueForIndexedFile = keyIsUniqueForIndexedFile;
}
@@ -68,9 +70,11 @@ class ValueContainerMap<Key, Value> extends PersistentHashMap<Key, UpdatableValu
private static final class ValueContainerExternalizer<T> implements DataExternalizer<UpdatableValueContainer<T>> {
@NotNull private final DataExternalizer<T> myValueExternalizer;
@NotNull private final IntIntFunction myInputRemapping;
private ValueContainerExternalizer(@NotNull DataExternalizer<T> valueExternalizer) {
private ValueContainerExternalizer(@NotNull DataExternalizer<T> valueExternalizer, @NotNull IntIntFunction inputRemapping) {
myValueExternalizer = valueExternalizer;
myInputRemapping = inputRemapping;
}
@Override
@@ -83,7 +87,7 @@ class ValueContainerMap<Key, Value> extends PersistentHashMap<Key, UpdatableValu
public UpdatableValueContainer<T> read(@NotNull final DataInput in) throws IOException {
final ValueContainerImpl<T> valueContainer = new ValueContainerImpl<>();
valueContainer.readFrom((DataInputStream)in, myValueExternalizer);
valueContainer.readFrom((DataInputStream)in, myValueExternalizer, myInputRemapping);
return valueContainer;
}
}