CompositeDataIndexer (add file types to api methods)

GitOrigin-RevId: 0ebf8973ab3283734d0d4c666586cc986f1359b6
This commit is contained in:
Dmitry Batkovich
2019-12-17 14:06:19 +00:00
committed by intellij-monorepo-bot
parent 4b12ff67d9
commit d3798ea0ef
5 changed files with 58 additions and 36 deletions
@@ -8,9 +8,10 @@ import com.intellij.openapi.vfs.VirtualFileWithId;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import com.intellij.testFramework.fixtures.TempDirTestFixture;
import com.intellij.testFramework.fixtures.impl.TempDirTestFixtureImpl;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.indexing.CompositeDataIndexer;
import com.intellij.util.indexing.FileContent;
import com.intellij.util.indexing.IndexedFile;
import com.intellij.util.indexing.IndexedFileImpl;
import com.intellij.util.io.EnumeratorStringDescriptor;
import com.intellij.util.io.KeyDescriptor;
import org.jetbrains.annotations.NotNull;
@@ -18,9 +19,7 @@ import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
public class PersistentSubIndexerVersionEnumeratorTest extends LightJavaCodeInsightFixtureTestCase {
private TempDirTestFixture myDirTestFixture;
@@ -103,13 +102,8 @@ public class PersistentSubIndexerVersionEnumeratorTest extends LightJavaCodeInsi
private static class MyPerFileIndexExtension implements CompositeDataIndexer<String, String, MyIndexFileAttribute, String> {
@Nullable
@Override
public MyIndexFileAttribute calculateSubIndexer(@NotNull VirtualFile content) {
return content.getUserData(ATTRIBUTE_KEY);
}
@Override
public boolean requiresContentForSubIndexerEvaluation(@NotNull VirtualFile content) {
return false;
public MyIndexFileAttribute calculateSubIndexer(@NotNull IndexedFile file) {
return file.getFile().getUserData(ATTRIBUTE_KEY);
}
@NotNull
@@ -161,7 +155,7 @@ public class PersistentSubIndexerVersionEnumeratorTest extends LightJavaCodeInsi
VirtualFile file = file(attribute);
file.putUserData(ATTRIBUTE_KEY, attribute);
try {
myMap.persistIndexedState(((VirtualFileWithId)file).getId(), file);
myMap.setIndexedState(((VirtualFileWithId) file).getId(), new IndexedFileImpl(file));
}
catch (IOException e) {
LOG.error(e);
@@ -175,7 +169,7 @@ public class PersistentSubIndexerVersionEnumeratorTest extends LightJavaCodeInsi
VirtualFile file = file(attribute);
file.putUserData(ATTRIBUTE_KEY, attribute);
try {
return myMap.isIndexed(((VirtualFileWithId)file).getId(), file);
return myMap.isIndexed(((VirtualFileWithId)file).getId(), new IndexedFileImpl(file));
}
catch (IOException e) {
LOG.error(e);
@@ -12,6 +12,10 @@ public class IndexedFileImpl extends UserDataHolderBase implements IndexedFile {
protected final String myFileName;
protected final FileType myFileType;
public IndexedFileImpl(@NotNull VirtualFile file) {
this(file, file.getFileType());
}
public IndexedFileImpl(@NotNull VirtualFile file, @NotNull FileType type) {
myFile = file;
myFileName = file.getName();
@@ -1,24 +1,25 @@
// 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.util.indexing;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.io.KeyDescriptor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
public interface CompositeDataIndexer<K, V, SubIndexerType, SubIndexerVersion> extends DataIndexer<K, V, FileContent> {
/**
* @return null if file is not acceptable for indexing
*/
@Nullable
SubIndexerType calculateSubIndexer(@NotNull VirtualFile content);
SubIndexerType calculateSubIndexer(@NotNull IndexedFile file);
/**
* determine should we load content to provide sub-indexer
*/
default boolean requiresContentForSubIndexerEvaluation(@NotNull VirtualFile content) {
default boolean requiresContentForSubIndexerEvaluation(@NotNull IndexedFile file) {
return false;
}
@@ -29,13 +30,16 @@ public interface CompositeDataIndexer<K, V, SubIndexerType, SubIndexerVersion> e
@NotNull
SubIndexerVersion getSubIndexerVersion(@NotNull SubIndexerType subIndexerType);
/**
* SubIndexerVersion descriptor must depend only on corresponding index version, should be read even SubIndexerType is not available anymore
*/
@NotNull
KeyDescriptor<SubIndexerVersion> getSubIndexerVersionDescriptor();
@NotNull
@Override
default Map<K, V> map(@NotNull FileContent inputData) {
SubIndexerType subIndexerType = calculateSubIndexer(inputData.getFile());
SubIndexerType subIndexerType = calculateSubIndexer(inputData);
if (subIndexerType == null) return Collections.emptyMap();
return map(inputData, subIndexerType);
}
@@ -0,0 +1,28 @@
// 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.util.indexing;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
public abstract class SingleEntryCompositeIndexer<V, SubIndexerType, SubIndexerVersion> extends SingleEntryIndexer<V> implements CompositeDataIndexer<Integer, V, SubIndexerType, SubIndexerVersion> {
protected SingleEntryCompositeIndexer(boolean acceptNullValues) {
super(acceptNullValues);
}
@NotNull
@Override
public final Map<Integer, V> map(@NotNull FileContent inputData, @NotNull SubIndexerType indexerType) {
throw new AssertionError();
}
@Nullable
@Override
protected V computeValue(@NotNull FileContent inputData) {
return computeValue(inputData, calculateSubIndexer(inputData));
}
@Nullable
protected abstract V computeValue(@NotNull FileContent inputData, @NotNull SubIndexerType indexerType);
}
@@ -1,13 +1,9 @@
// 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.util.indexing.impl.perFileVersion;
import com.intellij.concurrency.ConcurrentCollectionFactory;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.FileAttribute;
import com.intellij.openapi.vfs.newvfs.persistent.FSRecords;
import com.intellij.util.containers.ConcurrentFactoryMap;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.indexing.*;
import com.intellij.util.io.DataInputOutputUtil;
import gnu.trove.THashMap;
@@ -22,11 +18,9 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
class PersistentSubIndexerRetriever<SubIndexerType, SubIndexerVersion> {
public class PersistentSubIndexerRetriever<SubIndexerType, SubIndexerVersion> {
private static final String INDEXED_VERSIONS = "indexed_versions";
@NotNull
private final Map<SubIndexerType, SubIndexerVersion> myVersionOwnerMap;
@NotNull
private final PersistentSubIndexerVersionEnumerator<SubIndexerVersion> myPersistentVersionEnumerator;
@NotNull
@@ -34,7 +28,7 @@ class PersistentSubIndexerRetriever<SubIndexerType, SubIndexerVersion> {
@NotNull
private final CompositeDataIndexer<?, ?, SubIndexerType, SubIndexerVersion> myIndexer;
PersistentSubIndexerRetriever(@NotNull ID<?, ?> id,
public PersistentSubIndexerRetriever(@NotNull ID<?, ?> id,
int indexVersion,
@NotNull CompositeDataIndexer<?, ?, SubIndexerType, SubIndexerVersion> indexer) throws IOException {
this(IndexInfrastructure.getIndexRootDir(id), id.getName(), indexVersion, indexer);
@@ -48,23 +42,20 @@ class PersistentSubIndexerRetriever<SubIndexerType, SubIndexerVersion> {
Path versionMapRoot = root.toPath().resolve(versionMapRoot());
myFileAttribute = getFileAttribute(indexName, indexVersion);
myIndexer = indexer;
myVersionOwnerMap = ConcurrentFactoryMap.create(indexer::getSubIndexerVersion,
() -> ConcurrentCollectionFactory.createMap(ContainerUtil.identityStrategy()));
myPersistentVersionEnumerator = new PersistentSubIndexerVersionEnumerator<>(
versionMapRoot.resolve(INDEXED_VERSIONS).toFile(),
indexer.getSubIndexerVersionDescriptor());
}
void clear() throws IOException {
public void clear() throws IOException {
myPersistentVersionEnumerator.clear();
}
void close() throws IOException {
public void close() throws IOException {
myPersistentVersionEnumerator.close();
}
void flush() throws IOException {
public void flush() throws IOException {
myPersistentVersionEnumerator.flush();
}
@@ -72,13 +63,13 @@ class PersistentSubIndexerRetriever<SubIndexerType, SubIndexerVersion> {
return Paths.get(".perFileVersion", INDEXED_VERSIONS);
}
public void persistIndexedState(int fileId, @NotNull VirtualFile file) throws IOException {
public void setIndexedState(int fileId, @NotNull IndexedFile file) throws IOException {
try (DataOutputStream stream = FSRecords.writeAttribute(fileId, myFileAttribute)) {
DataInputOutputUtil.writeINT(stream, getFileIndexerId(file));
}
}
public boolean isIndexed(int fileId, @NotNull VirtualFile file) throws IOException {
public boolean isIndexed(int fileId, @NotNull IndexedFile file) throws IOException {
DataInputStream stream = FSRecords.readAttributeWithLock(fileId, myFileAttribute);
int currentIndexedVersion;
if (stream != null) {
@@ -89,9 +80,10 @@ class PersistentSubIndexerRetriever<SubIndexerType, SubIndexerVersion> {
return false;
}
private int getFileIndexerId(@NotNull VirtualFile file) throws IOException {
SubIndexerVersion version = myVersionOwnerMap.get(myIndexer.calculateSubIndexer(file));
if (version == null) return -1;
private int getFileIndexerId(@NotNull IndexedFile file) throws IOException {
SubIndexerType type = myIndexer.calculateSubIndexer(file);
if (type == null) return -1;
SubIndexerVersion version = myIndexer.getSubIndexerVersion(type);
return myPersistentVersionEnumerator.enumerate(version);
}